BinaryCacheStore: Avoid recreating NAR listing

We already have it, so let's just use it!
This commit is contained in:
John Ericson
2026-01-13 14:01:25 -05:00
parent 3d18d73003
commit ce28cb32e9
4 changed files with 36 additions and 20 deletions

View File

@@ -147,7 +147,7 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
write the compressed NAR to disk), into a HashSink (to get the
NAR hash), and into a NarAccessor (to get the NAR listing). */
HashSink fileHashSink{HashAlgorithm::SHA256};
std::shared_ptr<SourceAccessor> narAccessor;
std::shared_ptr<NarAccessor> narAccessor;
HashSink narHashSink{HashAlgorithm::SHA256};
{
FdSink fileSink(fdTemp.get());
@@ -205,7 +205,7 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
if (config.writeNARListing) {
nlohmann::json j = {
{"version", 1},
{"root", listNarDeep(*narAccessor, CanonPath::root)},
{"root", narAccessor->getListing()},
};
upsertFile(std::string(info.path.hashPart()) + ".ls", j.dump(), "application/json");

View File

@@ -84,7 +84,7 @@ std::shared_ptr<SourceAccessor> RemoteFSAccessor::accessObject(const StorePath &
auto narAccessor = makeNarAccessor(std::move(nar));
try {
nlohmann::json j = listNarDeep(*narAccessor, CanonPath::root);
nlohmann::json j = narAccessor->getListing();
writeFile(listingFile, j.dump());
} catch (...) {
ignoreExceptionExceptInterrupt();

View File

@@ -10,13 +10,24 @@ namespace nix {
struct Source;
/**
* A SourceAccessor for NAR files that provides access to the listing structure.
*/
struct NarAccessor : SourceAccessor
{
/**
* Get the NAR listing structure.
*/
virtual const NarListing & getListing() const = 0;
};
/**
* Return an object that provides access to the contents of a NAR
* file.
*/
ref<SourceAccessor> makeNarAccessor(std::string && nar);
ref<NarAccessor> makeNarAccessor(std::string && nar);
ref<SourceAccessor> makeNarAccessor(Source & source);
ref<NarAccessor> makeNarAccessor(Source & source);
/**
* Create a NAR accessor from a NAR listing (in the format produced by
@@ -33,12 +44,12 @@ GetNarBytes seekableGetNarBytes(const std::filesystem::path & path);
GetNarBytes seekableGetNarBytes(Descriptor fd);
ref<SourceAccessor> makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes);
ref<NarAccessor> makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes);
/**
* Creates a NAR accessor from a given stream and a GetNarBytes getter.
* @param source Consumed eagerly. References to it are not persisted in the resulting SourceAccessor.
*/
ref<SourceAccessor> makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes);
ref<NarAccessor> makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes);
} // namespace nix

View File

@@ -4,13 +4,18 @@
namespace nix {
struct NarAccessor : public SourceAccessor
struct NarAccessorImpl : NarAccessor
{
NarListing root;
GetNarBytes getNarBytes;
NarAccessor(std::string && nar)
const NarListing & getListing() const override
{
return root;
}
NarAccessorImpl(std::string && nar)
: root{[&nar]() {
StringSource source(nar);
return parseNarListing(source);
@@ -20,18 +25,18 @@ struct NarAccessor : public SourceAccessor
{
}
NarAccessor(Source & source)
NarAccessorImpl(Source & source)
: root{parseNarListing(source)}
{
}
NarAccessor(Source & source, GetNarBytes getNarBytes)
NarAccessorImpl(Source & source, GetNarBytes getNarBytes)
: root{parseNarListing(source)}
, getNarBytes{std::move(getNarBytes)}
{
}
NarAccessor(NarListing && listing, GetNarBytes getNarBytes)
NarAccessorImpl(NarListing && listing, GetNarBytes getNarBytes)
: root{std::move(listing)}
, getNarBytes{std::move(getNarBytes)}
{
@@ -127,24 +132,24 @@ struct NarAccessor : public SourceAccessor
}
};
ref<SourceAccessor> makeNarAccessor(std::string && nar)
ref<NarAccessor> makeNarAccessor(std::string && nar)
{
return make_ref<NarAccessor>(std::move(nar));
return make_ref<NarAccessorImpl>(std::move(nar));
}
ref<SourceAccessor> makeNarAccessor(Source & source)
ref<NarAccessor> makeNarAccessor(Source & source)
{
return make_ref<NarAccessor>(source);
return make_ref<NarAccessorImpl>(source);
}
ref<SourceAccessor> makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes)
ref<NarAccessor> makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes)
{
return make_ref<NarAccessor>(std::move(listing), getNarBytes);
return make_ref<NarAccessorImpl>(std::move(listing), getNarBytes);
}
ref<SourceAccessor> makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes)
ref<NarAccessor> makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes)
{
return make_ref<NarAccessor>(source, getNarBytes);
return make_ref<NarAccessorImpl>(source, getNarBytes);
}
GetNarBytes seekableGetNarBytes(const std::filesystem::path & path)