refactor: Remove throw from queryPathInfoFromClientCache

Return a value instead of throwing.

Rather than the more trivial refactor of wrapping the return value in
another std::optional, we retain the meaning of the outer optional:
"we know at least something."

So we have changed:
return nullopt    -> return nullopt
throw InvalidPath -> return make_optional(nullptr)
return vpi        -> return make_optional(vpi)
This commit is contained in:
Robert Hensing 2024-01-19 17:00:39 +01:00
parent 8983ee8b2e
commit d19627e8b4
2 changed files with 18 additions and 14 deletions

View File

@ -685,7 +685,8 @@ static bool goodStorePath(const StorePath & expected, const StorePath & actual)
&& (expected.name() == Store::MissingName || expected.name() == actual.name());
}
std::optional<ref<const ValidPathInfo>> Store::queryPathInfoFromClientCache(const StorePath & storePath)
std::optional<std::shared_ptr<const ValidPathInfo>> Store::queryPathInfoFromClientCache(const StorePath & storePath)
{
auto hashPart = std::string(storePath.hashPart());
@ -693,9 +694,10 @@ std::optional<ref<const ValidPathInfo>> Store::queryPathInfoFromClientCache(cons
auto res = state.lock()->pathInfoCache.get(std::string(storePath.to_string()));
if (res && res->isKnownNow()) {
stats.narInfoReadAverted++;
if (!res->didExist())
throw InvalidPath("path '%s' is not valid", printStorePath(storePath));
return ref(res->value);
if (res->didExist())
return std::make_optional(res->value);
else
return std::make_optional(nullptr);
}
}
@ -709,9 +711,10 @@ std::optional<ref<const ValidPathInfo>> Store::queryPathInfoFromClientCache(cons
res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue{ .value = res.second });
if (res.first == NarInfoDiskCache::oInvalid ||
!goodStorePath(storePath, res.second->path))
throw InvalidPath("path '%s' is not valid", printStorePath(storePath));
return std::make_optional(nullptr);
}
return ref(res.second);
assert(res.second);
return std::make_optional(res.second);
}
}
@ -727,8 +730,11 @@ void Store::queryPathInfo(const StorePath & storePath,
try {
auto r = queryPathInfoFromClientCache(storePath);
if (r.has_value()) {
ref<const ValidPathInfo> & info = *r;
return callback(ref(info));
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(); }

View File

@ -283,16 +283,14 @@ public:
Callback<ref<const ValidPathInfo>> callback) noexcept;
/**
* NOTE: this is not the final interface - to be modified in next commit.
*
* Version of queryPathInfo() that only queries the local narinfo cache and not
* the actual store.
*
* @return `std::make_optional(vpi)` if the path is known
* @return `std::null_opt` if the path was not known to be valid or invalid
* @throw InvalidPathError if the path is known to be invalid
* @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<ref<const ValidPathInfo>> queryPathInfoFromClientCache(const StorePath & path);
std::optional<std::shared_ptr<const ValidPathInfo>> queryPathInfoFromClientCache(const StorePath & path);
/**
* Query the information about a realisation.