Merge pull request #9794 from hercules-ci/queryPathInfoFromClientCache

refactor: Extract `Store::queryPathInfoFromClientCache`
This commit is contained in:
Robert Hensing 2024-01-19 17:33:52 +01:00 committed by GitHub
commit 382fa51ff0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 25 deletions

View file

@ -686,19 +686,18 @@ static bool goodStorePath(const StorePath & expected, const StorePath & actual)
} }
void Store::queryPathInfo(const StorePath & storePath, std::optional<std::shared_ptr<const ValidPathInfo>> Store::queryPathInfoFromClientCache(const StorePath & storePath)
Callback<ref<const ValidPathInfo>> callback) noexcept
{ {
auto hashPart = std::string(storePath.hashPart()); auto hashPart = std::string(storePath.hashPart());
try {
{ {
auto res = state.lock()->pathInfoCache.get(std::string(storePath.to_string())); auto res = state.lock()->pathInfoCache.get(std::string(storePath.to_string()));
if (res && res->isKnownNow()) { if (res && res->isKnownNow()) {
stats.narInfoReadAverted++; stats.narInfoReadAverted++;
if (!res->didExist()) if (res->didExist())
throw InvalidPath("path '%s' is not valid", printStorePath(storePath)); return std::make_optional(res->value);
return callback(ref<const ValidPathInfo>(res->value)); else
return std::make_optional(nullptr);
} }
} }
@ -712,12 +711,31 @@ void Store::queryPathInfo(const StorePath & storePath,
res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue{ .value = res.second }); res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue{ .value = res.second });
if (res.first == NarInfoDiskCache::oInvalid || if (res.first == NarInfoDiskCache::oInvalid ||
!goodStorePath(storePath, res.second->path)) !goodStorePath(storePath, res.second->path))
throw InvalidPath("path '%s' is not valid", printStorePath(storePath)); return std::make_optional(nullptr);
} }
return callback(ref<const ValidPathInfo>(res.second)); assert(res.second);
return std::make_optional(res.second);
} }
} }
return std::nullopt;
}
void Store::queryPathInfo(const StorePath & storePath,
Callback<ref<const ValidPathInfo>> callback) noexcept
{
auto hashPart = std::string(storePath.hashPart());
try {
auto r = queryPathInfoFromClientCache(storePath);
if (r.has_value()) {
std::shared_ptr<const ValidPathInfo> & info = *r;
if (info)
return callback(ref(info));
else
throw InvalidPath("path '%s' is not valid", printStorePath(storePath));
}
} catch (...) { return callback.rethrow(); } } catch (...) { return callback.rethrow(); }
auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback)); auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback));

View file

@ -282,6 +282,16 @@ public:
void queryPathInfo(const StorePath & path, void queryPathInfo(const StorePath & path,
Callback<ref<const ValidPathInfo>> callback) noexcept; Callback<ref<const ValidPathInfo>> callback) noexcept;
/**
* Version of queryPathInfo() that only queries the local narinfo cache and not
* the actual store.
*
* @return `std::nullopt` if nothing is known about the path in the local narinfo cache.
* @return `std::make_optional(nullptr)` if the path is known to not exist.
* @return `std::make_optional(validPathInfo)` if the path is known to exist.
*/
std::optional<std::shared_ptr<const ValidPathInfo>> queryPathInfoFromClientCache(const StorePath & path);
/** /**
* Query the information about a realisation. * Query the information about a realisation.
*/ */