forked from smallnest/kvbench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapstore.go
More file actions
165 lines (153 loc) · 3.08 KB
/
mapstore.go
File metadata and controls
165 lines (153 loc) · 3.08 KB
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
package kvbench
import (
"strings"
"sync"
"time"
"github.com/tidwall/match"
)
type mapStore struct {
mu sync.RWMutex
keys map[string][]byte
aof *AOF
}
func NewMapStore(path string, fsync bool) (Store, error) {
keys := make(map[string][]byte)
var err error
var aof *AOF
if path == ":memory:" {
log.Printf("persistance disabled")
} else {
var count int
start := time.Now()
aof, err = openAOF(path, fsync, func(args [][]byte) error {
switch strings.ToLower(string(args[0])) {
case "set":
if len(args) >= 3 {
keys[string(args[1])] = bcopy(args[2])
}
case "del":
if len(args) >= 2 {
delete(keys, string(args[1]))
}
case "flushdb":
keys = make(map[string][]byte)
}
count++
return nil
})
if err != nil {
return nil, err
}
if count > 0 {
log.Printf("loaded %d commands in %s", count, time.Since(start))
}
}
return &mapStore{
aof: aof,
keys: keys,
}, nil
}
func (s *mapStore) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.aof != nil {
s.aof.Close()
}
return nil
}
func (s *mapStore) PSet(keys, values [][]byte) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.aof != nil {
s.aof.BeginBuffer()
for i := range keys {
s.aof.AppendBuffer([]byte("set"), keys[i], values[i])
}
err := s.aof.WriteBuffer()
if err != nil {
return err
}
}
for i := range keys {
s.keys[string(keys[i])] = bcopy(values[i])
}
return nil
}
func (s *mapStore) PGet(keys [][]byte) ([][]byte, []bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var values [][]byte
var oks []bool
for i := range keys {
v, ok := s.keys[string(keys[i])]
if !ok {
values = append(values, nil)
oks = append(oks, false)
} else {
values = append(values, v)
oks = append(oks, true)
}
}
return values, oks, nil
}
func (s *mapStore) Set(key, value []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.aof != nil {
if err := s.aof.Write([]byte("set"), key, value); err != nil {
return err
}
}
s.keys[string(key)] = bcopy(value)
return nil
}
func (s *mapStore) Get(key []byte) ([]byte, bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
v, ok := s.keys[string(key)]
return v, ok, nil
}
func (s *mapStore) Del(key []byte) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.keys[string(key)]
if ok {
if s.aof != nil {
if err := s.aof.Write([]byte("del"), key); err != nil {
return false, err
}
}
delete(s.keys, string(key))
}
return ok, nil
}
func (s *mapStore) Keys(pattern []byte, limit int, withvalues bool) ([][]byte, [][]byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
spattern := string(pattern)
var keys [][]byte
var vals [][]byte
for key, value := range s.keys {
if limit > -1 && len(keys) >= limit {
break
}
if match.Match(key, spattern) {
keys = append(keys, []byte(key))
if withvalues {
vals = append(vals, bcopy(value))
}
}
}
return keys, vals, nil
}
func (s *mapStore) FlushDB() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.aof != nil {
if err := s.aof.Write([]byte("flushdb")); err != nil {
return err
}
}
s.keys = make(map[string][]byte)
return nil
}