-
Notifications
You must be signed in to change notification settings - Fork 4
/
tx.go
140 lines (112 loc) · 2.93 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
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package kvstore
import (
"errors"
"fmt"
"github.com/cockroachdb/pebble"
)
type Tx[T any] struct {
table *Table[T]
readOnly bool
batch *pebble.Batch
}
func NewTx[T any](table *Table[T], readOnly bool) (*Tx[T], error) {
if table.db == nil {
return nil, errors.New("the table has not been added to a db")
}
if table.db.store == nil {
return nil, errors.New("the db has not been added to a store")
}
if table.db.store.rawDB == nil {
return nil, errors.New("the store has not been opened")
}
return &Tx[T]{
table: table,
readOnly: readOnly,
batch: table.db.store.rawDB.NewIndexedBatch(),
}, nil
}
func (this *Tx[T]) Set(key string, value T) error {
if this.readOnly {
return errors.New("can not set value in readonly transaction")
}
if len(key) > KeyMaxLength {
return ErrKeyTooLong
}
valueBytes, err := this.table.encoder.Encode(value)
if err != nil {
return err
}
return this.table.set(this, key, valueBytes, value, false, false)
}
func (this *Tx[T]) SetSync(key string, value T) error {
if this.readOnly {
return errors.New("can not set value in readonly transaction")
}
if len(key) > KeyMaxLength {
return ErrKeyTooLong
}
valueBytes, err := this.table.encoder.Encode(value)
if err != nil {
return err
}
return this.table.set(this, key, valueBytes, value, false, true)
}
func (this *Tx[T]) Insert(key string, value T) error {
if this.readOnly {
return errors.New("can not set value in readonly transaction")
}
if len(key) > KeyMaxLength {
return ErrKeyTooLong
}
valueBytes, err := this.table.encoder.Encode(value)
if err != nil {
return err
}
return this.table.set(this, key, valueBytes, value, true, false)
}
func (this *Tx[T]) Get(key string) (value T, err error) {
if this.table.isClosed {
err = NewTableClosedErr(this.table.name)
return
}
return this.table.get(this, key)
}
func (this *Tx[T]) Delete(key string) error {
if this.table.isClosed {
return NewTableClosedErr(this.table.name)
}
if this.readOnly {
return errors.New("can not delete value in readonly transaction")
}
return this.table.deleteKeys(this, key)
}
func (this *Tx[T]) NewIterator(opt *IteratorOptions) (*pebble.Iterator, error) {
return this.batch.NewIter(opt.RawOptions())
}
func (this *Tx[T]) Close() error {
return this.batch.Close()
}
func (this *Tx[T]) Commit() (err error) {
return this.commit(DefaultWriteOptions)
}
func (this *Tx[T]) CommitSync() (err error) {
return this.commit(DefaultWriteSyncOptions)
}
func (this *Tx[T]) Query() *Query[T] {
var query = NewQuery[T]()
query.SetTx(this)
return query
}
func (this *Tx[T]) commit(opt *pebble.WriteOptions) (err error) {
defer func() {
var panicErr = recover()
if panicErr != nil {
resultErr, ok := panicErr.(error)
if ok {
err = fmt.Errorf("commit batch failed: %w", resultErr)
}
}
}()
return this.batch.Commit(opt)
}