-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
64 lines (55 loc) · 1.4 KB
/
map.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
package kasper
// Map wraps a map[string][]byte value and implements the Store interface.
type Map struct {
m map[string][]byte
}
// NewMap creates a new map of the given size.
func NewMap(size int) *Map {
return &Map{
make(map[string][]byte, size),
}
}
// Get gets a value by key. Returns (nil, nil) if the key is not present.
func (s *Map) Get(key string) ([]byte, error) {
src, found := s.m[key]
if !found {
return nil, nil
}
return src, nil
}
// GetAll returns multiple values by key. The returned map does not contain entries for missing documents.
func (s *Map) GetAll(keys []string) (map[string][]byte, error) {
kvs := make(map[string][]byte, len(keys))
for _, key := range keys {
value, _ := s.Get(key)
if value != nil {
kvs[key] = value
}
}
return kvs, nil
}
// Put inserts or updates a value by key.
func (s *Map) Put(key string, value []byte) error {
s.m[key] = value
return nil
}
// PutAll inserts or updates multiple key-value pairs.
func (s *Map) PutAll(kvs map[string][]byte) error {
for key, value := range kvs {
_ = s.Put(key, value)
}
return nil
}
// Delete removes a single value by key. Does not return an error if the key is not present.
func (s *Map) Delete(key string) error {
delete(s.m, key)
return nil
}
// Flush does nothing.
func (s *Map) Flush() error {
return nil
}
// GetMap returns the underlying map.
func (s *Map) GetMap() map[string][]byte {
return s.m
}