-
-
Notifications
You must be signed in to change notification settings - Fork 402
/
indexer.go
245 lines (216 loc) · 6.5 KB
/
indexer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (C) 2017 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy ofthe License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specificlanguage governing permissions and
* limitations under the License.
*
*/
package graph
import (
"github.com/cnf/structhash"
"github.com/safchain/insanelock"
)
// NodeHasher describes a callback that is called to map a node to
// a set of hash,value pairs
type NodeHasher func(n *Node) map[string]interface{}
// Hash computes the hash of the passed parameters
func Hash(values ...interface{}) string {
if len(values) == 1 {
if s, ok := values[0].(string); ok {
return s
}
}
h, _ := structhash.Hash(values, 1)
return h
}
// Indexer provides a way to index graph nodes. A node can be mapped to
// multiple hash,value pairs. A hash can also be mapped to multiple nodes.
type Indexer struct {
insanelock.RWMutex
DefaultGraphListener
graph *Graph
eventHandler *EventHandler
listenerHandler ListenerHandler
hashNode NodeHasher
appendOnly bool
hashToValues map[string]map[Identifier]interface{}
nodeToHashes map[Identifier]map[string]bool
}
// Get computes the hash of the passed parameters and returns the matching
// nodes with their respective value
func (i *Indexer) Get(values ...interface{}) ([]*Node, []interface{}) {
return i.FromHash(Hash(values...))
}
// GetNode computes the hash of the passed parameters and returns the first
// matching node with its respective value
func (i *Indexer) GetNode(values ...interface{}) (*Node, interface{}) {
nodes, values := i.Get(values...)
if len(nodes) > 0 && len(values) > 0 {
return nodes[0], values[0]
}
return nil, nil
}
func (i *Indexer) index(id Identifier, h string, value interface{}) {
if _, found := i.hashToValues[h]; !found {
i.hashToValues[h] = make(map[Identifier]interface{})
}
i.hashToValues[h][id] = value
i.nodeToHashes[id][h] = true
}
func (i *Indexer) unindex(id Identifier, h string) {
delete(i.hashToValues[h], id)
if len(i.hashToValues[h]) == 0 {
delete(i.hashToValues, h)
}
}
// Index indexes a node with a set of hash -> value map
func (i *Indexer) Index(id Identifier, n *Node, kv map[string]interface{}) {
i.Lock()
defer i.Unlock()
if hashes, found := i.nodeToHashes[id]; !found {
// Node was not in the cache
i.nodeToHashes[id] = make(map[string]bool)
for k, v := range kv {
i.index(id, k, v)
}
i.eventHandler.NotifyEvent(NodeAdded, n)
} else {
// Node already was in the cache
if !i.appendOnly {
for h := range hashes {
if _, found := kv[h]; !found {
i.unindex(id, h)
}
}
}
for k, v := range kv {
i.index(id, k, v)
}
i.eventHandler.NotifyEvent(NodeUpdated, n)
}
}
// Unindex removes the node and its associated hashes from the index
func (i *Indexer) Unindex(id Identifier, n *Node) {
i.Lock()
defer i.Unlock()
if hashes, found := i.nodeToHashes[id]; found {
delete(i.nodeToHashes, id)
for h := range hashes {
delete(i.hashToValues[h], id)
}
i.eventHandler.NotifyEvent(NodeDeleted, n)
}
}
// OnNodeAdded event
func (i *Indexer) OnNodeAdded(n *Node) {
if kv := i.hashNode(n); len(kv) != 0 {
i.Index(n.ID, n, kv)
}
}
// OnNodeUpdated event
func (i *Indexer) OnNodeUpdated(n *Node, ops []PartiallyUpdatedOp) {
if kv := i.hashNode(n); len(kv) != 0 {
i.Index(n.ID, n, kv)
} else {
i.Unindex(n.ID, n)
}
}
// OnNodeDeleted event
func (i *Indexer) OnNodeDeleted(n *Node) {
i.Unindex(n.ID, n)
}
// FromHash returns the nodes mapped by a hash along with their associated values
func (i *Indexer) FromHash(hash string) (nodes []*Node, values []interface{}) {
if ids, found := i.hashToValues[hash]; found {
for id, obj := range ids {
nodes = append(nodes, i.graph.GetNode(id))
values = append(values, obj)
}
}
return
}
// Start registers the graph indexer as a graph listener
func (i *Indexer) Start() error {
if i.listenerHandler != nil {
i.listenerHandler.AddEventListener(i)
}
return nil
}
// Stop removes the graph indexer from the graph listeners
func (i *Indexer) Stop() {
if i.listenerHandler != nil {
i.listenerHandler.RemoveEventListener(i)
}
}
// AddEventListener subscribes a new graph listener
func (i *Indexer) AddEventListener(l EventListener) {
i.eventHandler.AddEventListener(l)
}
// RemoveEventListener unsubscribe a graph listener
func (i *Indexer) RemoveEventListener(l EventListener) {
i.eventHandler.RemoveEventListener(l)
}
// NewIndexer returns a new graph indexer with the associated hashing callback
func NewIndexer(g *Graph, listenerHandler ListenerHandler, hashNode NodeHasher, appendOnly bool) *Indexer {
indexer := &Indexer{
graph: g,
eventHandler: NewEventHandler(maxEvents),
listenerHandler: listenerHandler,
hashNode: hashNode,
hashToValues: make(map[string]map[Identifier]interface{}),
nodeToHashes: make(map[Identifier]map[string]bool),
appendOnly: appendOnly,
}
return indexer
}
// MetadataIndexer describes a metadata based graph indexer
type MetadataIndexer struct {
*Indexer
indexes []string
matcher ElementMatcher
}
// NewMetadataIndexer returns a new metadata graph indexer for the nodes
// matching the graph filter `m`, indexing the metadata with `indexes`
func NewMetadataIndexer(g *Graph, listenerHandler ListenerHandler, m ElementMatcher, indexes ...string) (indexer *MetadataIndexer) {
if len(indexes) == 0 {
panic("MetadataIndexer object can't be created with no indexes")
}
indexer = &MetadataIndexer{
matcher: m,
indexes: indexes,
Indexer: NewIndexer(g, listenerHandler, func(n *Node) (kv map[string]interface{}) {
if match := n.MatchMetadata(m); match {
kv = make(map[string]interface{})
values, err := getFieldsAsArray(n, indexes)
if err == nil {
for _, fields := range values {
if len(indexes) == len(fields) {
kv[Hash(fields...)] = fields
}
}
}
}
return
}, false),
}
return
}
// Sync synchronizes the index with the graph
func (i *MetadataIndexer) Sync() {
i.hashToValues = make(map[string]map[Identifier]interface{})
i.nodeToHashes = make(map[Identifier]map[string]bool)
for _, n := range i.graph.GetNodes(i.matcher) {
if kv := i.hashNode(n); len(kv) != 0 {
i.Index(n.ID, n, kv)
}
}
}