-
Notifications
You must be signed in to change notification settings - Fork 1
/
obj_store.go
107 lines (83 loc) · 2.07 KB
/
obj_store.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
// package object_kvstore define a generic wrapper
// store for a protobuff serializable entity
package raccoon
type ObjectPredicate[Obj any] func(Obj) bool
type ObjectStore[Obj any] struct {
store KVStore
marshaler Marshaler[Obj]
ider Ider[Obj]
}
func NewObjStore[O any](kv KVStore, marshaler Marshaler[O], ider Ider[O]) ObjectStore[O] {
return ObjectStore[O]{
store: kv,
marshaler: marshaler,
ider: ider,
}
}
func (s *ObjectStore[Obj]) GetObject(id []byte) (Option[Obj], error) {
bytes, err := s.store.Get(id)
if err != nil || bytes == nil {
return None[Obj](), err
}
obj, err := s.marshaler.Unmarshal(bytes)
if err != nil {
return None[Obj](), err
}
return Some(obj), nil
}
func (s *ObjectStore[Obj]) SetObject(obj Obj) error {
bytes, err := s.marshaler.Marshal(&obj)
if err != nil {
return err
}
key := s.ider.Id(obj)
return s.store.Set(key, bytes)
}
func (s *ObjectStore[Obj]) DeleteById(id []byte) error {
return s.store.Delete(id)
}
func (s *ObjectStore[Obj]) Delete(obj Obj) error {
id := s.ider.Id(obj)
return s.DeleteById(id)
}
func (s *ObjectStore[Obj]) HasById(id []byte) (bool, error) {
return s.store.Has(id)
}
func (s *ObjectStore[Obj]) Has(obj Obj) (bool, error) {
id := s.ider.Id(obj)
return s.store.Has(id)
}
func (s *ObjectStore[Obj]) ListIds() ([][]byte, error) {
iter := s.store.Iterator(nil, nil)
var ids [][]byte
for ; iter.Valid(); iter.Next() {
key := iter.Key()
if err := iter.Error(); err != nil {
return nil, err
}
ids = append(ids, key)
}
return ids, nil
}
func (s *ObjectStore[Obj]) List() ([]Obj, error) {
identity := func(o Obj) bool { return true }
return s.Filter(identity)
}
func (s *ObjectStore[Obj]) Filter(predicate ObjectPredicate[Obj]) ([]Obj, error) {
iter := s.store.Iterator(nil, nil)
var objs []Obj
for ; iter.Valid(); iter.Next() {
if err := iter.Error(); err != nil {
return nil, err
}
value := iter.Value()
obj, err := s.marshaler.Unmarshal(value)
if err != nil {
return nil, err
}
if predicate(obj) {
objs = append(objs, obj)
}
}
return objs, nil
}