2020-03-30 14:04:18 +00:00
|
|
|
#include "cache.hh"
|
|
|
|
#include "sqlite.hh"
|
|
|
|
#include "sync.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
|
|
|
|
|
|
|
static const char * schema = R"sql(
|
|
|
|
|
|
|
|
create table if not exists Cache (
|
|
|
|
input text not null,
|
|
|
|
info text not null,
|
|
|
|
path text not null,
|
|
|
|
immutable integer not null,
|
|
|
|
timestamp integer not null,
|
|
|
|
primary key (input)
|
|
|
|
);
|
|
|
|
)sql";
|
|
|
|
|
|
|
|
struct CacheImpl : Cache
|
|
|
|
{
|
|
|
|
struct State
|
|
|
|
{
|
|
|
|
SQLite db;
|
|
|
|
SQLiteStmt add, lookup;
|
|
|
|
};
|
|
|
|
|
|
|
|
Sync<State> _state;
|
|
|
|
|
|
|
|
CacheImpl()
|
|
|
|
{
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
|
|
auto dbPath = getCacheDir() + "/nix/fetcher-cache-v1.sqlite";
|
|
|
|
createDirs(dirOf(dbPath));
|
|
|
|
|
|
|
|
state->db = SQLite(dbPath);
|
|
|
|
state->db.isCache();
|
|
|
|
state->db.exec(schema);
|
|
|
|
|
|
|
|
state->add.create(state->db,
|
|
|
|
"insert or replace into Cache(input, info, path, immutable, timestamp) values (?, ?, ?, ?, ?)");
|
|
|
|
|
|
|
|
state->lookup.create(state->db,
|
|
|
|
"select info, path, immutable, timestamp from Cache where input = ?");
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(
|
|
|
|
ref<Store> store,
|
|
|
|
const Attrs & inAttrs,
|
|
|
|
const Attrs & infoAttrs,
|
|
|
|
const StorePath & storePath,
|
2022-02-24 17:09:00 +00:00
|
|
|
bool locked) override
|
2020-03-30 14:04:18 +00:00
|
|
|
{
|
|
|
|
_state.lock()->add.use()
|
2020-10-28 14:41:18 +00:00
|
|
|
(attrsToJSON(inAttrs).dump())
|
|
|
|
(attrsToJSON(infoAttrs).dump())
|
2020-03-30 14:04:18 +00:00
|
|
|
(store->printStorePath(storePath))
|
2022-02-24 17:09:00 +00:00
|
|
|
(locked)
|
2020-03-30 14:04:18 +00:00
|
|
|
(time(0)).exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::pair<Attrs, StorePath>> lookup(
|
|
|
|
ref<Store> store,
|
|
|
|
const Attrs & inAttrs) override
|
|
|
|
{
|
|
|
|
if (auto res = lookupExpired(store, inAttrs)) {
|
|
|
|
if (!res->expired)
|
|
|
|
return std::make_pair(std::move(res->infoAttrs), std::move(res->storePath));
|
|
|
|
debug("ignoring expired cache entry '%s'",
|
2020-10-28 14:41:18 +00:00
|
|
|
attrsToJSON(inAttrs).dump());
|
2020-03-30 14:04:18 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Result> lookupExpired(
|
|
|
|
ref<Store> store,
|
|
|
|
const Attrs & inAttrs) override
|
|
|
|
{
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
2020-10-28 14:41:18 +00:00
|
|
|
auto inAttrsJSON = attrsToJSON(inAttrs).dump();
|
2020-03-30 14:04:18 +00:00
|
|
|
|
2020-10-28 14:41:18 +00:00
|
|
|
auto stmt(state->lookup.use()(inAttrsJSON));
|
2020-03-30 14:04:18 +00:00
|
|
|
if (!stmt.next()) {
|
2020-10-28 14:41:18 +00:00
|
|
|
debug("did not find cache entry for '%s'", inAttrsJSON);
|
2020-03-30 14:04:18 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:41:18 +00:00
|
|
|
auto infoJSON = stmt.getStr(0);
|
2020-03-30 14:04:18 +00:00
|
|
|
auto storePath = store->parseStorePath(stmt.getStr(1));
|
2022-02-24 17:09:00 +00:00
|
|
|
auto locked = stmt.getInt(2) != 0;
|
2020-03-30 14:04:18 +00:00
|
|
|
auto timestamp = stmt.getInt(3);
|
|
|
|
|
|
|
|
store->addTempRoot(storePath);
|
|
|
|
if (!store->isValidPath(storePath)) {
|
|
|
|
// FIXME: we could try to substitute 'storePath'.
|
2020-10-28 14:41:18 +00:00
|
|
|
debug("ignoring disappeared cache entry '%s'", inAttrsJSON);
|
2020-03-30 14:04:18 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("using cache entry '%s' -> '%s', '%s'",
|
2020-10-28 14:41:18 +00:00
|
|
|
inAttrsJSON, infoJSON, store->printStorePath(storePath));
|
2020-03-30 14:04:18 +00:00
|
|
|
|
|
|
|
return Result {
|
2022-02-24 17:09:00 +00:00
|
|
|
.expired = !locked && (settings.tarballTtl.get() == 0 || timestamp + settings.tarballTtl < time(0)),
|
2020-10-28 14:41:18 +00:00
|
|
|
.infoAttrs = jsonToAttrs(nlohmann::json::parse(infoJSON)),
|
2020-03-30 14:04:18 +00:00
|
|
|
.storePath = std::move(storePath)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ref<Cache> getCache()
|
|
|
|
{
|
|
|
|
static auto cache = std::make_shared<CacheImpl>();
|
|
|
|
return ref<Cache>(cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|