/
db.go
252 lines (234 loc) · 5.63 KB
/
db.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package emssdb
import (
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
//"runtime"
"sync"
"time"
)
// DB emssdb DB struct
type DB struct {
db *leveldb.DB
options opt.Options
writer *Writer
expireDelay time.Duration
end bool
waitgroup sync.WaitGroup
}
// OpenDB open a emssdb by options
func OpenDB(options Options) (that *DB, err error) {
mainDBPath := options.Path
cacheSize := options.CacheSize
writeBufferSize := 4
blockSize := 4
compression := options.Compression
if options.ExpireDelay <= time.Second {
options.ExpireDelay = time.Second
}
if cacheSize <= 0 {
cacheSize = 8
}
//log::path,cacheSize,blockSize,write_buffer,compression
var d DB
d.options.ErrorIfMissing = false
d.options.Filter = filter.NewBloomFilter(10)
//d.Options.BlockCacher = leveldb::NewLRUCache(cacheSize * 1048576)
d.options.BlockCacheCapacity = cacheSize * 1024 * 1024
d.options.BlockSize = blockSize * 1024
d.options.WriteBuffer = writeBufferSize * 1024 * 1024
d.expireDelay = options.ExpireDelay
if compression {
d.options.Compression = opt.SnappyCompression
} else {
d.options.Compression = opt.NoCompression
}
if tdb, err := leveldb.OpenFile(mainDBPath, &d.options); err == nil {
//runtime.SetFinalizer(d,
// func(d *DB) {
// d.db.Close()
// })
d.db = tdb
d.writer = NewWriter(d.db)
go d.expireDaemon()
return &d, nil
} else {
return nil, err
}
}
// Close close emssdb
func (d *DB) Close() {
d.end = true
d.waitgroup.Wait()
d.db.Close()
}
// return (start, end], not include start
func (d *DB) Iterator(start Bytes, end Bytes) (ret *Iterator) {
if len(start) == 0 {
start = nil
}
if len(end) == 0 {
end = nil
}
var iopt opt.ReadOptions
iopt.DontFillCache = true
it := d.db.NewIterator(&util.Range{Start: start, Limit: end}, &iopt)
return NewIterator(it, FORWARD)
}
func (d *DB) RevIterator(start Bytes, end Bytes) (ret *Iterator) {
if len(start) == 0 {
start = nil
}
if len(end) == 0 {
end = nil
}
var iopt opt.ReadOptions
iopt.DontFillCache = true
it := d.db.NewIterator(&util.Range{Start: start, Limit: end}, &iopt)
it.Last()
it.Next()
return NewIterator(it, BACKWARD)
}
func (d *DB) Info() (ret map[string]string) {
// "leveldb.num-files-at-level<N>" - return the number of files at level <N>,
// where <N> is an ASCII representation of a level number (e.g. "0").
// "leveldb.stats" - returns a multi-line string that describes statistics
// about the internal operation of the DB.
// "leveldb.sstables" - returns a multi-line string that describes all
// of the sstables that make up the db contents.
var keys []string
info := make(map[string]string)
//for i := 0; i < 7; i++ {
// s := fmt.Sprintf("leveldb.num-files-at-level%d", i)
// keys = append(keys, s)
//}
keys = append(keys, "leveldb.stats")
//keys = append(keys, "leveldb.sstables")
for _, key := range keys {
if val, err := d.db.GetProperty(key); err == nil {
info[key] = val
}
}
return info
}
func (d *DB) Compact() (err error) {
return d.db.CompactRange(util.Range{})
}
func (d *DB) KeyRange(keys []string) {
// int ret = 0;
// std::string kstart, kend;
// std::string hstart, hend;
// std::string zstart, zend;
//
// Iterator *it;
//
// it = d->iterator(encode_kv_key(""), "", 1);
// if(it->next()){
// Bytes ks = it->key();
// if(ks.data()[0] == DataType::KV){
// std::string n;
// if(decode_kv_key(ks, &n) == -1){
// ret = -1;
// }else{
// kstart = n;
// }
// }
// }
// delete it;
//
// it = d->rev_iterator(encode_kv_key("\xff"), "", 1);
// if(it->next()){
// Bytes ks = it->key();
// if(ks.data()[0] == DataType::KV){
// std::string n;
// if(decode_kv_key(ks, &n) == -1){
// ret = -1;
// }else{
// kend = n;
// }
// }
// }
// delete it;
//
// it = d->iterator(encode_hsize_key(""), "", 1);
// if(it->next()){
// Bytes ks = it->key();
// if(ks.data()[0] == DataType::HSIZE){
// std::string n;
// if(decode_hsize_key(ks, &n) == -1){
// ret = -1;
// }else{
// hstart = n;
// }
// }
// }
// delete it;
//
// it = d->rev_iterator(encode_hsize_key("\xff"), "", 1);
// if(it->next()){
// Bytes ks = it->key();
// if(ks.data()[0] == DataType::HSIZE){
// std::string n;
// if(decode_hsize_key(ks, &n) == -1){
// ret = -1;
// }else{
// hend = n;
// }
// }
// }
// delete it;
//
// it = d->iterator(encode_zsize_key(""), "", 1);
// if(it->next()){
// Bytes ks = it->key();
// if(ks.data()[0] == DataType::ZSIZE){
// std::string n;
// if(decode_hsize_key(ks, &n) == -1){
// ret = -1;
// }else{
// zstart = n;
// }
// }
// }
// delete it;
//
// it = d->rev_iterator(encode_zsize_key("\xff"), "", 1);
// if(it->next()){
// Bytes ks = it->key();
// if(ks.data()[0] == DataType::ZSIZE){
// std::string n;
// if(decode_hsize_key(ks, &n) == -1){
// ret = -1;
// }else{
// zend = n;
// }
// }
// }
// delete it;
//
// keys->push_back(kstart);
// keys->push_back(kend);
// keys->push_back(hstart);
// keys->push_back(hend);
// keys->push_back(zstart);
// keys->push_back(zend);
//
// return ret;
}
//
// /* raw operates */
//
// // repl: whether to sync d operation to slaves
func (d *DB) RawSet(key Bytes, val Bytes) (err error) {
//var writeOpts opt.WriteOptions
return d.db.Put(key, val, nil)
}
func (d *DB) RawDel(key Bytes) (err error) {
//var writeOpts opt.WriteOptions
return d.db.Delete(key, nil)
}
func (d *DB) RawGet(key Bytes) (val Bytes, err error) {
//var writeOpts opt.WriteOptions
return d.db.Get(key, nil)
}