-
Notifications
You must be signed in to change notification settings - Fork 3
/
inmem.go
154 lines (131 loc) · 3.01 KB
/
inmem.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
package planb
import (
"bufio"
"encoding/binary"
"errors"
"io"
"sync"
)
var errInvalidStorageKey = errors.New("planb: invalid storage key")
const numInMemShards = 64
type InmemStore struct {
shards [numInMemShards]*inMemShard
}
// NewInmemStore opens a new simplistic, non-transactional in-memory KVStore
func NewInmemStore() *InmemStore {
store := new(InmemStore)
for i := 0; i < numInMemShards; i++ {
store.shards[i] = &inMemShard{data: make(map[string][]byte)}
}
return store
}
// Get retrieves a key
func (s *InmemStore) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errInvalidStorageKey
}
val := s.shards[fnv32a(key)%numInMemShards].Get(key)
return val, nil
}
// Put sets a key
func (s *InmemStore) Put(key, val []byte) error {
if len(key) == 0 {
return errInvalidStorageKey
}
s.shards[fnv32a(key)%numInMemShards].Put(key, val)
return nil
}
// Delete deletes a key
func (s *InmemStore) Delete(key []byte) error {
return s.Put(key, nil)
}
// Snapshot implements Store
func (s *InmemStore) Snapshot(w io.Writer) error {
buf := make([]byte, binary.MaxVarintLen64)
for i := 0; i < numInMemShards; i++ {
if err := s.shards[i].Snapshot(buf, w); err != nil {
return err
}
}
return nil
}
// Restore implements Store
func (s *InmemStore) Restore(r io.Reader) error {
snap := &inMemSnapshotIterator{Reader: bufio.NewReader(r)}
for {
err := snap.Next()
if err == io.EOF {
return nil
} else if err != nil {
return err
}
if err := s.Put(snap.key, snap.val); err != nil {
return err
}
}
}
// --------------------------------------------------------------------
type inMemShard struct {
data map[string][]byte
mu sync.RWMutex
}
func (s *inMemShard) Snapshot(buf []byte, w io.Writer) error {
s.mu.RLock()
defer s.mu.RUnlock()
for key, val := range s.data {
n := binary.PutUvarint(buf[:binary.MaxVarintLen64], uint64(len(key)))
if _, err := w.Write(buf[:n]); err != nil {
return err
}
if _, err := io.WriteString(w, key); err != nil {
return err
}
n = binary.PutUvarint(buf[:binary.MaxVarintLen64], uint64(len(val)))
if _, err := w.Write(buf[:n]); err != nil {
return err
}
if _, err := w.Write(val); err != nil {
return err
}
}
return nil
}
func (s *inMemShard) Get(key []byte) []byte {
s.mu.RLock()
val := s.data[string(key)]
s.mu.RUnlock()
return val
}
func (s *inMemShard) Put(key, val []byte) {
s.mu.Lock()
defer s.mu.Unlock()
if val == nil {
delete(s.data, string(key))
} else {
s.data[string(key)] = val
}
}
// --------------------------------------------------------------------
type inMemSnapshotIterator struct {
*bufio.Reader
key, val []byte
}
func (s *inMemSnapshotIterator) Next() error {
u, err := binary.ReadUvarint(s)
if err != nil {
return err
}
s.key = make([]byte, int(u))
if _, err := io.ReadFull(s, s.key); err != nil {
return err
}
u, err = binary.ReadUvarint(s)
if err != nil {
return err
}
s.val = make([]byte, int(u))
if _, err := io.ReadFull(s, s.val); err != nil {
return err
}
return nil
}