2016-04-20 14:12:38 +02:00
|
|
|
|
#include "nar-info-disk-cache.hh"
|
|
|
|
|
#include "sync.hh"
|
|
|
|
|
#include "sqlite.hh"
|
|
|
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
static const char * schema = R"sql(
|
|
|
|
|
|
|
|
|
|
create table if not exists BinaryCaches (
|
|
|
|
|
id integer primary key autoincrement not null,
|
|
|
|
|
url text unique not null,
|
|
|
|
|
timestamp integer not null,
|
|
|
|
|
storeDir text not null,
|
|
|
|
|
wantMassQuery integer not null,
|
|
|
|
|
priority integer not null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
create table if not exists NARs (
|
|
|
|
|
cache integer not null,
|
2016-04-21 17:53:47 +02:00
|
|
|
|
hashPart text not null,
|
|
|
|
|
namePart text not null,
|
2016-04-20 14:12:38 +02:00
|
|
|
|
url text,
|
|
|
|
|
compression text,
|
|
|
|
|
fileHash text,
|
|
|
|
|
fileSize integer,
|
|
|
|
|
narHash text,
|
|
|
|
|
narSize integer,
|
|
|
|
|
refs text,
|
|
|
|
|
deriver text,
|
|
|
|
|
sigs text,
|
|
|
|
|
timestamp integer not null,
|
2016-04-21 17:53:47 +02:00
|
|
|
|
primary key (cache, hashPart),
|
2016-04-20 14:12:38 +02:00
|
|
|
|
foreign key (cache) references BinaryCaches(id) on delete cascade
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
create table if not exists NARExistence (
|
|
|
|
|
cache integer not null,
|
|
|
|
|
storePath text not null,
|
|
|
|
|
exist integer not null,
|
|
|
|
|
timestamp integer not null,
|
|
|
|
|
primary key (cache, storePath),
|
|
|
|
|
foreign key (cache) references BinaryCaches(id) on delete cascade
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
)sql";
|
|
|
|
|
|
|
|
|
|
class NarInfoDiskCacheImpl : public NarInfoDiskCache
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
/* How long negative lookups are valid. */
|
|
|
|
|
const int ttlNegative = 3600;
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
struct Cache
|
|
|
|
|
{
|
|
|
|
|
int id;
|
|
|
|
|
Path storeDir;
|
2016-06-01 15:15:21 +02:00
|
|
|
|
bool wantMassQuery;
|
|
|
|
|
int priority;
|
2016-06-01 14:49:12 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-04-20 14:12:38 +02:00
|
|
|
|
struct State
|
|
|
|
|
{
|
|
|
|
|
SQLite db;
|
|
|
|
|
SQLiteStmt insertCache, queryCache, insertNAR, queryNAR, insertNARExistence, queryNARExistence;
|
2016-06-01 14:49:12 +02:00
|
|
|
|
std::map<std::string, Cache> caches;
|
2016-04-20 14:12:38 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Sync<State> _state;
|
|
|
|
|
|
|
|
|
|
NarInfoDiskCacheImpl()
|
|
|
|
|
{
|
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
|
Path dbPath = getCacheDir() + "/nix/binary-cache-v4.sqlite";
|
2016-04-20 14:12:38 +02:00
|
|
|
|
createDirs(dirOf(dbPath));
|
|
|
|
|
|
|
|
|
|
if (sqlite3_open_v2(dbPath.c_str(), &state->db.db,
|
|
|
|
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0) != SQLITE_OK)
|
|
|
|
|
throw Error(format("cannot open store cache ‘%s’") % dbPath);
|
|
|
|
|
|
|
|
|
|
if (sqlite3_busy_timeout(state->db, 60 * 60 * 1000) != SQLITE_OK)
|
|
|
|
|
throwSQLiteError(state->db, "setting timeout");
|
|
|
|
|
|
|
|
|
|
// We can always reproduce the cache.
|
|
|
|
|
if (sqlite3_exec(state->db, "pragma synchronous = off", 0, 0, 0) != SQLITE_OK)
|
|
|
|
|
throwSQLiteError(state->db, "making database asynchronous");
|
|
|
|
|
if (sqlite3_exec(state->db, "pragma main.journal_mode = truncate", 0, 0, 0) != SQLITE_OK)
|
|
|
|
|
throwSQLiteError(state->db, "setting journal mode");
|
|
|
|
|
|
|
|
|
|
if (sqlite3_exec(state->db, schema, 0, 0, 0) != SQLITE_OK)
|
|
|
|
|
throwSQLiteError(state->db, "initialising database schema");
|
|
|
|
|
|
|
|
|
|
state->insertCache.create(state->db,
|
|
|
|
|
"insert or replace into BinaryCaches(url, timestamp, storeDir, wantMassQuery, priority) values (?, ?, ?, ?, ?)");
|
|
|
|
|
|
|
|
|
|
state->queryCache.create(state->db,
|
|
|
|
|
"select id, storeDir, wantMassQuery, priority from BinaryCaches where url = ?");
|
|
|
|
|
|
|
|
|
|
state->insertNAR.create(state->db,
|
2016-04-21 17:53:47 +02:00
|
|
|
|
"insert or replace into NARs(cache, hashPart, namePart, url, compression, fileHash, fileSize, narHash, "
|
|
|
|
|
"narSize, refs, deriver, sigs, timestamp) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
2016-04-20 14:12:38 +02:00
|
|
|
|
|
|
|
|
|
state->queryNAR.create(state->db,
|
2016-04-21 17:53:47 +02:00
|
|
|
|
"select * from NARs where cache = ? and hashPart = ?");
|
2016-04-20 14:12:38 +02:00
|
|
|
|
|
|
|
|
|
state->insertNARExistence.create(state->db,
|
|
|
|
|
"insert or replace into NARExistence(cache, storePath, exist, timestamp) values (?, ?, ?, ?)");
|
|
|
|
|
|
|
|
|
|
state->queryNARExistence.create(state->db,
|
|
|
|
|
"select exist, timestamp from NARExistence where cache = ? and storePath = ?");
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
Cache & getCache(State & state, const std::string & uri)
|
2016-04-20 14:12:38 +02:00
|
|
|
|
{
|
|
|
|
|
auto i = state.caches.find(uri);
|
|
|
|
|
if (i == state.caches.end()) abort();
|
|
|
|
|
return i->second;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
void createCache(const std::string & uri, const Path & storeDir, bool wantMassQuery, int priority) override
|
2016-04-20 14:12:38 +02:00
|
|
|
|
{
|
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
|
|
|
|
// FIXME: race
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
state->insertCache.use()(uri)(time(0))(storeDir)(wantMassQuery)(priority).exec();
|
2016-04-20 14:12:38 +02:00
|
|
|
|
assert(sqlite3_changes(state->db) == 1);
|
2016-06-01 15:15:21 +02:00
|
|
|
|
state->caches[uri] = Cache{(int) sqlite3_last_insert_rowid(state->db), storeDir, wantMassQuery, priority};
|
2016-04-20 14:12:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 15:15:21 +02:00
|
|
|
|
bool cacheExists(const std::string & uri,
|
|
|
|
|
bool & wantMassQuery, int & priority) override
|
2016-04-20 14:12:38 +02:00
|
|
|
|
{
|
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
|
|
|
|
auto i = state->caches.find(uri);
|
2016-06-01 15:15:21 +02:00
|
|
|
|
if (i == state->caches.end()) {
|
|
|
|
|
auto queryCache(state->queryCache.use()(uri));
|
|
|
|
|
if (!queryCache.next()) return false;
|
|
|
|
|
state->caches.emplace(uri,
|
|
|
|
|
Cache{(int) queryCache.getInt(0), queryCache.getStr(1), queryCache.getInt(2), (int) queryCache.getInt(3)});
|
|
|
|
|
}
|
2016-04-20 14:12:38 +02:00
|
|
|
|
|
2016-06-01 15:15:21 +02:00
|
|
|
|
auto & cache(getCache(*state, uri));
|
2016-04-20 14:12:38 +02:00
|
|
|
|
|
2016-06-01 15:15:21 +02:00
|
|
|
|
wantMassQuery = cache.wantMassQuery;
|
|
|
|
|
priority = cache.priority;
|
2016-04-20 14:12:38 +02:00
|
|
|
|
|
2016-06-01 15:15:21 +02:00
|
|
|
|
return true;
|
2016-04-20 14:12:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<Outcome, std::shared_ptr<NarInfo>> lookupNarInfo(
|
2016-04-21 17:53:47 +02:00
|
|
|
|
const std::string & uri, const std::string & hashPart) override
|
2016-04-20 14:12:38 +02:00
|
|
|
|
{
|
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
auto & cache(getCache(*state, uri));
|
|
|
|
|
|
|
|
|
|
auto queryNAR(state->queryNAR.use()(cache.id)(hashPart));
|
2016-04-20 14:12:38 +02:00
|
|
|
|
|
|
|
|
|
if (!queryNAR.next())
|
|
|
|
|
// FIXME: check NARExistence
|
|
|
|
|
return {oUnknown, 0};
|
|
|
|
|
|
|
|
|
|
auto narInfo = make_ref<NarInfo>();
|
|
|
|
|
|
|
|
|
|
// FIXME: implement TTL.
|
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
|
auto namePart = queryNAR.getStr(2);
|
2016-06-01 14:49:12 +02:00
|
|
|
|
narInfo->path = cache.storeDir + "/" +
|
2016-04-21 17:53:47 +02:00
|
|
|
|
hashPart + (namePart.empty() ? "" : "-" + namePart);
|
|
|
|
|
narInfo->url = queryNAR.getStr(3);
|
|
|
|
|
narInfo->compression = queryNAR.getStr(4);
|
|
|
|
|
if (!queryNAR.isNull(5))
|
|
|
|
|
narInfo->fileHash = parseHash(queryNAR.getStr(5));
|
|
|
|
|
narInfo->fileSize = queryNAR.getInt(6);
|
|
|
|
|
narInfo->narHash = parseHash(queryNAR.getStr(7));
|
|
|
|
|
narInfo->narSize = queryNAR.getInt(8);
|
|
|
|
|
for (auto & r : tokenizeString<Strings>(queryNAR.getStr(9), " "))
|
2016-06-01 14:49:12 +02:00
|
|
|
|
narInfo->references.insert(cache.storeDir + "/" + r);
|
2016-04-21 17:53:47 +02:00
|
|
|
|
if (!queryNAR.isNull(10))
|
2016-06-01 14:49:12 +02:00
|
|
|
|
narInfo->deriver = cache.storeDir + "/" + queryNAR.getStr(10);
|
2016-04-21 17:53:47 +02:00
|
|
|
|
for (auto & sig : tokenizeString<Strings>(queryNAR.getStr(11), " "))
|
2016-04-20 14:12:38 +02:00
|
|
|
|
narInfo->sigs.insert(sig);
|
|
|
|
|
|
|
|
|
|
return {oValid, narInfo};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void upsertNarInfo(
|
2016-04-21 17:53:47 +02:00
|
|
|
|
const std::string & uri, const std::string & hashPart,
|
|
|
|
|
std::shared_ptr<ValidPathInfo> info) override
|
2016-04-20 14:12:38 +02:00
|
|
|
|
{
|
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
auto & cache(getCache(*state, uri));
|
|
|
|
|
|
2016-04-20 14:12:38 +02:00
|
|
|
|
if (info) {
|
|
|
|
|
|
|
|
|
|
auto narInfo = std::dynamic_pointer_cast<NarInfo>(info);
|
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
|
assert(hashPart == storePathToHash(info->path));
|
|
|
|
|
|
2016-04-20 14:12:38 +02:00
|
|
|
|
state->insertNAR.use()
|
2016-06-01 14:49:12 +02:00
|
|
|
|
(cache.id)
|
2016-04-21 17:53:47 +02:00
|
|
|
|
(hashPart)
|
|
|
|
|
(storePathToName(info->path))
|
2016-04-20 14:12:38 +02:00
|
|
|
|
(narInfo ? narInfo->url : "", narInfo != 0)
|
|
|
|
|
(narInfo ? narInfo->compression : "", narInfo != 0)
|
|
|
|
|
(narInfo && narInfo->fileHash ? narInfo->fileHash.to_string() : "", narInfo && narInfo->fileHash)
|
|
|
|
|
(narInfo ? narInfo->fileSize : 0, narInfo != 0 && narInfo->fileSize)
|
|
|
|
|
(info->narHash.to_string())
|
|
|
|
|
(info->narSize)
|
|
|
|
|
(concatStringsSep(" ", info->shortRefs()))
|
|
|
|
|
(info->deriver != "" ? baseNameOf(info->deriver) : "", info->deriver != "")
|
|
|
|
|
(concatStringsSep(" ", info->sigs))
|
|
|
|
|
(time(0)).exec();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// not implemented
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ref<NarInfoDiskCache> getNarInfoDiskCache()
|
|
|
|
|
{
|
|
|
|
|
static Sync<std::shared_ptr<NarInfoDiskCache>> cache;
|
|
|
|
|
|
|
|
|
|
auto cache_(cache.lock());
|
|
|
|
|
if (!*cache_) *cache_ = std::make_shared<NarInfoDiskCacheImpl>();
|
|
|
|
|
return ref<NarInfoDiskCache>(*cache_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|