-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstores.go
164 lines (136 loc) · 4.59 KB
/
stores.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
package raccoon
const (
inPrefix = "/in"
outPrefix = "/out"
)
// TODO refactor type constraints to DirectedEdge
// Store Wrapper for records indexed by a certain field
// Keys in store have the pattern: {prefix}/{indexName}/{mappedValue}/{edgeKey}
type SecondaryIndexStore[Edg Edge[N], N any] struct {
store KVStore
prefix []byte
indexName string
mapper Mapper[Edg]
keyer SecondaryIdxKeyer[Edg, N]
marshaler Marshaler[Edg]
}
func NewSecondaryIdxStore[Edg Edge[N], N any](store KVStore, prefix []byte, indexName string, keyer NodeKeyer[N], mapper Mapper[Edg], marshaler Marshaler[Edg]) SecondaryIndexStore[Edg, N] {
idxKeyer := NewSecIdxKeyer[Edg, N](keyer, mapper, indexName, prefix)
return SecondaryIndexStore[Edg, N]{
store: store,
prefix: prefix,
indexName: indexName,
mapper: mapper,
keyer: idxKeyer,
marshaler: marshaler,
}
}
func (s *SecondaryIndexStore[Edg, N]) Set(edg Edg) error {
bytes, err := s.marshaler.Marshal(&edg)
if err != nil {
return err
}
key := s.keyer.Key(edg)
s.store.Set(key.ToBytes(), bytes)
return nil
}
func (s *SecondaryIndexStore[Edg, N]) Delete(edg Edg) error {
key := s.keyer.Key(edg)
s.store.Delete(key.ToBytes())
return nil
}
func (s *SecondaryIndexStore[Edg, N]) GetByIdx(idx []byte) ([]Edg, error) {
min, max := s.keyer.IterKeys(idx)
iter := s.store.Iterator(min.ToBytes(), max.ToBytes())
return consumeIter[Edg](iter, s.marshaler)
}
func (s *SecondaryIndexStore[Edg, N]) FilterByIdx(idx []byte, predicate Predicate[Edg]) ([]Edg, error) {
min, max := s.keyer.IterKeys(idx)
iter := s.store.Iterator(min.ToBytes(), max.ToBytes())
return filterConsumeIter(iter, s.marshaler, predicate)
}
// Store for Graph Edges
//
// Provides the usual Get, Set, Delete operations for records.
// Also exposes a higher level operation to return all records which
// contain a Node's sucessor and ancestor.
//
// EdgeStore maintains an internal index for a node's ancestors and sucessors,
// allowing for fast lookup.
// Setting and Deleting records updates internal indexes to guarantee consistency.
// TODO make it atomic
type EdgeStore[Edg Edge[N], N any] struct {
store KVStore
prefix []byte
keyer EdgeKeyer[N]
marshaler Marshaler[Edg]
}
func NewEdgeStore[Edg Edge[N], N any](store KVStore, prefix []byte, keyer NodeKeyer[N], marshaler Marshaler[Edg]) EdgeStore[Edg, N] {
base := Key{}.Append(prefix)
edgKeyer := NewEdgeKeyer[N](keyer, base, []byte(inPrefix), []byte(outPrefix))
return EdgeStore[Edg, N]{
store: store,
prefix: prefix,
keyer: edgKeyer,
marshaler: marshaler,
}
}
// Set a record in the backend storage engine.
func (s *EdgeStore[Edg, N]) Set(edg Edg) error {
bytes, err := s.marshaler.Marshal(&edg)
if err != nil {
return err
}
src := edg.GetSource()
dst := edg.GetDest()
outKey := s.keyer.OutgoingKey(src, dst)
s.store.Set(outKey.ToBytes(), bytes)
inKey := s.keyer.IncomingKey(src, dst)
s.store.Set(inKey.ToBytes(), bytes)
return nil
}
// Fetch stored Record for the given edge.
// Return record, an ok flag and a potential error
// If the OK flag is false, no record was found or an error ocurred
func (s *EdgeStore[Edg, N]) GetEdg(source, dest N) (Option[Edg], error) {
key := s.keyer.OutgoingKey(source, dest)
bytes, err := s.store.Get(key.ToBytes())
if bytes == nil || err != nil {
return None[Edg](), nil
}
edg, err := s.marshaler.Unmarshal(bytes)
if err != nil {
return None[Edg](), err
}
return Some(edg), nil
}
// Delete record associated to edge entry from store
func (s *EdgeStore[Edg, N]) Delete(edg Edg) error {
source := edg.GetSource()
dest := edg.GetDest()
key := s.keyer.IncomingKey(source, dest)
s.store.Delete(key.ToBytes())
key = s.keyer.OutgoingKey(source, dest)
s.store.Delete(key.ToBytes())
return nil
}
// Return all Records in which `node` is the edge's source
func (s *EdgeStore[Edg, N]) GetSucessors(node N) ([]Edg, error) {
min, max := s.keyer.SucessorsIterKeys(node)
iter := s.store.Iterator(min.ToBytes(), max.ToBytes())
return consumeIter[Edg](iter, s.marshaler)
}
// Return all Records in which `node` is the edge's target
func (s *EdgeStore[Edg, N]) GetAncestors(node N) ([]Edg, error) {
min, max := s.keyer.AncestorsIterKey(node)
iter := s.store.Iterator(min.ToBytes(), max.ToBytes())
return consumeIter[Edg](iter, s.marshaler)
}
// List dumps all Edges in the EdgeStore.
// Performs pre-loading as opposed to streaming
func (s *EdgeStore[Edg, N]) List() ([]Edg, error) {
start, end := s.keyer.AllEdges()
iter := s.store.Iterator(start.ToBytes(), end.ToBytes())
return consumeIter[Edg](iter, s.marshaler)
}
// TODO define FilterAncestors and FilterSucessors