From 7b9cfda0841db03a1292cd0198ada14c061be2af Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 4 Jul 2018 10:50:57 -0700 Subject: [PATCH] add support for datastore plugins License: MIT Signed-off-by: Jeromy --- plugin/datastore.go | 14 ++++++++++++++ plugin/loader/initializer.go | 8 +++++++- repo/fsrepo/datastores.go | 17 ++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 plugin/datastore.go diff --git a/plugin/datastore.go b/plugin/datastore.go new file mode 100644 index 00000000000..735eedc9d76 --- /dev/null +++ b/plugin/datastore.go @@ -0,0 +1,14 @@ +package plugin + +import ( + "github.com/ipfs/go-ipfs/repo/fsrepo" +) + +// PluginDatastore is an interface that can be implemented to add handlers for +// for different datastores +type PluginDatastore interface { + Plugin + + DatastoreTypeName() string + DatastoreConfigParser() fsrepo.ConfigFromMap +} diff --git a/plugin/loader/initializer.go b/plugin/loader/initializer.go index 10e0fc11258..9d03dfeeaba 100644 --- a/plugin/loader/initializer.go +++ b/plugin/loader/initializer.go @@ -3,8 +3,9 @@ package loader import ( "github.com/ipfs/go-ipfs/core/coredag" "github.com/ipfs/go-ipfs/plugin" - "gx/ipfs/QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo/opentracing-go" + "github.com/ipfs/go-ipfs/repo/fsrepo" + "gx/ipfs/QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo/opentracing-go" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" ) @@ -32,6 +33,11 @@ func run(plugins []plugin.Plugin) error { if err != nil { return err } + case plugin.PluginDatastore: + err := fsrepo.AddDatastoreConfigHandler(pl.DatastoreTypeName(), pl.DatastoreConfigParser()) + if err != nil { + return err + } default: panic(pl) } diff --git a/repo/fsrepo/datastores.go b/repo/fsrepo/datastores.go index 111aaad89cb..ab8b0696f91 100644 --- a/repo/fsrepo/datastores.go +++ b/repo/fsrepo/datastores.go @@ -36,7 +36,12 @@ type DatastoreConfig interface { Create(path string) (repo.Datastore, error) } -// DiskSpec is the type returned by the DatastoreConfig's DiskSpec method +// DiskSpec is a minimal representation of the characteristic values of the +// datastore. If two diskspecs are the same, the loader assumes that they refer +// to exactly the same datastore. If they differ at all, it is assumed they are +// completely different datastores and a migration will be performed. Runtime +// values such as cache options or concurrency options should not be added +// here. type DiskSpec map[string]interface{} // Bytes returns a minimal JSON encoding of the DiskSpec @@ -68,6 +73,16 @@ func init() { } } +func AddDatastoreConfigHandler(name string, dsc ConfigFromMap) error { + _, ok := datastores[name] + if ok { + return fmt.Errorf("already have a datastore named %q", name) + } + + datastores[name] = dsc + return nil +} + // AnyDatastoreConfig returns a DatastoreConfig from a spec based on // the "type" parameter func AnyDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error) {