From 7d14f5c3310f5380ca14391e79bd1fc214d5f5c9 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 21 Apr 2016 17:53:47 +0200 Subject: Implement S3BinaryCacheStore::queryAllValidPaths() This allows commands like "nix verify --all" or "nix path-info --all" to work on S3 caches. Unfortunately, this requires some ugly hackery: when querying the contents of the bucket, we don't want to have to read every .narinfo file. But the S3 bucket keys only include the hash part of each store path, not the name part. So as a special exception queryAllValidPaths() can now return store paths *without* the name part, and queryPathInfo() accepts such store paths (returning a ValidPathInfo object containing the full name). --- src/libstore/store-api.cc | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'src/libstore/store-api.cc') diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index cac137a99d7c..9870e36b5494 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -63,13 +63,16 @@ Path followLinksToStorePath(const Path & path) string storePathToName(const Path & path) { assertStorePath(path); - return string(path, settings.nixStore.size() + storePathHashLen + 2); + auto l = settings.nixStore.size() + 1 + storePathHashLen; + assert(path.size() >= l); + return path.size() == l ? "" : string(path, l + 1); } string storePathToHash(const Path & path) { assertStorePath(path); + assert(path.size() >= settings.nixStore.size() + 1 + storePathHashLen); return string(path, settings.nixStore.size() + 1, storePathHashLen); } @@ -234,9 +237,11 @@ std::string Store::getUri() bool Store::isValidPath(const Path & storePath) { + auto hashPart = storePathToHash(storePath); + { auto state_(state.lock()); - auto res = state_->pathInfoCache.get(storePath); + auto res = state_->pathInfoCache.get(hashPart); if (res) { stats.narInfoReadAverted++; return *res != 0; @@ -244,10 +249,11 @@ bool Store::isValidPath(const Path & storePath) } if (diskCache) { - auto res = diskCache->lookupNarInfo(getUri(), storePath); + auto res = diskCache->lookupNarInfo(getUri(), hashPart); if (res.first != NarInfoDiskCache::oUnknown) { + stats.narInfoReadAverted++; auto state_(state.lock()); - state_->pathInfoCache.upsert(storePath, + state_->pathInfoCache.upsert(hashPart, res.first == NarInfoDiskCache::oInvalid ? 0 : res.second); return res.first == NarInfoDiskCache::oValid; } @@ -261,9 +267,11 @@ bool Store::isValidPath(const Path & storePath) ref Store::queryPathInfo(const Path & storePath) { + auto hashPart = storePathToHash(storePath); + { auto state_(state.lock()); - auto res = state_->pathInfoCache.get(storePath); + auto res = state_->pathInfoCache.get(hashPart); if (res) { stats.narInfoReadAverted++; if (!*res) @@ -273,12 +281,14 @@ ref Store::queryPathInfo(const Path & storePath) } if (diskCache) { - auto res = diskCache->lookupNarInfo(getUri(), storePath); + auto res = diskCache->lookupNarInfo(getUri(), hashPart); if (res.first != NarInfoDiskCache::oUnknown) { + stats.narInfoReadAverted++; auto state_(state.lock()); - state_->pathInfoCache.upsert(storePath, + state_->pathInfoCache.upsert(hashPart, res.first == NarInfoDiskCache::oInvalid ? 0 : res.second); - if (res.first == NarInfoDiskCache::oInvalid) + if (res.first == NarInfoDiskCache::oInvalid || + (res.second->path != storePath && storePathToName(storePath) != "")) throw InvalidPath(format("path ā€˜%sā€™ is not valid") % storePath); return ref(res.second); } @@ -287,14 +297,16 @@ ref Store::queryPathInfo(const Path & storePath) auto info = queryPathInfoUncached(storePath); if (diskCache && info) - diskCache->upsertNarInfo(getUri(), info); + diskCache->upsertNarInfo(getUri(), hashPart, info); { auto state_(state.lock()); - state_->pathInfoCache.upsert(storePath, info); + state_->pathInfoCache.upsert(hashPart, info); } - if (!info) { + if (!info + || (info->path != storePath && storePathToName(storePath) != "")) + { stats.narInfoMissing++; throw InvalidPath(format("path ā€˜%sā€™ is not valid") % storePath); } -- cgit 1.4.1