-
Notifications
You must be signed in to change notification settings - Fork 9
/
serialize.go
802 lines (659 loc) · 28.9 KB
/
serialize.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package tiledb
/*
#include <tiledb/tiledb.h>
#include <tiledb/tiledb_serialization.h>
#include <stdio.h>
#include <stdlib.h>
*/
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)
// SerializeArraySchemaToBuffer serializes an array schema and returns a Buffer object containing the payload.
func SerializeArraySchemaToBuffer(schema *ArraySchema, serializationType SerializationType, clientSide bool) (*Buffer, error) {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
buffer := Buffer{context: schema.context}
freeOnGC(&buffer)
ret := C.tiledb_serialize_array_schema(schema.context.tiledbContext, schema.tiledbArraySchema, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array schema: %w", schema.context.LastError())
}
return &buffer, nil
}
// SerializeArraySchema serializes an array schema.
//
// Deprecated: Use SerializeArraySchemaToBuffer instead.
func SerializeArraySchema(schema *ArraySchema, serializationType SerializationType, clientSide bool) ([]byte, error) {
buffer, err := SerializeArraySchemaToBuffer(schema, serializationType, clientSide)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeArraySchema deserializes a new array schema from the given buffer.
func DeserializeArraySchema(buffer *Buffer, serializationType SerializationType, clientSide bool) (*ArraySchema, error) {
schema := ArraySchema{context: buffer.context}
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
ret := C.tiledb_deserialize_array_schema(schema.context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, &schema.tiledbArraySchema)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error deserializing array schema: %w", schema.context.LastError())
}
// This needs to happen *after* the tiledb_deserialize_array_schema call
// because that may leave the arraySchema with a non-nil pointer
// to already-freed memory.
freeOnGC(&schema)
return &schema, nil
}
// SerializeArraySchemaEvolution serializes the given array schema evolution and serializes the group metadata and returns a Buffer object containing the payload.
func SerializeArraySchemaEvolutionToBuffer(arraySchemaEvolution *ArraySchemaEvolution, serializationType SerializationType, clientSide bool) (*Buffer, error) {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
buffer := Buffer{context: arraySchemaEvolution.context}
freeOnGC(&buffer)
ret := C.tiledb_serialize_array_schema_evolution(
arraySchemaEvolution.context.tiledbContext,
arraySchemaEvolution.tiledbArraySchemaEvolution,
C.tiledb_serialization_type_t(serializationType),
cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array schem evolution: %w",
arraySchemaEvolution.context.LastError())
}
return &buffer, nil
}
// SerializeArraySchemaEvolution serializes the given array schema evolution.
//
// Deprecated: Use SerializeArraySchemaEvolutionToBuffer instead.
func SerializeArraySchemaEvolution(arraySchemaEvolution *ArraySchemaEvolution, serializationType SerializationType, clientSide bool) ([]byte, error) {
buffer, err := SerializeArraySchemaEvolutionToBuffer(arraySchemaEvolution, serializationType, clientSide)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeArraySchemaEvolution deserializes a new array schema evolution object from the given buffer.
func DeserializeArraySchemaEvolution(buffer *Buffer, serializationType SerializationType, clientSide bool) (*ArraySchemaEvolution, error) {
arraySchemaEvolution := ArraySchemaEvolution{context: buffer.context}
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
ret := C.tiledb_deserialize_array_schema_evolution(
arraySchemaEvolution.context.tiledbContext, buffer.tiledbBuffer,
C.tiledb_serialization_type_t(serializationType),
cClientSide, &arraySchemaEvolution.tiledbArraySchemaEvolution)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error deserializing array schema evolution: %w", arraySchemaEvolution.context.LastError())
}
// This needs to happen *after* the tiledb_deserialize_array_schema_evolution
// call because that may leave the schemaEvolution with a non-nil pointer
// to already-freed memory.
freeOnGC(&arraySchemaEvolution)
return &arraySchemaEvolution, nil
}
// SerializeArrayNonEmptyDomainToBuffer gets and serializes the array nonempty domain and returns a Buffer object containing the payload.
func SerializeArrayNonEmptyDomainToBuffer(a *Array, serializationType SerializationType) (*Buffer, error) {
schema, err := a.Schema()
if err != nil {
return nil, err
}
domain, err := schema.Domain()
if err != nil {
return nil, err
}
domainType, err := domain.Type()
if err != nil {
return nil, err
}
ndims, err := domain.NDim()
if err != nil {
return nil, err
}
subarraySize := 2 * ndims * uint(domainType.Size())
var isEmpty C.int32_t
tmpDomain := make([]uint8, subarraySize)
ret := C.tiledb_array_get_non_empty_domain(a.context.tiledbContext, a.tiledbArray, slicePtr(tmpDomain), &isEmpty)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array nonempty domain: %w", a.context.LastError())
}
buffer := Buffer{context: schema.context}
freeOnGC(&buffer)
var cClientSide = C.int32_t(0) // Currently this parameter is unused in libtiledb
ret = C.tiledb_serialize_array_nonempty_domain(a.context.tiledbContext, a.tiledbArray, slicePtr(tmpDomain), isEmpty, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array nonempty domain: %w", a.context.LastError())
}
return &buffer, nil
}
// SerializeArrayNonEmptyDomain gets and serializes the array nonempty domain.
//
// Deprecated: Use SerializeArrayNonEmptyDomainToBuffer instead.
func SerializeArrayNonEmptyDomain(a *Array, serializationType SerializationType) ([]byte, error) {
buffer, err := SerializeArrayNonEmptyDomainToBuffer(a, serializationType)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeArrayNonEmptyDomain deserializes an array nonempty domain.
func DeserializeArrayNonEmptyDomain(a *Array, buffer *Buffer, serializationType SerializationType) ([]NonEmptyDomain, bool, error) {
schema, err := a.Schema()
if err != nil {
return nil, false, err
}
domain, err := schema.Domain()
if err != nil {
return nil, false, err
}
domainType, err := domain.Type()
if err != nil {
return nil, false, err
}
ndims, err := domain.NDim()
if err != nil {
return nil, false, err
}
tmpDomain, tmpDomainPtr, err := domainType.MakeSlice(uint64(2 * ndims))
if err != nil {
return nil, false, err
}
var cClientSide = C.int32_t(0) // Currently this parameter is unused in libtiledb
var isEmpty C.int32_t
ret := C.tiledb_deserialize_array_nonempty_domain(a.context.tiledbContext, a.tiledbArray, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, tmpDomainPtr, &isEmpty)
if ret != C.TILEDB_OK {
return nil, false, fmt.Errorf("error serializing array nonempty domain: %w", a.context.LastError())
}
if isEmpty == 1 {
return nil, true, nil
}
nonEmptyDomains := make([]NonEmptyDomain, ndims)
for i := range nonEmptyDomains {
dimension, err := domain.DimensionFromIndex(uint(i))
if err != nil {
return nil, false, err
}
var nonEmptyDomain *NonEmptyDomain
switch tmpDomain := tmpDomain.(type) {
case []int:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []int8:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []int16:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []int32:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []int64:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []uint:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []uint8:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []uint16:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []uint32:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []uint64:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []float32:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []float64:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
case []bool:
nonEmptyDomain, err = getNonEmptyDomainForDim(dimension, tmpDomain[2*i:2*i+2])
}
if err != nil {
return nil, false, err
}
nonEmptyDomains[i] = *nonEmptyDomain
}
return nonEmptyDomains, false, nil
}
// SerializeArrayNonEmptyDomainAllDimensionsToBuffer gets and serializes the array nonempty domain and returns a Buffer object containing the payload.
func SerializeArrayNonEmptyDomainAllDimensionsToBuffer(a *Array, serializationType SerializationType) (*Buffer, error) {
buffer := Buffer{context: a.context}
freeOnGC(&buffer)
var cClientSide = C.int32_t(0) // Currently this parameter is unused in libtiledb
ret := C.tiledb_serialize_array_non_empty_domain_all_dimensions(a.context.tiledbContext, a.tiledbArray, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array nonempty domain: %w", a.context.LastError())
}
return &buffer, nil
}
// SerializeArrayNonEmptyDomainAllDimensions gets and serializes the array nonempty domain.
//
// Deprecated: Use SerializeArrayNonEmptyDomainAllDimensionsToBuffer instead.
func SerializeArrayNonEmptyDomainAllDimensions(a *Array, serializationType SerializationType) ([]byte, error) {
buffer, err := SerializeArrayNonEmptyDomainAllDimensionsToBuffer(a, serializationType)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeArrayNonEmptyDomainAllDimensions deserializes an array nonempty domain.
func DeserializeArrayNonEmptyDomainAllDimensions(a *Array, buffer *Buffer, serializationType SerializationType) error {
var cClientSide = C.int32_t(0) // Currently this parameter is unused in libtiledb
ret := C.tiledb_deserialize_array_non_empty_domain_all_dimensions(a.context.tiledbContext, a.tiledbArray, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing array nonempty domain: %w", a.context.LastError())
}
return nil
}
// SerializeQuery serializes a query.
func SerializeQuery(query *Query, serializationType SerializationType, clientSide bool) (*BufferList, error) {
bufferList := BufferList{context: query.context}
freeOnGC(&bufferList)
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
ret := C.tiledb_serialize_query(query.context.tiledbContext, query.tiledbQuery, C.tiledb_serialization_type_t(serializationType), cClientSide, &bufferList.tiledbBufferList)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing query: %w", query.context.LastError())
}
return &bufferList, nil
}
// DeserializeQuery deserializes a buffer into an existing query.
func DeserializeQuery(query *Query, buffer *Buffer, serializationType SerializationType, clientSide bool) error {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
ret := C.tiledb_deserialize_query(query.context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, query.tiledbQuery)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing query: %w", query.context.LastError())
}
return nil
}
// SerializeArrayMetadataToBuffer gets and serializes the array metadata and returns a Buffer object containing the payload.
func SerializeArrayMetadataToBuffer(a *Array, serializationType SerializationType) (*Buffer, error) {
buffer := Buffer{context: a.context}
freeOnGC(&buffer)
ret := C.tiledb_serialize_array_metadata(a.context.tiledbContext, a.tiledbArray, C.tiledb_serialization_type_t(serializationType), &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array metadata: %w", a.context.LastError())
}
return &buffer, nil
}
// SerializeArrayMetadata gets and serializes the array metadata.
//
// Deprecated: Use SerializeArrayMetadataToBuffer instead.
func SerializeArrayMetadata(a *Array, serializationType SerializationType) ([]byte, error) {
buffer, err := SerializeArrayMetadataToBuffer(a, serializationType)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeArrayMetadata deserializes array metadata.
func DeserializeArrayMetadata(a *Array, buffer *Buffer, serializationType SerializationType) error {
ret := C.tiledb_deserialize_array_metadata(a.context.tiledbContext, a.tiledbArray, C.tiledb_serialization_type_t(serializationType), buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing array metadata: %w", a.context.LastError())
}
return nil
}
// SerializeQueryEstResultSizesToBuffer gets and serializes the query estimated result sizes and returns a Buffer object containing the payload.
func SerializeQueryEstResultSizesToBuffer(q *Query, serializationType SerializationType, clientSide bool) (*Buffer, error) {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
buffer := Buffer{context: q.context}
freeOnGC(&buffer)
ret := C.tiledb_serialize_query_est_result_sizes(q.context.tiledbContext, q.tiledbQuery, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing query est buffer sizes: %w", q.context.LastError())
}
return &buffer, nil
}
// SerializeQueryEstResultSizes gets and serializes the query estimated result sizes.
//
// Deprecated: Use SerializeQueryEstResultSizesToBuffer instead.
func SerializeQueryEstResultSizes(q *Query, serializationType SerializationType, clientSide bool) ([]byte, error) {
buffer, err := SerializeQueryEstResultSizesToBuffer(q, serializationType, clientSide)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeQueryEstResultSizes deserializes query estimated result sizes.
func DeserializeQueryEstResultSizes(q *Query, buffer *Buffer, serializationType SerializationType, clientSide bool) error {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
ret := C.tiledb_deserialize_query_est_result_sizes(q.context.tiledbContext, q.tiledbQuery, C.tiledb_serialization_type_t(serializationType), cClientSide, buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing query est buffer sizes: %w", q.context.LastError())
}
return nil
}
// SerializeArrayToBuffer serializes an array and returns a Buffer object containing the payload.
func SerializeArrayToBuffer(array *Array, serializationType SerializationType, clientSide bool) (*Buffer, error) {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
buffer := Buffer{context: array.context}
// Set finalizer for free C pointer on gc
freeOnGC(&buffer)
ret := C.tiledb_serialize_array(array.context.tiledbContext, array.tiledbArray, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array: %w", array.context.LastError())
}
return &buffer, nil
}
// SerializeArray serializes an array.
//
// Deprecated: Use SerializeArrayToBuffer instead.
func SerializeArray(array *Array, serializationType SerializationType, clientSide bool) ([]byte, error) {
buffer, err := SerializeArrayToBuffer(array, serializationType, clientSide)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeArray deserializes a new array from the given buffer.
func DeserializeArray(buffer *Buffer, serializationType SerializationType, clientSide bool, arrayURI string) (*Array, error) {
array := Array{context: buffer.context}
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
cArrayURI := C.CString(arrayURI)
defer C.free(unsafe.Pointer(cArrayURI))
ret := C.tiledb_deserialize_array(array.context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, cArrayURI, &array.tiledbArray)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error deserializing array: %w", array.context.LastError())
}
// Set finalizer for free C pointer on gc
// This needs to happen *after* the tiledb_deserialize_array call
// because that may leave the array with a non-nil pointer
// to already-freed memory.
freeOnGC(&array)
return &array, nil
}
// SerializeFragmentInfoToBuffer serializes fragment info and returns a Buffer object containing the payload.
func SerializeFragmentInfoToBuffer(fragmentInfo *FragmentInfo, serializationType SerializationType, clientSide bool) (*Buffer, error) {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
buffer := Buffer{context: fragmentInfo.context}
// Set finalizer for free C pointer on gc
freeOnGC(&buffer)
ret := C.tiledb_serialize_fragment_info(fragmentInfo.context.tiledbContext, fragmentInfo.tiledbFragmentInfo, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array: %w", fragmentInfo.context.LastError())
}
return &buffer, nil
}
// SerializeFragmentInfo serializes fragment info.
//
// Deprecated: Use SerializeFragmentInfoToBuffer instead.
func SerializeFragmentInfo(fragmentInfo *FragmentInfo, serializationType SerializationType, clientSide bool) ([]byte, error) {
buffer, err := SerializeFragmentInfoToBuffer(fragmentInfo, serializationType, clientSide)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeFragmentInfo deserializes an existing fragment info from the given buffer.
func DeserializeFragmentInfo(fragmentInfo FragmentInfo, buffer *Buffer, arrayURI string, serializationType SerializationType, clientSide bool) error {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
cArrayURI := C.CString(arrayURI)
defer C.free(unsafe.Pointer(cArrayURI))
ret := C.tiledb_deserialize_fragment_info(fragmentInfo.context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cArrayURI, cClientSide, fragmentInfo.tiledbFragmentInfo)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing array: %w", fragmentInfo.context.LastError())
}
return nil
}
// SerializeFragmentInfoRequestToBuffer serializes fragment info and returns a Buffer object containing the payload.
func SerializeFragmentInfoRequestToBuffer(fragmentInfo *FragmentInfo, serializationType SerializationType, clientSide bool) (*Buffer, error) {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
buffer := Buffer{context: fragmentInfo.context}
// Set finalizer for free C pointer on gc
freeOnGC(&buffer)
ret := C.tiledb_serialize_fragment_info_request(fragmentInfo.context.tiledbContext, fragmentInfo.tiledbFragmentInfo, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array: %w", fragmentInfo.context.LastError())
}
return &buffer, nil
}
// SerializeFragmentInfoRequest serializes fragment info.
//
// Deprecated: Use SerializeFragmentInfoRequestToBuffer instead.
func SerializeFragmentInfoRequest(fragmentInfo *FragmentInfo, serializationType SerializationType, clientSide bool) ([]byte, error) {
buffer, err := SerializeFragmentInfoRequestToBuffer(fragmentInfo, serializationType, clientSide)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeFragmentInfoRequest deserializes an existing fragment info from the given buffer.
func DeserializeFragmentInfoRequest(fragmentInfo FragmentInfo, buffer *Buffer, serializationType SerializationType, clientSide bool) error {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
ret := C.tiledb_deserialize_fragment_info_request(fragmentInfo.context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, fragmentInfo.tiledbFragmentInfo)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing array: %w", fragmentInfo.context.LastError())
}
return nil
}
func DeserializeQueryAndArray(context *Context, buffer *Buffer, serializationType SerializationType, clientSide bool, arrayURI string) (*Array, *Query, error) {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
cArrayURI := C.CString(arrayURI)
defer C.free(unsafe.Pointer(cArrayURI))
array := &Array{
context: context,
}
query := &Query{
context: context,
array: array,
}
ret := C.tiledb_deserialize_query_and_array(context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, cArrayURI, &query.tiledbQuery, &array.tiledbArray)
if ret != C.TILEDB_OK {
return nil, nil, fmt.Errorf("error deserializing query: %w", context.LastError())
}
freeOnGC(array)
freeOnGC(query)
query.resultBufferElements = make(map[string][3]*uint64)
// Make sure the buffer stays alive untill after the deserialization is complete
runtime.KeepAlive(buffer)
return array, query, nil
}
// SerializeGroupMetadata gets and serializes the group metadata and returns a Buffer object containing the payload
func SerializeGroupMetadataToBuffer(g *Group, serializationType SerializationType) (*Buffer, error) {
buffer := Buffer{context: g.context}
freeOnGC(&buffer)
ret := C.tiledb_serialize_group_metadata(g.context.tiledbContext, g.group, C.tiledb_serialization_type_t(serializationType), &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing group metadata: %w", g.context.LastError())
}
return &buffer, nil
}
// SerializeGroupMetadata gets and serializes the group metadata
//
// Deprecated: Use SerializeGroupMetadataToBuffer instead.
func SerializeGroupMetadata(g *Group, serializationType SerializationType) ([]byte, error) {
buffer, err := SerializeGroupMetadataToBuffer(g, serializationType)
if err != nil {
return nil, err
}
return buffer.Serialize(serializationType)
}
// DeserializeGroupMetadata deserializes group metadata
func DeserializeGroupMetadata(g *Group, buffer *Buffer, serializationType SerializationType) error {
b, err := buffer.dataCopy()
if err != nil {
return errors.New("failed to retrieve bytes from buffer")
}
// cstrings are null terminated. Go's are not, add it as a suffix
if err := buffer.SetBuffer(append(b, []byte("\u0000")...)); err != nil {
return errors.New("failed to add null terminator to buffer")
}
ret := C.tiledb_deserialize_group_metadata(g.context.tiledbContext, g.group, C.tiledb_serialization_type_t(serializationType), buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing group metadata: %w", g.context.LastError())
}
return nil
}
// Deserialize deserializes the group from the given buffer.
func (g *Group) Deserialize(buffer *Buffer, serializationType SerializationType, clientSide bool) error {
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
b, err := buffer.dataCopy()
if err != nil {
return errors.New("failed to retrieve bytes from buffer")
}
// cstrings are null terminated. Go's are not, add it as a suffix
if err := buffer.SetBuffer(append(b, []byte("\u0000")...)); err != nil {
return errors.New("failed to add null terminator to buffer")
}
ret := C.tiledb_deserialize_group(g.context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, g.group)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing group: %w", g.context.LastError())
}
return nil
}
// HandleLoadArraySchemaRequest Passes the array and serialized LoadArraySchemaRequest to core which returns the
// serialized LoadArraySchemaResponse. The request contains a TileDB Config used to load the schema, the response
// contains the latest array schema loaded and a map of all array schemas.
func HandleLoadArraySchemaRequest(array *Array, request *Buffer, serializationType SerializationType) (*Buffer, error) {
response, err := NewBuffer(array.context)
if err != nil {
return nil, fmt.Errorf("error creating LoadArraySchemaResponse buffer: %w", array.context.LastError())
}
ret := C.tiledb_handle_load_array_schema_request(array.context.tiledbContext, array.tiledbArray, C.tiledb_serialization_type_t(serializationType), request.tiledbBuffer, response.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error handling LoadArraySchemaRequset: %w", array.context.LastError())
}
runtime.KeepAlive(request)
return response, nil
}
// HandleArrayDeleteFragmentsTimestampsRequest is used by TileDB cloud to handle DeleteFragments with tiledb:// uris.
func HandleArrayDeleteFragmentsTimestampsRequest(context *Context, array *Array, buffer *Buffer, serializationType SerializationType) error {
ret := C.tiledb_handle_array_delete_fragments_timestamps_request(context.tiledbContext, array.tiledbArray,
C.tiledb_serialization_type_t(serializationType), buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing delete fragments timestamps: %w", context.LastError())
}
runtime.KeepAlive(buffer)
return nil
}
// HandleArrayDeleteFragmentsListRequest is used by TileDB cloud to handle DeleteFragmentsList with tiledb:// uris.
func HandleArrayDeleteFragmentsListRequest(context *Context, array *Array, buffer *Buffer, serializationType SerializationType) error {
ret := C.tiledb_handle_array_delete_fragments_list_request(context.tiledbContext, array.tiledbArray,
C.tiledb_serialization_type_t(serializationType), buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing delete fragments list: %w", context.LastError())
}
runtime.KeepAlive(buffer)
return nil
}
// HandleQueryPlanRequest handles a request for a query plan. This is used by TileDB-Cloud
// It returns a buffer with the serialized response. The caller should free the buffer after use.
func HandleQueryPlanRequest(array *Array, serializationType SerializationType, request *Buffer) (*Buffer, error) {
opContext := array.context
response, err := NewBuffer(opContext)
if err != nil {
return nil, fmt.Errorf("error allocating tiledb buffer: %w", opContext.LastError())
}
ret := C.tiledb_handle_query_plan_request(opContext.tiledbContext, array.tiledbArray, C.tiledb_serialization_type_t(serializationType),
request.tiledbBuffer, response.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error handling query plan request: %w", opContext.LastError())
}
runtime.KeepAlive(request)
runtime.KeepAlive(array)
return response, nil
}
// HandleConsolidationPlanRequest handles a request for a consolidation plan. This is used by TileDB-Cloud
// It returns a buffer with the serialized response. The caller should free the buffer after use.
func HandleConsolidationPlanRequest(array *Array, serializationType SerializationType, request *Buffer) (*Buffer, error) {
opContext := array.context
response, err := NewBuffer(opContext)
if err != nil {
return nil, fmt.Errorf("error allocating tiledb buffer: %w", opContext.LastError())
}
ret := C.tiledb_handle_consolidation_plan_request(opContext.tiledbContext, array.tiledbArray, C.tiledb_serialization_type_t(serializationType),
request.tiledbBuffer, response.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error handling consolidation plan request: %w", opContext.LastError())
}
runtime.KeepAlive(request)
runtime.KeepAlive(array)
return response, nil
}
// DeserializeLoadEnumerationsRequest deserializes a LoadEnumerationsRequests. This is used by TileDB-Cloud.
func DeserializeLoadEnumerationsRequest(array *Array, serializationType SerializationType, request *Buffer) (*Buffer, error) {
response, err := NewBuffer(array.context)
if err != nil {
return nil, fmt.Errorf("error deserializing load enumerations request: %w", array.context.LastError())
}
ret := C.tiledb_handle_load_enumerations_request(array.context.tiledbContext, array.tiledbArray, C.tiledb_serialization_type_t(serializationType),
request.tiledbBuffer, response.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error deserializing load enumerations request: %w", array.context.LastError())
}
runtime.KeepAlive(request)
runtime.KeepAlive(array)
return response, nil
}