-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
db.go
845 lines (696 loc) · 19.6 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
package clover
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sync/atomic"
"github.com/gofrs/uuid/v5"
d "github.com/ostafen/clover/v2/document"
"github.com/ostafen/clover/v2/index"
"github.com/ostafen/clover/v2/internal"
"github.com/ostafen/clover/v2/query"
"github.com/ostafen/clover/v2/store"
"github.com/ostafen/clover/v2/store/bbolt"
)
// Collection creation errors
var (
ErrCollectionExist = errors.New("collection already exist")
ErrCollectionNotExist = errors.New("no such collection")
ErrIndexExist = errors.New("index already exist")
ErrIndexNotExist = errors.New("no such index")
ErrDocumentNotExist = errors.New("no such document")
ErrDuplicateKey = errors.New("duplicate key")
)
type docConsumer func(doc *d.Document) error
// DB represents the entry point of each clover database.
type DB struct {
store store.Store
closed uint32
}
type collectionMetadata struct {
Size int
Indexes []index.Info
}
// CreateCollection creates a new empty collection with the given name.
func (db *DB) CreateCollection(name string) error {
tx, err := db.store.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
ok, err := db.hasCollection(name, tx)
if err != nil {
return err
}
if ok {
return ErrCollectionExist
}
meta := &collectionMetadata{Size: 0}
if err := db.saveCollectionMetadata(name, meta, tx); err != nil {
return err
}
return tx.Commit()
}
func (db *DB) CreateCollectionByQuery(name string, q *query.Query) error {
err := db.CreateCollection(name)
if err != nil {
return err
}
docs, err := db.FindAll(q)
if err != nil {
return err
}
if len(docs) == 0 { // just an empty collection
return nil
} else {
return db.Insert(name, docs...)
}
}
func (db *DB) saveCollectionMetadata(collection string, meta *collectionMetadata, tx store.Tx) error {
rawMeta, err := json.Marshal(meta)
if err != nil {
return err
}
return tx.Set([]byte(getCollectionKey(collection)), rawMeta)
}
func (db *DB) hasCollection(name string, tx store.Tx) (bool, error) {
value, err := tx.Get([]byte(getCollectionKey(name)))
return value != nil, err
}
func getCollectionKey(name string) string {
return getCollectionKeyPrefix() + name
}
func getCollectionKeyPrefix() string {
return "coll:"
}
// DropCollection removes the collection with the given name, deleting any content on disk.
func (db *DB) DropCollection(name string) error {
tx, err := db.store.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
if err := db.deleteAll(tx, name); err != nil {
return err
}
if err := tx.Delete([]byte(getCollectionKey(name))); err != nil {
return err
}
return tx.Commit()
}
func (db *DB) deleteAll(tx store.Tx, collName string) error {
return db.replaceDocs(tx, query.NewQuery(collName), func(_ *d.Document) *d.Document {
return nil
})
}
// HasCollection returns true if and only if the database contains a collection with the given name.
func (db *DB) HasCollection(name string) (bool, error) {
txn, err := db.store.Begin(false)
if err != nil {
return false, err
}
defer txn.Rollback()
return db.hasCollection(name, txn)
}
func NewObjectId() string {
objId, _ := uuid.NewV4()
return objId.String()
}
// Insert adds the supplied documents to a collection.
func (db *DB) Insert(collectionName string, docs ...*d.Document) error {
for _, doc := range docs {
if !doc.Has(d.ObjectIdField) || doc.Get(d.ObjectIdField) == "" {
objectId := NewObjectId()
doc.Set(d.ObjectIdField, objectId)
}
}
tx, err := db.store.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
meta, err := db.getCollectionMeta(collectionName, tx)
if err != nil {
return err
}
indexes := db.getIndexes(tx, collectionName, meta)
for _, doc := range docs {
if err := db.addDocToIndexes(tx, indexes, doc); err != nil {
return err
}
key := []byte(getDocumentKey(collectionName, doc.ObjectId()))
value, err := tx.Get(key)
if err != nil {
return err
}
if value != nil {
return ErrDuplicateKey
}
if err := saveDocument(doc, key, tx); err != nil {
return err
}
}
meta.Size += len(docs)
if err := db.saveCollectionMetadata(collectionName, meta, tx); err != nil {
return err
}
return tx.Commit()
}
func (db *DB) getIndexes(tx store.Tx, collection string, meta *collectionMetadata) []index.Index {
indexes := make([]index.Index, 0)
for _, info := range meta.Indexes {
indexes = append(indexes, index.CreateIndex(collection, info.Field, info.Type, tx))
}
return indexes
}
func saveDocument(doc *d.Document, key []byte, tx store.Tx) error {
if err := d.Validate(doc); err != nil {
return err
}
data, err := d.Encode(doc)
if err != nil {
return err
}
return tx.Set(key, data)
}
func (db *DB) addDocToIndexes(tx store.Tx, indexes []index.Index, doc *d.Document) error {
// update indexes
for _, idx := range indexes {
fieldVal := doc.Get(idx.Field()) // missing fields are treated as null
err := idx.Add(doc.ObjectId(), fieldVal, doc.TTL())
if err != nil {
return err
}
}
return nil
}
func getDocumentKey(collection string, id string) string {
return getDocumentKeyPrefix(collection) + id
}
func getDocumentKeyPrefix(collection string) string {
return "c:" + collection + ";" + "d:"
}
func (db *DB) getCollectionMeta(collection string, tx store.Tx) (*collectionMetadata, error) {
value, err := tx.Get([]byte(getCollectionKey(collection)))
if err != nil {
return nil, err
}
if value == nil {
return nil, ErrCollectionNotExist
}
m := &collectionMetadata{}
err = json.Unmarshal(value, m)
return m, err
}
// Save or update a document, If you pass in a custom struct instead of a Document object,
// it is recommended to specify the _id field using struct tags.
func (db *DB) Save(collectionName string, data interface{}) error {
doc := d.NewDocumentOf(data)
if !doc.Has(d.ObjectIdField) || doc.Get(d.ObjectIdField) == "" {
return db.Insert(collectionName, doc)
}
return db.ReplaceById(collectionName, doc.ObjectId(), doc)
}
// InsertOne inserts a single document to an existing collection. It returns the id of the inserted document.
func (db *DB) InsertOne(collectionName string, doc *d.Document) (string, error) {
err := db.Insert(collectionName, doc)
return doc.ObjectId(), err
}
// Open opens a new clover database on the supplied path. If such a folder doesn't exist, it is automatically created.
func Open(dir string) (*DB, error) {
dataStore, err := bbolt.Open(dir)
if err != nil {
return nil, err
}
return OpenWithStore(dataStore)
}
// OpenWithStore opens a new clover database using the provided store.
func OpenWithStore(store store.Store) (*DB, error) {
return &DB{store: store}, nil
}
// Close releases all the resources and closes the database. After the call, the instance will no more be usable.
func (db *DB) Close() error {
if atomic.CompareAndSwapUint32(&db.closed, 0, 1) {
return db.store.Close()
}
return nil
}
// FindAll selects all the documents satisfying q.
func (db *DB) FindAll(q *query.Query) ([]*d.Document, error) {
q, err := normalizeCriteria(q)
if err != nil {
return nil, err
}
docs := make([]*d.Document, 0)
err = db.IterateDocs(q, func(doc *d.Document) error {
docs = append(docs, doc)
return nil
})
return docs, err
}
func (db *DB) IterateDocs(q *query.Query, consumer docConsumer) error {
tx, err := db.store.Begin(false)
if err != nil {
return err
}
defer tx.Rollback()
return db.iterateDocs(tx, q, consumer)
}
// FindFirst returns the first document (if any) satisfying the query.
func (db *DB) FindFirst(q *query.Query) (*d.Document, error) {
docs, err := db.FindAll(q.Limit(1))
var doc *d.Document
if len(docs) > 0 {
doc = docs[0]
}
return doc, err
}
// ForEach runs the consumer function for each document matching the provided query.
// If false is returned from the consumer function, then the iteration is stopped.
func (db *DB) ForEach(q *query.Query, consumer func(_ *d.Document) bool) error {
q, err := normalizeCriteria(q)
if err != nil {
return err
}
return db.IterateDocs(q, func(doc *d.Document) error {
if !consumer(doc) {
return internal.ErrStopIteration
}
return nil
})
}
// Count returns the number of documents which satisfy the query (i.e. len(q.FindAll()) == q.Count()).
func (db *DB) Count(q *query.Query) (int, error) {
q, err := normalizeCriteria(q)
if err != nil {
return -1, err
}
if q.Criteria() == nil { // simply return the size of the collection in this case
return db.countCollection(q)
}
num := 0
err = db.IterateDocs(q, func(doc *d.Document) error {
num++
return nil
})
return num, err
}
func (db *DB) countCollection(q *query.Query) (int, error) {
size, err := db.getCollectionSize(q.Collection())
size -= q.GetSkip()
if size < 0 {
size = 0
}
if q.GetLimit() >= 0 && q.GetLimit() < size {
return q.GetLimit(), err
}
return size, err
}
func (db *DB) getCollectionSize(collection string) (int, error) {
tx, err := db.store.Begin(false)
if err != nil {
return -1, err
}
defer tx.Rollback()
meta, err := db.getCollectionMeta(collection, tx)
if err != nil {
return -1, err
}
return meta.Size, nil
}
// Exists returns true if and only if the query result set is not empty.
func (db *DB) Exists(q *query.Query) (bool, error) {
doc, err := db.FindFirst(q)
return doc != nil, err
}
// FindById returns the document with the given id, if such a document exists and satisfies the underlying query, or null.
func (db *DB) FindById(collection string, id string) (*d.Document, error) {
tx, err := db.store.Begin(false)
if err != nil {
return nil, err
}
defer tx.Rollback()
ok, err := db.hasCollection(collection, tx)
if err != nil {
return nil, err
}
if !ok {
return nil, ErrCollectionNotExist
}
return getDocumentById(collection, id, tx)
}
func getDocumentById(collectionName string, id string, tx store.Tx) (*d.Document, error) {
value, err := tx.Get([]byte(getDocumentKey(collectionName, id)))
if value == nil || err != nil {
return nil, err
}
return d.Decode(value)
}
// DeleteById removes the document with the given id from the underlying collection, provided that such a document exists and satisfies the underlying query.
func (db *DB) DeleteById(collection string, id string) error {
tx, err := db.store.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
meta, err := db.getCollectionMeta(collection, tx)
if err != nil {
return err
}
indexes := db.getIndexes(tx, collection, meta)
if err := db.getDocAndDeleteFromIndexes(tx, indexes, collection, id); err != nil {
return err
}
if err := tx.Delete([]byte(getDocumentKey(collection, id))); err != nil {
return err
}
meta.Size--
if err := db.saveCollectionMetadata(collection, meta, tx); err != nil {
return err
}
return tx.Commit()
}
func (db *DB) getDocAndDeleteFromIndexes(tx store.Tx, indexes []index.Index, collection string, docId string) error {
if len(indexes) == 0 {
return nil
}
doc, err := getDocumentById(collection, docId, tx)
if err != nil {
return err
}
if doc == nil {
return nil
}
for _, idx := range indexes {
value := doc.Get(idx.Field())
if err := idx.Remove(doc.ObjectId(), value); err != nil {
return err
}
}
return nil
}
// UpdateById updates the document with the specified id using the supplied update map.
// If no document with the specified id exists, an ErrDocumentNotExist is returned.
func (db *DB) UpdateById(collectionName string, docId string, updater func(doc *d.Document) *d.Document) error {
tx, err := db.store.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
meta, err := db.getCollectionMeta(collectionName, tx)
if err != nil {
return err
}
indexes := db.getIndexes(tx, collectionName, meta)
docKey := getDocumentKey(collectionName, docId)
value, err := tx.Get([]byte(docKey))
if err != nil {
return err
}
if value == nil {
return ErrDocumentNotExist
}
doc, err := d.Decode(value)
if err != nil {
return err
}
updatedDoc := updater(doc)
if err := db.updateIndexesOnDocUpdate(tx, indexes, doc, updatedDoc); err != nil {
return err
}
if err := saveDocument(updatedDoc, []byte(docKey), tx); err != nil {
return err
}
return tx.Commit()
}
func (db *DB) updateIndexesOnDocUpdate(tx store.Tx, indexes []index.Index, oldDoc, newDoc *d.Document) error {
if err := db.deleteDocFromIndexes(indexes, oldDoc); err != nil {
return err
}
if newDoc != nil {
if err := db.addDocToIndexes(tx, indexes, newDoc); err != nil {
return err
}
}
return nil
}
func (db *DB) deleteDocFromIndexes(indexes []index.Index, doc *d.Document) error {
for _, idx := range indexes {
value := doc.Get(idx.Field())
if err := idx.Remove(doc.ObjectId(), value); err != nil {
return err
}
}
return nil
}
// ReplaceById replaces the document with the specified id with the one provided.
// If no document exists, an ErrDocumentNotExist is returned.
func (db *DB) ReplaceById(collection, docId string, doc *d.Document) error {
if doc.ObjectId() != docId {
return fmt.Errorf("the id of the document must match the one supplied")
}
return db.UpdateById(collection, docId, func(_ *d.Document) *d.Document {
return doc
})
}
// Update updates all the document selected by q using the provided updateMap.
// Each update is specified by a mapping fieldName -> newValue.
func (db *DB) Update(q *query.Query, updateMap map[string]interface{}) error {
q, err := normalizeCriteria(q)
if err != nil {
return err
}
return db.UpdateFunc(q, func(doc *d.Document) *d.Document {
newDoc := doc.Copy()
newDoc.SetAll(updateMap)
return newDoc
})
}
// UpdateFunc updates all the document selected by q using the provided function.
func (db *DB) UpdateFunc(q *query.Query, updateFunc func(doc *d.Document) *d.Document) error {
txn, err := db.store.Begin(true)
if err != nil {
return err
}
defer txn.Rollback()
q, err = normalizeCriteria(q)
if err != nil {
return err
}
if err := db.replaceDocs(txn, q, updateFunc); err != nil {
return err
}
return txn.Commit()
}
type docUpdater func(doc *d.Document) *d.Document
func (db *DB) replaceDocs(tx store.Tx, q *query.Query, updater docUpdater) error {
meta, err := db.getCollectionMeta(q.Collection(), tx)
if err != nil {
return err
}
indexes := db.getIndexes(tx, q.Collection(), meta)
deletedDocs := 0
err = db.iterateDocs(tx, q, func(doc *d.Document) error {
docKey := []byte(getDocumentKey(q.Collection(), doc.ObjectId()))
newDoc := updater(doc)
if err := db.updateIndexesOnDocUpdate(tx, indexes, doc, newDoc); err != nil {
return err
}
if newDoc == nil {
deletedDocs++
return tx.Delete(docKey)
}
return saveDocument(newDoc, docKey, tx)
})
if err != nil {
return err
}
if deletedDocs > 0 {
meta.Size -= deletedDocs
if err := db.saveCollectionMetadata(q.Collection(), meta, tx); err != nil {
return err
}
}
return nil
}
func (db *DB) iterateDocs(tx store.Tx, q *query.Query, consumer docConsumer) error {
meta, err := db.getCollectionMeta(q.Collection(), tx)
if err != nil {
return err
}
nd := buildQueryPlan(q, db.getIndexes(tx, q.Collection(), meta), &consumerNode{consumer: consumer})
return execPlan(nd, tx)
}
// Delete removes all the documents selected by q from the underlying collection.
func (db *DB) Delete(q *query.Query) error {
q, err := normalizeCriteria(q)
if err != nil {
return err
}
tx, err := db.store.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
if err := db.replaceDocs(tx, q, func(_ *d.Document) *d.Document { return nil }); err != nil {
return err
}
return tx.Commit()
}
// ListCollections returns a slice of strings containing the name of each collection stored in the db.
func (db *DB) ListCollections() ([]string, error) {
tx, err := db.store.Begin(true)
if err != nil {
return nil, err
}
defer tx.Rollback()
collections := make([]string, 0)
prefix := []byte(getCollectionKeyPrefix())
err = iteratePrefix(prefix, tx, func(item store.Item) error {
collectionName := string(bytes.TrimPrefix(item.Key, prefix))
collections = append(collections, collectionName)
return nil
})
return collections, err
}
func iteratePrefix(prefix []byte, tx store.Tx, itemConsumer func(item store.Item) error) error {
cursor, err := tx.Cursor(true)
if err != nil {
return err
}
defer cursor.Close()
if err := cursor.Seek(prefix); err != nil {
return err
}
for ; cursor.Valid(); cursor.Next() {
item, err := cursor.Item()
if err != nil {
return err
}
if !bytes.HasPrefix(item.Key, prefix) {
return nil
}
err = itemConsumer(item)
// do not propagate iteration stop error
if errors.Is(err, internal.ErrStopIteration) {
return nil
}
if err != nil {
return err
}
}
return nil
}
// CreateIndex creates an index for the specified for the specified (index, collection) pair.
func (db *DB) CreateIndex(collection, field string) error {
return db.createIndex(collection, field, index.SingleField)
}
func (db *DB) createIndex(collection, field string, indexType index.Type) error {
tx, err := db.store.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
meta, err := db.getCollectionMeta(collection, tx)
if err != nil {
return err
}
for i := 0; i < len(meta.Indexes); i++ {
if meta.Indexes[i].Field == field {
return ErrIndexExist
}
}
if meta.Indexes == nil {
meta.Indexes = make([]index.Info, 0)
}
meta.Indexes = append(meta.Indexes, index.Info{Field: field, Type: indexType})
idx := index.CreateIndex(collection, field, indexType, tx)
err = db.iterateDocs(tx, query.NewQuery(collection), func(doc *d.Document) error {
value := doc.Get(field)
return idx.Add(doc.ObjectId(), value, doc.TTL())
})
if err != nil {
return err
}
if err := db.saveCollectionMetadata(collection, meta, tx); err != nil {
return err
}
return tx.Commit()
}
// HasIndex returns true if an index exists for the specified (index, collection) pair.
func (db *DB) HasIndex(collection, field string) (bool, error) {
tx, err := db.store.Begin(false)
if err != nil {
return false, err
}
defer tx.Rollback()
return db.hasIndex(tx, collection, field)
}
func (db *DB) hasIndex(tx store.Tx, collection, field string) (bool, error) {
meta, err := db.getCollectionMeta(collection, tx)
if err == nil {
for _, idx := range meta.Indexes {
if idx.Field == field {
return true, nil
}
}
}
return false, err
}
// DropIndex deletes the index, is such index exists for the specified (index, collection) pair.
func (db *DB) DropIndex(collection, field string) error {
txn, err := db.store.Begin(true)
if err != nil {
return err
}
defer txn.Rollback()
meta, err := db.getCollectionMeta(collection, txn)
if err != nil {
return err
}
j := -1
for i := 0; i < len(meta.Indexes); i++ {
if meta.Indexes[i].Field == field {
j = i
}
}
if j < 0 {
return ErrIndexNotExist
}
idxType := meta.Indexes[j].Type
meta.Indexes[j] = meta.Indexes[0]
meta.Indexes = meta.Indexes[1:]
idx := index.CreateIndex(collection, field, idxType, txn)
if err := idx.Drop(); err != nil {
return err
}
if err := db.saveCollectionMetadata(collection, meta, txn); err != nil {
return err
}
return txn.Commit()
}
// ListIndexes returns a list containing the names of all the indexes for the specified collection.
func (db *DB) ListIndexes(collection string) ([]index.Info, error) {
txn, err := db.store.Begin(false)
if err != nil {
return nil, err
}
defer txn.Rollback()
return db.listIndexes(collection, txn)
}
func (db *DB) listIndexes(collection string, tx store.Tx) ([]index.Info, error) {
meta, err := db.getCollectionMeta(collection, tx)
return meta.Indexes, err
}
func normalizeCriteria(q *query.Query) (*query.Query, error) {
if q.Criteria() != nil {
v := &CriteriaNormalizeVisitor{}
c := q.Criteria().Accept(v)
if v.err != nil {
return nil, v.err
}
q = q.Where(c.(query.Criteria))
}
return q, nil
}