libstore: de-callback-ify BinaryCacheStore::getFile

Change-Id: I36b3eb9f645aa04058151e7b2353e15e6f29057b
This commit is contained in:
eldritch horrors 2024-04-27 19:55:54 +02:00
parent 1a002d1a11
commit c77bd88259
3 changed files with 29 additions and 83 deletions

View file

@ -67,26 +67,9 @@ void BinaryCacheStore::upsertFile(const std::string & path,
upsertFile(path, std::make_shared<std::stringstream>(std::move(data)), mimeType); upsertFile(path, std::make_shared<std::stringstream>(std::move(data)), mimeType);
} }
void BinaryCacheStore::getFile(const std::string & path,
Callback<std::optional<std::string>> callback) noexcept
{
try {
callback(getFile(path));
} catch (...) { callback.rethrow(); }
}
void BinaryCacheStore::getFile(const std::string & path, Sink & sink) void BinaryCacheStore::getFile(const std::string & path, Sink & sink)
{ {
std::promise<std::optional<std::string>> promise; sink(*getFile(path));
getFile(path,
{[&](std::future<std::optional<std::string>> result) {
try {
promise.set_value(result.get());
} catch (...) {
promise.set_exception(std::current_exception());
}
}});
sink(*promise.get_future().get());
} }
std::optional<std::string> BinaryCacheStore::getFile(const std::string & path) std::optional<std::string> BinaryCacheStore::getFile(const std::string & path)
@ -377,25 +360,17 @@ void BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath,
auto narInfoFile = narInfoFileFor(storePath); auto narInfoFile = narInfoFileFor(storePath);
auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback));
getFile(narInfoFile,
{[=,this](std::future<std::optional<std::string>> fut) {
try { try {
auto data = fut.get(); auto data = getFile(narInfoFile);
if (!data) return (*callbackPtr)({}); if (!data) return callback({});
stats.narInfoRead++; stats.narInfoRead++;
(*callbackPtr)((std::shared_ptr<ValidPathInfo>) callback(std::make_shared<NarInfo>(*this, *data, narInfoFile));
std::make_shared<NarInfo>(*this, *data, narInfoFile));
(void) act; // force Activity into this lambda to ensure it stays alive
} catch (...) { } catch (...) {
callbackPtr->rethrow(); callback.rethrow();
} }
}});
} }
StorePath BinaryCacheStore::addToStore( StorePath BinaryCacheStore::addToStore(
@ -477,24 +452,16 @@ void BinaryCacheStore::queryRealisationUncached(const DrvOutput & id,
{ {
auto outputInfoFilePath = realisationsPrefix + "/" + id.to_string() + ".doi"; auto outputInfoFilePath = realisationsPrefix + "/" + id.to_string() + ".doi";
auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback));
Callback<std::optional<std::string>> newCallback = {
[=](std::future<std::optional<std::string>> fut) {
try { try {
auto data = fut.get(); auto data = getFile(outputInfoFilePath);
if (!data) return (*callbackPtr)({}); if (!data) return callback({});
auto realisation = Realisation::fromJSON( auto realisation = Realisation::fromJSON(
nlohmann::json::parse(*data), outputInfoFilePath); nlohmann::json::parse(*data), outputInfoFilePath);
return (*callbackPtr)(std::make_shared<const Realisation>(realisation)); return callback(std::make_shared<const Realisation>(realisation));
} catch (...) { } catch (...) {
callbackPtr->rethrow(); callback.rethrow();
} }
}
};
getFile(outputInfoFilePath, std::move(newCallback));
} }
void BinaryCacheStore::registerDrvOutput(const Realisation& info) { void BinaryCacheStore::registerDrvOutput(const Realisation& info) {

View file

@ -85,15 +85,7 @@ public:
*/ */
virtual void getFile(const std::string & path, Sink & sink); virtual void getFile(const std::string & path, Sink & sink);
/** virtual std::optional<std::string> getFile(const std::string & path);
* Fetch the specified file and call the specified callback with
* the result. A subclass may implement this asynchronously.
*/
virtual void getFile(
const std::string & path,
Callback<std::optional<std::string>> callback) noexcept;
std::optional<std::string> getFile(const std::string & path);
public: public:

View file

@ -165,33 +165,20 @@ protected:
} }
} }
void getFile(const std::string & path, std::optional<std::string> getFile(const std::string & path) override
Callback<std::optional<std::string>> callback) noexcept override
{ {
try {
checkEnabled(); checkEnabled();
} catch (...) {
callback.rethrow();
return;
}
auto request(makeRequest(path)); auto request(makeRequest(path));
auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback));
getFileTransfer()->enqueueFileTransfer(request,
{[callbackPtr, this](std::future<FileTransferResult> result) {
try { try {
(*callbackPtr)(std::move(result.get().data)); return std::move(getFileTransfer()->enqueueFileTransfer(request).get().data);
} catch (FileTransferError & e) { } catch (FileTransferError & e) {
if (e.error == FileTransfer::NotFound || e.error == FileTransfer::Forbidden) if (e.error == FileTransfer::NotFound || e.error == FileTransfer::Forbidden)
return (*callbackPtr)({}); return {};
maybeDisable(); maybeDisable();
callbackPtr->rethrow(); throw;
} catch (...) {
callbackPtr->rethrow();
} }
}});
} }
/** /**