Separate the instantiation and initialisation of the stores

Add a new `init()` method to the `Store` class that is supposed to
handle all the effectful initialisation needed to set-up the store.
The constructor should remain side-effect free and just initialize the
c++ data structure.

The goal behind that is that we can create “dummy” instances of each
store to query static properties about it (the parameters it accepts for
example)
This commit is contained in:
regnat 2020-09-09 11:29:17 +02:00
parent fa32560169
commit 3b57181f8e
9 changed files with 43 additions and 6 deletions

View file

@ -58,7 +58,7 @@ public:
public: public:
virtual void init(); virtual void init() override;
private: private:

View file

@ -5,6 +5,10 @@ namespace nix {
struct DummyStore : public Store struct DummyStore : public Store
{ {
DummyStore(const std::string uri, const Params & params) DummyStore(const std::string uri, const Params & params)
: DummyStore(params)
{ }
DummyStore(const Params & params)
: Store(params) : Store(params)
{ } { }

View file

@ -23,6 +23,12 @@ private:
public: public:
HttpBinaryCacheStore(
const Params & params)
: HttpBinaryCacheStore("dummy", params)
{
}
HttpBinaryCacheStore( HttpBinaryCacheStore(
const Path & _cacheUri, const Path & _cacheUri,
const Params & params) const Params & params)

View file

@ -37,6 +37,9 @@ struct LegacySSHStore : public Store
static std::vector<std::string> uriPrefixes() { return {"ssh"}; } static std::vector<std::string> uriPrefixes() { return {"ssh"}; }
LegacySSHStore(const Params & params)
: LegacySSHStore("dummy", params)
{}
LegacySSHStore(const string & host, const Params & params) LegacySSHStore(const string & host, const Params & params)
: Store(params) : Store(params)

View file

@ -12,6 +12,12 @@ private:
public: public:
LocalBinaryCacheStore(
const Params & params)
: LocalBinaryCacheStore("dummy", params)
{
}
LocalBinaryCacheStore( LocalBinaryCacheStore(
const Path & binaryCacheDir, const Path & binaryCacheDir,
const Params & params) const Params & params)

View file

@ -192,6 +192,9 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
S3Helper s3Helper; S3Helper s3Helper;
S3BinaryCacheStoreImpl(const Params & params)
: S3BinaryCacheStoreImpl("dummy-bucket", params) {}
S3BinaryCacheStoreImpl( S3BinaryCacheStoreImpl(
const std::string & bucketName, const std::string & bucketName,
const Params & params) const Params & params)

View file

@ -17,6 +17,12 @@ public:
const Setting<Path> remoteProgram{(Store*) this, "nix-daemon", "remote-program", "path to the nix-daemon executable on the remote system"}; const Setting<Path> remoteProgram{(Store*) this, "nix-daemon", "remote-program", "path to the nix-daemon executable on the remote system"};
const Setting<std::string> remoteStore{(Store*) this, "", "remote-store", "URI of the store on the remote system"}; const Setting<std::string> remoteStore{(Store*) this, "", "remote-store", "URI of the store on the remote system"};
SSHStore(
const Params & params)
: SSHStore("dummy", params)
{
}
SSHStore(const std::string & host, const Params & params) SSHStore(const std::string & host, const Params & params)
: Store(params) : Store(params)
, RemoteStore(params) , RemoteStore(params)

View file

@ -1094,8 +1094,9 @@ ref<Store> openStore(const std::string & uri_,
} }
for (auto implem : *Implementations::registered) { for (auto implem : *Implementations::registered) {
auto store = implem.open(uri, params); auto store = implem.create(uri, params);
if (store) { if (store) {
store->init();
store->warnUnknownSettings(); store->warnUnknownSettings();
return ref<Store>(store); return ref<Store>(store);
} }

View file

@ -200,9 +200,12 @@ protected:
Store(const Params & params); Store(const Params & params);
std::shared_ptr<Config> getConfig();
public: public:
/**
* Perform any necessary effectful operation to make the store up and
* running
*/
virtual void init() {};
virtual ~Store() { } virtual ~Store() { }
@ -749,7 +752,8 @@ std::list<ref<Store>> getDefaultSubstituters();
struct StoreFactory struct StoreFactory
{ {
std::function<std::shared_ptr<Store> (const std::string & uri, const Store::Params & params)> open; std::function<std::shared_ptr<Store> (const std::string & uri, const Store::Params & params)> create;
std::function<std::shared_ptr<Store> (const Store::Params & params)> createDummy;
}; };
struct Implementations struct Implementations
{ {
@ -760,10 +764,14 @@ struct Implementations
{ {
if (!registered) registered = new std::vector<StoreFactory>(); if (!registered) registered = new std::vector<StoreFactory>();
StoreFactory factory{ StoreFactory factory{
.open = .create =
([](const std::string & uri, const Store::Params & params) ([](const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store> -> std::shared_ptr<Store>
{ return std::make_shared<T>(uri, params); }), { return std::make_shared<T>(uri, params); }),
.createDummy =
([](const Store::Params & params)
-> std::shared_ptr<Store>
{ return std::make_shared<T>(params); })
}; };
registered->push_back(factory); registered->push_back(factory);
} }