-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.go
67 lines (52 loc) · 1.33 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package kvstore
import (
"sync"
ipfslog "berty.tech/go-ipfs-log"
"berty.tech/go-orbit-db/iface"
"berty.tech/go-orbit-db/stores/operation"
"github.com/pkg/errors"
)
type kvIndex struct {
index map[string][]byte
muIndex sync.RWMutex
}
func (i *kvIndex) Get(key string) interface{} {
i.muIndex.RLock()
defer i.muIndex.RUnlock()
return i.index[key]
}
func (i *kvIndex) UpdateIndex(oplog ipfslog.Log, _ []ipfslog.Entry) error {
entries := oplog.Values().Slice()
size := len(entries)
handled := map[string]struct{}{}
i.muIndex.Lock()
defer i.muIndex.Unlock()
for idx := range entries {
item, err := operation.ParseOperation(entries[size-idx-1])
if err != nil {
return errors.Wrap(err, "unable to parse log kv operation")
}
key := item.GetKey()
if key == nil {
// ignoring entries with nil keys
continue
}
if _, ok := handled[*item.GetKey()]; !ok {
handled[*item.GetKey()] = struct{}{}
if item.GetOperation() == "PUT" {
i.index[*item.GetKey()] = item.GetValue()
} else if item.GetOperation() == "DEL" {
delete(i.index, *item.GetKey())
}
}
}
return nil
}
// NewKVIndex Creates a new Index instance for a KeyValue store
func NewKVIndex(_ []byte) iface.StoreIndex {
return &kvIndex{
index: map[string][]byte{},
}
}
var _ iface.IndexConstructor = NewKVIndex
var _ iface.StoreIndex = &kvIndex{}