-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode.go
More file actions
514 lines (454 loc) · 13.4 KB
/
node.go
File metadata and controls
514 lines (454 loc) · 13.4 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package graphdb
import (
"context"
"fmt"
bolt "go.etcd.io/bbolt"
)
// AddNode creates a new node with the given arbitrary properties.
// Returns the auto-generated NodeID. Safe for concurrent use.
func (db *DB) AddNode(props Props) (NodeID, error) {
if db.isClosed() {
return 0, fmt.Errorf("graphdb: database is closed")
}
if err := db.writeGuard(); err != nil {
return 0, err
}
// For sharded mode, allocate ID from primary shard to ensure global uniqueness.
s := db.primaryShard()
id := s.allocNodeID()
// Determine which shard owns this node.
target := db.shardFor(id)
data, err := encodeProps(props)
if err != nil {
return 0, fmt.Errorf("graphdb: failed to encode node properties: %w", err)
}
// writeUpdate acquires the write semaphore before entering bbolt's
// single-writer lock, providing bounded backpressure under load.
err = target.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
if err := b.Put(encodeNodeID(id), data); err != nil {
return err
}
// Maintain secondary indexes (same tx, no extra fsync).
if err := db.indexNodeProps(tx, id, props); err != nil {
return err
}
return nil
})
if err != nil {
db.log.Error("failed to add node", "error", err)
return 0, fmt.Errorf("graphdb: failed to add node: %w", err)
}
// Increment counter after successful commit (outside batch fn to avoid
// double-counting if bbolt Batch retries the function on rollback).
target.nodeCount.Add(1)
// WAL: log the committed mutation for replication.
db.walAppend(OpAddNode, WALAddNode{ID: id, Props: props})
db.ncache.Put(&Node{ID: id, Props: props})
if db.metrics != nil {
db.metrics.NodesCreated.Add(1)
}
db.log.Debug("node added", "id", id)
return id, nil
}
// AddNodeBatch creates multiple nodes in a single transaction for performance.
// Returns the list of auto-generated NodeIDs. Safe for concurrent use.
//
// Note: AddNodeBatch does NOT auto-maintain secondary indexes for performance.
// Large batches with inline index maintenance cause excessive B+tree page splits,
// degrading insert throughput by orders of magnitude. Call ReIndex() or
// CreateIndex() after a batch insert to rebuild indexes.
//
// Single-node AddNode, UpdateNode, SetNodeProps, and DeleteNode DO auto-maintain
// indexes since the overhead is negligible for individual operations.
func (db *DB) AddNodeBatch(propsList []Props) ([]NodeID, error) {
if db.isClosed() {
return nil, fmt.Errorf("graphdb: database is closed")
}
if err := db.writeGuard(); err != nil {
return nil, err
}
ids := make([]NodeID, len(propsList))
if len(db.shards) == 1 {
// Single-shard fast path: all nodes go into one transaction.
s := db.shards[0]
err := s.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
for i, props := range propsList {
id := s.allocNodeID()
ids[i] = id
data, err := encodeProps(props)
if err != nil {
return err
}
if err := b.Put(encodeNodeID(id), data); err != nil {
return err
}
}
return nil
})
if err == nil {
s.nodeCount.Add(uint64(len(propsList)))
}
if err != nil {
db.log.Error("batch add failed", "count", len(propsList), "error", err)
return nil, fmt.Errorf("graphdb: batch add failed: %w", err)
}
// WAL: log the batch as a single entry.
nodes := make([]WALBatchNode, len(propsList))
for i, p := range propsList {
nodes[i] = WALBatchNode{ID: ids[i], Props: p}
}
db.walAppend(OpAddNodeBatch, WALAddNodeBatch{Nodes: nodes})
db.log.Debug("batch nodes added", "count", len(propsList))
return ids, nil
}
// Multi-shard path: group nodes by target shard.
type shardEntry struct {
index int
id NodeID
data []byte
}
shardGroups := make(map[int][]shardEntry)
s := db.primaryShard()
for i, props := range propsList {
id := s.allocNodeID()
ids[i] = id
data, err := encodeProps(props)
if err != nil {
return nil, fmt.Errorf("graphdb: failed to encode props at index %d: %w", i, err)
}
shardIdx := int(uint64(id) % uint64(len(db.shards)))
shardGroups[shardIdx] = append(shardGroups[shardIdx], shardEntry{
index: i, id: id, data: data,
})
}
// Write each shard's batch in a separate transaction.
for shardIdx, entries := range shardGroups {
target := db.shards[shardIdx]
err := target.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
for _, e := range entries {
if err := b.Put(encodeNodeID(e.id), e.data); err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("graphdb: batch add failed on shard %d: %w", shardIdx, err)
}
target.nodeCount.Add(uint64(len(entries)))
}
// WAL: log the batch as a single entry.
nodes := make([]WALBatchNode, len(propsList))
for i, p := range propsList {
nodes[i] = WALBatchNode{ID: ids[i], Props: p}
}
db.walAppend(OpAddNodeBatch, WALAddNodeBatch{Nodes: nodes})
return ids, nil
}
// GetNode retrieves a node by its ID. Safe for concurrent use.
func (db *DB) GetNode(id NodeID) (*Node, error) {
if db.isClosed() {
return nil, fmt.Errorf("graphdb: database is closed")
}
return db.getNode(id)
}
// getNode is the lock-free internal version of GetNode.
// Called by BFS/DFS/ShortestPath etc. which need to call this many times during traversal.
// Uses the hot-node LRU cache to avoid repeated bbolt lookups.
func (db *DB) getNode(id NodeID) (*Node, error) {
// Fast path: cache hit.
if n := db.ncache.Get(id); n != nil {
return n, nil
}
s := db.shardFor(id)
var node *Node
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
data := b.Get(encodeNodeID(id))
if data == nil {
return fmt.Errorf("graphdb: node %d not found", id)
}
props, err := decodeProps(data)
if err != nil {
return err
}
labels := loadLabels(tx, id)
node = &Node{ID: id, Labels: labels, Props: props}
return nil
})
// Populate cache on successful read.
if err == nil && node != nil {
db.ncache.Put(node)
}
return node, err
}
// UpdateNode updates the properties of an existing node.
// The update is a merge: existing properties are kept unless overwritten.
// Secondary indexes are updated automatically for changed indexed properties.
func (db *DB) UpdateNode(id NodeID, props Props) error {
if db.isClosed() {
return fmt.Errorf("graphdb: database is closed")
}
if err := db.writeGuard(); err != nil {
return err
}
s := db.shardFor(id)
err := s.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
key := encodeNodeID(id)
existing := b.Get(key)
if existing == nil {
return fmt.Errorf("graphdb: node %d not found", id)
}
// Decode old properties for index maintenance.
oldProps, err := decodeProps(existing)
if err != nil {
return err
}
// Remove old index entries before mutation.
if err := db.unindexNodeProps(tx, id, oldProps); err != nil {
return err
}
// Merge properties.
for k, v := range props {
oldProps[k] = v
}
// Enforce unique constraints on the merged properties.
labels := loadLabels(tx, id)
if len(labels) > 0 {
// Unindex old unique values, check new ones, then re-index.
_ = db.unindexUniqueConstraints(tx, id, labels, oldProps)
if err := db.checkUniqueConstraintsInTx(tx, labels, oldProps, id); err != nil {
return err
}
}
data, err := encodeProps(oldProps)
if err != nil {
return err
}
if err := b.Put(key, data); err != nil {
return err
}
// Add new index entries with merged properties.
if err := db.indexNodeProps(tx, id, oldProps); err != nil {
return err
}
// Re-index unique constraints with new values.
if len(labels) > 0 {
_ = db.indexUniqueConstraints(tx, id, labels, oldProps)
}
return nil
})
if err != nil {
db.log.Error("failed to update node", "id", id, "error", err)
} else {
db.walAppend(OpUpdateNode, WALUpdateNode{ID: id, Props: props})
db.ncache.Invalidate(id)
db.log.Debug("node updated", "id", id)
}
return err
}
// SetNodeProps replaces all properties of a node (full overwrite).
// Secondary indexes are updated automatically.
func (db *DB) SetNodeProps(id NodeID, props Props) error {
if db.isClosed() {
return fmt.Errorf("graphdb: database is closed")
}
if err := db.writeGuard(); err != nil {
return err
}
s := db.shardFor(id)
err := s.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
key := encodeNodeID(id)
existing := b.Get(key)
if existing == nil {
return fmt.Errorf("graphdb: node %d not found", id)
}
// Remove old index entries.
oldProps, err := decodeProps(existing)
if err != nil {
return err
}
if err := db.unindexNodeProps(tx, id, oldProps); err != nil {
return err
}
// Enforce unique constraints on the new properties.
labels := loadLabels(tx, id)
if len(labels) > 0 {
_ = db.unindexUniqueConstraints(tx, id, labels, oldProps)
if err := db.checkUniqueConstraintsInTx(tx, labels, props, id); err != nil {
return err
}
}
data, err := encodeProps(props)
if err != nil {
return err
}
if err := b.Put(key, data); err != nil {
return err
}
// Add new index entries.
if err := db.indexNodeProps(tx, id, props); err != nil {
return err
}
if len(labels) > 0 {
_ = db.indexUniqueConstraints(tx, id, labels, props)
}
return nil
})
if err != nil {
db.log.Error("failed to set node props", "id", id, "error", err)
} else {
db.walAppend(OpSetNodeProps, WALSetNodeProps{ID: id, Props: props})
db.ncache.Invalidate(id)
db.log.Debug("node props set", "id", id)
}
return err
}
// DeleteNode removes a node and all its associated edges.
func (db *DB) DeleteNode(id NodeID) error {
if db.isClosed() {
return fmt.Errorf("graphdb: database is closed")
}
if err := db.writeGuard(); err != nil {
return err
}
// First, collect all edges connected to this node.
edges, err := db.getEdgesForNode(id, Both)
if err != nil {
return err
}
// Delete all connected edges.
for _, e := range edges {
if err := db.deleteEdgeInternal(e); err != nil {
return fmt.Errorf("graphdb: failed to delete edge %d while deleting node %d: %w", e.ID, id, err)
}
}
// Delete the node itself (and clean up index entries).
s := db.shardFor(id)
err = s.writeUpdate(context.Background(), func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
key := encodeNodeID(id)
existing := b.Get(key)
if existing == nil {
return fmt.Errorf("graphdb: node %d not found", id)
}
// Remove property index entries before deleting the node.
props, _ := decodeProps(existing)
if len(props) > 0 {
_ = db.unindexNodeProps(tx, id, props)
}
// Remove label index entries and unique constraint index.
labels := loadLabels(tx, id)
if len(labels) > 0 {
// Clean up unique constraint index entries.
if len(props) > 0 {
_ = db.unindexUniqueConstraints(tx, id, labels, props)
}
idxBucket := tx.Bucket(bucketIdxNodeLabel)
for _, l := range labels {
_ = idxBucket.Delete(encodeLabelIndexKey(l, id))
}
_ = tx.Bucket(bucketNodeLabels).Delete(encodeNodeID(id))
}
if err := b.Delete(key); err != nil {
return err
}
return nil
})
if err != nil {
db.log.Error("failed to delete node", "id", id, "error", err)
} else {
s.nodeCount.Add(^uint64(0)) // decrement by 1
db.walAppend(OpDeleteNode, WALDeleteNode{ID: id})
db.ncache.Invalidate(id)
if db.metrics != nil {
db.metrics.NodesDeleted.Add(1)
}
db.log.Debug("node deleted", "id", id, "edges_removed", len(edges))
}
return err
}
// NodeExists checks if a node exists. Safe for concurrent use.
func (db *DB) NodeExists(id NodeID) (bool, error) {
if db.isClosed() {
return false, fmt.Errorf("graphdb: database is closed")
}
s := db.shardFor(id)
exists := false
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
exists = b.Get(encodeNodeID(id)) != nil
return nil
})
return exists, err
}
// NodeCount returns the total number of nodes in the database.
func (db *DB) NodeCount() uint64 {
var total uint64
for _, s := range db.shards {
total += s.nodeCount.Load()
}
return total
}
// ForEachNode iterates over all nodes, calling fn for each.
// Return a non-nil error from fn to stop iteration. Safe for concurrent use.
func (db *DB) ForEachNode(fn func(*Node) error) error {
if db.isClosed() {
return fmt.Errorf("graphdb: database is closed")
}
return db.forEachNode(fn)
}
// forEachNode is the lock-free internal version.
func (db *DB) forEachNode(fn func(*Node) error) error {
for _, s := range db.shards {
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
return b.ForEach(func(k, v []byte) error {
id := decodeNodeID(k)
props, err := decodeProps(v)
if err != nil {
return err
}
labels := loadLabels(tx, id)
return fn(&Node{ID: id, Labels: labels, Props: props})
})
})
if err != nil {
return err
}
}
return nil
}
// FindNodes returns all nodes matching the given filter. Safe for concurrent use.
func (db *DB) FindNodes(filter NodeFilter) ([]*Node, error) {
if db.isClosed() {
return nil, fmt.Errorf("graphdb: database is closed")
}
var results []*Node
for _, s := range db.shards {
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketNodes)
return b.ForEach(func(k, v []byte) error {
id := decodeNodeID(k)
props, err := decodeProps(v)
if err != nil {
return err
}
node := &Node{ID: id, Props: props}
if filter(node) {
results = append(results, node)
}
return nil
})
})
if err != nil {
return nil, err
}
}
return results, nil
}