forked from alash3al/goukv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
executable file
·65 lines (52 loc) · 1.26 KB
/
provider.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
59
60
61
62
63
64
65
package goukv
import (
"sync"
"time"
)
// providers a providers for available drivers
var (
providersMap = map[string]Provider{}
providersLock = &sync.RWMutex{}
)
// Provider an interface describes a storage backend
type Provider interface {
Open(*DSN) (Provider, error)
Put(*Entry) error
Get([]byte) ([]byte, error)
TTL([]byte) (*time.Time, error)
Delete([]byte) error
Batch([]*Entry) error
Scan(ScanOpts) error
Close() error
}
// Register register a new driver
func Register(name string, provider Provider) error {
providersLock.Lock()
defer providersLock.Unlock()
if providersMap[name] != nil {
return ErrDriverAlreadyExists
}
providersMap[name] = provider
return nil
}
// Get returns a driver from the registery
func Get(providerName string) (Provider, error) {
providersLock.Lock()
defer providersLock.Unlock()
if providersMap[providerName] == nil {
return nil, ErrDriverNotFound
}
return providersMap[providerName], nil
}
// Open initialize the specified provider and returns its instance
func Open(providerName, dsn string) (Provider, error) {
dsnParsed, err := NewDSN(dsn)
if err != nil {
return nil, err
}
providerInterface, err := Get(providerName)
if err != nil {
return nil, err
}
return providerInterface.Open(dsnParsed)
}