-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
496 lines (441 loc) · 12.9 KB
/
db.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
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
package anystore
import (
"context"
"errors"
"fmt"
"log"
"sync"
"sync/atomic"
"zombiezen.com/go/sqlite"
"github.com/anyproto/any-store/internal/driver"
"github.com/anyproto/any-store/internal/objectid"
"github.com/anyproto/any-store/internal/registry"
"github.com/anyproto/any-store/internal/sql"
"github.com/anyproto/any-store/internal/syncpool"
)
// DB represents a document-oriented database.
type DB interface {
// CreateCollection creates a new collection with the specified name.
// Returns the created Collection or an error if the collection already exists.
// Possible errors:
// - ErrCollectionExists: if the collection already exists.
CreateCollection(ctx context.Context, collectionName string) (Collection, error)
// OpenCollection opens an existing collection with the specified name.
// Returns the opened Collection or an error if the collection does not exist.
// Possible errors:
// - ErrCollectionNotFound: if the collection does not exist.
OpenCollection(ctx context.Context, collectionName string) (Collection, error)
// Collection is a convenience method to get or create a collection.
// It first attempts to open the collection, and if it does not exist, it creates the collection.
// Returns the Collection or an error if there is an issue creating or opening the collection.
Collection(ctx context.Context, collectionName string) (Collection, error)
// GetCollectionNames returns a list of all collection names in the database.
// Returns a slice of collection names or an error if there is an issue retrieving the names.
GetCollectionNames(ctx context.Context) ([]string, error)
// Stats returns the statistics of the database.
// Returns a DBStats struct containing the database statistics or an error if there is an issue retrieving the stats.
Stats(ctx context.Context) (DBStats, error)
// QuickCheck performs PRAGMA quick_check to sqlite. If result not ok returns error.
QuickCheck(ctx context.Context) (err error)
// Checkpoint performs PRAGMA wal_checkpoint to sqlite. isFull=true - wal_checkpoint(FULL), isFull=false - wal_checkpoint(PASSIVE);
Checkpoint(ctx context.Context, isFull bool) (err error)
// Backup creates a backup of the database at the specified file path.
// Returns an error if the operation fails.
Backup(ctx context.Context, path string) (err error)
// ReadTx starts a new read-only transaction.
// Returns a ReadTx or an error if there is an issue starting the transaction.
ReadTx(ctx context.Context) (ReadTx, error)
// WriteTx starts a new read-write transaction.
// Returns a WriteTx or an error if there is an issue starting the transaction.
WriteTx(ctx context.Context) (WriteTx, error)
// Close closes the database connection.
// Returns an error if there is an issue closing the connection.
Close() error
}
// DBStats represents the statistics of the database.
type DBStats struct {
// CollectionsCount is the total number of collections in the database.
CollectionsCount int
// IndexesCount is the total number of indexes across all collections in the database.
IndexesCount int
// TotalSizeBytes is the total size of the database in bytes.
TotalSizeBytes int
// DataSizeBytes is the total size of the data stored in the database in bytes, excluding free space.
DataSizeBytes int
}
// Open opens a database at the specified path with the given configuration.
// The config parameter can be nil for default settings.
// Returns a DB instance or an error.
func Open(ctx context.Context, path string, config *Config) (DB, error) {
if config == nil {
config = &Config{}
}
config.setDefaults()
sPool := syncpool.NewSyncPool(config.SyncPoolElementMaxSize)
registryBufSize := (config.ReadConnections + 1) * 4
ds := &db{
instanceId: objectid.NewObjectID().Hex(),
config: config,
syncPool: sPool,
filterReg: registry.NewFilterRegistry(sPool, registryBufSize),
sortReg: registry.NewSortRegistry(sPool, registryBufSize),
openedCollections: make(map[string]Collection),
}
var err error
if ds.cm, err = driver.NewConnManager(
path,
config.pragma(),
1,
config.ReadConnections,
ds.filterReg,
ds.sortReg,
2, // sqlite user_version
); err != nil {
return nil, err
}
if err = ds.init(ctx); err != nil {
_ = ds.cm.Close()
return nil, err
}
return ds, nil
}
type db struct {
instanceId string
config *Config
cm *driver.ConnManager
filterReg *registry.FilterRegistry
sortReg *registry.SortRegistry
syncPool *syncpool.SyncPool
sql sql.DBSql
stmt struct {
registerCollection,
removeCollection,
renameCollection,
renameCollectionIndex,
registerIndex,
removeIndex *driver.Stmt
}
openedCollections map[string]Collection
closed atomic.Bool
mu sync.Mutex
}
func (db *db) init(ctx context.Context) error {
return db.doWriteTx(ctx, func(c *driver.Conn) (err error) {
if err = c.ExecNoResult(ctx, db.sql.InitDB()); err != nil {
return
}
if db.stmt.registerCollection, err = c.Prepare(db.sql.RegisterCollectionStmt()); err != nil {
return
}
if db.stmt.removeCollection, err = c.Prepare(db.sql.RemoveCollectionStmt()); err != nil {
return
}
if db.stmt.renameCollection, err = c.Prepare(db.sql.RenameCollectionStmt()); err != nil {
return
}
if db.stmt.renameCollectionIndex, err = c.Prepare(db.sql.RenameCollectionIndexStmt()); err != nil {
return
}
if db.stmt.registerIndex, err = c.Prepare(db.sql.RegisterIndexStmt()); err != nil {
return
}
if db.stmt.removeIndex, err = c.Prepare(db.sql.RemoveIndexStmt()); err != nil {
return
}
return
})
}
func (db *db) newWriteTx(ctx context.Context) (WriteTx, error) {
connWrite, err := db.cm.GetWrite(ctx)
if err != nil {
return nil, err
}
if err = connWrite.BeginImmediate(ctx); err != nil {
db.cm.ReleaseWrite(connWrite)
return nil, err
}
tx := writeTxPool.Get().(*writeTx)
tx.db = db
tx.initialCtx = ctx
tx.ctx = context.WithValue(ctx, ctxKeyTx, tx)
tx.con = connWrite
tx.reset()
return tx, nil
}
func (db *db) ReadTx(ctx context.Context) (ReadTx, error) {
connRead, err := db.cm.GetRead(ctx)
if err != nil {
return nil, err
}
if err = connRead.Begin(ctx); err != nil {
db.cm.ReleaseRead(connRead)
return nil, err
}
tx := readTxPool.Get().(*readTx)
tx.db = db
tx.initialCtx = ctx
tx.ctx = context.WithValue(ctx, ctxKeyTx, tx)
tx.con = connRead
tx.reset()
return tx, nil
}
func (db *db) CreateCollection(ctx context.Context, collectionName string) (Collection, error) {
db.mu.Lock()
defer db.mu.Unlock()
return db.createCollection(ctx, collectionName)
}
func (db *db) createCollection(ctx context.Context, collectionName string) (Collection, error) {
if _, ok := db.openedCollections[collectionName]; ok {
return nil, ErrCollectionExists
}
err := db.doWriteTx(ctx, func(c *driver.Conn) error {
err := db.stmt.registerCollection.Exec(ctx, func(stmt *sqlite.Stmt) {
stmt.BindText(1, collectionName)
}, driver.StmtExecNoResults)
if err != nil {
return replaceUniqErr(err, ErrCollectionExists)
}
if err = c.ExecNoResult(ctx, db.sql.Collection(collectionName).Create()); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
coll, err := newCollection(ctx, db, collectionName)
if err != nil {
return nil, err
}
db.openedCollections[collectionName] = coll
return coll, nil
}
func (db *db) OpenCollection(ctx context.Context, collectionName string) (Collection, error) {
db.mu.Lock()
defer db.mu.Unlock()
return db.openCollection(ctx, collectionName)
}
func (db *db) openCollection(ctx context.Context, collectionName string) (Collection, error) {
coll, ok := db.openedCollections[collectionName]
if ok {
return coll, nil
}
err := db.doReadTx(ctx, func(c *driver.Conn) error {
return c.Exec(ctx, db.sql.FindCollection(), func(stmt *sqlite.Stmt) {
stmt.BindText(1, collectionName)
}, func(stmt *sqlite.Stmt) error {
hasRow, stepErr := stmt.Step()
if stepErr != nil {
return nil
}
if !hasRow {
return ErrCollectionNotFound
}
return nil
})
})
if err != nil {
return nil, err
}
coll, err = newCollection(ctx, db, collectionName)
if err != nil {
return nil, err
}
db.openedCollections[collectionName] = coll
return coll, nil
}
func (db *db) Collection(ctx context.Context, collectionName string) (Collection, error) {
db.mu.Lock()
defer db.mu.Unlock()
coll, err := db.createCollection(ctx, collectionName)
if err == nil {
return coll, nil
}
if err != nil && !errors.Is(err, ErrCollectionExists) {
return nil, err
}
return db.openCollection(ctx, collectionName)
}
func (db *db) GetCollectionNames(ctx context.Context) (collectionNames []string, err error) {
err = db.doReadTx(ctx, func(c *driver.Conn) error {
return c.ExecCached(ctx, db.sql.FindCollections(), nil, func(stmt *sqlite.Stmt) error {
for {
hasRow, stepErr := stmt.Step()
if stepErr != nil {
return stepErr
}
if !hasRow {
break
}
collectionNames = append(collectionNames, stmt.ColumnText(0))
}
return nil
})
})
if err != nil {
return nil, err
}
return
}
func (db *db) Stats(ctx context.Context) (stats DBStats, err error) {
err = db.doReadTx(ctx, func(cn *driver.Conn) (txErr error) {
var getIntByQuery = func(q string) (result int, err error) {
err = cn.Exec(ctx, q, nil, func(stmt *sqlite.Stmt) error {
hasRow, stepErr := stmt.Step()
if !hasRow {
return nil
}
if stepErr != nil {
return stepErr
}
result = stmt.ColumnInt(0)
return nil
})
return
}
if stats.CollectionsCount, txErr = getIntByQuery(db.sql.CountCollections()); txErr != nil {
return
}
if stats.IndexesCount, txErr = getIntByQuery(db.sql.CountIndexes()); txErr != nil {
return
}
if stats.TotalSizeBytes, txErr = getIntByQuery(db.sql.StatsTotalSize()); txErr != nil {
return
}
if stats.DataSizeBytes, txErr = getIntByQuery(db.sql.StatsDataSize()); txErr != nil {
return
}
return
})
return
}
func (db *db) QuickCheck(ctx context.Context) (err error) {
return db.doWriteTx(ctx, func(c *driver.Conn) error {
return c.Exec(ctx, "PRAGMA quick_check", nil, func(stmt *sqlite.Stmt) error {
hasRow, stepErr := stmt.Step()
if !hasRow {
return nil
}
if stepErr != nil {
return stepErr
}
result := stmt.ColumnText(0)
if result != "ok" {
return fmt.Errorf("quick_check not ok: %s", result)
}
return nil
})
})
}
func (db *db) Checkpoint(ctx context.Context, isFull bool) (err error) {
var q = "PRAGMA wal_checkpoint(PASSIVE)"
if isFull {
q = "PRAGMA wal_checkpoint(FULL)"
}
conn, err := db.cm.GetWrite(ctx)
if err != nil {
return err
}
defer db.cm.ReleaseWrite(conn)
return conn.ExecNoResult(ctx, q)
}
func (db *db) Backup(ctx context.Context, path string) (err error) {
conn, err := db.cm.GetWrite(ctx)
if err != nil {
return err
}
defer db.cm.ReleaseWrite(conn)
return conn.Backup(ctx, path)
}
func (db *db) WriteTx(ctx context.Context) (tx WriteTx, err error) {
ctxTx := ctx.Value(ctxKeyTx)
if ctxTx == nil {
return db.newWriteTx(ctx)
}
var ok bool
if tx, ok = ctxTx.(WriteTx); ok {
if tx.Done() {
return nil, ErrTxIsUsed
}
if tx.instanceId() != db.instanceId {
return nil, ErrTxOtherInstance
}
return newSavepointTx(ctx, tx)
}
return nil, ErrTxIsReadOnly
}
func (db *db) doWriteTx(ctx context.Context, do func(c *driver.Conn) error) error {
tx, err := db.WriteTx(ctx)
if err != nil {
return err
}
if err = do(tx.conn()); err != nil {
err = replaceInterruptErr(err)
return errors.Join(err, tx.Rollback())
}
return tx.Commit()
}
func (db *db) getReadTx(ctx context.Context) (tx ReadTx, err error) {
ctxTx := ctx.Value(ctxKeyTx)
if ctxTx == nil {
return db.ReadTx(ctx)
}
var ok bool
if tx, ok = ctxTx.(ReadTx); ok {
if tx.Done() {
return nil, ErrTxIsUsed
}
if tx.instanceId() != db.instanceId {
return nil, ErrTxOtherInstance
}
return noOpTx{ReadTx: tx}, nil
}
return nil, ErrTxIsReadOnly
}
func (db *db) doReadTx(ctx context.Context, do func(c *driver.Conn) error) error {
tx, err := db.getReadTx(ctx)
if err != nil {
return err
}
if err = do(tx.conn()); err != nil {
err = replaceInterruptErr(err)
_ = tx.Commit()
return err
}
return tx.Commit()
}
func (db *db) Close() error {
if !db.closed.CompareAndSwap(false, true) {
return ErrDBIsClosed
}
if _, err := db.cm.GetWrite(context.Background()); err != nil {
return err
}
for _, stmt := range []*driver.Stmt{
db.stmt.registerCollection,
db.stmt.removeCollection,
db.stmt.renameCollection,
db.stmt.renameCollectionIndex,
db.stmt.registerIndex,
db.stmt.removeIndex,
} {
_ = stmt.Close()
}
var collToClose []Collection
db.mu.Lock()
for _, c := range db.openedCollections {
collToClose = append(collToClose, c)
}
db.mu.Unlock()
for _, c := range collToClose {
if cErr := c.(*collection).close(); cErr != nil {
log.Printf("collection close error: %v", cErr)
}
}
return db.cm.Close()
}
func (db *db) onCollectionClose(name string) {
db.mu.Lock()
delete(db.openedCollections, name)
db.mu.Unlock()
}