forked from absolute8511/ZanRedisDB
/
kvstore.go
139 lines (120 loc) · 3.02 KB
/
kvstore.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package node
import (
"errors"
"os"
"github.com/youzan/ZanRedisDB/common"
"github.com/youzan/ZanRedisDB/engine"
"github.com/youzan/ZanRedisDB/rockredis"
)
// a key-value store
type KVStore struct {
*rockredis.RockDB
opts *KVOptions
}
type KVOptions struct {
DataDir string
KeepBackup int
EngType string
ExpirationPolicy common.ExpirationPolicy
DataVersion common.DataVersionT
RockOpts engine.RockOptions
SharedConfig engine.SharedRockConfig
}
func NewKVStore(kvopts *KVOptions) (*KVStore, error) {
s := &KVStore{
opts: kvopts,
}
if err := s.openDB(); err != nil {
return nil, err
}
return s, nil
}
func (s *KVStore) openDB() error {
var err error
if s.opts.EngType == rockredis.EngType {
cfg := rockredis.NewRockRedisDBConfig()
cfg.DataDir = s.opts.DataDir
cfg.RockOptions = s.opts.RockOpts
cfg.ExpirationPolicy = s.opts.ExpirationPolicy
cfg.DataVersion = s.opts.DataVersion
cfg.SharedConfig = s.opts.SharedConfig
cfg.KeepBackup = s.opts.KeepBackup
s.RockDB, err = rockredis.OpenRockDB(cfg)
if err != nil {
nodeLog.Warningf("failed to open rocksdb: %v, %v", err, cfg.DataDir)
}
} else {
return errors.New("Not recognized engine type:" + s.opts.EngType)
}
return err
}
func (s *KVStore) CleanData() error {
if s.RockDB == nil {
nodeLog.Warningf("the db is not opened while clean data")
return nil
}
nodeLog.Infof("the store %v is cleaning data", s.opts.DataDir)
dataPath := s.GetDataDir()
s.Close()
os.RemoveAll(dataPath)
return s.openDB()
}
// the caller should make sure call this after close/stop finished.
// and no any more operation on this store during and after the destroy
func (s *KVStore) Destroy() error {
if s.RockDB != nil {
nodeLog.Infof("the store %v is destroyed", s.opts.DataDir)
dataPath := s.GetDataDir()
s.Close()
return os.RemoveAll(dataPath)
} else {
if s.opts.EngType == rockredis.EngType {
f, err := engine.GetDataDirFromBase(s.opts.RockOpts.EngineType, s.opts.DataDir)
if err != nil {
return err
}
return os.RemoveAll(f)
}
}
return nil
}
func (s *KVStore) LocalLookup(key []byte) ([]byte, error) {
value, err := s.KVGet(key)
return value, err
}
func (s *KVStore) LocalDelete(key []byte) (int64, error) {
return s.DelKeys(key)
}
func (s *KVStore) LocalPut(ts int64, key []byte, value []byte) error {
err := s.KVSet(ts, key, value)
return err
}
func (s *KVStore) LocalWriteBatch(cmd ...common.WriteCmd) error {
return nil
}
func (s *KVStore) IsBatchableWrite(cmdName string) bool {
if s.opts.EngType == rockredis.EngType {
return rockredis.IsBatchableWrite(cmdName)
} else {
return false
}
}
func (s *KVStore) BeginBatchWrite() error {
if s.opts.EngType == rockredis.EngType {
return s.RockDB.BeginBatchWrite()
} else {
return nil
}
}
func (s *KVStore) CommitBatchWrite() error {
if s.opts.EngType == rockredis.EngType {
return s.RockDB.CommitBatchWrite()
} else {
return nil
}
}
func (s *KVStore) AbortBatch() {
if s.opts.EngType == rockredis.EngType {
s.RockDB.AbortBatch()
}
}