/
kv.go
98 lines (87 loc) · 3.31 KB
/
kv.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
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logdb
import (
"github.com/lni/dragonboat/raftio"
)
// IKvStore is the interface used by the RDB struct to access the underlying
// Key-Value store.
type IKvStore interface {
// Name is the IKvStore name.
Name() string
// Close closes the underlying Key-Value store.
Close() error
// IterateValue iterates the key range specified by the first key fk and
// last key lk. The inc boolean flag indicates whether it is inclusive for
// the last key lk. For each iterated entry, the specified op will be invoked
// on that key-value pair, the specified op func returns a boolean flag to
// indicate whether IterateValue should continue to iterate entries.
IterateValue(fk []byte,
lk []byte, inc bool, op func(key []byte, data []byte) (bool, error))
// GetValue queries the value specified the input key, the returned value
// byte slice is passed to the specified op func.
GetValue(key []byte, op func([]byte) error) error
// Save value saves the specified key value pair to the underlying key-value
// pair.
SaveValue(key []byte, value []byte) error
// DeleteValue deletes the key-value pair specified by the input key.
DeleteValue(key []byte) error
// GetWriteBatch returns an IWriteBatch object to be used by RDB.
GetWriteBatch(ctx raftio.IContext) IWriteBatch
// CommitWriteBatch atomically writes everything included in the write batch
// to the underlying key-value store.
CommitWriteBatch(wb IWriteBatch) error
// RemoveEntries removes entries specified by the range [firstKey, lastKey).
// RemoveEntries is called in the main execution thread of raft, it is
// suppose to immediately return without significant delay.
RemoveEntries(firstKey []byte, lastKey []byte) error
// Compaction is called by the compaction goroutine to compact the key-value
// store for the specified range [firstKey, lastKey). This method is expected
// to complete in the order of seconds.
Compaction(firstKey []byte, lastKey []byte) error
}
// IWriteBatch is the interface representing a write batch capable of
// atomically writing many key-value pairs to the key-value store.
type IWriteBatch interface {
Destroy()
Put([]byte, []byte)
Clear()
Count() int
}
type kvpair struct {
key []byte
val []byte
}
type simpleWriteBatch struct {
vals []kvpair
}
func newSimpleWriteBatch() *simpleWriteBatch {
return &simpleWriteBatch{vals: make([]kvpair, 0)}
}
func (wb *simpleWriteBatch) Destroy() {
wb.vals = nil
}
func (wb *simpleWriteBatch) Put(key []byte, val []byte) {
k := make([]byte, len(key))
v := make([]byte, len(val))
copy(k, key)
copy(v, val)
wb.vals = append(wb.vals, kvpair{key: k, val: v})
}
func (wb *simpleWriteBatch) Clear() {
wb.vals = make([]kvpair, 0)
}
func (wb *simpleWriteBatch) Count() int {
return len(wb.vals)
}