-
Notifications
You must be signed in to change notification settings - Fork 33
/
database_memory.go
183 lines (148 loc) · 3.78 KB
/
database_memory.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
package db
import (
"encoding/gob"
"io"
"path"
"sort"
"strings"
)
// MemoryDatabase is a purely in memory database.
type MemoryDatabase struct {
data map[string][]byte
oldData map[string][]byte
haveWrites bool
refCount int
}
// a shallow copy is enough here.
func shallowCopyMap(src map[string][]byte) map[string][]byte {
dst := make(map[string][]byte)
for k, v := range src {
copyV := make([]byte, len(v))
copy(copyV, v)
dst[k] = copyV
}
return dst
}
// NewMemoryDatabase allocates a new empty MemoryDatabase
func NewMemoryDatabase() *MemoryDatabase {
return &MemoryDatabase{
data: make(map[string][]byte),
}
}
// Batch is a no-op for a memory database.
func (mdb *MemoryDatabase) Batch() Batch {
if mdb.refCount == 0 {
mdb.oldData = shallowCopyMap(mdb.data)
}
mdb.refCount++
return mdb
}
// Flush is a no-op for a memory database.
func (mdb *MemoryDatabase) Flush() error {
mdb.refCount--
if mdb.refCount == 0 {
mdb.haveWrites = false
}
return nil
}
// Rollback is a no-op for a memory database
func (mdb *MemoryDatabase) Rollback() {
if mdb.oldData != nil {
mdb.data = shallowCopyMap(mdb.oldData)
mdb.oldData = nil
}
mdb.refCount = 0
}
// Get returns `key` of `bucket`.
func (mdb *MemoryDatabase) Get(key ...string) ([]byte, error) {
data, ok := mdb.data[path.Join(key...)]
if !ok {
return nil, ErrNoSuchKey
}
return data, nil
}
// Put sets `key` in `bucket` to `data`.
func (mdb *MemoryDatabase) Put(data []byte, key ...string) {
mdb.haveWrites = true
mdb.data[path.Join(key...)] = data
}
// Clear removes all keys includin and below `key`.
func (mdb *MemoryDatabase) Clear(key ...string) error {
mdb.haveWrites = true
joinedKey := path.Join(key...)
for mapKey := range mdb.data {
if strings.HasPrefix(mapKey, joinedKey) {
delete(mdb.data, mapKey)
}
}
return nil
}
// Erase removes `key`
func (mdb *MemoryDatabase) Erase(key ...string) {
fullKey := path.Join(key...)
mdb.haveWrites = true
delete(mdb.data, fullKey)
}
// Keys will return all keys currently stored in the memory map
func (mdb *MemoryDatabase) Keys(fn func(key []string) error, prefix ...string) error {
keys := []string{}
for key := range mdb.data {
splitKey := strings.Split(key, "/")
hasPrefix := len(prefix) <= len(splitKey)
for i := 0; hasPrefix && i < len(prefix) && i < len(splitKey); i++ {
if prefix[i] != splitKey[i] {
hasPrefix = false
}
}
if hasPrefix {
keys = append(keys, key)
}
}
sort.Strings(keys)
for _, key := range keys {
if err := fn(strings.Split(key, "/")); err != nil {
return err
}
}
return nil
}
// HaveWrites returns true if there are any open writes.
func (mdb *MemoryDatabase) HaveWrites() bool {
return mdb.haveWrites
}
// Glob returns all keys starting with `prefix`.
func (mdb *MemoryDatabase) Glob(prefix []string) ([][]string, error) {
prefixKey := path.Join(prefix...)
var result [][]string
err := mdb.Keys(func(key []string) error {
fullKey := path.Join(key...)
if strings.HasPrefix(fullKey, prefixKey) {
// Filter "directories":
suffix := fullKey[len(prefixKey):]
if !strings.Contains(suffix, "/") {
result = append(result, strings.Split(fullKey, "/"))
}
}
return nil
})
if err != nil {
return nil, err
}
sort.Slice(result, func(i, j int) bool {
return path.Join(result[i]...) < path.Join(result[j]...)
})
return result, nil
}
// Export encodes the internal memory map to a gob structure,
// and writes it to `w`.
func (mdb *MemoryDatabase) Export(w io.Writer) error {
return gob.NewEncoder(w).Encode(mdb.data)
}
// Import imports a previously exported dump and decodes the gob structure.
func (mdb *MemoryDatabase) Import(r io.Reader) error {
return gob.NewDecoder(r).Decode(&mdb.data)
}
// Close the memory - a no op.
func (mdb *MemoryDatabase) Close() error {
return nil
}