-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.go
More file actions
254 lines (221 loc) · 6.79 KB
/
index.go
File metadata and controls
254 lines (221 loc) · 6.79 KB
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
246
247
248
249
250
251
252
253
254
package graphdb
import (
"bytes"
"context"
"fmt"
bolt "go.etcd.io/bbolt"
)
// CreateIndex creates a secondary index on a node property.
// This allows fast lookups of nodes by property value.
// Example: CreateIndex("name") enables fast FindByProperty("name", "Alice").
// Cypher queries automatically use available indexes for inline property filters.
func (db *DB) CreateIndex(propName string) error {
if db.isClosed() {
return fmt.Errorf("graphdb: database is closed")
}
if err := db.writeGuard(); err != nil {
return err
}
for _, s := range db.shards {
err := s.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
idxBucket := tx.Bucket(bucketIdxProp)
nodesBucket := tx.Bucket(bucketNodes)
// Scan all nodes and index the specified property.
return nodesBucket.ForEach(func(k, v []byte) error {
props, err := decodeProps(v)
if err != nil {
return nil // skip corrupted entries
}
val, ok := props[propName]
if !ok {
return nil
}
// Create index key: "propName:value" + nodeID
idxKeyStr := fmt.Sprintf("%s:%v", propName, val)
nodeID := decodeNodeID(k)
idxKey := encodeIndexKey(idxKeyStr, uint64(nodeID))
return idxBucket.Put(idxKey, nil)
})
})
if err != nil {
return fmt.Errorf("graphdb: failed to create index on %s: %w", propName, err)
}
}
// Register the index in memory for the query optimizer.
db.indexedProps.Store(propName, true)
db.walAppend(OpCreateIndex, WALCreateIndex{PropName: propName})
return nil
}
// FindByProperty finds nodes where the given property equals the given value.
// Uses the secondary index if available, otherwise falls back to full scan.
func (db *DB) FindByProperty(propName string, value interface{}) ([]*Node, error) {
if db.isClosed() {
return nil, fmt.Errorf("graphdb: database is closed")
}
_, hasIndex := db.indexedProps.Load(propName)
if hasIndex && db.metrics != nil {
db.metrics.IndexLookups.Add(1)
}
idxKeyStr := fmt.Sprintf("%s:%v", propName, value)
prefix := encodeIndexPrefix(idxKeyStr)
var nodes []*Node
for _, s := range db.shards {
err := s.db.View(func(tx *bolt.Tx) error {
idxBucket := tx.Bucket(bucketIdxProp)
nodesBucket := tx.Bucket(bucketNodes)
if hasIndex {
// Fast path: use the secondary index (prefix seek).
c := idxBucket.Cursor()
for k, _ := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, _ = c.Next() {
nodeIDBytes := k[len(prefix):]
if len(nodeIDBytes) < 8 {
continue
}
nodeID := decodeNodeID(nodeIDBytes)
data := nodesBucket.Get(encodeNodeID(nodeID))
if data == nil {
continue
}
props, err := decodeProps(data)
if err != nil {
continue
}
nodes = append(nodes, &Node{ID: nodeID, Props: props})
}
// Trust the index: if no entries match, the result is empty.
return nil
}
// Slow path: no index — full scan fallback.
return nodesBucket.ForEach(func(k, v []byte) error {
props, err := decodeProps(v)
if err != nil {
return nil
}
if val, ok := props[propName]; ok && fmt.Sprintf("%v", val) == fmt.Sprintf("%v", value) {
nodeID := decodeNodeID(k)
nodes = append(nodes, &Node{ID: nodeID, Props: props})
}
return nil
})
})
if err != nil {
return nil, err
}
}
return nodes, nil
}
// DropIndex removes a secondary index on a property.
func (db *DB) DropIndex(propName string) error {
if db.isClosed() {
return fmt.Errorf("graphdb: database is closed")
}
if err := db.writeGuard(); err != nil {
return err
}
prefix := []byte(propName + ":")
for _, s := range db.shards {
err := s.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
idxBucket := tx.Bucket(bucketIdxProp)
c := idxBucket.Cursor()
var toDelete [][]byte
for k, _ := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, _ = c.Next() {
keyCopy := make([]byte, len(k))
copy(keyCopy, k)
toDelete = append(toDelete, keyCopy)
}
for _, k := range toDelete {
if err := idxBucket.Delete(k); err != nil {
return err
}
}
return nil
})
if err != nil {
return fmt.Errorf("graphdb: failed to drop index on %s: %w", propName, err)
}
}
// Remove from in-memory tracking.
db.indexedProps.Delete(propName)
db.walAppend(OpDropIndex, WALDropIndex{PropName: propName})
return nil
}
// ListIndexes returns the names of all properties that currently have a secondary index.
func (db *DB) ListIndexes() []string {
var names []string
db.indexedProps.Range(func(key, _ any) bool {
names = append(names, key.(string))
return true
})
if names == nil {
names = []string{} // never return nil
}
return names
}
// ReIndex rebuilds the property index from scratch.
// Useful after bulk inserts where index maintenance was skipped.
func (db *DB) ReIndex(propName string) error {
if err := db.DropIndex(propName); err != nil {
return err
}
return db.CreateIndex(propName)
}
// ---------------------------------------------------------------------------
// Index maintenance helpers — called inside bbolt write transactions by
// AddNode, UpdateNode, SetNodeProps, DeleteNode to keep indexes up-to-date.
// ---------------------------------------------------------------------------
// indexNodeProps adds index entries for all indexed properties of a node.
// Must be called within a bbolt Update transaction on the correct shard.
func (db *DB) indexNodeProps(tx *bolt.Tx, nodeID NodeID, props Props) error {
if len(props) == 0 {
return nil
}
idxBucket := tx.Bucket(bucketIdxProp)
var firstErr error
db.indexedProps.Range(func(key, _ any) bool {
propName := key.(string)
val, ok := props[propName]
if !ok {
return true // property not present in this node
}
idxKeyStr := fmt.Sprintf("%s:%v", propName, val)
idxKey := encodeIndexKey(idxKeyStr, uint64(nodeID))
if err := idxBucket.Put(idxKey, nil); err != nil {
firstErr = err
return false
}
return true
})
if firstErr != nil {
return firstErr
}
// Maintain composite indexes.
return db.indexNodeComposite(tx, nodeID, props)
}
// unindexNodeProps removes index entries for all indexed properties of a node.
// Must be called within a bbolt Update transaction on the correct shard.
func (db *DB) unindexNodeProps(tx *bolt.Tx, nodeID NodeID, props Props) error {
if len(props) == 0 {
return nil
}
idxBucket := tx.Bucket(bucketIdxProp)
var firstErr error
db.indexedProps.Range(func(key, _ any) bool {
propName := key.(string)
val, ok := props[propName]
if !ok {
return true
}
idxKeyStr := fmt.Sprintf("%s:%v", propName, val)
idxKey := encodeIndexKey(idxKeyStr, uint64(nodeID))
if err := idxBucket.Delete(idxKey); err != nil {
firstErr = err
return false
}
return true
})
if firstErr != nil {
return firstErr
}
// Maintain composite indexes.
return db.unindexNodeComposite(tx, nodeID, props)
}