libnetwork/kvstore: rewrite code for new location

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-06-26 18:25:01 +02:00
parent 3887475971
commit 5d25143ef3
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 13 additions and 16 deletions

View file

@ -10,8 +10,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/docker/libkv" store "github.com/docker/docker/libnetwork/internal/kvstore"
"github.com/docker/libkv/store"
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
) )
@ -27,7 +26,7 @@ const (
filePerm os.FileMode = 0644 filePerm os.FileMode = 0644
) )
//BoltDB type implements the Store interface // BoltDB type implements the Store interface
type BoltDB struct { type BoltDB struct {
client *bolt.DB client *bolt.DB
boltBucket []byte boltBucket []byte
@ -50,7 +49,7 @@ const (
// Register registers boltdb to libkv // Register registers boltdb to libkv
func Register() { func Register() {
libkv.AddStore(store.BOLTDB, New) store.AddStore(store.BOLTDB, New)
} }
// New opens a new BoltDB connection to the specified path and bucket // New opens a new BoltDB connection to the specified path and bucket
@ -167,7 +166,7 @@ func (b *BoltDB) Get(key string) (*store.KVPair, error) {
return &store.KVPair{Key: key, Value: val, LastIndex: (dbIndex)}, nil return &store.KVPair{Key: key, Value: val, LastIndex: (dbIndex)}, nil
} }
//Put the key, value pair. index number metadata is prepended to the value // Put the key, value pair. index number metadata is prepended to the value
func (b *BoltDB) Put(key string, value []byte, opts *store.WriteOptions) error { func (b *BoltDB) Put(key string, value []byte, opts *store.WriteOptions) error {
var ( var (
dbIndex uint64 dbIndex uint64
@ -203,7 +202,7 @@ func (b *BoltDB) Put(key string, value []byte, opts *store.WriteOptions) error {
return err return err
} }
//Delete the value for the given key. // Delete the value for the given key.
func (b *BoltDB) Delete(key string) error { func (b *BoltDB) Delete(key string) error {
var ( var (
db *bolt.DB db *bolt.DB

View file

@ -1,4 +1,4 @@
package store package kvstore
import ( import (
"crypto/tls" "crypto/tls"

View file

@ -1,19 +1,17 @@
package libkv package kvstore
import ( import (
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
"github.com/docker/libkv/store"
) )
// Initialize creates a new Store object, initializing the client // Initialize creates a new Store object, initializing the client
type Initialize func(addrs []string, options *store.Config) (store.Store, error) type Initialize func(addrs []string, options *Config) (Store, error)
var ( var (
// Backend initializers // Backend initializers
initializers = make(map[store.Backend]Initialize) initializers = make(map[Backend]Initialize)
supportedBackend = func() string { supportedBackend = func() string {
keys := make([]string, 0, len(initializers)) keys := make([]string, 0, len(initializers))
@ -25,16 +23,16 @@ var (
}() }()
) )
// NewStore creates an instance of store // New creates an instance of store
func NewStore(backend store.Backend, addrs []string, options *store.Config) (store.Store, error) { func New(backend Backend, addrs []string, options *Config) (Store, error) {
if init, exists := initializers[backend]; exists { if init, exists := initializers[backend]; exists {
return init(addrs, options) return init(addrs, options)
} }
return nil, fmt.Errorf("%s %s", store.ErrBackendNotSupported.Error(), supportedBackend) return nil, fmt.Errorf("%s %s", ErrBackendNotSupported.Error(), supportedBackend)
} }
// AddStore adds a new store backend to libkv // AddStore adds a new store backend to libkv
func AddStore(store store.Backend, init Initialize) { func AddStore(store Backend, init Initialize) {
initializers[store] = init initializers[store] = init
} }