-
Notifications
You must be signed in to change notification settings - Fork 337
/
sql_tx.go
142 lines (105 loc) · 3.22 KB
/
sql_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
/*
Copyright 2022 Codenotary Inc. 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 sql
import (
"context"
"errors"
"time"
"github.com/codenotary/immudb/embedded/store"
)
// SQLTx (no-thread safe) represents an interactive or incremental transaction with support of RYOW
type SQLTx struct {
engine *Engine
opts *TxOptions
tx *store.OngoingTx
catalog *Catalog // in-mem catalog
mutatedCatalog bool // set when a DDL stmt was executed within the current tx
updatedRows int
lastInsertedPKs map[string]int64 // last inserted PK by table name
firstInsertedPKs map[string]int64 // first inserted PK by table name
txHeader *store.TxHeader // header is set once tx is committed
}
func (sqlTx *SQLTx) Catalog() *Catalog {
return sqlTx.catalog
}
func (sqlTx *SQLTx) IsExplicitCloseRequired() bool {
return sqlTx.opts.ExplicitClose
}
func (sqlTx *SQLTx) RequireExplicitClose() error {
if sqlTx.updatedRows != 0 {
return store.ErrIllegalState
}
sqlTx.opts.ExplicitClose = true
return nil
}
func (sqlTx *SQLTx) Timestamp() time.Time {
return sqlTx.tx.Timestamp()
}
func (sqlTx *SQLTx) UpdatedRows() int {
return sqlTx.updatedRows
}
func (sqlTx *SQLTx) LastInsertedPKs() map[string]int64 {
return sqlTx.lastInsertedPKs
}
func (sqlTx *SQLTx) FirstInsertedPKs() map[string]int64 {
return sqlTx.firstInsertedPKs
}
func (sqlTx *SQLTx) TxHeader() *store.TxHeader {
return sqlTx.txHeader
}
func (sqlTx *SQLTx) sqlPrefix() []byte {
return sqlTx.engine.prefix
}
func (sqlTx *SQLTx) distinctLimit() int {
return sqlTx.engine.distinctLimit
}
func (sqlTx *SQLTx) newKeyReader(rSpec store.KeyReaderSpec) (store.KeyReader, error) {
return sqlTx.tx.NewKeyReader(rSpec)
}
func (sqlTx *SQLTx) get(key []byte) (store.ValueRef, error) {
return sqlTx.tx.Get(key)
}
func (sqlTx *SQLTx) set(key []byte, metadata *store.KVMetadata, value []byte) error {
return sqlTx.tx.Set(key, metadata, value)
}
func (sqlTx *SQLTx) existKeyWith(prefix, neq []byte) (bool, error) {
_, _, err := sqlTx.tx.GetWithPrefix(prefix, neq)
if errors.Is(err, store.ErrKeyNotFound) {
return false, nil
}
return err == nil, err
}
func (sqlTx *SQLTx) Cancel() error {
return sqlTx.tx.Cancel()
}
func (sqlTx *SQLTx) Commit(ctx context.Context) error {
err := sqlTx.tx.RequireMVCCOnFollowingTxs(sqlTx.mutatedCatalog)
if err != nil {
return err
}
// no need to wait for indexing to be up to date during commit phase
sqlTx.txHeader, err = sqlTx.tx.AsyncCommit(ctx)
if err != nil && !errors.Is(err, store.ErrNoEntriesProvided) {
return err
}
return nil
}
func (sqlTx *SQLTx) Closed() bool {
return sqlTx.tx.Closed()
}
func (sqlTx *SQLTx) Committed() bool {
return sqlTx.txHeader != nil
}
func (sqlTx *SQLTx) delete(key []byte) error {
return sqlTx.tx.Delete(key)
}