-
Notifications
You must be signed in to change notification settings - Fork 7
/
array.go
1182 lines (1008 loc) · 34.5 KB
/
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
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tiledb
/*
#include <tiledb/tiledb.h>
#include <stdlib.h>
*/
import "C"
import (
"encoding/json"
"fmt"
"runtime"
"time"
"unsafe"
)
/*
Array struct representing a TileDB array object.
An Array object represents array data in TileDB at some persisted location,
e.g. on disk, in an S3 bucket, etc. Once an array has been opened for reading
or writing, interact with the data through Query objects.
*/
type Array struct {
tiledbArray *C.tiledb_array_t
context *Context
uri string
config *Config
}
// ArrayMetadata defines metadata for the array
type ArrayMetadata struct {
Key string
KeyLen uint32
Datatype Datatype
ValueNum uint
Value interface{}
}
// MarshalJSON implements the Marshaler interface for ArrayMetadata.
func (a ArrayMetadata) MarshalJSON() ([]byte, error) {
switch v := a.Value.(type) {
case []byte:
return json.Marshal(string(v))
default:
return json.Marshal(v)
}
}
// NonEmptyDomain contains the non empty dimension bounds and dimension name
type NonEmptyDomain struct {
DimensionName string
Bounds interface{}
}
// NewArray allocates a new array.
// If the provided Context is nil, a default context is allocated and used.
func NewArray(tdbCtx *Context, uri string) (*Array, error) {
if tdbCtx == nil {
newCtx, err := NewContext(nil)
if err != nil {
return nil, err
}
tdbCtx = newCtx
}
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
array := Array{context: tdbCtx, uri: uri}
ret := C.tiledb_array_alloc(array.context.tiledbContext, curi, &array.tiledbArray)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Error creating tiledb array: %s", array.context.LastError())
}
freeOnGC(&array)
return &array, nil
}
// Free releases the internal TileDB core data that was allocated on the C heap.
// It is automatically called when this object is garbage collected, but can be
// called earlier to manually release memory if needed. Free is idempotent and
// can safely be called many times on the same object; if it has already
// been freed, it will not be freed again.
func (a *Array) Free() {
if a.tiledbArray != nil {
a.Close()
C.tiledb_array_free(&a.tiledbArray)
}
}
// Context exposes the internal TileDB context used to initialize the array.
func (a *Array) Context() *Context {
return a.context
}
// ArrayOpenOptions defines the flexible parameters in which arrays can be opened with.
type ArrayOpenOption func(tdbArray *Array) error
// WithEndTime sets the subsequent Open call to use the given time
// as its end timestamp. If "end" is the zero value, does nothing.
func WithEndTime(end time.Time) ArrayOpenOption {
if end.IsZero() {
return func(*Array) error { return nil }
}
return WithEndTimestamp(uint64(end.UnixMilli()))
}
// WithStartTime sets the subsequent Open call to use the given time
// as its start timestamp. If "start" is the zero value, does nothing.
func WithStartTime(start time.Time) ArrayOpenOption {
if start.IsZero() {
return func(*Array) error { return nil }
}
return WithStartTimestamp(uint64(start.UnixMilli()))
}
// WithEndTimestamp sets the subsequent Open call to use the end_timestamp of the passed value.
func WithEndTimestamp(endTimestamp uint64) ArrayOpenOption {
return func(tdbArray *Array) error {
ret := C.tiledb_array_set_open_timestamp_end(tdbArray.context.tiledbContext, tdbArray.tiledbArray, C.uint64_t(endTimestamp))
if ret != C.TILEDB_OK {
return fmt.Errorf("Error setting end timestamp option: %s", tdbArray.context.LastError())
}
return nil
}
}
// WithStartTimestamp sets the subsequent Open call to use the start_timestamp of the passed value.
func WithStartTimestamp(startTimestamp uint64) ArrayOpenOption {
return func(tdbArray *Array) error {
ret := C.tiledb_array_set_open_timestamp_start(tdbArray.context.tiledbContext, tdbArray.tiledbArray, C.uint64_t(startTimestamp))
if ret != C.TILEDB_OK {
return fmt.Errorf("Error setting start timestamp option: %s", tdbArray.context.LastError())
}
return nil
}
}
/*
OpenWithOptions opens the array with options. The array is opened using a query type as input.
This is to indicate that queries created for this Array object will inherit
the query type. In other words, Array objects are opened to receive only one
type of query. They can always be closed and be re-opened with another query
type. Also there may be many different Array objects created and opened with
different query types. For instance, one may create and open an array object
array_read for reads and another one array_write for writes, and interleave
creation and submission of queries for both these array objects.
*/
func (a *Array) OpenWithOptions(queryType QueryType, opts ...ArrayOpenOption) error {
for _, opt := range opts {
if err := opt(a); err != nil {
return err
}
}
ret := C.tiledb_array_open(a.context.tiledbContext, a.tiledbArray, C.tiledb_query_type_t(queryType))
if ret != C.TILEDB_OK {
return fmt.Errorf("Error opening tiledb array for querying: %s", a.context.LastError())
}
return nil
}
/*
Open the array. The array is opened using a query type as input.
This is to indicate that queries created for this Array object will inherit
the query type. In other words, Array objects are opened to receive only one
type of queries. They can always be closed and be re-opened with another query
type. Also there may be many different Array objects created and opened with
different query types. For instance, one may create and open an array object
array_read for reads and another one array_write for writes, and interleave
creation and submission of queries for both these array objects.
*/
func (a *Array) Open(queryType QueryType) error {
ret := C.tiledb_array_open(a.context.tiledbContext, a.tiledbArray, C.tiledb_query_type_t(queryType))
if ret != C.TILEDB_OK {
return fmt.Errorf("Error opening tiledb array for querying: %s", a.context.LastError())
}
return nil
}
/*
Reopen the array (the array must be already open). This is useful when the
array got updated after it got opened and the Array object got created.
To sync-up with the updates, the user must either close the array and open
with open(), or just use reopen() without closing. This function will be
generally faster than the former alternative.
*/
func (a *Array) Reopen() error {
ret := C.tiledb_array_reopen(a.context.tiledbContext, a.tiledbArray)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error reopening tiledb array for querying: %s", a.context.LastError())
}
return nil
}
// Close closes a tiledb array. This is automatically called on garbage collection.
func (a *Array) Close() error {
ret := C.tiledb_array_close(a.context.tiledbContext, a.tiledbArray)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error closing tiledb array for querying: %s", a.context.LastError())
}
return nil
}
// Create creates a new TileDB array given an input schema.
func (a *Array) Create(arraySchema *ArraySchema) error {
curi := C.CString(a.uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_array_create(a.context.tiledbContext, curi, arraySchema.tiledbArraySchema)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error creating tiledb array: %s", a.context.LastError())
}
return nil
}
// Consolidate consolidates the fragments of an array into a single fragment.
// You must first finalize all queries to the array before consolidation can
// begin (as consolidation temporarily acquires an exclusive lock on the array).
func (a *Array) Consolidate(config *Config) error {
if config == nil {
return fmt.Errorf("Config must not be nil for Consolidate")
}
curi := C.CString(a.uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_array_consolidate(a.context.tiledbContext, curi, config.tiledbConfig)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error consolidating tiledb array: %s", a.context.LastError())
}
runtime.KeepAlive(config)
return nil
}
// Vacuum cleans up the array, such as consolidated fragments and array metadata.
func (a *Array) Vacuum(config *Config) error {
if config == nil {
return fmt.Errorf("Config must not be nil for Vacuum")
}
curi := C.CString(a.uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_array_vacuum(a.context.tiledbContext, curi, config.tiledbConfig)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error vacuumimg tiledb array: %s", a.context.LastError())
}
runtime.KeepAlive(config)
return nil
}
// Schema returns the ArraySchema for the array.
func (a *Array) Schema() (*ArraySchema, error) {
arraySchema := ArraySchema{context: a.context}
ret := C.tiledb_array_get_schema(a.context.tiledbContext, a.tiledbArray, &arraySchema.tiledbArraySchema)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Error getting schema for tiledb array: %s", a.context.LastError())
}
freeOnGC(&arraySchema)
return &arraySchema, nil
}
// QueryType returns the current query type of an open array.
func (a *Array) QueryType() (QueryType, error) {
var queryType C.tiledb_query_type_t
ret := C.tiledb_array_get_query_type(a.context.tiledbContext, a.tiledbArray, &queryType)
if ret != C.TILEDB_OK {
return -1, fmt.Errorf("Error getting QueryType for tiledb array: %s", a.context.LastError())
}
return QueryType(queryType), nil
}
// OpenStartTime returns the current start_timestamp of an open array,
// converted to a UTC time.Time.
func (a *Array) OpenStartTime() (time.Time, error) {
ts, err := a.OpenStartTimestamp()
if err != nil {
return time.Time{}, err
}
return millisToTime(ts), nil
}
// OpenEndTime returns the current end_timestamp of an open array,
// converted to a UTC time.Time.
func (a *Array) OpenEndTime() (time.Time, error) {
ts, err := a.OpenEndTimestamp()
if err != nil {
return time.Time{}, err
}
return millisToTime(ts), nil
}
func millisToTime(epochMillis uint64) time.Time {
secs, millis := int64(epochMillis/1000), int64(epochMillis%1000)
return time.Unix(secs, millis*1_000_000).UTC()
}
// OpenStartTimestamp returns the current start_timestamp value of an open array.
func (a *Array) OpenStartTimestamp() (uint64, error) {
var start C.uint64_t
ret := C.tiledb_array_get_open_timestamp_start(a.context.tiledbContext, a.tiledbArray, &start)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("Error getting start timestamp for tiledb array: %s", a.context.LastError())
}
return uint64(start), nil
}
// OpenEndTimestamp returns the current end_timestamp value of an open array.
func (a *Array) OpenEndTimestamp() (uint64, error) {
var end C.uint64_t
ret := C.tiledb_array_get_open_timestamp_end(a.context.tiledbContext, a.tiledbArray, &end)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("Error getting end timestamp for tiledb array: %s", a.context.LastError())
}
return uint64(end), nil
}
// getNonEmptyDomainForDim creates a NonEmptyDomain from a generic dimension-typed slice.
func getNonEmptyDomainForDim(dimension *Dimension, bounds interface{}) (*NonEmptyDomain, error) {
dimensionType, err := dimension.Type()
if err != nil {
return nil, err
}
name, err := dimension.Name()
if err != nil {
return nil, err
}
switch ds := bounds.(type) {
case []int8:
return makeNonEmptyDomain(name, ds)
case []int16:
return makeNonEmptyDomain(name, ds)
case []int32:
return makeNonEmptyDomain(name, ds)
case []int64:
return makeNonEmptyDomain(name, ds)
case []uint8:
return makeNonEmptyDomain(name, ds)
case []uint16:
return makeNonEmptyDomain(name, ds)
case []uint32:
return makeNonEmptyDomain(name, ds)
case []uint64:
return makeNonEmptyDomain(name, ds)
case []float32:
return makeNonEmptyDomain(name, ds)
case []float64:
return makeNonEmptyDomain(name, ds)
case []bool:
return makeNonEmptyDomain(name, ds)
case []any:
if dimensionType != TILEDB_STRING_ASCII {
return nil, fmt.Errorf(
"type mismatch between non-empty domain type (%T) and dimension type (%v); expected %v",
ds[0], dimensionType, TILEDB_STRING_ASCII,
)
}
lo, hi := ds[0].([]byte), ds[1].([]byte)
return &NonEmptyDomain{DimensionName: name, Bounds: []string{string(lo), string(hi)}}, nil
}
return nil, fmt.Errorf(
"error creating nonempty domain: unknown data type (slice %T; type %v)",
bounds, dimensionType,
)
}
func makeNonEmptyDomain[T any](name string, bounds []T) (*NonEmptyDomain, error) {
return &NonEmptyDomain{DimensionName: name, Bounds: []T{bounds[0], bounds[1]}}, nil
}
// NonEmptyDomain retrieves the non-empty domain from an array.
// This returns the bounding coordinates for each dimension.
func (a *Array) NonEmptyDomain() ([]NonEmptyDomain, bool, error) {
schema, err := a.Schema()
if err != nil {
return nil, false, err
}
defer schema.Free()
domain, err := schema.Domain()
if err != nil {
return nil, false, err
}
defer domain.Free()
ndims, err := domain.NDim()
if err != nil {
return nil, false, err
}
isDomainEmpty := true
nonEmptyDomains := make([]NonEmptyDomain, 0)
for dimIdx := uint(0); dimIdx < ndims; dimIdx++ {
// Wrapped in a function so `dimension` will be cleaned up with defer each time the function completes.
err := func() error {
dimension, err := domain.DimensionFromIndex(dimIdx)
if err != nil {
return err
}
defer dimension.Free()
dimensionType, err := dimension.Type()
if err != nil {
return err
}
tmpDimension, tmpDimensionPtr, err := dimensionType.MakeSlice(uint64(2))
if err != nil {
return err
}
var isEmpty C.int32_t
ret := C.tiledb_array_get_non_empty_domain_from_index(
a.context.tiledbContext,
a.tiledbArray,
(C.uint32_t)(dimIdx),
tmpDimensionPtr, &isEmpty)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in getting non empty domain for dimension: %s", a.context.LastError())
}
if isEmpty == 1 {
return nil
}
// If at least one domain for a dimension is empty the union of domains is non-empty
isDomainEmpty = false
nonEmptyDomain, err := getNonEmptyDomainForDim(dimension, tmpDimension)
if err != nil {
return err
}
nonEmptyDomains = append(nonEmptyDomains, *nonEmptyDomain)
return nil
}()
if err != nil {
return nil, false, err
}
}
if isDomainEmpty {
return nil, isDomainEmpty, nil
}
return nonEmptyDomains, isDomainEmpty, nil
}
// NonEmptyDomainMap returns a map[string]interface{} where key is the
// dimension name and value is the non empty domain for the given dimension or
// the empty interface. It covers both var-sized and non-var-sized dimensions.
func (a *Array) NonEmptyDomainMap() (map[string]interface{}, error) {
schema, err := a.Schema()
if err != nil {
return nil, err
}
domain, err := schema.Domain()
if err != nil {
return nil, err
}
ndims, err := domain.NDim()
if err != nil {
return nil, err
}
nonEmptyDomainMap := make(map[string]interface{})
for dimIdx := uint(0); dimIdx < ndims; dimIdx++ {
dimension, err := domain.DimensionFromIndex(dimIdx)
if err != nil {
return nil, err
}
dimensionName, err := dimension.Name()
if err != nil {
return nil, err
}
dimensionType, err := dimension.Type()
if err != nil {
return nil, err
}
cellValNum, err := dimension.CellValNum()
if err != nil {
return nil, err
}
if cellValNum == TILEDB_VAR_NUM {
nonEmptyDomain, isEmpty, err := a.NonEmptyDomainVarFromName(dimensionName)
if err != nil {
return nil, err
}
if isEmpty {
var empty interface{}
nonEmptyDomainMap[dimensionName] = empty
} else {
nonEmptyDomainMap[nonEmptyDomain.DimensionName] = nonEmptyDomain.Bounds
}
} else {
tmpDimension, tmpDimensionPtr, err := dimensionType.MakeSlice(uint64(2))
if err != nil {
return nil, err
}
var isEmpty C.int32_t
ret := C.tiledb_array_get_non_empty_domain_from_index(
a.context.tiledbContext,
a.tiledbArray,
(C.uint32_t)(dimIdx),
tmpDimensionPtr, &isEmpty)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error in getting non empty domain for dimension: %s", a.context.LastError())
}
if isEmpty == 1 {
var empty interface{}
nonEmptyDomainMap[dimensionName] = empty
} else {
// If at least one domain for a dimension is empty the union of domains is non-empty
nonEmptyDomain, err := getNonEmptyDomainForDim(dimension, tmpDimension)
if err != nil {
return nil, err
}
nonEmptyDomainMap[nonEmptyDomain.DimensionName] = nonEmptyDomain.Bounds
}
}
}
return nonEmptyDomainMap, nil
}
// NonEmptyDomainVarFromName retrieves the non-empty domain from an array for a
// given var-sized dimension name. Supports only TILEDB_STRING_ASCII type
// Returns the bounding coordinates for the dimension.
func (a *Array) NonEmptyDomainVarFromName(dimName string) (*NonEmptyDomain, bool, error) {
schema, err := a.Schema()
if err != nil {
return nil, false, err
}
defer schema.Free()
domain, err := schema.Domain()
if err != nil {
return nil, false, err
}
defer domain.Free()
hasDim, err := domain.HasDimension(dimName)
if err != nil {
return nil, false, err
}
if !hasDim {
return nil, false, fmt.Errorf("Dimension: %s was not found in domain", dimName)
}
dimension, err := domain.DimensionFromName(dimName)
if err != nil {
return nil, false, fmt.Errorf("could not get dimension: %s", dimName)
}
defer dimension.Free()
dimType, err := dimension.Type()
if err != nil {
return nil, false, err
}
cDimName := C.CString(dimName)
defer C.free(unsafe.Pointer(cDimName))
var cstartSize C.uint64_t
var cendSize C.uint64_t
var isEmpty C.int32_t
var start interface{}
var end interface{}
var cstart unsafe.Pointer
var cend unsafe.Pointer
ret := C.tiledb_array_get_non_empty_domain_var_size_from_name(
a.context.tiledbContext,
a.tiledbArray,
cDimName,
&cstartSize,
&cendSize,
&isEmpty)
if ret != C.TILEDB_OK {
return nil, false, fmt.Errorf("error in getting non empty domain size for dimension %s for array: %s", dimName, a.context.LastError())
}
if isEmpty == 1 {
return nil, true, nil
}
bounds := make([]interface{}, 0)
start, cstart, err = dimType.MakeSlice(uint64(cstartSize))
if err != nil {
return nil, false, err
}
bounds = append(bounds, start)
end, cend, err = dimType.MakeSlice(uint64(cendSize))
if err != nil {
return nil, false, err
}
bounds = append(bounds, end)
ret = C.tiledb_array_get_non_empty_domain_var_from_name(
a.context.tiledbContext,
a.tiledbArray,
cDimName,
cstart,
cend,
&isEmpty)
if ret != C.TILEDB_OK {
return nil, false, fmt.Errorf("error in getting non empty domain for dimension %s for array: %s", dimName, a.context.LastError())
}
if isEmpty == 1 {
return nil, true, nil
}
nonEmptyDomain, err := getNonEmptyDomainForDim(dimension, bounds)
if err != nil {
return nil, false, err
}
return nonEmptyDomain, false, nil
}
// NonEmptyDomainVarFromIndex retrieves the non-empty domain from an array for a
// given var-sized dimension index. Supports only TILEDB_STRING_ASCII type
// Returns the bounding coordinates for the dimension.
func (a *Array) NonEmptyDomainVarFromIndex(dimIdx uint) (*NonEmptyDomain, bool, error) {
schema, err := a.Schema()
if err != nil {
return nil, false, err
}
defer schema.Free()
domain, err := schema.Domain()
if err != nil {
return nil, false, err
}
defer domain.Free()
dimension, err := domain.DimensionFromIndex(dimIdx)
if err != nil {
return nil, false, fmt.Errorf("Could not get dimension having index: %d", dimIdx)
}
defer dimension.Free()
dimType, err := dimension.Type()
if err != nil {
return nil, false, err
}
var cstartSize C.uint64_t
var cendSize C.uint64_t
var isEmpty C.int32_t
var start interface{}
var end interface{}
var cstart unsafe.Pointer
var cend unsafe.Pointer
ret := C.tiledb_array_get_non_empty_domain_var_size_from_index(
a.context.tiledbContext,
a.tiledbArray,
(C.uint32_t)(dimIdx),
&cstartSize,
&cendSize,
&isEmpty)
if ret != C.TILEDB_OK {
return nil, false, fmt.Errorf("Error in getting non empty domain size for dimension %d for array: %s", dimIdx, a.context.LastError())
}
if isEmpty == 1 {
return nil, true, nil
}
bounds := make([]interface{}, 0)
start, cstart, err = dimType.MakeSlice(uint64(cstartSize))
if err != nil {
return nil, false, err
}
bounds = append(bounds, start)
end, cend, err = dimType.MakeSlice(uint64(cendSize))
if err != nil {
return nil, false, err
}
bounds = append(bounds, end)
ret = C.tiledb_array_get_non_empty_domain_var_from_index(
a.context.tiledbContext,
a.tiledbArray,
(C.uint32_t)(dimIdx),
cstart,
cend,
&isEmpty)
if ret != C.TILEDB_OK {
return nil, false, fmt.Errorf("Error in getting non empty domain for dimension index %d for array: %s", dimIdx, a.context.LastError())
}
if isEmpty == 1 {
return nil, true, nil
}
nonEmptyDomain, err := getNonEmptyDomainForDim(dimension, bounds)
if err != nil {
return nil, false, err
}
return nonEmptyDomain, false, nil
}
func (a Array) GetNonEmptyDomainSliceFromIndex(dimIdx uint) (*Dimension, interface{}, unsafe.Pointer, error) {
schema, err := a.Schema()
if err != nil {
return nil, nil, nil, err
}
domain, err := schema.Domain()
if err != nil {
return nil, nil, nil, err
}
dimension, err := domain.DimensionFromIndex(dimIdx)
if err != nil {
return nil, nil, nil, fmt.Errorf("Could not get dimension: %d", dimIdx)
}
dimensionType, err := dimension.Type()
if err != nil {
return nil, nil, nil, err
}
tmpDimension, tmpDimensionPtr, err := dimensionType.MakeSlice(uint64(2))
if err != nil {
return nil, nil, nil, err
}
return dimension, tmpDimension, tmpDimensionPtr, nil
}
func (a Array) GetNonEmptyDomainSliceFromName(dimName string) (*Dimension, interface{}, unsafe.Pointer, error) {
schema, err := a.Schema()
if err != nil {
return nil, nil, nil, err
}
domain, err := schema.Domain()
if err != nil {
return nil, nil, nil, err
}
hasDim, err := domain.HasDimension(dimName)
if err != nil {
return nil, nil, nil, err
}
if !hasDim {
return nil, nil, nil, fmt.Errorf("Dimension: %s was not found in domain", dimName)
}
dimension, err := domain.DimensionFromName(dimName)
if err != nil {
return nil, nil, nil, fmt.Errorf("Could not get dimension: %s", dimName)
}
dimensionType, err := dimension.Type()
if err != nil {
return nil, nil, nil, err
}
tmpDimension, tmpDimensionPtr, err := dimensionType.MakeSlice(uint64(2))
if err != nil {
return nil, nil, nil, err
}
return dimension, tmpDimension, tmpDimensionPtr, nil
}
// NonEmptyDomainFromIndex retrieves the non-empty domain from an array for a
// given fixed-sized dimension index.
// Returns the bounding coordinates for the dimension.
func (a *Array) NonEmptyDomainFromIndex(dimIdx uint) (*NonEmptyDomain, bool, error) {
dimension, tmpDimension, tmpDimensionPtr, err := a.GetNonEmptyDomainSliceFromIndex(dimIdx)
if err != nil {
return nil, false, err
}
var isEmpty C.int32_t
ret := C.tiledb_array_get_non_empty_domain_from_index(
a.context.tiledbContext,
a.tiledbArray,
(C.uint32_t)(dimIdx),
tmpDimensionPtr, &isEmpty)
if ret != C.TILEDB_OK {
return nil, false, fmt.Errorf("Error in getting non empty domain for dimension: %s", a.context.LastError())
}
if isEmpty == 1 {
return nil, true, nil
}
// If at least one domain for a dimension is empty the union of domains is non-empty
nonEmptyDomain, err := getNonEmptyDomainForDim(dimension, tmpDimension)
if err != nil {
return nil, false, err
}
return nonEmptyDomain, false, nil
}
// NonEmptyDomainFromName retrieves the non-empty domain from an array for a
// given fixed-sized dimension name.
// Returns the bounding coordinates for the dimension.
func (a *Array) NonEmptyDomainFromName(dimName string) (*NonEmptyDomain, bool, error) {
dimension, tmpDimension, tmpDimensionPtr, err := a.GetNonEmptyDomainSliceFromName(dimName)
if err != nil {
return nil, false, err
}
cDimName := C.CString(dimName)
defer C.free(unsafe.Pointer(cDimName))
var isEmpty C.int32_t
ret := C.tiledb_array_get_non_empty_domain_from_name(
a.context.tiledbContext,
a.tiledbArray,
cDimName,
tmpDimensionPtr, &isEmpty)
if ret != C.TILEDB_OK {
return nil, false, fmt.Errorf("Error in getting non empty domain for dimension: %s", a.context.LastError())
}
if isEmpty == 1 {
return nil, true, nil
}
// If at least one domain for a dimension is empty the union of domains is non-empty
nonEmptyDomain, err := getNonEmptyDomainForDim(dimension, tmpDimension)
if err != nil {
return nil, false, err
}
return nonEmptyDomain, false, nil
}
// URI returns the array's uri.
func (a *Array) URI() (string, error) {
var curi *C.char
C.tiledb_array_get_uri(a.context.tiledbContext, a.tiledbArray, &curi)
uri := C.GoString(curi)
if uri == "" {
return uri, fmt.Errorf("Error getting URI for array: uri is empty")
}
return uri, nil
}
// PutCharMetadata adds char metadata to the array.
func (a *Array) PutCharMetadata(key string, charData string) error {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
var datatype Datatype = TILEDB_CHAR
valueNum := C.uint(len(charData))
ret := C.tiledb_array_put_metadata(a.context.tiledbContext, a.tiledbArray,
ckey, C.tiledb_datatype_t(datatype), valueNum, unsafe.Pointer(&[]byte(charData)[0]))
if ret != C.TILEDB_OK {
return fmt.Errorf("Error adding char metadata to array: %s", a.context.LastError())
}
return nil
}
// PutMetadata puts a metadata key-value item to an open array. The array must
// be opened in WRITE mode, otherwise the function will error out.
func (a *Array) PutMetadata(key string, value interface{}) error {
switch value := value.(type) {
case int:
return arrayPutScalarMetadata(a, tileDBInt, key, value)
case []int:
return arrayPutSliceMetadata(a, tileDBInt, key, value)
case int8:
return arrayPutScalarMetadata(a, TILEDB_INT8, key, value)
case []int8:
return arrayPutSliceMetadata(a, TILEDB_INT8, key, value)
case int16:
return arrayPutScalarMetadata(a, TILEDB_INT16, key, value)
case []int16:
return arrayPutSliceMetadata(a, TILEDB_INT16, key, value)
case int32:
return arrayPutScalarMetadata(a, TILEDB_INT32, key, value)
case []int32:
return arrayPutSliceMetadata(a, TILEDB_INT32, key, value)
case uint:
return arrayPutScalarMetadata(a, tileDBUint, key, value)
case []uint:
return arrayPutSliceMetadata(a, tileDBUint, key, value)
case int64:
return arrayPutScalarMetadata(a, TILEDB_INT64, key, value)
case []int64:
return arrayPutSliceMetadata(a, TILEDB_INT64, key, value)
case uint8:
return arrayPutScalarMetadata(a, TILEDB_UINT8, key, value)
case []uint8:
return arrayPutSliceMetadata(a, TILEDB_UINT8, key, value)
case uint16:
return arrayPutScalarMetadata(a, TILEDB_UINT16, key, value)
case []uint16:
return arrayPutSliceMetadata(a, TILEDB_UINT16, key, value)
case uint32:
return arrayPutScalarMetadata(a, TILEDB_UINT32, key, value)
case []uint32:
return arrayPutSliceMetadata(a, TILEDB_UINT32, key, value)
case uint64:
return arrayPutScalarMetadata(a, TILEDB_UINT64, key, value)
case []uint64:
return arrayPutSliceMetadata(a, TILEDB_UINT64, key, value)
case float32:
return arrayPutScalarMetadata(a, TILEDB_FLOAT32, key, value)
case []float32:
return arrayPutSliceMetadata(a, TILEDB_FLOAT32, key, value)
case float64:
return arrayPutScalarMetadata(a, TILEDB_FLOAT64, key, value)
case []float64:
return arrayPutSliceMetadata(a, TILEDB_FLOAT64, key, value)
case bool:
return arrayPutScalarMetadata(a, TILEDB_BOOL, key, value)
case []bool:
return arrayPutSliceMetadata(a, TILEDB_BOOL, key, value)
case string:
valPtr := unsafe.Pointer(C.CString(value))
defer C.free(valPtr)
return arrayPutMetadata(a, TILEDB_STRING_UTF8, key, valPtr, len(value))
}
return fmt.Errorf("can't write %q metadata: unrecognized value type %T", key, value)
}
func arrayPutSliceMetadata[T scalarType](a *Array, dt Datatype, key string, value []T) error {
if len(value) == 0 {
return fmt.Errorf("length of %q metadata %T value must be nonzero", key, value)
}
return arrayPutMetadata(a, dt, key, slicePtr(value), len(value))
}
func arrayPutScalarMetadata[T scalarType](a *Array, dt Datatype, key string, value T) error {
return arrayPutMetadata(a, dt, key, unsafe.Pointer(&value), 1)
}
func arrayPutMetadata(a *Array, dt Datatype, key string, valuePtr unsafe.Pointer, count int) error {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
ret := C.tiledb_array_put_metadata(
a.context.tiledbContext,
a.tiledbArray,
cKey,
C.tiledb_datatype_t(dt),
C.uint(count),
valuePtr,
)
if ret != C.TILEDB_OK {
return fmt.Errorf("could not add metadata to array: %w", a.context.LastError())
}
return nil
}
// DeleteMetadata deletes a metadata key-value item from an open array. The array must
// be opened in WRITE mode, otherwise the function will error out.
func (a *Array) DeleteMetadata(key string) error {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
ret := C.tiledb_array_delete_metadata(a.context.tiledbContext, a.tiledbArray, ckey)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error deleting metadata from array: %s", a.context.LastError())
}
return nil
}
// GetMetadata gets a metadata key-value item from an open array. The array must
// be opened in READ mode, otherwise the function will error out.
func (a *Array) GetMetadata(key string) (Datatype, uint, interface{}, error) {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))