2015-06-12 16:51:57 -04:00
|
|
|
package libkv
|
|
|
|
|
|
|
|
import (
|
2015-09-16 14:54:28 -04:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
"github.com/docker/libkv/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Initialize creates a new Store object, initializing the client
|
|
|
|
type Initialize func(addrs []string, options *store.Config) (store.Store, error)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Backend initializers
|
2015-09-16 14:54:28 -04:00
|
|
|
initializers = make(map[store.Backend]Initialize)
|
|
|
|
|
|
|
|
supportedBackend = func() string {
|
|
|
|
keys := make([]string, 0, len(initializers))
|
|
|
|
for k := range initializers {
|
|
|
|
keys = append(keys, string(k))
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
return strings.Join(keys, ", ")
|
|
|
|
}()
|
2015-06-12 16:51:57 -04:00
|
|
|
)
|
|
|
|
|
2016-05-08 03:31:30 -04:00
|
|
|
// NewStore creates an instance of store
|
2015-06-12 16:51:57 -04:00
|
|
|
func NewStore(backend store.Backend, addrs []string, options *store.Config) (store.Store, error) {
|
|
|
|
if init, exists := initializers[backend]; exists {
|
|
|
|
return init(addrs, options)
|
|
|
|
}
|
|
|
|
|
2015-10-13 14:33:47 -04:00
|
|
|
return nil, fmt.Errorf("%s %s", store.ErrBackendNotSupported.Error(), supportedBackend)
|
2015-09-16 14:54:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddStore adds a new store backend to libkv
|
|
|
|
func AddStore(store store.Backend, init Initialize) {
|
|
|
|
initializers[store] = init
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|