forked from Restream/reindexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
189 lines (155 loc) · 4.11 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
package reindexer
import (
"time"
"github.com/restream/reindexer/bindings"
"github.com/restream/reindexer/cjson"
)
type Tx struct {
namespace string
started bool
forceCommitCount uint32
db *Reindexer
ns *reindexerNamespace
counter uint32
ctx bindings.TxCtx
}
func newTx(db *Reindexer, namespace string) (*Tx, error) {
tx := &Tx{db: db, namespace: namespace, forceCommitCount: 0}
tx.startTx()
tx.ns, _ = tx.db.getNS(tx.namespace)
return tx, nil
}
func newTxAutocommit(db *Reindexer, namespace string, forceCommitCount uint32) (*Tx, error) {
tx := &Tx{db: db, namespace: namespace, forceCommitCount: forceCommitCount}
tx.startTx()
tx.ns, _ = tx.db.getNS(tx.namespace)
return tx, nil
}
func (tx *Tx) startTx() (err error) {
if tx.started {
return nil
}
tx.counter = 0
tx.started = true
tx.ctx, err = tx.db.binding.BeginTx(tx.namespace)
if err != nil {
return err
}
return nil
}
func (tx *Tx) Insert(item interface{}, precepts ...string) error {
tx.startTx()
return tx.modifyInternal(item, nil, modeInsert, precepts...)
}
func (tx *Tx) Update(item interface{}, precepts ...string) error {
tx.startTx()
return tx.modifyInternal(item, nil, modeUpdate, precepts...)
}
// Upsert (Insert or Update) item to index
func (tx *Tx) Upsert(item interface{}, precepts ...string) error {
tx.startTx()
return tx.modifyInternal(item, nil, modeUpsert, precepts...)
}
// UpsertJSON (Insert or Update) item to index
func (tx *Tx) UpsertJSON(json []byte, precepts ...string) error {
tx.startTx()
return tx.modifyInternal(nil, json, modeUpsert, precepts...)
}
// Delete - remove item by id from namespace
func (tx *Tx) Delete(item interface{}, precepts ...string) error {
tx.startTx()
return tx.modifyInternal(item, nil, modeDelete, precepts...)
}
// DeleteJSON - remove item by id from namespace
func (tx *Tx) DeleteJSON(json []byte, precepts ...string) error {
tx.startTx()
return tx.modifyInternal(nil, json, modeDelete, precepts...)
}
// Commit apply changes with count
func (tx *Tx) CommitWithCount(updatedAt *time.Time) (count int, err error) {
if !tx.started {
return 0, nil
}
if count, err = tx.commitInternal(); err != nil {
return
}
if updatedAt == nil {
now := time.Now().UTC()
updatedAt = &now
}
tx.db.setUpdatedAt(tx.ns, *updatedAt)
return
}
// Commit apply changes
func (tx *Tx) Commit(updatedAt *time.Time) error {
_, err := tx.CommitWithCount(updatedAt)
return err
}
func (tx *Tx) MustCommit(updatedAt *time.Time) int {
count, err := tx.CommitWithCount(updatedAt)
if err != nil {
panic(err)
}
return count
}
func (tx *Tx) modifyInternal(item interface{}, json []byte, mode int, precepts ...string) (err error) {
for tryCount := 0; tryCount < 2; tryCount++ {
ser := cjson.NewPoolSerializer()
defer ser.Close()
format := 0
stateToken := 0
if format, stateToken, err = packItem(tx.ns, item, json, ser); err != nil {
return err
}
err := tx.db.binding.ModifyItemTx(&tx.ctx, format, ser.Bytes(), mode, precepts, stateToken)
if err != nil {
rerr, ok := err.(bindings.Error)
if ok && rerr.Code() == bindings.ErrStateInvalidated {
tx.db.Query(tx.ns.name).Limit(0).Exec()
err = rerr
continue
}
return err
}
tx.counter++
if tx.counter > tx.forceCommitCount && tx.forceCommitCount != 0 {
tx.counter = 0
_, err := tx.commitInternal()
return err
}
return nil
}
return nil
}
// Commit apply changes
func (tx *Tx) commitInternal() (count int, err error) {
count = 0
out, err := tx.db.binding.CommitTx(&tx.ctx)
if err != nil {
return 0, err
}
defer out.Free()
rdSer := newSerializer(out.GetBuf())
rawQueryParams := rdSer.readRawQueryParams(func(nsid int) {
tx.ns.cjsonState.ReadPayloadType(&rdSer.Serializer)
})
if rawQueryParams.count == 0 {
return
}
tx.ns.cacheLock.Lock()
for i := 0; i < rawQueryParams.count; i++ {
count++
item := rdSer.readRawtItemParams()
delete(tx.ns.cacheItems, item.id)
}
tx.ns.cacheLock.Unlock()
return
}
// Rollback update
func (tx *Tx) Rollback() error {
err := tx.db.binding.RollbackTx(&tx.ctx)
if err != nil {
return err
}
return nil
}