forked from voidDB/voidDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxn.go
More file actions
352 lines (280 loc) · 7.15 KB
/
txn.go
File metadata and controls
352 lines (280 loc) · 7.15 KB
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
package voidDB
import (
"errors"
"os"
"syscall"
"time"
"github.com/voidDB/voidDB/common"
"github.com/voidDB/voidDB/cursor"
"github.com/voidDB/voidDB/node"
"github.com/voidDB/voidDB/reader"
)
// A Txn is a transaction handle necessary for interacting with a database. A
// transaction is the sum of the state of the database as at the beginning of
// that transaction and any changes made within it. See [*Void.BeginTxn] for
// more information.
type Txn struct {
read readFunc
write writeFunc
sync syncFunc
abort func() error
oldestReader int
meta voidMeta
saveList map[int][]byte
freeWarm map[int][]int
freeCool map[int][]int
*cursor.Cursor
}
// SerialNumber returns a serial number identifying a particular state of the
// database as at the beginning of the transaction. All transactions beginning
// from the same state share the same serial number.
func (txn *Txn) SerialNumber() int {
return txn.meta.getSerialNumber()
}
// Timestamp returns the time as at the beginning of the transaction.
func (txn *Txn) Timestamp() time.Time {
return txn.meta.getTimestamp()
}
// OpenCursor returns a handle on a cursor associated with the transaction and
// a particular keyspace. Keyspaces allow multiple datasets with potentially
// intersecting (overlapping) sets of keys to reside within the same database
// without conflict, provided that all keys are unique within their respective
// keyspaces. Argument keyspace must not be simultaneously non-nil and of zero
// length, or otherwise longer than [node.MaxKeyLength]. Passing nil as the
// argument causes OpenCursor to return a cursor in the default keyspace.
//
// CAUTION: An application utilising keyspaces should avoid modifying records
// within the default keyspace, as it is used to store pointers to all the
// other keyspaces. There is virtually no limit on the number of keyspaces in a
// database.
//
// Unless multiple keyspaces are required, there is usually no need to invoke
// OpenCursor because the transaction handle embeds a [*cursor.Cursor]
// associated with the default keyspace.
func (txn *Txn) OpenCursor(keyspace []byte) (c *cursor.Cursor, e error) {
var (
pointer []byte
)
if keyspace == nil {
return txn.Cursor, nil
}
pointer, e = txn.Cursor.Get(keyspace)
switch {
case errors.Is(e, common.ErrorNotFound) && !txn.isReadOnly():
e = txn.setRootNodePointer(keyspace,
medium{txn, nil}.Save(
node.NewNode(),
),
)
if e != nil {
return
}
return txn.OpenCursor(keyspace)
case e != nil:
return
}
c = cursor.NewCursor(medium{txn, keyspace},
common.GetIntFromWord(pointer),
)
return
}
// Abort discards all changes made in a read-write transaction, and releases
// the exclusive write lock. In the case of a read-only transaction, Abort ends
// the moratorium on recycling of pages constituting its view of the dataset.
// For this reason, applications should not be slow to abort transactions that
// have outlived their usefulness lest they prevent effective resource
// utilisation. Following an invocation of Abort, the transaction handle must
// no longer be used.
func (txn *Txn) Abort() (e error) {
e = txn.abort()
*txn = Txn{}
return
}
// Commit persists all changes to data made in a transaction. The state of the
// database is not really updated until Commit has been invoked. If it returns
// a nil error, effects of the transaction would be perceived in subsequent
// transactions, whereas pre-existing transactions will remain oblivious as
// intended. Whether Commit waits on [*os.File.Sync] depends on the mustSync
// argument passed to [*Void.BeginTxn]. The transaction handle is not safe to
// reuse after the first invocation of Commit, regardless of the result.
func (txn *Txn) Commit() (e error) {
var (
data []byte
offset int
abort = func() {
e = errors.Join(e,
txn.Abort(),
)
}
)
defer abort()
txn.enqueueFreeLists()
for offset, data = range txn.saveList {
e = txn.write(data, offset)
if e != nil {
return
}
}
if txn.sync != nil {
e = txn.sync()
if e != nil {
return
}
}
e = txn.putMeta()
if e != nil {
return
}
if txn.sync != nil {
e = txn.sync()
if e != nil {
return
}
}
return
}
func newTxn(path string, readerTable *reader.ReaderTable,
read readFunc, write writeFunc, sync syncFunc,
) (
txn *Txn, e error,
) {
var (
lockfile *os.File
)
txn = &Txn{
read: read,
write: write,
sync: sync,
}
e = txn.getMeta()
if e != nil {
return
}
txn.meta.setTimestamp()
txn.meta.setSerialNumber(
txn.meta.getSerialNumber() + 1,
)
switch {
case txn.isReadOnly():
txn.write = denyPermission
txn.abort = readerTable.AcquireHold(
txn.meta.getSerialNumber(),
)
default:
txn.saveList = make(map[int][]byte)
txn.freeWarm = make(map[int][]int)
txn.freeCool = make(map[int][]int)
lockfile, e = os.OpenFile(path, os.O_RDONLY, 0)
if e != nil {
return
}
e = syscall.Flock(
int(lockfile.Fd()),
syscall.LOCK_EX|syscall.LOCK_NB,
)
if e != nil {
return
}
txn.oldestReader = readerTable.OldestReader()
txn.abort = lockfile.Close
}
txn.Cursor = cursor.NewCursor(medium{txn, nil},
txn.meta.getRootNodePointer(),
)
return
}
func (txn *Txn) isReadOnly() bool {
return txn.write == nil
}
func (txn *Txn) setRootNodePointer(keyspace []byte, pointer int) (e error) {
var (
value []byte
)
if keyspace == nil {
txn.meta.setRootNodePointer(pointer)
return
}
value = common.NewWord()
common.PutIntIntoWord(value, pointer)
return txn.Cursor.Put(keyspace, value)
}
func (txn *Txn) getMeta() (e error) {
var (
meta0 voidMeta = txn.read(0, common.PageSize)
meta1 voidMeta = txn.read(common.PageSize, common.PageSize)
)
switch {
case meta0.isMeta() && meta1.isMeta() &&
meta0.getSerialNumber() < meta1.getSerialNumber():
txn.meta = meta1
case meta0.isMeta() && meta1.isMeta():
txn.meta = meta0
case meta0.isMeta():
txn.meta = meta0
case meta1.isMeta():
txn.meta = meta1
default:
e = common.ErrorInvalid
}
txn.meta = txn.meta.makeCopy(
txn.isReadOnly(),
)
return
}
func (txn *Txn) putMeta() error {
return txn.write(txn.meta,
txn.meta.getSerialNumber()%2*common.PageSize,
)
}
func (txn *Txn) enqueueFreeLists() {
var (
size int
)
for size = range txn.freeWarm {
if size == common.PageSize {
continue
}
txn.meta.freeQueue(size).Enqueue(
medium{txn, nil},
txn.meta.getSerialNumber(),
txn.freeWarm[size],
false,
)
}
for size = range txn.freeCool {
if size == common.PageSize {
continue
}
txn.meta.freeQueue(size).Enqueue(
medium{txn, nil},
txn.meta.getSerialNumber(),
txn.freeCool[size],
false,
)
}
if len(txn.freeWarm[common.PageSize]) > 0 {
txn.meta.freeQueue(common.PageSize).Enqueue(
medium{txn, nil},
txn.meta.getSerialNumber(),
txn.freeWarm[common.PageSize],
false,
)
}
if len(txn.freeCool[common.PageSize]) > 0 {
txn.meta.freeQueue(common.PageSize).Enqueue(
medium{txn, nil},
txn.meta.getSerialNumber(),
txn.freeCool[common.PageSize],
true,
)
}
return
}
type readFunc func(int, int) []byte
type writeFunc func([]byte, int) error
var (
denyPermission writeFunc = func([]byte, int) error {
return syscall.EACCES
}
)
type syncFunc func() error