-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_t_array.go
541 lines (507 loc) · 21 KB
/
buffer_t_array.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
//////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2018-2022 YottaDB LLC and/or its subsidiaries. //
// All rights reserved. //
// //
// This source code contains the intellectual property //
// of its copyright holder(s), and is made available //
// under a license. If you do not know the terms of //
// the license, please stop and do not read further. //
// //
//////////////////////////////////////////////////////////////////
package yottadb
import (
"fmt"
"io"
"os"
"runtime"
"sync"
"sync/atomic"
"unsafe"
)
// #include <stdlib.h>
// #include <string.h>
// #include "libyottadb.h"
// int ydb_tp_st_wrapper_cgo(uint64_t tptoken, void *tpfnparm);
import "C"
// Variables used by TpST to wrap passing in func so the callback can retrieve it without passing pointers to C.
var tpIndex uint64
var tpMap sync.Map
// BufferTArray is an array of ydb_buffer_t structures. The reason this is not an array of BufferT structures is because
// we can't pass a pointer to those Go structures to a C routine (cgo restriction) so we have to have this separate
// array of the C structures instead. Also, cgo doesn't support indexing of C structures so we have to do that ourselves
// as well. Because this structure's contents contain pointers to C allocated storage, this structure is NOT safe for
// concurrent access unless those accesses are to different array elements and do not affect the overall structure.
type BufferTArray struct {
cbuftary *internalBufferTArray
}
type internalBufferTArray struct {
elemUsed uint32 // Number of elements used
elemAlloc uint32 // Number of elements in array
cbuftary *[]C.ydb_buffer_t // C flavor of ydb_buffer_t array
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Data manipulation methods for BufferTArray
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Alloc is a method to allocate an array of 'numBufs' ydb_buffer_t structures anchored in this BufferTArray and also
// for each of those buffers, allocate 'nBytes' byte buffers anchoring them in the ydb_buffer_t structure.
func (buftary *BufferTArray) Alloc(numBufs, nBytes uint32) {
var i uint32
printEntry("BufferTArray.Alloc()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of Alloc() cannot be nil")
}
// Forget the previous structure, then allocate a new one if needed
buftary.cbuftary = nil
if 0 != numBufs {
// Allocate new ydb_buffer_t array and initialize
len := C.size_t(uint32(C.sizeof_ydb_buffer_t) * numBufs)
cbuftary := (*[]C.ydb_buffer_t)(allocMem(len))
buftary.cbuftary = &internalBufferTArray{0, numBufs, cbuftary}
// Allocate a buffer for each ydb_buffer_t structure of nBytes bytes
for i = 0; numBufs > i; i++ {
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cbuftary)) +
uintptr(C.sizeof_ydb_buffer_t*i)))
elemptr.buf_addr = (*C.char)(allocMem(C.size_t(nBytes)))
elemptr.len_alloc = C.uint(nBytes)
elemptr.len_used = 0
}
runtime.SetFinalizer(buftary.cbuftary, func(o *internalBufferTArray) {
o.Free()
})
}
runtime.KeepAlive(buftary)
}
// Dump is a STAPI method to dump (print) the contents of this BufferTArray block for debugging purposes. It dumps to stdout
// - cbuftary as a hexadecimal address,
// - elemAlloc and elemUsed as integers,
// - and for each element of the smaller of elemAlloc and elemUsed elements of the ydb_buffer_t array referenced by cbuftary,
// buf_addr as a hexadecimal address, len_alloc and len_used as integers and the smaller of len_used and len_alloc bytes at
// the address buf_addr in zwrite format.
func (buftary *BufferTArray) Dump() {
buftary.DumpToWriter(os.Stdout)
}
//DumpToWriter is a writer that allows the tests or user code to dump to other than stdout.
func (buftary *BufferTArray) DumpToWriter(writer io.Writer) {
printEntry("BufferTArray.Dump()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of Dump() cannot be nil")
}
cbuftary := buftary.getCPtr()
if nil != cbuftary {
fmt.Fprintf(writer, "BufferTArray.Dump(): cbuftary: %p, elemAlloc: %d, elemUsed: %d, elemLenAlloc: %d\n", cbuftary,
buftary.ElemAlloc(), buftary.ElemUsed(), buftary.ElemLenAlloc())
for i := 0; int(buftary.ElemUsed()) > i; i++ {
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer((uintptr(unsafe.Pointer(cbuftary)) +
uintptr(C.sizeof_ydb_buffer_t*i))))
// It is possible len_used is greater than len_alloc (if this buffer was populated by SimpleAPI C code)
// Ensure we do not overrun the allocated buffer while dumping this object in that case.
min := elemptr.len_used
if min > elemptr.len_alloc {
min = elemptr.len_alloc
}
valstr := C.GoStringN(elemptr.buf_addr, C.int(min))
fmt.Fprintf(writer, " %d: %s\n", i, valstr)
}
}
runtime.KeepAlive(buftary)
}
// Free is a method to release all allocated C storage in a BufferTArray. It is the inverse of the Alloc() method: release the numSubs buffers
// and the ydb_buffer_t array. Set cbuftary to nil, and elemAlloc and elemUsed to zero.
func (buftary *BufferTArray) Free() {
printEntry("BufferTArray.Free()")
if nil == buftary {
return
}
buftary.cbuftary.Free()
buftary.cbuftary = nil
}
// Free cleans up C memory for the internal BufferTArray object
func (ibuftary *internalBufferTArray) Free() {
printEntry("internalBufferTArray.Free()")
if nil == ibuftary {
return
}
// Deallocate the buffers in each ydb_buffer_t
cbuftary := ibuftary.cbuftary
if nil == cbuftary {
return // Nothing to do
}
for i := 0; int(ibuftary.elemAlloc) > i; i++ {
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cbuftary)) +
uintptr(C.sizeof_ydb_buffer_t*i)))
if 0 != elemptr.len_alloc {
freeMem(unsafe.Pointer(elemptr.buf_addr), C.size_t(elemptr.len_alloc))
}
}
// Array buffers are freed, now free the array of ydb_buffer_t structs if it exists
freeMem(unsafe.Pointer(cbuftary), C.size_t(C.sizeof_ydb_buffer_t*ibuftary.elemAlloc))
// The below keeps ibuftary around long enough to get rid of this block's C memory. No KeepAlive() necessary.
ibuftary.cbuftary = nil
}
// getCPtr returns a pointer to the internal C.ydb_buffer_t
func (buftary *BufferTArray) getCPtr() *C.ydb_buffer_t {
ptr := (*C.ydb_buffer_t)(nil)
if (nil != buftary) && (nil != buftary.cbuftary) {
ptr = (*C.ydb_buffer_t)(unsafe.Pointer(buftary.cbuftary.cbuftary))
}
return ptr
}
// ElemAlloc is a method to return elemAlloc from a BufferTArray.
func (buftary *BufferTArray) ElemAlloc() uint32 {
printEntry("BufferTArray.ElemAlloc()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of ElemAlloc() cannot be nil")
}
if nil == buftary.cbuftary {
return 0
}
return buftary.cbuftary.elemAlloc
}
// ElemLenAlloc is a method to retrieve the buffer allocation length associated with our BufferTArray.
// Since all buffers are the same size in this array, just return the value from the first array entry.
// If nothing is allocated yet, return 0.
func (buftary *BufferTArray) ElemLenAlloc() uint32 {
var retlen uint32
printEntry("BufferTArray.ElemLenAlloc()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of ElemLenAlloc() cannot be nil")
}
cbuftary := buftary.getCPtr()
if nil != cbuftary {
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(cbuftary))
retlen = uint32(elemptr.len_alloc)
} else { // Nothing is allocated yet so "allocated length" is 0
retlen = 0
}
runtime.KeepAlive(buftary)
return retlen
}
// ElemLenUsed is a method to retrieve the buffer used length associated with a given buffer referenced by its index.
func (buftary *BufferTArray) ElemLenUsed(tptoken uint64, errstr *BufferT, idx uint32) (uint32, error) {
printEntry("BufferTArray.ElemLenUsed()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of ElemLenUsed() cannot be nil")
}
cbuftary := buftary.getCPtr()
if nil == cbuftary {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_STRUCTUNALLOCD))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching STRUCTUNALLOCD: %s", err))
}
return 0, &YDBError{(int)(YDB_ERR_STRUCTUNALLOCD), errmsg}
}
elemcnt := buftary.ElemAlloc()
if idx >= elemcnt { // Request for non-existant element
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_INSUFFSUBS))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching INSUFFSUBS: %s", err))
}
return 0, &YDBError{(int)(YDB_ERR_INSUFFSUBS), errmsg}
}
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cbuftary)) + uintptr(C.sizeof_ydb_buffer_t*idx)))
retlen := uint32(elemptr.len_used)
runtime.KeepAlive(buftary)
return retlen, nil
}
// ElemUsed is a method to return elemUsed from a BufferTArray.
func (buftary *BufferTArray) ElemUsed() uint32 {
printEntry("BufferTArray.ElemUsed()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of ElemUsed() cannot be nil")
}
if nil == buftary.cbuftary {
return 0
}
return buftary.cbuftary.elemUsed
}
// ValBAry is a method to fetch the buffer of the indicated array element and return it as a byte array.
func (buftary *BufferTArray) ValBAry(tptoken uint64, errstr *BufferT, idx uint32) ([]byte, error) {
var bary []byte
printEntry("BufferTArray.ValBAry()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of ValBAry() cannot be nil")
}
elemcnt := buftary.ElemAlloc()
if !(idx < elemcnt) {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_INSUFFSUBS))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching INSUFFSUBS: %s", err))
}
return nil, &YDBError{(int)(YDB_ERR_INSUFFSUBS), errmsg}
}
cbuftary := buftary.getCPtr()
if nil == cbuftary {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_STRUCTUNALLOCD))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching STRUCTUNALLOCD: %s", err))
}
return nil, &YDBError{(int)(YDB_ERR_STRUCTUNALLOCD), errmsg}
}
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cbuftary)) + uintptr(C.sizeof_ydb_buffer_t*idx)))
lenalloc := elemptr.len_alloc
lenused := elemptr.len_used
cbufptr := elemptr.buf_addr
if lenused > lenalloc { // INVSTRLEN from last operation return what we can and give error
bary = C.GoBytes(unsafe.Pointer(cbufptr), C.int(lenalloc)) // Return what we can (alloc size)
errmsg := formatINVSTRLEN(tptoken, errstr, lenalloc, lenused)
return bary, &YDBError{(int)(YDB_ERR_INVSTRLEN), errmsg}
}
// The entire buffer is there so return that
bary = C.GoBytes(unsafe.Pointer(cbufptr), C.int(lenused))
runtime.KeepAlive(buftary)
return bary, nil
}
// ValStr is a method to fetch the buffer of the indicated array element and return it as a string.
func (buftary *BufferTArray) ValStr(tptoken uint64, errstr *BufferT, idx uint32) (string, error) {
var str string
printEntry("BufferTArray.ValStr()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of ValStr() cannot be nil")
}
elemcnt := buftary.ElemAlloc()
if !(idx < elemcnt) {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_INSUFFSUBS))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching INSUFFSUBS: %s", err))
}
return "", &YDBError{(int)(YDB_ERR_INSUFFSUBS), errmsg}
}
cbuftary := buftary.getCPtr()
if nil == cbuftary {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_STRUCTUNALLOCD))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching STRUCTUNALLOCD: %s", err))
}
return "", &YDBError{(int)(YDB_ERR_STRUCTUNALLOCD), errmsg}
}
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cbuftary)) + uintptr(C.sizeof_ydb_buffer_t*idx)))
lenalloc := elemptr.len_alloc
lenused := elemptr.len_used
cbufptr := elemptr.buf_addr
if lenused > lenalloc { // INVSTRLEN from last operation return what we can and give error
str = C.GoStringN(cbufptr, C.int(lenalloc)) // Return what we can (alloc size)
errmsg := formatINVSTRLEN(tptoken, errstr, lenalloc, lenused)
return str, &YDBError{(int)(YDB_ERR_INVSTRLEN), errmsg}
}
// The entire buffer is there so return that
str = C.GoStringN(cbufptr, C.int(lenused))
runtime.KeepAlive(buftary)
return str, nil
}
// SetElemLenUsed is a method to set the len_used field of a given ydb_buffer_t struct in the BufferTArray.
func (buftary *BufferTArray) SetElemLenUsed(tptoken uint64, errstr *BufferT, idx, newLen uint32) error {
printEntry("BufferTArray.SetElemLenUsed()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of SetElemLenUsed() cannot be nil")
}
elemcnt := buftary.ElemAlloc()
if !(idx < elemcnt) {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_INSUFFSUBS))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching INSUFFSUBS: %s", err))
}
return &YDBError{(int)(YDB_ERR_INSUFFSUBS), errmsg}
}
cbuftary := buftary.getCPtr()
if nil == cbuftary {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_STRUCTUNALLOCD))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching STRUCTUNALLOCD: %s", err))
}
return &YDBError{(int)(YDB_ERR_STRUCTUNALLOCD), errmsg}
}
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cbuftary)) + uintptr(C.sizeof_ydb_buffer_t*idx)))
lenalloc := elemptr.len_alloc
if newLen > uint32(lenalloc) { // INVSTRLEN from last operation - return what we can and give error
errmsg := formatINVSTRLEN(tptoken, errstr, lenalloc, C.uint(newLen))
return &YDBError{(int)(YDB_ERR_INVSTRLEN), errmsg}
}
// Set the new used length
elemptr.len_used = C.uint(newLen)
runtime.KeepAlive(buftary)
return nil
}
// SetElemUsed is a method to set the number of used buffers in the BufferTArray.
func (buftary *BufferTArray) SetElemUsed(tptoken uint64, errstr *BufferT, newUsed uint32) error {
printEntry("BufferTArray.SetElemUsed()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of SetElemUsed() cannot be nil")
}
elemcnt := buftary.ElemAlloc()
if newUsed > elemcnt {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_INSUFFSUBS))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching INSUFFSUBS: %s", err))
}
return &YDBError{(int)(YDB_ERR_INSUFFSUBS), errmsg}
}
// Set the number of used buffers
if nil != buftary.cbuftary {
buftary.cbuftary.elemUsed = newUsed
}
return nil
}
// SetValBAry is a method to set a byte array (value) into the buffer at the given index (idx).
func (buftary *BufferTArray) SetValBAry(tptoken uint64, errstr *BufferT, idx uint32, value []byte) error {
printEntry("BufferTArray.SetValBAry()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of SetValBAry() cannot be nil")
}
elemcnt := buftary.ElemAlloc()
if !(idx < elemcnt) {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_INSUFFSUBS))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching INSUFFSUBS: %s", err))
}
return &YDBError{(int)(YDB_ERR_INSUFFSUBS), errmsg}
}
cbuftary := buftary.getCPtr()
if nil == cbuftary {
// Create an error to return
errmsg, err := MessageT(tptoken, errstr, (int)(YDB_ERR_STRUCTUNALLOCD))
if nil != err {
panic(fmt.Sprintf("YDB: Error fetching STRUCTUNALLOCD: %s", err))
}
return &YDBError{(int)(YDB_ERR_STRUCTUNALLOCD), errmsg}
}
elemptr := (*C.ydb_buffer_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cbuftary)) + uintptr(C.sizeof_ydb_buffer_t*idx)))
lenalloc := uint32(elemptr.len_alloc)
vallen := uint32(len(value))
if vallen > lenalloc { // INVSTRLEN from last operation - return what we can and give error
errmsg := formatINVSTRLEN(tptoken, errstr, C.uint(lenalloc), C.uint(vallen))
return &YDBError{(int)(YDB_ERR_INVSTRLEN), errmsg}
}
// Copy the Go buffer to the C buffer
if 0 < vallen {
C.memcpy(unsafe.Pointer(elemptr.buf_addr), unsafe.Pointer(&value[0]), C.size_t(vallen))
}
elemptr.len_used = C.uint(vallen) // Set the used length of the buffer for this element
runtime.KeepAlive(buftary)
return nil
}
// SetValStr is a method to set a string (value) into the buffer at the given index (idx).
func (buftary *BufferTArray) SetValStr(tptoken uint64, errstr *BufferT, idx uint32, value string) error {
printEntry("BufferTArray.SetValStr()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of SetValBAry() cannot be nil")
}
valuebary := []byte(value)
return buftary.SetValBAry(tptoken, errstr, idx, valuebary)
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Simple (Threaded) API methods for BufferTArray
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// DeleteExclST is a method to delete all local variables EXCEPT the variables listed in the method BufferTArray.
// If the input array is empty, then ALL local variables are deleted. DeleteExclST() wraps ydb_delete_excl_st() to delete all
// local variable trees except those of local variables whose names are specified in the BufferTArray structure. In the special case
// where elemUsed is zero, the method deletes all local variable trees.
//
// In the event that the elemUsed exceeds YDB_MAX_NAMES, the error return is ERRNAMECOUNT2HI.
//
// As M and Go application code cannot be mixed in the same process, the warning in ydb_delete_excl_s() does not apply.
func (buftary *BufferTArray) DeleteExclST(tptoken uint64, errstr *BufferT) error {
var cbuft *C.ydb_buffer_t
printEntry("BufferTArray.DeleteExclST()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of DeleteExclST() cannot be nil")
}
if 1 != atomic.LoadUint32(&ydbInitialized) {
initializeYottaDB()
}
if nil != errstr {
cbuft = errstr.getCPtr()
}
rc := C.ydb_delete_excl_st(C.uint64_t(tptoken), cbuft, C.int(buftary.ElemUsed()), buftary.getCPtr())
if YDB_OK != rc {
err := NewError(tptoken, errstr, int(rc))
return err
}
runtime.KeepAlive(buftary)
runtime.KeepAlive(errstr)
return nil
}
// TpST wraps ydb_tp_st() to implement transaction processing.
//
// Any function implementing logic for a transaction should return an error code with one of the following:
//
// - A normal return (nil) to indicate that per application logic, the transaction can be committed. The YottaDB database engine
// will commit the transaction if it is able to, and if not, will call the function again.
//
// - TPRESTART to indicate that the transaction should restart, either because application logic has so determined or because a YottaDB
// function called by the function has returned TPRESTART.
//
// - ROLLBACK to indicate that TpST() should not commit the transaction, and should return ROLLBACK to the caller.
//
// The BufferTArray receiving the TpST() method is a list of local variables whose values should be saved, and restored to their
// original values when the transaction restarts. If the cbuftary structures have not been allocated or elemUsed is zero, no
// local variables are saved and restored; and if elemUsed is 1, and that sole element references the string "*" all local variables
// are saved and restored.
//
// A case-insensitive value of "BA" or "BATCH" for transid indicates to YottaDB that it need not ensure Durability for this
// transaction (it continues to ensure Atomicity, Consistency, and Isolation)
//
// Parameters:
//
// tptoken - the token used to identify nested transaction; start with yottadb.NOTTP
// errstr - Buffer to hold error string that is used to report errors and avoid race conditions with setting $ZSTATUS.
// tpfn - the closure function which will be run during the transaction. This closure function may get invoked multiple times
// if a transaction fails for some reason (concurrent changes, for example), so should not change any data outside of
// the database.
// transid - See docs for ydb_tp_s() in the MLPG.
func (buftary *BufferTArray) TpST(tptoken uint64, errstr *BufferT, tpfn func(uint64, *BufferT) int32, transid string) error {
var cbuft *C.ydb_buffer_t
printEntry("TpST()")
if nil == buftary {
panic("YDB: *BufferTArray receiver of TpST() cannot be nil")
}
if 1 != atomic.LoadUint32(&ydbInitialized) {
initializeYottaDB()
}
tid := C.CString(transid)
defer freeMem(unsafe.Pointer(tid), C.size_t(len(transid)))
tpfnparm := atomic.AddUint64(&tpIndex, 1)
tpMap.Store(tpfnparm, tpfn)
if nil != errstr {
cbuft = errstr.getCPtr()
}
cbuftary := buftary.getCPtr()
rc := C.ydb_tp_st(C.uint64_t(tptoken), cbuft, (C.ydb_tpfnptr_t)(C.ydb_tp_st_wrapper_cgo),
unsafe.Pointer(&tpfnparm), tid, C.int(buftary.ElemUsed()), cbuftary)
tpMap.Delete(tpfnparm)
if YDB_OK != rc {
err := NewError(tptoken, errstr, int(rc))
return err
}
runtime.KeepAlive(buftary)
runtime.KeepAlive(errstr)
return nil
}
// YdbTpStWrapper is a private callback to wrap calls to the Go closure required for TpST.
//export ydbTpStWrapper
func ydbTpStWrapper(tptoken uint64, errstr *C.ydb_buffer_t, tpfnparm unsafe.Pointer) int32 {
var errbuff BufferT
index := *((*uint64)(tpfnparm))
v, ok := tpMap.Load(index)
if !ok {
panic("YDB: Could not find callback routine")
}
errbuff.bufferTFromPtr((unsafe.Pointer)(errstr))
retval := (v.(func(uint64, *BufferT) int32))(tptoken, &errbuff)
runtime.KeepAlive(errstr)
return retval
}