Refactor libkv to not directly import storage backends

Signed-off-by: Alexandre Beslic <abronan@docker.com>
This commit is contained in:
Alexandre Beslic 2015-09-07 10:43:01 -07:00
parent b1d5d7edeb
commit 3ec6dfa346
2 changed files with 13 additions and 10 deletions

View file

@ -9,6 +9,7 @@ import (
"sync/atomic"
"github.com/boltdb/bolt"
"github.com/docker/libkv"
"github.com/docker/libkv/store"
)
@ -35,6 +36,11 @@ const (
libkvmetadatalen = 8
)
// Register registers boltdb to libkv
func Register() {
libkv.AddStore(store.BOLTDB, New)
}
// New opens a new BoltDB connection to the specified path and bucket
func New(endpoints []string, options *store.Config) (store.Store, error) {
if len(endpoints) > 1 {

View file

@ -67,10 +67,6 @@ import (
"strings"
"github.com/docker/libkv/store"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libkv/store/consul"
"github.com/docker/libkv/store/etcd"
"github.com/docker/libkv/store/zookeeper"
)
// Initialize creates a new Store object, initializing the client
@ -78,12 +74,8 @@ type Initialize func(addrs []string, options *store.Config) (store.Store, error)
var (
// Backend initializers
initializers = map[store.Backend]Initialize{
store.CONSUL: consul.New,
store.ETCD: etcd.New,
store.ZK: zookeeper.New,
store.BOLTDB: boltdb.New,
}
initializers = make(map[store.Backend]Initialize)
supportedBackend = func() string {
keys := make([]string, 0, len(initializers))
for k := range initializers {
@ -102,3 +94,8 @@ func NewStore(backend store.Backend, addrs []string, options *store.Config) (sto
return nil, fmt.Errorf("%s %s", store.ErrNotSupported.Error(), supportedBackend)
}
// AddStore adds a new store backend to libkv
func AddStore(store store.Backend, init Initialize) {
initializers[store] = init
}