forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_memory_index.go
52 lines (40 loc) · 1.04 KB
/
in_memory_index.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
package index
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type inMemoryIndex struct {
entryMap map[string][]byte
}
func NewInMemoryIndex() Index {
return &inMemoryIndex{
entryMap: map[string][]byte{},
}
}
func (ri *inMemoryIndex) Find(key interface{}, valuePtr interface{}) error {
keyBytes, err := json.Marshal(key)
if err != nil {
return bosherr.WrapErrorf(err, "Marshalling key %#v", key)
}
valueBytes, exists := ri.entryMap[string(keyBytes)]
if !exists {
return ErrNotFound
}
err = json.Unmarshal(valueBytes, valuePtr)
if err != nil {
return bosherr.WrapErrorf(err, "Unmarshaling value for key %#v", key)
}
return nil
}
func (ri *inMemoryIndex) Save(key interface{}, value interface{}) error {
keyBytes, err := json.Marshal(key)
if err != nil {
return bosherr.WrapErrorf(err, "Marshalling key %#v", key)
}
valueBytes, err := json.Marshal(value)
if err != nil {
return bosherr.WrapErrorf(err, "Marshalling value %#v", value)
}
ri.entryMap[string(keyBytes)] = valueBytes
return nil
}