-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv.go
68 lines (57 loc) · 1.3 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
// package sdk defined spanner sdk for tikv transaction
package sdk
import (
"fmt"
"sync"
"go.uber.org/zap"
)
// Key give out spanner sdk key
type Key string
func (k Key) String() string { return string(k) }
// Value defined a byte slice for transaction sdk value field
type Value struct {
Type Type
Value []byte
}
func (v Value) String() string {
return fmt.Sprintf("value: %v, type: %v", string(v.Value), v.Type)
}
// warpKV to operation for pd client
func warpKV(k Key, v Value) Operation {
return Operation{
Type: v.Type,
Key: []byte(k),
Value: v.Value,
}
}
// KeyStore store all keys locally
// TODO should be an interface
type KeyStore struct {
sync.Mutex
store map[Key]Value
}
// NewKVStore return a local in memory kv storage
func NewKVStore() *KeyStore {
return &KeyStore{
store: make(map[Key]Value),
}
}
func (s *KeyStore) SetOp(k, v []byte, op Type) {
s.Lock()
s.store[Key(k)] = Value{
Type: OpPut,
Value: v,
}
s.Unlock()
}
// GetAllOperations return all k-v pairs store in kvstore in operation set
func (s *KeyStore) GetAllOperations() []Operation {
resultSet := make([]Operation, 0)
s.Lock()
defer s.Unlock()
for k, v := range s.store {
resultSet = append(resultSet, warpKV(k, v))
}
lg.Debug("call get all operations", zap.Int("len", len(resultSet)))
return resultSet
}