2019-06-07 20:25:48 +00:00
|
|
|
#include "eval-cache.hh"
|
|
|
|
#include "sqlite.hh"
|
2019-06-07 20:38:39 +00:00
|
|
|
#include "eval.hh"
|
2020-04-20 11:13:52 +00:00
|
|
|
#include "eval-inline.hh"
|
2020-06-29 14:39:41 +00:00
|
|
|
#include "store-api.hh"
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
namespace nix::eval_cache {
|
2019-06-07 20:25:48 +00:00
|
|
|
|
|
|
|
static const char * schema = R"sql(
|
|
|
|
create table if not exists Attributes (
|
2020-04-20 11:13:52 +00:00
|
|
|
parent integer not null,
|
|
|
|
name text,
|
|
|
|
type integer not null,
|
2019-06-07 20:25:48 +00:00
|
|
|
value text,
|
2020-06-29 17:08:37 +00:00
|
|
|
context text,
|
2020-04-20 11:13:52 +00:00
|
|
|
primary key (parent, name)
|
2019-06-07 20:25:48 +00:00
|
|
|
);
|
|
|
|
)sql";
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
struct AttrDb
|
2019-06-07 20:25:48 +00:00
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
std::atomic_bool failed{false};
|
|
|
|
|
2022-03-12 00:28:00 +00:00
|
|
|
const Store & cfg;
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
struct State
|
|
|
|
{
|
|
|
|
SQLite db;
|
|
|
|
SQLiteStmt insertAttribute;
|
2020-06-29 17:08:37 +00:00
|
|
|
SQLiteStmt insertAttributeWithContext;
|
2020-04-20 11:13:52 +00:00
|
|
|
SQLiteStmt queryAttribute;
|
|
|
|
SQLiteStmt queryAttributes;
|
|
|
|
std::unique_ptr<SQLiteTxn> txn;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Sync<State>> _state;
|
|
|
|
|
2022-04-26 12:16:20 +00:00
|
|
|
SymbolTable & symbols;
|
|
|
|
|
|
|
|
AttrDb(
|
|
|
|
const Store & cfg,
|
|
|
|
const Hash & fingerprint,
|
|
|
|
SymbolTable & symbols)
|
2022-03-12 00:28:00 +00:00
|
|
|
: cfg(cfg)
|
|
|
|
, _state(std::make_unique<Sync<State>>())
|
2022-04-26 12:16:20 +00:00
|
|
|
, symbols(symbols)
|
2020-04-20 11:13:52 +00:00
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
2022-05-16 00:28:21 +00:00
|
|
|
Path cacheDir = getCacheDir() + "/nix/eval-cache-v4";
|
2020-04-20 11:13:52 +00:00
|
|
|
createDirs(cacheDir);
|
|
|
|
|
|
|
|
Path dbPath = cacheDir + "/" + fingerprint.to_string(Base16, false) + ".sqlite";
|
|
|
|
|
|
|
|
state->db = SQLite(dbPath);
|
|
|
|
state->db.isCache();
|
|
|
|
state->db.exec(schema);
|
|
|
|
|
|
|
|
state->insertAttribute.create(state->db,
|
|
|
|
"insert or replace into Attributes(parent, name, type, value) values (?, ?, ?, ?)");
|
|
|
|
|
2020-06-29 17:08:37 +00:00
|
|
|
state->insertAttributeWithContext.create(state->db,
|
|
|
|
"insert or replace into Attributes(parent, name, type, value, context) values (?, ?, ?, ?, ?)");
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
state->queryAttribute.create(state->db,
|
2020-06-29 17:08:37 +00:00
|
|
|
"select rowid, type, value, context from Attributes where parent = ? and name = ?");
|
2020-04-20 11:13:52 +00:00
|
|
|
|
|
|
|
state->queryAttributes.create(state->db,
|
|
|
|
"select name from Attributes where parent = ?");
|
|
|
|
|
|
|
|
state->txn = std::make_unique<SQLiteTxn>(state->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AttrDb()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto state(_state->lock());
|
2020-07-14 13:17:38 +00:00
|
|
|
if (!failed)
|
|
|
|
state->txn->commit();
|
2020-04-20 11:13:52 +00:00
|
|
|
state->txn.reset();
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
template<typename F>
|
|
|
|
AttrId doSQLite(F && fun)
|
|
|
|
{
|
|
|
|
if (failed) return 0;
|
|
|
|
try {
|
|
|
|
return fun();
|
|
|
|
} catch (SQLiteError &) {
|
|
|
|
ignoreException();
|
|
|
|
failed = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
AttrId setAttrs(
|
|
|
|
AttrKey key,
|
|
|
|
const std::vector<Symbol> & attrs)
|
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 11:13:52 +00:00
|
|
|
|
|
|
|
state->insertAttribute.use()
|
2020-07-14 13:17:38 +00:00
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::FullAttrs)
|
2020-04-20 11:13:52 +00:00
|
|
|
(0, false).exec();
|
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
AttrId rowId = state->db.getLastInsertedRowId();
|
|
|
|
assert(rowId);
|
|
|
|
|
|
|
|
for (auto & attr : attrs)
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(rowId)
|
2022-04-22 19:45:39 +00:00
|
|
|
(symbols[attr])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::Placeholder)
|
|
|
|
(0, false).exec();
|
|
|
|
|
|
|
|
return rowId;
|
|
|
|
});
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setString(
|
|
|
|
AttrKey key,
|
2020-06-29 17:08:37 +00:00
|
|
|
std::string_view s,
|
|
|
|
const char * * context = nullptr)
|
2020-04-20 11:13:52 +00:00
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
if (context) {
|
|
|
|
std::string ctx;
|
|
|
|
for (const char * * p = context; *p; ++p) {
|
|
|
|
if (p != context) ctx.push_back(' ');
|
|
|
|
ctx.append(*p);
|
|
|
|
}
|
|
|
|
state->insertAttributeWithContext.use()
|
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::String)
|
|
|
|
(s)
|
|
|
|
(ctx).exec();
|
|
|
|
} else {
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::String)
|
2020-06-29 17:08:37 +00:00
|
|
|
(s).exec();
|
2020-07-14 13:17:38 +00:00
|
|
|
}
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:29:26 +00:00
|
|
|
AttrId setBool(
|
|
|
|
AttrKey key,
|
|
|
|
bool b)
|
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-27 14:29:26 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::Bool)
|
|
|
|
(b ? 1 : 0).exec();
|
2020-04-27 14:29:26 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-27 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 00:28:21 +00:00
|
|
|
AttrId setInt(
|
|
|
|
AttrKey key,
|
|
|
|
int n)
|
|
|
|
{
|
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
|
|
|
(symbols[key.second])
|
|
|
|
(AttrType::Int)
|
|
|
|
(n).exec();
|
|
|
|
|
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:39:47 +00:00
|
|
|
AttrId setListOfStrings(
|
|
|
|
AttrKey key,
|
|
|
|
const std::vector<std::string> & l)
|
|
|
|
{
|
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
|
|
|
(symbols[key.second])
|
|
|
|
(AttrType::ListOfStrings)
|
|
|
|
(concatStringsSep("\t", l)).exec();
|
|
|
|
|
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
AttrId setPlaceholder(AttrKey key)
|
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::Placeholder)
|
|
|
|
(0, false).exec();
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setMissing(AttrKey key)
|
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::Missing)
|
|
|
|
(0, false).exec();
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setMisc(AttrKey key)
|
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::Misc)
|
|
|
|
(0, false).exec();
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setFailed(AttrKey key)
|
|
|
|
{
|
2020-07-14 13:17:38 +00:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 12:16:20 +00:00
|
|
|
(symbols[key.second])
|
2020-07-14 13:17:38 +00:00
|
|
|
(AttrType::Failed)
|
|
|
|
(0, false).exec();
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-07-14 13:17:38 +00:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:16:20 +00:00
|
|
|
std::optional<std::pair<AttrId, AttrValue>> getAttr(AttrKey key)
|
2020-04-20 11:13:52 +00:00
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
2022-04-26 12:16:20 +00:00
|
|
|
auto queryAttribute(state->queryAttribute.use()(key.first)(symbols[key.second]));
|
2020-04-20 11:13:52 +00:00
|
|
|
if (!queryAttribute.next()) return {};
|
|
|
|
|
|
|
|
auto rowId = (AttrType) queryAttribute.getInt(0);
|
|
|
|
auto type = (AttrType) queryAttribute.getInt(1);
|
|
|
|
|
2020-04-27 14:29:26 +00:00
|
|
|
switch (type) {
|
|
|
|
case AttrType::Placeholder:
|
|
|
|
return {{rowId, placeholder_t()}};
|
|
|
|
case AttrType::FullAttrs: {
|
|
|
|
// FIXME: expensive, should separate this out.
|
|
|
|
std::vector<Symbol> attrs;
|
|
|
|
auto queryAttributes(state->queryAttributes.use()(rowId));
|
|
|
|
while (queryAttributes.next())
|
2022-04-22 19:45:39 +00:00
|
|
|
attrs.emplace_back(symbols.create(queryAttributes.getStr(0)));
|
2020-04-27 14:29:26 +00:00
|
|
|
return {{rowId, attrs}};
|
|
|
|
}
|
2020-06-29 17:08:37 +00:00
|
|
|
case AttrType::String: {
|
2021-03-10 02:20:32 +00:00
|
|
|
NixStringContext context;
|
2020-06-29 17:08:37 +00:00
|
|
|
if (!queryAttribute.isNull(3))
|
|
|
|
for (auto & s : tokenizeString<std::vector<std::string>>(queryAttribute.getStr(3), ";"))
|
2022-03-12 00:28:00 +00:00
|
|
|
context.push_back(decodeContext(cfg, s));
|
2020-06-29 17:08:37 +00:00
|
|
|
return {{rowId, string_t{queryAttribute.getStr(2), context}}};
|
|
|
|
}
|
2020-04-27 14:29:26 +00:00
|
|
|
case AttrType::Bool:
|
|
|
|
return {{rowId, queryAttribute.getInt(2) != 0}};
|
2022-05-16 00:28:21 +00:00
|
|
|
case AttrType::Int:
|
2022-05-16 13:17:35 +00:00
|
|
|
return {{rowId, (int_t) queryAttribute.getInt(2)}};
|
2022-04-20 14:39:47 +00:00
|
|
|
case AttrType::ListOfStrings:
|
|
|
|
return {{rowId, tokenizeString<std::vector<std::string>>(queryAttribute.getStr(2), "\t")}};
|
2020-04-27 14:29:26 +00:00
|
|
|
case AttrType::Missing:
|
|
|
|
return {{rowId, missing_t()}};
|
|
|
|
case AttrType::Misc:
|
|
|
|
return {{rowId, misc_t()}};
|
|
|
|
case AttrType::Failed:
|
|
|
|
return {{rowId, failed_t()}};
|
|
|
|
default:
|
|
|
|
throw Error("unexpected type in evaluation cache");
|
|
|
|
}
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
};
|
|
|
|
|
2022-04-26 12:16:20 +00:00
|
|
|
static std::shared_ptr<AttrDb> makeAttrDb(
|
|
|
|
const Store & cfg,
|
|
|
|
const Hash & fingerprint,
|
|
|
|
SymbolTable & symbols)
|
2020-07-14 13:17:38 +00:00
|
|
|
{
|
|
|
|
try {
|
2022-04-26 12:16:20 +00:00
|
|
|
return std::make_shared<AttrDb>(cfg, fingerprint, symbols);
|
2020-07-14 13:17:38 +00:00
|
|
|
} catch (SQLiteError &) {
|
|
|
|
ignoreException();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
EvalCache::EvalCache(
|
2020-07-16 14:58:53 +00:00
|
|
|
std::optional<std::reference_wrapper<const Hash>> useCache,
|
2020-04-20 11:13:52 +00:00
|
|
|
EvalState & state,
|
|
|
|
RootLoader rootLoader)
|
2022-04-26 12:16:20 +00:00
|
|
|
: db(useCache ? makeAttrDb(*state.store, *useCache, state.symbols) : nullptr)
|
2020-04-20 11:13:52 +00:00
|
|
|
, state(state)
|
|
|
|
, rootLoader(rootLoader)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Value * EvalCache::getRootValue()
|
|
|
|
{
|
|
|
|
if (!value) {
|
|
|
|
debug("getting root value");
|
|
|
|
value = allocRootValue(rootLoader());
|
|
|
|
}
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
2022-04-14 12:04:19 +00:00
|
|
|
ref<AttrCursor> EvalCache::getRoot()
|
2019-06-07 20:25:48 +00:00
|
|
|
{
|
2022-04-14 12:04:19 +00:00
|
|
|
return make_ref<AttrCursor>(ref(shared_from_this()), std::nullopt);
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
AttrCursor::AttrCursor(
|
|
|
|
ref<EvalCache> root,
|
|
|
|
Parent parent,
|
|
|
|
Value * value,
|
|
|
|
std::optional<std::pair<AttrId, AttrValue>> && cachedValue)
|
|
|
|
: root(root), parent(parent), cachedValue(std::move(cachedValue))
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
_value = allocRootValue(value);
|
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
AttrKey AttrCursor::getKey()
|
|
|
|
{
|
|
|
|
if (!parent)
|
2022-04-26 12:16:20 +00:00
|
|
|
return {0, root->state.sEpsilon};
|
2020-04-20 11:13:52 +00:00
|
|
|
if (!parent->first->cachedValue) {
|
2022-04-26 12:16:20 +00:00
|
|
|
parent->first->cachedValue = root->db->getAttr(parent->first->getKey());
|
2020-04-20 11:13:52 +00:00
|
|
|
assert(parent->first->cachedValue);
|
|
|
|
}
|
2022-04-26 12:16:20 +00:00
|
|
|
return {parent->first->cachedValue->first, parent->second};
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
Value & AttrCursor::getValue()
|
|
|
|
{
|
|
|
|
if (!_value) {
|
|
|
|
if (parent) {
|
|
|
|
auto & vParent = parent->first->getValue();
|
2022-01-21 16:44:19 +00:00
|
|
|
root->state.forceAttrs(vParent, noPos);
|
2022-04-22 19:45:39 +00:00
|
|
|
auto attr = vParent.attrs->get(parent->second);
|
2020-04-20 11:13:52 +00:00
|
|
|
if (!attr)
|
|
|
|
throw Error("attribute '%s' is unexpectedly missing", getAttrPathStr());
|
|
|
|
_value = allocRootValue(attr->value);
|
|
|
|
} else
|
|
|
|
_value = allocRootValue(root->getRootValue());
|
|
|
|
}
|
|
|
|
return **_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Symbol> AttrCursor::getAttrPath() const
|
|
|
|
{
|
|
|
|
if (parent) {
|
|
|
|
auto attrPath = parent->first->getAttrPath();
|
|
|
|
attrPath.push_back(parent->second);
|
|
|
|
return attrPath;
|
|
|
|
} else
|
|
|
|
return {};
|
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
std::vector<Symbol> AttrCursor::getAttrPath(Symbol name) const
|
|
|
|
{
|
|
|
|
auto attrPath = getAttrPath();
|
|
|
|
attrPath.push_back(name);
|
|
|
|
return attrPath;
|
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
std::string AttrCursor::getAttrPathStr() const
|
|
|
|
{
|
2022-04-22 19:45:39 +00:00
|
|
|
return concatStringsSep(".", root->state.symbols.resolve(getAttrPath()));
|
2019-06-07 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
std::string AttrCursor::getAttrPathStr(Symbol name) const
|
|
|
|
{
|
2022-04-22 19:45:39 +00:00
|
|
|
return concatStringsSep(".", root->state.symbols.resolve(getAttrPath(name)));
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
Value & AttrCursor::forceValue()
|
2019-06-07 20:25:48 +00:00
|
|
|
{
|
2022-04-20 14:39:47 +00:00
|
|
|
debug("evaluating uncached attribute '%s'", getAttrPathStr());
|
2019-06-07 20:38:39 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
auto & v = getValue();
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
try {
|
2022-01-21 16:44:19 +00:00
|
|
|
root->state.forceValue(v, noPos);
|
2020-04-20 11:13:52 +00:00
|
|
|
} catch (EvalError &) {
|
|
|
|
debug("setting '%s' to failed", getAttrPathStr());
|
|
|
|
if (root->db)
|
|
|
|
cachedValue = {root->db->setFailed(getKey()), failed_t()};
|
|
|
|
throw;
|
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
if (root->db && (!cachedValue || std::get_if<placeholder_t>(&cachedValue->second))) {
|
2020-12-17 13:45:45 +00:00
|
|
|
if (v.type() == nString)
|
2020-09-02 12:16:44 +00:00
|
|
|
cachedValue = {root->db->setString(getKey(), v.string.s, v.string.context),
|
|
|
|
string_t{v.string.s, {}}};
|
2020-12-29 01:40:04 +00:00
|
|
|
else if (v.type() == nPath)
|
2020-12-08 20:16:06 +00:00
|
|
|
cachedValue = {root->db->setString(getKey(), v.path), string_t{v.path, {}}};
|
2020-12-29 01:40:04 +00:00
|
|
|
else if (v.type() == nBool)
|
2020-04-27 14:29:26 +00:00
|
|
|
cachedValue = {root->db->setBool(getKey(), v.boolean), v.boolean};
|
2022-05-16 00:28:21 +00:00
|
|
|
else if (v.type() == nInt)
|
|
|
|
cachedValue = {root->db->setInt(getKey(), v.integer), v.integer};
|
2020-12-17 13:45:45 +00:00
|
|
|
else if (v.type() == nAttrs)
|
2020-04-20 11:13:52 +00:00
|
|
|
; // FIXME: do something?
|
|
|
|
else
|
|
|
|
cachedValue = {root->db->setMisc(getKey()), misc_t()};
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
2019-06-07 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:44:00 +00:00
|
|
|
Suggestions AttrCursor::getSuggestionsForAttr(Symbol name)
|
|
|
|
{
|
|
|
|
auto attrNames = getAttrs();
|
|
|
|
std::set<std::string> strAttrNames;
|
|
|
|
for (auto & name : attrNames)
|
2022-04-22 19:45:39 +00:00
|
|
|
strAttrNames.insert(root->state.symbols[name]);
|
2022-03-04 08:44:00 +00:00
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
return Suggestions::bestMatches(strAttrNames, root->state.symbols[name]);
|
2022-03-04 08:44:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:01:21 +00:00
|
|
|
std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(Symbol name, bool forceErrors)
|
2019-06-07 20:25:48 +00:00
|
|
|
{
|
2020-04-20 11:13:52 +00:00
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 12:16:20 +00:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-20 11:13:52 +00:00
|
|
|
|
|
|
|
if (cachedValue) {
|
|
|
|
if (auto attrs = std::get_if<std::vector<Symbol>>(&cachedValue->second)) {
|
|
|
|
for (auto & attr : *attrs)
|
2022-04-26 12:01:21 +00:00
|
|
|
if (attr == name)
|
2022-04-22 19:45:39 +00:00
|
|
|
return std::make_shared<AttrCursor>(root, std::make_pair(shared_from_this(), attr));
|
2020-04-20 11:13:52 +00:00
|
|
|
return nullptr;
|
|
|
|
} else if (std::get_if<placeholder_t>(&cachedValue->second)) {
|
2022-04-26 12:16:20 +00:00
|
|
|
auto attr = root->db->getAttr({cachedValue->first, name});
|
2020-04-20 11:13:52 +00:00
|
|
|
if (attr) {
|
|
|
|
if (std::get_if<missing_t>(&attr->second))
|
|
|
|
return nullptr;
|
2020-08-07 12:46:00 +00:00
|
|
|
else if (std::get_if<failed_t>(&attr->second)) {
|
|
|
|
if (forceErrors)
|
|
|
|
debug("reevaluating failed cached attribute '%s'");
|
|
|
|
else
|
2022-04-26 12:01:21 +00:00
|
|
|
throw CachedEvalError("cached failure of attribute '%s'", getAttrPathStr(name));
|
2020-08-07 12:46:00 +00:00
|
|
|
} else
|
2020-04-20 11:13:52 +00:00
|
|
|
return std::make_shared<AttrCursor>(root,
|
2022-04-26 12:01:21 +00:00
|
|
|
std::make_pair(shared_from_this(), name), nullptr, std::move(attr));
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
// Incomplete attrset, so need to fall thru and
|
|
|
|
// evaluate to see whether 'name' exists
|
|
|
|
} else
|
|
|
|
return nullptr;
|
|
|
|
//throw TypeError("'%s' is not an attribute set", getAttrPathStr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
if (v.type() != nAttrs)
|
2020-04-20 11:13:52 +00:00
|
|
|
return nullptr;
|
|
|
|
//throw TypeError("'%s' is not an attribute set", getAttrPathStr());
|
|
|
|
|
2022-03-04 08:44:00 +00:00
|
|
|
for (auto & attr : *v.attrs) {
|
|
|
|
if (root->db)
|
2022-04-26 12:16:20 +00:00
|
|
|
root->db->setPlaceholder({cachedValue->first, attr.name});
|
2022-03-04 08:44:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:01:21 +00:00
|
|
|
auto attr = v.attrs->get(name);
|
2019-06-07 20:38:39 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
if (!attr) {
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = {root->db->setPlaceholder(getKey()), placeholder_t()};
|
2022-04-26 12:16:20 +00:00
|
|
|
root->db->setMissing({cachedValue->first, name});
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
std::optional<std::pair<AttrId, AttrValue>> cachedValue2;
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = {root->db->setPlaceholder(getKey()), placeholder_t()};
|
2022-04-26 12:16:20 +00:00
|
|
|
cachedValue2 = {root->db->setPlaceholder({cachedValue->first, name}), placeholder_t()};
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2022-03-04 08:44:00 +00:00
|
|
|
return make_ref<AttrCursor>(
|
2022-04-26 12:01:21 +00:00
|
|
|
root, std::make_pair(shared_from_this(), name), attr->value, std::move(cachedValue2));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(std::string_view name)
|
|
|
|
{
|
|
|
|
return maybeGetAttr(root->state.symbols.create(name));
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:01:21 +00:00
|
|
|
ref<AttrCursor> AttrCursor::getAttr(Symbol name, bool forceErrors)
|
2020-04-20 11:13:52 +00:00
|
|
|
{
|
2020-08-07 12:46:00 +00:00
|
|
|
auto p = maybeGetAttr(name, forceErrors);
|
2020-04-20 11:13:52 +00:00
|
|
|
if (!p)
|
2022-04-26 12:01:21 +00:00
|
|
|
throw Error("attribute '%s' does not exist", getAttrPathStr(name));
|
2022-03-04 08:44:00 +00:00
|
|
|
return ref(p);
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:01:21 +00:00
|
|
|
ref<AttrCursor> AttrCursor::getAttr(std::string_view name)
|
|
|
|
{
|
|
|
|
return getAttr(root->state.symbols.create(name));
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:44:00 +00:00
|
|
|
OrSuggestions<ref<AttrCursor>> AttrCursor::findAlongAttrPath(const std::vector<Symbol> & attrPath, bool force)
|
2020-04-20 11:13:52 +00:00
|
|
|
{
|
|
|
|
auto res = shared_from_this();
|
|
|
|
for (auto & attr : attrPath) {
|
2022-04-26 12:01:21 +00:00
|
|
|
auto child = res->maybeGetAttr(attr, force);
|
2022-03-04 08:44:00 +00:00
|
|
|
if (!child) {
|
|
|
|
auto suggestions = res->getSuggestionsForAttr(attr);
|
|
|
|
return OrSuggestions<ref<AttrCursor>>::failed(suggestions);
|
|
|
|
}
|
|
|
|
res = child;
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
2022-03-04 08:44:00 +00:00
|
|
|
return ref(res);
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string AttrCursor::getString()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 12:16:20 +00:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-20 11:13:52 +00:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
2020-06-29 17:08:37 +00:00
|
|
|
if (auto s = std::get_if<string_t>(&cachedValue->second)) {
|
2020-04-20 11:13:52 +00:00
|
|
|
debug("using cached string attribute '%s'", getAttrPathStr());
|
2020-06-29 17:08:37 +00:00
|
|
|
return s->first;
|
2020-04-20 11:13:52 +00:00
|
|
|
} else
|
|
|
|
throw TypeError("'%s' is not a string", getAttrPathStr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
if (v.type() != nString && v.type() != nPath)
|
|
|
|
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()));
|
2020-04-20 11:13:52 +00:00
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
return v.type() == nString ? v.string.s : v.path;
|
2020-04-20 11:13:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:08:37 +00:00
|
|
|
string_t AttrCursor::getStringWithContext()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 12:16:20 +00:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-06-29 17:08:37 +00:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto s = std::get_if<string_t>(&cachedValue->second)) {
|
2020-11-19 19:59:36 +00:00
|
|
|
bool valid = true;
|
|
|
|
for (auto & c : s->second) {
|
2022-03-12 00:28:00 +00:00
|
|
|
if (!root->state.store->isValidPath(c.first)) {
|
2020-11-19 19:59:36 +00:00
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
debug("using cached string attribute '%s'", getAttrPathStr());
|
|
|
|
return *s;
|
|
|
|
}
|
2020-06-29 17:08:37 +00:00
|
|
|
} else
|
|
|
|
throw TypeError("'%s' is not a string", getAttrPathStr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
if (v.type() == nString)
|
2022-03-12 00:28:00 +00:00
|
|
|
return {v.string.s, v.getContext(*root->state.store)};
|
2020-12-17 13:45:45 +00:00
|
|
|
else if (v.type() == nPath)
|
2020-06-29 17:08:37 +00:00
|
|
|
return {v.path, {}};
|
|
|
|
else
|
2020-12-17 13:45:45 +00:00
|
|
|
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()));
|
2020-06-29 17:08:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:29:26 +00:00
|
|
|
bool AttrCursor::getBool()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 12:16:20 +00:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-27 14:29:26 +00:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto b = std::get_if<bool>(&cachedValue->second)) {
|
|
|
|
debug("using cached Boolean attribute '%s'", getAttrPathStr());
|
|
|
|
return *b;
|
|
|
|
} else
|
|
|
|
throw TypeError("'%s' is not a Boolean", getAttrPathStr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
if (v.type() != nBool)
|
2020-04-27 14:29:26 +00:00
|
|
|
throw TypeError("'%s' is not a Boolean", getAttrPathStr());
|
|
|
|
|
|
|
|
return v.boolean;
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:02:28 +00:00
|
|
|
NixInt AttrCursor::getInt()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = root->db->getAttr(getKey());
|
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
2022-05-16 13:17:35 +00:00
|
|
|
if (auto i = std::get_if<int_t>(&cachedValue->second)) {
|
2022-05-13 20:02:28 +00:00
|
|
|
debug("using cached Integer attribute '%s'", getAttrPathStr());
|
2022-05-16 13:17:35 +00:00
|
|
|
return (*i).x;
|
2022-05-13 20:02:28 +00:00
|
|
|
} else
|
|
|
|
throw TypeError("'%s' is not an Integer", getAttrPathStr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
|
|
|
if (v.type() != nInt)
|
|
|
|
throw TypeError("'%s' is not an Integer", getAttrPathStr());
|
|
|
|
|
|
|
|
return v.integer;
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:39:47 +00:00
|
|
|
std::vector<std::string> AttrCursor::getListOfStrings()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = root->db->getAttr(getKey());
|
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto l = std::get_if<std::vector<std::string>>(&cachedValue->second)) {
|
|
|
|
debug("using cached list of strings attribute '%s'", getAttrPathStr());
|
|
|
|
return *l;
|
|
|
|
} else
|
|
|
|
throw TypeError("'%s' is not a list of strings", getAttrPathStr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("evaluating uncached attribute '%s'", getAttrPathStr());
|
|
|
|
|
|
|
|
auto & v = getValue();
|
|
|
|
root->state.forceValue(v, noPos);
|
|
|
|
|
|
|
|
if (v.type() != nList)
|
|
|
|
throw TypeError("'%s' is not a list", getAttrPathStr());
|
|
|
|
|
|
|
|
std::vector<std::string> res;
|
|
|
|
|
|
|
|
for (auto & elem : v.listItems())
|
|
|
|
res.push_back(std::string(root->state.forceStringNoCtx(*elem)));
|
|
|
|
|
2022-05-04 19:13:49 +00:00
|
|
|
if (root->db)
|
|
|
|
cachedValue = {root->db->setListOfStrings(getKey(), res), res};
|
2022-04-20 14:39:47 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
std::vector<Symbol> AttrCursor::getAttrs()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 12:16:20 +00:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-20 11:13:52 +00:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto attrs = std::get_if<std::vector<Symbol>>(&cachedValue->second)) {
|
|
|
|
debug("using cached attrset attribute '%s'", getAttrPathStr());
|
|
|
|
return *attrs;
|
|
|
|
} else
|
|
|
|
throw TypeError("'%s' is not an attribute set", getAttrPathStr());
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
auto & v = forceValue();
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
if (v.type() != nAttrs)
|
2020-04-20 11:13:52 +00:00
|
|
|
throw TypeError("'%s' is not an attribute set", getAttrPathStr());
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
std::vector<Symbol> attrs;
|
|
|
|
for (auto & attr : *getValue().attrs)
|
2022-04-22 19:45:39 +00:00
|
|
|
attrs.push_back(attr.name);
|
|
|
|
std::sort(attrs.begin(), attrs.end(), [&](Symbol a, Symbol b) {
|
|
|
|
std::string_view sa = root->state.symbols[a], sb = root->state.symbols[b];
|
|
|
|
return sa < sb;
|
2020-04-20 11:13:52 +00:00
|
|
|
});
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
if (root->db)
|
2022-04-26 12:16:20 +00:00
|
|
|
cachedValue = {root->db->setAttrs(getKey(), attrs), attrs};
|
2019-06-07 20:25:48 +00:00
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
return attrs;
|
2019-06-07 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 11:13:52 +00:00
|
|
|
bool AttrCursor::isDerivation()
|
2019-06-07 20:25:48 +00:00
|
|
|
{
|
2020-04-20 11:13:52 +00:00
|
|
|
auto aType = maybeGetAttr("type");
|
|
|
|
return aType && aType->getString() == "derivation";
|
2019-06-07 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 14:39:41 +00:00
|
|
|
StorePath AttrCursor::forceDerivation()
|
|
|
|
{
|
2022-04-26 12:01:21 +00:00
|
|
|
auto aDrvPath = getAttr(root->state.sDrvPath, true);
|
2020-06-29 14:39:41 +00:00
|
|
|
auto drvPath = root->state.store->parseStorePath(aDrvPath->getString());
|
|
|
|
if (!root->state.store->isValidPath(drvPath) && !settings.readOnlyMode) {
|
|
|
|
/* The eval cache contains 'drvPath', but the actual path has
|
|
|
|
been garbage-collected. So force it to be regenerated. */
|
|
|
|
aDrvPath->forceValue();
|
|
|
|
if (!root->state.store->isValidPath(drvPath))
|
|
|
|
throw Error("don't know how to recreate store derivation '%s'!",
|
|
|
|
root->state.store->printStorePath(drvPath));
|
|
|
|
}
|
|
|
|
return drvPath;
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:25:48 +00:00
|
|
|
}
|