forked from akrennmair/epos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backends.go
43 lines (36 loc) · 1.33 KB
/
backends.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
package epos
import (
"fmt"
)
type StorageType string
const (
STORAGE_AUTO StorageType = "auto"
STORAGE_DISKV StorageType = "diskv"
STORAGE_LEVELDB StorageType = "leveldb"
)
type StorageBackend interface {
Read(key string) ([]byte, error)
Write(key string, value []byte) error
Erase(key string) error
Keys() <-chan string
}
var storageBackends map[StorageType]func(string) StorageBackend
func init() {
storageBackends = make(map[StorageType]func(string) StorageBackend)
RegisterStorageBackend(string(STORAGE_LEVELDB), NewLevelDBStorageBackend)
RegisterStorageBackend(string(STORAGE_DISKV), NewDiskvStorageBackend)
}
// RegisterStorageBackend registers a new custom storage backend under a new
// name. If the name is already used, an error is returned.
//
// In order to create a new custom storage backend, the programmer must also
// provide a function that takes the path where the storage backend must write
// its data (as a single file or within a directory) and that returns an object
// that satisfies the interface StorageBackend
func RegisterStorageBackend(name string, factoryFunc func(path string) StorageBackend) error {
if _, contains := storageBackends[StorageType(name)]; contains {
return fmt.Errorf("storage backend %s already registered", name)
}
storageBackends[StorageType(name)] = factoryFunc
return nil
}