forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
675 lines (552 loc) · 17.1 KB
/
tx.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
// Copyright 2019 The nutsdb Author. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nutsdb
import (
"bytes"
"errors"
"os"
"strings"
"sync"
"time"
"github.com/bwmarrin/snowflake"
"github.com/xujiajun/nutsdb/ds/list"
"github.com/xujiajun/nutsdb/ds/set"
"github.com/xujiajun/nutsdb/ds/zset"
"github.com/xujiajun/utils/strconv2"
)
var (
// ErrKeyAndValSize is returned when given key and value size is too big.
ErrKeyAndValSize = errors.New("key and value size too big")
// ErrTxClosed is returned when committing or rolling back a transaction
// that has already been committed or rolled back.
ErrTxClosed = errors.New("tx is closed")
// ErrTxNotWritable is returned when performing a write operation on
// a read-only transaction.
ErrTxNotWritable = errors.New("tx not writable")
// ErrKeyEmpty is returned if an empty key is passed on an update function.
ErrKeyEmpty = errors.New("key cannot be empty")
// ErrBucketEmpty is returned if bucket is empty.
ErrBucketEmpty = errors.New("bucket is empty")
// ErrRangeScan is returned when range scanning not found the result
ErrRangeScan = errors.New("range scans not found")
// ErrPrefixScan is returned when prefix scanning not found the result
ErrPrefixScan = errors.New("prefix scans not found")
// ErrPrefixSearchScan is returned when prefix and search scanning not found the result
ErrPrefixSearchScan = errors.New("prefix and search scans not found")
// ErrNotFoundKey is returned when key not found int the bucket on an view function.
ErrNotFoundKey = errors.New("key not found in the bucket")
)
var cachePool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
// Tx represents a transaction.
type Tx struct {
id uint64
db *DB
writable bool
pendingWrites []*Entry
ReservedStoreTxIDIdxes map[int64]*BPTree
}
// Begin opens a new transaction.
// Multiple read-only transactions can be opened at the same time but there can
// only be one read/write transaction at a time. Attempting to open a read/write
// transactions while another one is in progress will result in blocking until
// the current read/write transaction is completed.
// All transactions must be closed by calling Commit() or Rollback() when done.
func (db *DB) Begin(writable bool) (tx *Tx, err error) {
tx, err = newTx(db, writable)
if err != nil {
return nil, err
}
tx.lock()
if db.closed {
tx.unlock()
return nil, ErrDBClosed
}
return
}
// newTx returns a newly initialized Tx object at given writable.
func newTx(db *DB, writable bool) (tx *Tx, err error) {
var txID uint64
tx = &Tx{
db: db,
writable: writable,
pendingWrites: []*Entry{},
ReservedStoreTxIDIdxes: make(map[int64]*BPTree),
}
txID, err = tx.getTxID()
if err != nil {
return nil, err
}
tx.id = txID
return
}
// getTxID returns the tx id.
func (tx *Tx) getTxID() (id uint64, err error) {
node, err := snowflake.NewNode(tx.db.opt.NodeNum)
if err != nil {
return 0, err
}
id = uint64(node.Generate().Int64())
return
}
// Commit commits the transaction, following these steps:
//
// 1. check the length of pendingWrites.If there are no writes, return immediately.
//
// 2. check if the ActiveFile has not enough space to store entry. if not, call rotateActiveFile function.
//
// 3. write pendingWrites to disk, if a non-nil error,return the error.
//
// 4. build Hint index.
//
// 5. Unlock the database and clear the db field.
func (tx *Tx) Commit() error {
var (
e *Entry
bucketMetaTemp BucketMeta
)
if tx.db == nil {
return ErrDBClosed
}
writesLen := len(tx.pendingWrites)
if writesLen == 0 {
tx.unlock()
tx.db = nil
return nil
}
lastIndex := writesLen - 1
countFlag := CountFlagEnabled
if tx.db.isMerging {
countFlag = CountFlagDisabled
}
buff := cachePool.Get().(*bytes.Buffer)
defer func() {
buff.Reset()
cachePool.Put(buff)
}()
for i := 0; i < writesLen; i++ {
entry := tx.pendingWrites[i]
entrySize := entry.Size()
if entrySize > tx.db.opt.SegmentSize {
return ErrKeyAndValSize
}
bucket := string(entry.Meta.Bucket)
if tx.db.ActiveFile.ActualSize+int64(buff.Len())+entrySize > tx.db.opt.SegmentSize {
if _, err := tx.writeData(buff.Bytes()); err != nil {
return err
}
buff.Reset()
if err := tx.rotateActiveFile(); err != nil {
return err
}
}
offset := tx.db.ActiveFile.writeOff + int64(buff.Len())
if entry.Meta.Ds == DataStructureBPTree {
tx.db.BPTreeKeyEntryPosMap[string(entry.Meta.Bucket)+string(entry.Key)] = offset
}
if i == lastIndex {
entry.Meta.Status = Committed
}
if _, err := buff.Write(entry.Encode()); err != nil {
return err
}
if i == lastIndex {
if _, err := tx.writeData(buff.Bytes()); err != nil {
return err
}
}
if tx.db.opt.EntryIdxMode == HintBPTSparseIdxMode {
bucketMetaTemp = tx.buildTempBucketMetaIdx(bucket, entry.Key, bucketMetaTemp)
}
if i == lastIndex {
txID := entry.Meta.TxID
if tx.db.opt.EntryIdxMode == HintBPTSparseIdxMode {
if err := tx.buildTxIDRootIdx(txID, countFlag); err != nil {
return err
}
if err := tx.buildBucketMetaIdx(bucket, entry.Key, bucketMetaTemp); err != nil {
return err
}
} else {
tx.db.committedTxIds[txID] = struct{}{}
}
}
e = nil
if tx.db.opt.EntryIdxMode == HintKeyValAndRAMIdxMode {
e = entry
}
if entry.Meta.Ds == DataStructureBPTree {
tx.buildBPTreeIdx(bucket, entry, e, offset, countFlag)
}
if entry.Meta.Ds == DataStructureNone && entry.Meta.Flag == DataBPTreeBucketDeleteFlag {
tx.db.deleteBucket(DataStructureBPTree, bucket)
}
}
tx.buildIdxes(writesLen)
tx.unlock()
tx.db = nil
tx.pendingWrites = nil
tx.ReservedStoreTxIDIdxes = nil
return nil
}
func (tx *Tx) buildTempBucketMetaIdx(bucket string, key []byte, bucketMetaTemp BucketMeta) BucketMeta {
keySize := uint32(len(key))
if bucketMetaTemp.start == nil {
bucketMetaTemp = BucketMeta{start: key, end: key, startSize: keySize, endSize: keySize}
} else {
if compare(bucketMetaTemp.start, key) > 0 {
bucketMetaTemp.start = key
bucketMetaTemp.startSize = keySize
}
if compare(bucketMetaTemp.end, key) < 0 {
bucketMetaTemp.end = key
bucketMetaTemp.endSize = keySize
}
}
return bucketMetaTemp
}
func (tx *Tx) buildBucketMetaIdx(bucket string, key []byte, bucketMetaTemp BucketMeta) error {
bucketMeta, ok := tx.db.bucketMetas[bucket]
start := bucketMetaTemp.start
startSize := uint32(len(start))
end := bucketMetaTemp.end
endSize := uint32(len(end))
var updateFlag bool
if !ok {
bucketMeta = &BucketMeta{start: start, end: end, startSize: startSize, endSize: endSize}
updateFlag = true
} else {
if compare(bucketMeta.start, bucketMetaTemp.start) > 0 {
bucketMeta.start = start
bucketMeta.startSize = startSize
updateFlag = true
}
if compare(bucketMeta.end, bucketMetaTemp.end) < 0 {
bucketMeta.end = end
bucketMeta.endSize = endSize
updateFlag = true
}
}
if updateFlag {
fd, err := os.OpenFile(tx.db.getBucketMetaFilePath(bucket), os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer fd.Close()
if _, err = fd.WriteAt(bucketMeta.Encode(), 0); err != nil {
return err
}
if tx.db.opt.SyncEnable {
if err = fd.Sync(); err != nil {
return err
}
}
tx.db.bucketMetas[bucket] = bucketMeta
}
return nil
}
func (tx *Tx) buildTxIDRootIdx(txID uint64, countFlag bool) error {
txIDStr := strconv2.IntToStr(int(txID))
tx.db.ActiveCommittedTxIdsIdx.Insert([]byte(txIDStr), nil, &Hint{Meta: &MetaData{Flag: DataSetFlag}}, countFlag)
if len(tx.ReservedStoreTxIDIdxes) > 0 {
for fID, txIDIdx := range tx.ReservedStoreTxIDIdxes {
filePath := tx.db.getBPTTxIDPath(fID)
txIDIdx.Insert([]byte(txIDStr), nil, &Hint{Meta: &MetaData{Flag: DataSetFlag}}, countFlag)
txIDIdx.Filepath = filePath
err := txIDIdx.WriteNodes(tx.db.opt.RWMode, tx.db.opt.SyncEnable, 2)
if err != nil {
return err
}
filePath = tx.db.getBPTRootTxIDPath(fID)
txIDRootIdx := NewTree()
rootAddress := strconv2.Int64ToStr(txIDIdx.root.Address)
txIDRootIdx.Insert([]byte(rootAddress), nil, &Hint{Meta: &MetaData{Flag: DataSetFlag}}, countFlag)
txIDRootIdx.Filepath = filePath
err = txIDRootIdx.WriteNodes(tx.db.opt.RWMode, tx.db.opt.SyncEnable, 2)
if err != nil {
return err
}
}
}
return nil
}
func (tx *Tx) buildIdxes(writesLen int) {
for i := 0; i < writesLen; i++ {
entry := tx.pendingWrites[i]
bucket := string(entry.Meta.Bucket)
if entry.Meta.Ds == DataStructureSet {
tx.buildSetIdx(bucket, entry)
}
if entry.Meta.Ds == DataStructureSortedSet {
tx.buildSortedSetIdx(bucket, entry)
}
if entry.Meta.Ds == DataStructureList {
tx.buildListIdx(bucket, entry)
}
if entry.Meta.Ds == DataStructureNone {
if entry.Meta.Flag == DataSetBucketDeleteFlag {
tx.db.deleteBucket(DataStructureSet, bucket)
}
if entry.Meta.Flag == DataSortedSetBucketDeleteFlag {
tx.db.deleteBucket(DataStructureSortedSet, bucket)
}
if entry.Meta.Flag == DataListBucketDeleteFlag {
tx.db.deleteBucket(DataStructureList, bucket)
}
}
tx.db.KeyCount++
}
}
func (tx *Tx) buildBPTreeIdx(bucket string, entry, e *Entry, offset int64, countFlag bool) {
if tx.db.opt.EntryIdxMode == HintBPTSparseIdxMode {
newKey := []byte(bucket)
newKey = append(newKey, entry.Key...)
_ = tx.db.ActiveBPTreeIdx.Insert(newKey, e, &Hint{
FileID: tx.db.ActiveFile.fileID,
Key: newKey,
Meta: entry.Meta,
DataPos: uint64(offset),
}, countFlag)
} else {
if _, ok := tx.db.BPTreeIdx[bucket]; !ok {
tx.db.BPTreeIdx[bucket] = NewTree()
}
if tx.db.BPTreeIdx[bucket] == nil {
tx.db.BPTreeIdx[bucket] = NewTree()
}
_ = tx.db.BPTreeIdx[bucket].Insert(entry.Key, e, &Hint{
FileID: tx.db.ActiveFile.fileID,
Key: entry.Key,
Meta: entry.Meta,
DataPos: uint64(offset),
}, countFlag)
}
}
func (tx *Tx) buildSetIdx(bucket string, entry *Entry) {
if _, ok := tx.db.SetIdx[bucket]; !ok {
tx.db.SetIdx[bucket] = set.New()
}
if entry.Meta.Flag == DataDeleteFlag {
_ = tx.db.SetIdx[bucket].SRem(string(entry.Key), entry.Value)
}
if entry.Meta.Flag == DataSetFlag {
_ = tx.db.SetIdx[bucket].SAdd(string(entry.Key), entry.Value)
}
}
func (tx *Tx) buildSortedSetIdx(bucket string, entry *Entry) {
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
tx.db.SortedSetIdx[bucket] = zset.New()
}
switch entry.Meta.Flag {
case DataZAddFlag:
keyAndScore := strings.Split(string(entry.Key), SeparatorForZSetKey)
key := keyAndScore[0]
score, _ := strconv2.StrToFloat64(keyAndScore[1])
_ = tx.db.SortedSetIdx[bucket].Put(key, zset.SCORE(score), entry.Value)
case DataZRemFlag:
_ = tx.db.SortedSetIdx[bucket].Remove(string(entry.Key))
case DataZRemRangeByRankFlag:
start, _ := strconv2.StrToInt(string(entry.Key))
end, _ := strconv2.StrToInt(string(entry.Value))
_ = tx.db.SortedSetIdx[bucket].GetByRankRange(start, end, true)
case DataZPopMaxFlag:
_ = tx.db.SortedSetIdx[bucket].PopMax()
case DataZPopMinFlag:
_ = tx.db.SortedSetIdx[bucket].PopMin()
}
}
func (tx *Tx) buildListIdx(bucket string, entry *Entry) {
if _, ok := tx.db.ListIdx[bucket]; !ok {
tx.db.ListIdx[bucket] = list.New()
}
key, value := entry.Key, entry.Value
switch entry.Meta.Flag {
case DataLPushFlag:
_, _ = tx.db.ListIdx[bucket].LPush(string(key), value)
case DataRPushFlag:
_, _ = tx.db.ListIdx[bucket].RPush(string(key), value)
case DataLRemFlag:
countAndValue := strings.Split(string(value), SeparatorForListKey)
count, _ := strconv2.StrToInt(countAndValue[0])
newValue := countAndValue[1]
_, _ = tx.db.ListIdx[bucket].LRem(string(key), count, []byte(newValue))
case DataLPopFlag:
_, _ = tx.db.ListIdx[bucket].LPop(string(key))
case DataRPopFlag:
_, _ = tx.db.ListIdx[bucket].RPop(string(key))
case DataLSetFlag:
keyAndIndex := strings.Split(string(key), SeparatorForListKey)
newKey := keyAndIndex[0]
index, _ := strconv2.StrToInt(keyAndIndex[1])
_ = tx.db.ListIdx[bucket].LSet(newKey, index, value)
case DataLTrimFlag:
keyAndStartIndex := strings.Split(string(key), SeparatorForListKey)
newKey := keyAndStartIndex[0]
start, _ := strconv2.StrToInt(keyAndStartIndex[1])
end, _ := strconv2.StrToInt(string(value))
_ = tx.db.ListIdx[bucket].Ltrim(newKey, start, end)
case DataLRemByIndex:
indexes, _ := UnmarshalInts(value)
_, _ = tx.db.ListIdx[bucket].LRemByIndex(string(key), indexes)
}
}
// rotateActiveFile rotates log file when active file is not enough space to store the entry.
func (tx *Tx) rotateActiveFile() error {
var err error
fID := tx.db.MaxFileID
tx.db.MaxFileID++
if !tx.db.opt.SyncEnable && tx.db.opt.RWMode == MMap {
if err := tx.db.ActiveFile.rwManager.Sync(); err != nil {
return err
}
}
if err := tx.db.ActiveFile.rwManager.Release(); err != nil {
return err
}
if tx.db.opt.EntryIdxMode == HintBPTSparseIdxMode {
tx.db.ActiveBPTreeIdx.Filepath = tx.db.getBPTPath(fID)
tx.db.ActiveBPTreeIdx.enabledKeyPosMap = true
tx.db.ActiveBPTreeIdx.SetKeyPosMap(tx.db.BPTreeKeyEntryPosMap)
err = tx.db.ActiveBPTreeIdx.WriteNodes(tx.db.opt.RWMode, tx.db.opt.SyncEnable, 1)
if err != nil {
return err
}
BPTreeRootIdx := &BPTreeRootIdx{
rootOff: uint64(tx.db.ActiveBPTreeIdx.root.Address),
fID: uint64(fID),
startSize: uint32(len(tx.db.ActiveBPTreeIdx.FirstKey)),
endSize: uint32(len(tx.db.ActiveBPTreeIdx.LastKey)),
start: tx.db.ActiveBPTreeIdx.FirstKey,
end: tx.db.ActiveBPTreeIdx.LastKey,
}
_, err := BPTreeRootIdx.Persistence(tx.db.getBPTRootPath(fID),
0, tx.db.opt.SyncEnable)
if err != nil {
return err
}
tx.db.BPTreeRootIdxes = append(tx.db.BPTreeRootIdxes, BPTreeRootIdx)
// clear and reset BPTreeKeyEntryPosMap
tx.db.BPTreeKeyEntryPosMap = nil
tx.db.BPTreeKeyEntryPosMap = make(map[string]int64)
// clear and reset ActiveBPTreeIdx
tx.db.ActiveBPTreeIdx = nil
tx.db.ActiveBPTreeIdx = NewTree()
tx.ReservedStoreTxIDIdxes[fID] = tx.db.ActiveCommittedTxIdsIdx
// clear and reset ActiveCommittedTxIdsIdx
tx.db.ActiveCommittedTxIdsIdx = nil
tx.db.ActiveCommittedTxIdsIdx = NewTree()
}
// reset ActiveFile
path := tx.db.getDataPath(tx.db.MaxFileID)
tx.db.ActiveFile, err = tx.db.fm.getDataFile(path, tx.db.opt.SegmentSize)
if err != nil {
return err
}
tx.db.ActiveFile.fileID = tx.db.MaxFileID
return nil
}
func (tx *Tx) writeData(data []byte) (n int, err error) {
if len(data) == 0 {
return
}
writeOffset := tx.db.ActiveFile.ActualSize
l := len(data)
if writeOffset+int64(l) > tx.db.opt.SegmentSize {
return 0, errors.New("not enough file space")
}
if n, err = tx.db.ActiveFile.WriteAt(data, writeOffset); err != nil {
return
}
tx.db.ActiveFile.writeOff += int64(l)
tx.db.ActiveFile.ActualSize += int64(l)
if tx.db.opt.SyncEnable {
if err := tx.db.ActiveFile.rwManager.Sync(); err != nil {
return 0, err
}
}
return
}
// Rollback closes the transaction.
func (tx *Tx) Rollback() error {
if tx.db == nil {
return ErrDBClosed
}
tx.unlock()
tx.db = nil
tx.pendingWrites = nil
return nil
}
// lock locks the database based on the transaction type.
func (tx *Tx) lock() {
if tx.writable {
tx.db.mu.Lock()
} else {
tx.db.mu.RLock()
}
}
// unlock unlocks the database based on the transaction type.
func (tx *Tx) unlock() {
if tx.writable {
tx.db.mu.Unlock()
} else {
tx.db.mu.RUnlock()
}
}
func (tx *Tx) PutWithTimestamp(bucket string, key, value []byte, ttl uint32, timestamp uint64) error {
return tx.put(bucket, key, value, ttl, DataSetFlag, timestamp, DataStructureBPTree)
}
// Put sets the value for a key in the bucket.
// a wrapper of the function put.
func (tx *Tx) Put(bucket string, key, value []byte, ttl uint32) error {
return tx.put(bucket, key, value, ttl, DataSetFlag, uint64(time.Now().Unix()), DataStructureBPTree)
}
func (tx *Tx) checkTxIsClosed() error {
if tx.db == nil {
return ErrTxClosed
}
return nil
}
// put sets the value for a key in the bucket.
// Returns an error if tx is closed, if performing a write operation on a read-only transaction, if the key is empty.
func (tx *Tx) put(bucket string, key, value []byte, ttl uint32, flag uint16, timestamp uint64, ds uint16) error {
if err := tx.checkTxIsClosed(); err != nil {
return err
}
if !tx.writable {
return ErrTxNotWritable
}
if len(key) == 0 {
return ErrKeyEmpty
}
tx.pendingWrites = append(tx.pendingWrites, &Entry{
Key: key,
Value: value,
Meta: &MetaData{
KeySize: uint32(len(key)),
ValueSize: uint32(len(value)),
Timestamp: timestamp,
Flag: flag,
TTL: ttl,
Bucket: []byte(bucket),
BucketSize: uint32(len(bucket)),
Status: UnCommitted,
Ds: ds,
TxID: tx.id,
},
})
return nil
}