Skip to content

Commit

Permalink
Dummy store compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Jul 17, 2024
1 parent 121e607 commit 596a682
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
35 changes: 26 additions & 9 deletions src/libstore/dummy-store.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include "store-api.hh"
#include "store-registration.hh"
#include "callback.hh"

namespace nix {

struct DummyStoreConfigDescription : virtual Store::Config::Description
{
DummyStoreConfigDescription() : StoreConfigDescription{Store::Config::schema} {}
};

struct DummyStoreConfig : virtual StoreConfig {
using StoreConfig::StoreConfig;

DummyStoreConfig(std::string_view scheme, std::string_view authority, const Params & params)
using Description = DummyStoreConfigDescription;

static const Description schema;

DummyStoreConfig(std::string_view scheme, std::string_view authority, const StoreReference::Params & params)
: StoreConfig(params)
{
if (!authority.empty())
Expand All @@ -25,18 +35,20 @@ struct DummyStoreConfig : virtual StoreConfig {
static std::set<std::string> uriSchemes() {
return {"dummy"};
}

std::shared_ptr<Store> open() override;
};

decltype(DummyStoreConfig::schema) DummyStoreConfig::schema{};

struct DummyStore : public virtual DummyStoreConfig, public virtual Store
{
DummyStore(std::string_view scheme, std::string_view authority, const Params & params)
: StoreConfig(params)
, DummyStoreConfig(scheme, authority, params)
, Store(params)
{ }
using Config = DummyStoreConfig;

DummyStore(const Params & params)
: DummyStore("dummy", "", params)
DummyStore(const DummyStoreConfig & config)
: StoreConfig(config)
, DummyStoreConfig(config)
, Store(config)
{ }

std::string getUri() override
Expand Down Expand Up @@ -86,6 +98,11 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store
{ unsupported("getFSAccessor"); }
};

static RegisterStoreImplementation<DummyStore, DummyStoreConfig> regDummyStore;
std::shared_ptr<Store> DummyStoreConfig::open()
{
return std::make_shared<DummyStore>(*this);
}

static RegisterStoreImplementation<DummyStore> regDummyStore;

}
10 changes: 5 additions & 5 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const StoreConfigT<config::JustValue> storeConfigDefaults = {
.systemFeatures = { .value = StoreConfig::getDefaultSystemFeatures() },
};

const StoreConfigT<config::SettingInfo> storeConfigDescriptions = {
decltype(StoreConfig::schema) StoreConfig::schema = {{
.pathInfoCacheSize = {
.name = "path-info-cache-size",
.description = "Size of the in-memory store path metadata cache.",
Expand Down Expand Up @@ -236,12 +236,12 @@ const StoreConfigT<config::SettingInfo> storeConfigDescriptions = {
// unsuitable to be rendered in the documentation.
.documentDefault = false,
},
};
}};

StoreConfigT<config::JustValue> parseStoreConfig(const StoreReference::Params & params)
{
constexpr auto & defaults = storeConfigDefaults;
constexpr auto & descriptions = storeConfigDescriptions;
constexpr auto & descriptions = StoreConfig::schema;

return {
CONFIG_ROW(pathInfoCacheSize),
Expand Down Expand Up @@ -497,8 +497,8 @@ StringSet StoreConfig::getDefaultSystemFeatures()
return res;
}

Store::Store(const StoreReference::Params & params)
: StoreConfig(params)
Store::Store(const Store::Config & config)
: Store::Config(config)
, state({(size_t) pathInfoCacheSize})
{
assertLibStoreInitialized();
Expand Down
22 changes: 19 additions & 3 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,22 @@ struct StoreConfigT

extern const StoreConfigT<config::JustValue> storeConfigDefaults;

extern const StoreConfigT<config::SettingInfo> storeConfigDescriptions;

StoreConfigT<config::JustValue> parseStoreConfig(const StoreReference::Params &);

struct StoreConfigDescription : StoreConfigT<config::SettingInfo>, StoreDirConfigT<config::SettingInfo>
{
};

struct StoreConfig : StoreConfigT<config::JustValue>, StoreDirConfig
{
using StoreDirConfig::StoreDirConfig;

StoreConfig() = delete;

using Description = StoreConfigDescription;

static const Description schema;

static StringSet getDefaultSystemFeatures();

virtual ~StoreConfig() { }
Expand All @@ -147,10 +153,20 @@ struct StoreConfig : StoreConfigT<config::JustValue>, StoreDirConfig
{
return std::nullopt;
}

/**
* Open a store of the type corresponding to this configuration
* type.
*/
virtual std::shared_ptr<Store> open() = 0;
};

class Store : public std::enable_shared_from_this<Store>, public virtual StoreConfig
{
public:

using Config = StoreConfig;

protected:

struct PathInfoCacheValue {
Expand Down Expand Up @@ -189,7 +205,7 @@ protected:

std::shared_ptr<NarInfoDiskCache> diskCache;

Store(const StoreReference::Params & params);
Store(const Store::Config & config);

public:
/**
Expand Down
23 changes: 12 additions & 11 deletions src/libstore/store-registration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,38 @@ struct StoreFactory
* The `authorityPath` parameter is `<authority>/<path>`, or really
* whatever comes after `<scheme>://` and before `?<query-params>`.
*/
std::function<std::shared_ptr<Store>(
std::string_view scheme, std::string_view authorityPath, const Store::Params & params)>
create;
std::function<std::shared_ptr<StoreConfig>()> getConfig;
std::function<std::shared_ptr<StoreConfig>(
std::string_view scheme, std::string_view authorityPath, const StoreReference::Params & params)>
parseConfig;
const StoreConfigDescription & configSchema;
};

struct Implementations
{
static std::vector<StoreFactory> * registered;

template<typename T, typename TConfig>
template<typename T>
static void add()
{
if (!registered)
registered = new std::vector<StoreFactory>();
StoreFactory factory{
.uriSchemes = TConfig::uriSchemes(),
.create = ([](auto scheme, auto uri, auto & params) -> std::shared_ptr<Store> {
return std::make_shared<T>(scheme, uri, params);
.uriSchemes = T::Config::uriSchemes(),
.parseConfig = ([](auto scheme, auto uri, auto & params) -> std::shared_ptr<StoreConfig> {
return std::make_shared<typename T::Config>(scheme, uri, params);
}),
.getConfig = ([]() -> std::shared_ptr<StoreConfig> { return std::make_shared<TConfig>(StringMap({})); })};
.configSchema = T::Config::schema,
};
registered->push_back(factory);
}
};

template<typename T, typename TConfig>
template<typename T>
struct RegisterStoreImplementation
{
RegisterStoreImplementation()
{
Implementations::add<T, TConfig>();
Implementations::add<T>();
}
};

Expand Down

0 comments on commit 596a682

Please sign in to comment.