diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index 642da9f04..58cb87a51 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -29,8 +29,27 @@ BinaryCacheStore::BinaryCacheStore(const StoreParams & params) void BinaryCacheStore::init() { std::string cacheInfoFile = "nix-cache-info"; - if (!fileExists(cacheInfoFile)) + + auto cacheInfo = getFile(cacheInfoFile); + if (!cacheInfo) { upsertFile(cacheInfoFile, "StoreDir: " + settings.nixStore + "\n"); + } else { + for (auto & line : tokenizeString(*cacheInfo, "\n")) { + size_t colon = line.find(':'); + if (colon == std::string::npos) continue; + auto name = line.substr(0, colon); + auto value = trim(line.substr(colon + 1, std::string::npos)); + if (name == "StoreDir") { + if (value != settings.nixStore) + throw Error(format("binary cache ‘%s’ is for Nix stores with prefix ‘%s’, not ‘%s’") + % getUri() % value % settings.nixStore); + } else if (name == "WantMassQuery") { + wantMassQuery_ = value == "1"; + } else if (name == "Priority") { + string2Int(value, priority); + } + } + } } void BinaryCacheStore::notImpl() diff --git a/src/libstore/binary-cache-store.hh b/src/libstore/binary-cache-store.hh index bb67a8581..c14ab8676 100644 --- a/src/libstore/binary-cache-store.hh +++ b/src/libstore/binary-cache-store.hh @@ -33,6 +33,9 @@ protected: doesn't exist. */ virtual std::shared_ptr getFile(const std::string & path) = 0; + bool wantMassQuery_ = false; + int priority = 50; + public: virtual void init(); @@ -78,6 +81,8 @@ public: SubstitutablePathInfos & infos) { } + bool wantMassQuery() { return wantMassQuery_; } + void addToStore(const ValidPathInfo & info, const std::string & nar, bool repair = false) override; diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 073f76cc9..8c8d545c6 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -5,6 +5,8 @@ namespace nix { +MakeError(UploadToHTTP, Error); + class HttpBinaryCacheStore : public BinaryCacheStore { private: @@ -38,9 +40,12 @@ public: { // FIXME: do this lazily? if (!diskCache->cacheExists(cacheUri)) { - if (!fileExists("nix-cache-info")) + try { + BinaryCacheStore::init(); + } catch (UploadToHTTP &) { throw Error(format("‘%s’ does not appear to be a binary cache") % cacheUri); - diskCache->createCache(cacheUri); + } + diskCache->createCache(cacheUri, wantMassQuery_, priority); } } @@ -66,7 +71,7 @@ protected: void upsertFile(const std::string & path, const std::string & data) override { - throw Error("uploading to an HTTP binary cache is not supported"); + throw UploadToHTTP("uploading to an HTTP binary cache is not supported"); } std::shared_ptr getFile(const std::string & path) override diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 6f33c1e81..acd02eb48 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -788,6 +788,7 @@ PathSet LocalStore::querySubstitutablePaths(const PathSet & paths) { PathSet res; for (auto & sub : getDefaultSubstituters()) { + if (!sub->wantMassQuery()) continue; for (auto & path : paths) { if (res.count(path)) continue; debug(format("checking substituter ‘%s’ for path ‘%s’") diff --git a/src/libstore/nar-info-disk-cache.cc b/src/libstore/nar-info-disk-cache.cc index d8b0815bf..ae368e152 100644 --- a/src/libstore/nar-info-disk-cache.cc +++ b/src/libstore/nar-info-disk-cache.cc @@ -113,13 +113,13 @@ public: return i->second; } - void createCache(const std::string & uri) override + void createCache(const std::string & uri, bool wantMassQuery, int priority) override { auto state(_state.lock()); // FIXME: race - state->insertCache.use()(uri)(time(0))(settings.nixStore)(1)(0).exec(); + state->insertCache.use()(uri)(time(0))(settings.nixStore)(wantMassQuery)(priority).exec(); assert(sqlite3_changes(state->db) == 1); state->caches[uri] = sqlite3_last_insert_rowid(state->db); } diff --git a/src/libstore/nar-info-disk-cache.hh b/src/libstore/nar-info-disk-cache.hh index f4e3fbbdc..ce5da062c 100644 --- a/src/libstore/nar-info-disk-cache.hh +++ b/src/libstore/nar-info-disk-cache.hh @@ -10,7 +10,7 @@ class NarInfoDiskCache public: typedef enum { oValid, oInvalid, oUnknown } Outcome; - virtual void createCache(const std::string & uri) = 0; + virtual void createCache(const std::string & uri, bool wantMassQuery, int priority) = 0; virtual bool cacheExists(const std::string & uri) = 0; diff --git a/src/libstore/s3-binary-cache-store.cc b/src/libstore/s3-binary-cache-store.cc index bd8abe482..8e85e2149 100644 --- a/src/libstore/s3-binary-cache-store.cc +++ b/src/libstore/s3-binary-cache-store.cc @@ -95,7 +95,7 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore BinaryCacheStore::init(); - diskCache->createCache(getUri()); + diskCache->createCache(getUri(), wantMassQuery, priority); } } diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index 65c4cdc97..8c618bf3e 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -253,6 +253,8 @@ public: virtual void querySubstitutablePathInfos(const PathSet & paths, SubstitutablePathInfos & infos) = 0; + virtual bool wantMassQuery() { return false; } + /* Import a path into the store. */ virtual void addToStore(const ValidPathInfo & info, const std::string & nar, bool repair = false) = 0;