-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
58 lines (45 loc) · 1.48 KB
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package goku
import "errors"
var (
activebackendType string
activeBackend Backend
stores = map[string]BackendFactory{}
NilValueErr = errors.New("No value found for specified key")
)
// BackendFactory is a func that can initialize and return a Backend implementation. This object is cached for later use
type BackendFactory func(string) (Backend, error)
// Backend is an interface for a storage backend
type Backend interface {
GetList(key string) ([][]byte, error)
Get(key string) ([]byte, error)
Put(key string, value []byte) error
Delete(key string) error
Close() error
}
// RegisterBackend registers a backend
func RegisterBackend(backendType string, bf BackendFactory) {
if backendType == "" {
panic("backendType cannot be empty")
}
stores[backendType] = bf
}
// NewBackend returns a new Backend implementation based on the backendType string. Uri can be a connection string or a file/directory path depending on the implementation being used
func NewBackend(backendType, uri string) (Backend, error) {
if activeBackend != nil {
if activebackendType != backendType {
return nil, errors.New("A backend with a different type has already been created")
}
return activeBackend, nil
}
backendFactory, ok := stores[backendType]
if !ok {
return nil, errors.New("backend type not registered")
}
backend, err := backendFactory(uri)
if err != nil {
return nil, err
}
activeBackend = backend
activebackendType = backendType
return backend, nil
}