Repository navigation
Expand file tree
/
Copy pathgraphdb.go
More file actions
342 lines (300 loc) · 9.96 KB
/
Copy pathgraphdb.go
File metadata and controls
342 lines (300 loc) · 9.96 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
package graphdb
import (
"bytes"
"fmt"
"log/slog"
"path/filepath"
"sync"
"sync/atomic"
bolt "go.etcd.io/bbolt"
)
// DB is the main graph database instance. It manages one or more shards,
// provides concurrent query execution, and exposes all graph operations.
//
// Concurrency model:
// - All read operations (GetNode, BFS, DFS, Query, etc.) can run fully in parallel.
// - Write operations (AddNode, AddEdge, etc.) are serialized per-shard by bbolt.
// - The closed flag is an atomic.Bool so reads never hold a mutex — no recursive lock issues.
type DB struct {
opts Options
dir string
shards []*shard
pool *workerPool
cache queryCache // Cypher AST cache — avoids re-parsing identical queries
ncache *nodeCache // LRU hot-node cache — avoids repeated bbolt lookups
log *slog.Logger // structured logger for all operations
mu sync.Mutex // only used in Close() to prevent double-close
closed atomic.Bool // atomic flag — checked by every operation without locking
indexedProps sync.Map // map[string]bool — tracks which property names have secondary indexes
compositeIndexes sync.Map // map[string]compositeIndexDef — tracks composite indexes
uniqueConstraints sync.Map // map[string]UniqueConstraint — tracks unique constraints
edgeBloom *bloomFilter // probabilistic edge-existence filter (avoids disk I/O for HasEdge)
metrics *Metrics // operational counters (Prometheus-compatible)
slowLog *slowQueryLog // ring buffer of recent slow queries
governor *queryGovernor // enforces per-query resource limits (row cap, default timeout)
compactQuit chan struct{} // closed in Close() to stop background compaction goroutine
wal *WAL // write-ahead log for replication (nil if disabled)
}
// Open creates or opens a graph database at the given directory path.
// The directory will be created if it doesn't exist.
func Open(dir string, opts Options) (*DB, error) {
if opts.ShardCount <= 0 {
opts.ShardCount = 1
}
if opts.WorkerPoolSize <= 0 {
opts.WorkerPoolSize = 8
}
if opts.CacheBudget <= 0 {
opts.CacheBudget = 128 * 1024 * 1024 // 128MB
}
logger := opts.Logger
if logger == nil {
logger = slog.Default()
}
db := &DB{
opts: opts,
dir: dir,
shards: make([]*shard, opts.ShardCount),
cache: newQueryCache(defaultQueryCacheCapacity),
ncache: newNodeCache(opts.CacheBudget),
log: logger,
}
// Open all shards.
for i := 0; i < opts.ShardCount; i++ {
path := filepath.Join(dir, fmt.Sprintf("shard_%04d.db", i))
s, err := openShard(path, opts)
if err != nil {
// Close any already-opened shards on failure.
for j := 0; j < i; j++ {
db.shards[j].close()
}
return nil, err
}
db.shards[i] = s
}
// Start the worker pool for concurrent queries.
db.pool = newWorkerPool(opts.WorkerPoolSize)
// Discover existing property indexes and constraints from disk (survives restart).
db.discoverIndexes()
db.discoverCompositeIndexes()
db.discoverUniqueConstraints()
// Initialize bloom filter for fast HasEdge() checks.
// Scans adj_out bucket to populate the filter with all existing edges.
db.initBloomFilter()
db.metrics = newMetrics(db)
db.slowLog = newSlowQueryLog(100)
db.governor = &queryGovernor{
maxRows: opts.MaxResultRows,
defaultTimeout: opts.DefaultQueryTimeout,
}
// Open WAL if replication is enabled.
// The WAL records all committed mutations for follower replay.
if opts.EnableWAL {
wal, err := OpenWAL(dir, opts.WALNoSync, logger)
if err != nil {
for _, s := range db.shards {
if s != nil {
s.close()
}
}
return nil, fmt.Errorf("graphdb: failed to open WAL: %w", err)
}
db.wal = wal
db.log.Info("WAL enabled", "next_lsn", wal.nextLSN.Load())
}
// Start background compaction if configured.
// The goroutine periodically rewrites shard files to reclaim disk space
// from deleted B+ tree pages. Stopped when compactQuit is closed in Close().
db.compactQuit = make(chan struct{})
if opts.CompactionInterval > 0 {
db.startCompactionLoop(opts.CompactionInterval, db.compactQuit)
db.log.Info("background compaction enabled", "interval", opts.CompactionInterval)
}
db.log.Info("database opened",
"dir", dir,
"shards", opts.ShardCount,
"workers", opts.WorkerPoolSize,
"cache_budget_bytes", opts.CacheBudget,
)
return db, nil
}
// discoverIndexes scans the idx_prop bucket to learn which properties are indexed.
// Uses seek-jumping so cost is O(#unique indexed properties), not O(#index entries).
func (db *DB) discoverIndexes() {
for _, s := range db.shards {
_ = s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketIdxProp)
if b == nil {
return nil
}
c := b.Cursor()
k, _ := c.First()
for k != nil {
colonIdx := bytes.IndexByte(k, ':')
if colonIdx > 0 {
prop := string(k[:colonIdx])
db.indexedProps.Store(prop, true)
// Jump past all entries for this property.
nextPrefix := make([]byte, colonIdx+1)
copy(nextPrefix, k[:colonIdx])
nextPrefix[colonIdx] = ':' + 1 // ';' > ':'
k, _ = c.Seek(nextPrefix)
} else {
k, _ = c.Next()
}
}
return nil
})
}
}
// HasIndex returns true if a secondary index exists for the given property name.
func (db *DB) HasIndex(propName string) bool {
_, ok := db.indexedProps.Load(propName)
return ok
}
// Close gracefully shuts down the database, flushing all pending writes.
func (db *DB) Close() error {
db.mu.Lock()
defer db.mu.Unlock()
if db.closed.Load() {
return nil
}
db.closed.Store(true)
// Stop background compaction goroutine (if running).
if db.compactQuit != nil {
close(db.compactQuit)
}
// Stop worker pool.
if db.pool != nil {
db.pool.stop()
}
// Close WAL (flush final entries).
if db.wal != nil {
if err := db.wal.Close(); err != nil {
db.log.Error("WAL close error", "error", err)
}
}
// Close all shards.
var firstErr error
for _, s := range db.shards {
if s != nil {
if err := s.close(); err != nil && firstErr == nil {
firstErr = err
}
}
}
if firstErr != nil {
db.log.Error("database closed with error", "error", firstErr)
} else {
db.log.Info("database closed")
}
return firstErr
}
// ErrReadOnlyReplica is returned when a write operation is attempted on a
// follower node. Followers only accept writes from the internal WAL applier.
var ErrReadOnlyReplica = fmt.Errorf("graphdb: this is a read-only replica")
// isFollower returns true if this DB instance is configured as a follower.
// Thread-safe — reads are protected by db.mu since SetRole can change the role.
func (db *DB) isFollower() bool {
db.mu.Lock()
defer db.mu.Unlock()
return db.opts.Role == "follower"
}
// writeGuard returns ErrReadOnlyReplica if the DB is a follower.
// Every public write method should call this before proceeding.
func (db *DB) writeGuard() error {
if db.isFollower() {
return ErrReadOnlyReplica
}
return nil
}
// walAppend is a convenience helper that appends a WAL entry if the WAL is
// enabled. It encodes the payload, appends the entry, and logs any errors.
// Called after a successful bbolt commit — the WAL contains only committed ops.
// Returns nil if the WAL is disabled or the append succeeds.
func (db *DB) walAppend(op OpType, payloadStruct any) {
if db.wal == nil {
return
}
payload, err := encodeWALPayload(payloadStruct)
if err != nil {
db.log.Error("WAL: failed to encode payload", "op", op, "error", err)
return
}
if _, err := db.wal.Append(op, payload); err != nil {
db.log.Error("WAL: append failed — mutation committed but not logged",
"op", op, "error", err)
}
}
// SetRole dynamically changes the node's role at runtime.
// This is called by the Raft election callback when leadership changes.
// Switching to "follower" causes all subsequent public writes to be rejected.
// Switching to "leader" re-enables writes.
func (db *DB) SetRole(role string) {
db.mu.Lock()
old := db.opts.Role
db.opts.Role = role
db.mu.Unlock()
db.log.Info("role changed", "old", old, "new", role)
}
// Role returns the current role of this DB instance.
func (db *DB) Role() string {
db.mu.Lock()
defer db.mu.Unlock()
return db.opts.Role
}
// WAL returns the write-ahead log, or nil if WAL is not enabled.
// Used by the replication package to create readers for log shipping.
func (db *DB) WAL() *WAL {
return db.wal
}
// Metrics returns the operational metrics collector.
// Use this to read counters or write Prometheus exposition format.
func (db *DB) Metrics() *Metrics {
return db.metrics
}
// isClosed is a cheap inline check used by every public method.
func (db *DB) isClosed() bool {
return db.closed.Load()
}
// IsClosed returns true if the database has been closed.
// Used by the health check endpoint to determine if the node is ready.
func (db *DB) IsClosed() bool {
return db.closed.Load()
}
// shardFor returns the shard responsible for the given node ID.
// Uses consistent hash-based partitioning.
func (db *DB) shardFor(id NodeID) *shard {
if len(db.shards) == 1 {
return db.shards[0]
}
return db.shards[uint64(id)%uint64(len(db.shards))]
}
// shardForEdge returns the shard responsible for the given edge.
// Edges are co-located with their source node for efficient outgoing traversals.
func (db *DB) shardForEdge(fromID NodeID) *shard {
return db.shardFor(fromID)
}
// primaryShard returns the first shard (used for single-shard operations).
func (db *DB) primaryShard() *shard {
return db.shards[0]
}
// Stats returns aggregate statistics across all shards.
func (db *DB) Stats() (*GraphStats, error) {
if db.isClosed() {
return nil, fmt.Errorf("graphdb: database is closed")
}
stats := &GraphStats{
ShardCount: len(db.shards),
}
for _, s := range db.shards {
stats.NodeCount += s.nodeCount.Load()
stats.EdgeCount += s.edgeCount.Load()
size, err := s.fileSize()
if err != nil {
return nil, err
}
stats.DiskSizeBytes += size
}
return stats, nil
}