11package datastore
22
33import (
4- "fmt"
4+ "errors"
5+ "path"
56 "strings"
67 "sync"
7- "time"
88
99 store "github.com/docker/docker/libnetwork/internal/kvstore"
1010 "github.com/docker/docker/libnetwork/internal/kvstore/boltdb"
@@ -50,18 +50,6 @@ type KVObject interface {
5050 CopyTo (KVObject ) error
5151}
5252
53- // ScopeCfg represents Datastore configuration.
54- type ScopeCfg struct {
55- Client ScopeClientCfg
56- }
57-
58- // ScopeClientCfg represents Datastore Client-only mode configuration
59- type ScopeClientCfg struct {
60- Provider string
61- Address string
62- Config * store.Config
63- }
64-
6553const (
6654 // NetworkKeyPrefix is the prefix for network key in the kv store
6755 NetworkKeyPrefix = "network"
7462 rootChain = defaultRootChain
7563)
7664
77- const defaultPrefix = "/var/lib/docker/network/files"
78-
79- // DefaultScope returns a default scope config for clients to use.
80- func DefaultScope (dataDir string ) ScopeCfg {
81- var dbpath string
82- if dataDir == "" {
83- dbpath = defaultPrefix + "/local-kv.db"
84- } else {
85- dbpath = dataDir + "/network/files/local-kv.db"
86- }
87-
88- return ScopeCfg {
89- Client : ScopeClientCfg {
90- Provider : string (store .BOLTDB ),
91- Address : dbpath ,
92- Config : & store.Config {
93- Bucket : "libnetwork" ,
94- ConnectionTimeout : time .Minute ,
95- },
96- },
97- }
98- }
99-
100- // IsValid checks if the scope config has valid configuration.
101- func (cfg * ScopeCfg ) IsValid () bool {
102- if cfg == nil || strings .TrimSpace (cfg .Client .Provider ) == "" || strings .TrimSpace (cfg .Client .Address ) == "" {
103- return false
104- }
105-
106- return true
107- }
65+ const DefaultBucket = "libnetwork"
10866
10967// Key provides convenient method to create a Key
11068func Key (key ... string ) string {
@@ -118,33 +76,23 @@ func Key(key ...string) string {
11876 return b .String ()
11977}
12078
121- // newClient used to connect to KV Store
122- func newClient ( kv string , addr string , config * store. Config ) (* Store , error ) {
123- if kv != string ( store . BOLTDB ) {
124- return nil , fmt . Errorf ( "unsupported KV store " )
79+ // New creates a new Store instance.
80+ func New ( dir , bucket string ) (* Store , error ) {
81+ if dir == "" {
82+ return nil , errors . New ( "empty dir " )
12583 }
126-
127- if config == nil {
128- config = & store.Config {}
84+ if bucket == "" {
85+ return nil , errors .New ("empty bucket" )
12986 }
13087
131- s , err := boltdb .New (addr , config )
88+ s , err := boltdb .New (path . Join ( dir , "local-kv.db" ), bucket )
13289 if err != nil {
13390 return nil , err
13491 }
13592
13693 return & Store {store : s , cache : newCache (s )}, nil
13794}
13895
139- // New creates a new Store instance.
140- func New (cfg ScopeCfg ) (* Store , error ) {
141- if cfg .Client .Provider == "" || cfg .Client .Address == "" {
142- cfg = DefaultScope ("" )
143- }
144-
145- return newClient (cfg .Client .Provider , cfg .Client .Address , cfg .Client .Config )
146- }
147-
14896// Close closes the data store.
14997func (ds * Store ) Close () {
15098 ds .store .Close ()
0 commit comments