| 123456789101112131415161718192021222324252627282930313233343536373839 |
- package storage
- import (
- "fmt"
- "os"
- )
- const DEFAULT_STORAGE_ENGINE = "bolt"
- var supportedStorage = map[string]func(path string) (Storage, error){
- "kv": openKVStorage,
- "bolt": openBoltStorage,
- }
- func RegisterStorageEngine(name string, fn func(path string) (Storage, error)) {
- supportedStorage[name] = fn
- }
- type Storage interface {
- Set(k, v []byte) error
- Get(k []byte) ([]byte, error)
- Delete(k []byte) error
- ForEach(fn func(k, v []byte) error) error
- Close() error
- WALName() string
- }
- func OpenStorage(path string, engine string) (Storage, error) {
- if len(engine) <= 0 {
- engine = os.Getenv("WUKONG_STORAGE_ENGINE")
- if engine == "" {
- engine = DEFAULT_STORAGE_ENGINE
- }
- }
- if fn, has := supportedStorage[engine]; has {
- return fn(path)
- }
- return nil, fmt.Errorf("unsupported storage engine %v", engine)
- }
|