Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/libstore-tests/nar-info-disk-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ TEST(NarInfoDiskCacheImpl, create_and_read)

// Set up "background noise" and check that different caches receive different ids
{
auto bc1 = cache->createCache("https://bar", "/nix/storedir", wantMassQuery, prio);
auto bc2 = cache->createCache("https://xyz", "/nix/storedir", false, 12);
auto bc1 =
cache->createCache("https://bar", "/nix/storedir", {.wantMassQuery = wantMassQuery, .priority = prio});
auto bc2 = cache->createCache("https://xyz", "/nix/storedir", {.priority = 12});
ASSERT_NE(bc1, bc2);
barId = bc1;
}

// Check that the fields are saved and returned correctly. This does not test
// the select statement yet, because of in-memory caching.
savedId = cache->createCache("http://foo", "/nix/storedir", wantMassQuery, prio);
savedId = cache->createCache("http://foo", "/nix/storedir", {.wantMassQuery = wantMassQuery, .priority = prio});
;
{
auto r = cache->upToDateCacheExists("http://foo");
Expand Down Expand Up @@ -84,7 +85,7 @@ TEST(NarInfoDiskCacheImpl, create_and_read)
}

// "Update", same data, check that the id number is reused
cache2->createCache("http://foo", "/nix/storedir", wantMassQuery, prio);
cache2->createCache("http://foo", "/nix/storedir", {.wantMassQuery = wantMassQuery, .priority = prio});

{
auto r = cache2->upToDateCacheExists("http://foo");
Expand All @@ -107,7 +108,8 @@ TEST(NarInfoDiskCacheImpl, create_and_read)
auto r0 = cache2->upToDateCacheExists("https://bar");
ASSERT_FALSE(r0);

cache2->createCache("https://bar", "/nix/storedir", !wantMassQuery, prio + 10);
cache2->createCache(
"https://bar", "/nix/storedir", {.wantMassQuery = !wantMassQuery, .priority = prio + 10});
auto r = cache2->upToDateCacheExists("https://bar");
ASSERT_EQ(r->wantMassQuery, !wantMassQuery);
ASSERT_EQ(r->priority, prio + 10);
Expand Down
3 changes: 2 additions & 1 deletion src/libstore/http-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void HttpBinaryCacheStore::init()
} catch (UploadToHTTP &) {
throw Error("'%s' does not appear to be a binary cache", config->cacheUri.to_string());
}
diskCache->createCache(cacheKey, config->storeDir, config->wantMassQuery, config->priority);
diskCache->createCache(
cacheKey, config->storeDir, {.wantMassQuery = config->wantMassQuery, .priority = config->priority});
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/libstore/include/nix/store/nar-info-disk-cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ struct NarInfoDiskCache

virtual ~NarInfoDiskCache() {}

virtual int
createCache(const std::string & uri, const std::string & storeDir, bool wantMassQuery, int priority) = 0;

struct CacheInfo
{
int id;
bool wantMassQuery;
int priority;
int id = 0;
bool wantMassQuery = false;
int priority = 0;
};

/**
* Create or update the cached nix-cache-info for the binary cache at `uri`.
* Note that `info.id` is ignored. This function returns the id of the cache entry.
*/
virtual int createCache(const std::string & uri, const std::string & storeDir, const CacheInfo & info) = 0;

virtual std::optional<CacheInfo> upToDateCacheExists(const std::string & uri) = 0;

virtual std::pair<Outcome, std::shared_ptr<NarInfo>>
Expand Down
50 changes: 24 additions & 26 deletions src/libstore/nar-info-disk-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache

struct Cache
{
int id;
std::string storeDir;
bool wantMassQuery;
int priority;
CacheInfo info;
};

struct State
Expand Down Expand Up @@ -192,18 +190,19 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
if (!queryCache.next())
return std::nullopt;
auto cache = Cache{
.id = (int) queryCache.getInt(0),
.storeDir = queryCache.getStr(1),
.wantMassQuery = queryCache.getInt(2) != 0,
.priority = (int) queryCache.getInt(3),
};
.info = {
.id = (int) queryCache.getInt(0),
.wantMassQuery = queryCache.getInt(2) != 0,
.priority = (int) queryCache.getInt(3),
}};
state.caches.emplace(uri, cache);
}
return getCache(state, uri);
}

public:
int createCache(const std::string & uri, const std::string & storeDir, bool wantMassQuery, int priority) override
int createCache(const std::string & uri, const std::string & storeDir, const CacheInfo & info) override
{
return retrySQLite<int>([&]() {
auto state(_state.lock());
Expand All @@ -214,32 +213,27 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
auto cache(queryCacheRaw(*state, uri));

if (cache)
return cache->id;
return cache->info.id;

Cache ret{
.id = -1, // set below
.storeDir = storeDir,
.wantMassQuery = wantMassQuery,
.priority = priority,
};
Cache ret{.storeDir = storeDir, .info = info};

{
auto r(state->insertCache.use()
.apply(uri)
.apply(time(nullptr))
.apply(storeDir)
.apply(wantMassQuery)
.apply(priority));
.apply(info.wantMassQuery)
.apply(info.priority));
if (!r.next()) {
unreachable();
}
ret.id = (int) r.getInt(0);
ret.info.id = (int) r.getInt(0);
}

state->caches[uri] = ret;

txn.commit();
return ret.id;
return ret.info.id;
});
}

Expand All @@ -250,7 +244,7 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
auto cache(queryCacheRaw(*state, uri));
if (!cache)
return std::nullopt;
return CacheInfo{.id = cache->id, .wantMassQuery = cache->wantMassQuery, .priority = cache->priority};
return cache->info;
});
}

Expand All @@ -266,7 +260,7 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
auto now = time(nullptr);

auto queryNAR(state->queryNAR.use()
.apply(cache.id)
.apply(cache.info.id)
.apply(hashPart)
.apply(now - settings.ttlNegative)
.apply(now - settings.ttlPositive));
Expand Down Expand Up @@ -312,7 +306,7 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
auto now = time(nullptr);

auto queryRealisation(state->queryRealisation.use()
.apply(cache.id)
.apply(cache.info.id)
.apply(id.to_string())
.apply(now - settings.ttlNegative)
.apply(now - settings.ttlPositive));
Expand Down Expand Up @@ -350,7 +344,7 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
// assert(hashPart == storePathToHash(info->path));

state->insertNAR.use()
.apply(cache.id)
.apply(cache.info.id)
.apply(hashPart)
.apply(std::string(info->path.name()))
.apply(narInfo ? narInfo->url : "", narInfo != 0)
Expand All @@ -372,7 +366,7 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
.exec();

} else {
state->insertMissingNAR.use().apply(cache.id).apply(hashPart).apply(time(nullptr)).exec();
state->insertMissingNAR.use().apply(cache.info.id).apply(hashPart).apply(time(nullptr)).exec();
}
});
}
Expand All @@ -385,7 +379,7 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
auto & cache(getCache(*state, uri));

state->insertRealisation.use()
.apply(cache.id)
.apply(cache.info.id)
.apply(realisation.id.to_string())
.apply(static_cast<nlohmann::json>(realisation).dump())
.apply(time(nullptr))
Expand All @@ -399,7 +393,11 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
auto state(_state.lock());

auto & cache(getCache(*state, uri));
state->insertMissingRealisation.use().apply(cache.id).apply(id.to_string()).apply(time(nullptr)).exec();
state->insertMissingRealisation.use()
.apply(cache.info.id)
.apply(id.to_string())
.apply(time(nullptr))
.exec();
});
}
};
Expand Down
Loading