-
Notifications
You must be signed in to change notification settings - Fork 9
/
subarray.go
433 lines (378 loc) · 13.3 KB
/
subarray.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
package tiledb
/*
#include <tiledb/tiledb.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"reflect"
"runtime"
"unsafe"
)
// Subarray is a container of dimension ranges for a tiledb Query.
type Subarray struct {
array *Array
subarray *C.tiledb_subarray_t
context *Context
}
// NewSubarray creates a new subarray for array. It has internal coalesce_ranges == true.
func (a *Array) NewSubarray() (*Subarray, error) {
var sa *C.tiledb_subarray_t
ret := C.tiledb_subarray_alloc(a.context.tiledbContext, a.tiledbArray, &sa)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error creating Subarray: %w", a.context.LastError())
}
subarray := &Subarray{array: a, subarray: sa, context: a.context}
freeOnGC(subarray)
return subarray, nil
}
// SetConfig sets the subarray config. Currently it overrides only sm.read_range_oob.
func (sa *Subarray) SetConfig(cfg *Config) error {
ret := C.tiledb_subarray_set_config(sa.context.tiledbContext, sa.subarray, cfg.tiledbConfig)
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting Config: %w", sa.context.LastError())
}
return 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.
func (sa *Subarray) Free() {
if sa.subarray != nil {
C.tiledb_subarray_free(&sa.subarray)
}
}
// SetSubArray sets a subarray, defined in the order dimensions were added.
// Coordinates are inclusive. For the case of writes, this is meaningful only
// for dense arrays, and specifically dense writes.
func (sa *Subarray) SetSubArray(subArray interface{}) error {
if reflect.TypeOf(subArray).Kind() != reflect.Slice {
return fmt.Errorf("subarray passed must be a slice, type passed was: %s", reflect.TypeOf(subArray).Kind().String())
}
subArrayType := reflect.TypeOf(subArray).Elem().Kind()
schema, err := sa.array.Schema()
if err != nil {
return fmt.Errorf("could not get array schema from array: %w", err)
}
defer schema.Free()
domain, err := schema.Domain()
if err != nil {
return fmt.Errorf("could not get domain from array schema: %w", err)
}
defer domain.Free()
domainType, err := domain.Type()
if err != nil {
return fmt.Errorf("could not get domain type: %w", err)
}
if subArrayType != domainType.ReflectKind() {
return fmt.Errorf("domain and subarray do not have the same data types. Domain: %s, Extent: %s", domainType.ReflectKind().String(), subArrayType.String())
}
var csubArray unsafe.Pointer
switch subArrayType {
case reflect.Int:
// Create subArray void*
tmpSubArray := subArray.([]int)
csubArray = slicePtr(tmpSubArray)
case reflect.Int8:
// Create subArray void*
tmpSubArray := subArray.([]int8)
csubArray = slicePtr(tmpSubArray)
case reflect.Int16:
// Create subArray void*
tmpSubArray := subArray.([]int16)
csubArray = slicePtr(tmpSubArray)
case reflect.Int32:
// Create subArray void*
tmpSubArray := subArray.([]int32)
csubArray = slicePtr(tmpSubArray)
case reflect.Int64:
// Create subArray void*
tmpSubArray := subArray.([]int64)
csubArray = slicePtr(tmpSubArray)
case reflect.Uint:
// Create subArray void*
tmpSubArray := subArray.([]uint)
csubArray = slicePtr(tmpSubArray)
case reflect.Uint8:
// Create subArray void*
tmpSubArray := subArray.([]uint8)
csubArray = slicePtr(tmpSubArray)
case reflect.Uint16:
// Create subArray void*
tmpSubArray := subArray.([]uint16)
csubArray = slicePtr(tmpSubArray)
case reflect.Uint32:
// Create subArray void*
tmpSubArray := subArray.([]uint32)
csubArray = slicePtr(tmpSubArray)
case reflect.Uint64:
// Create subArray void*
tmpSubArray := subArray.([]uint64)
csubArray = slicePtr(tmpSubArray)
case reflect.Float32:
// Create subArray void*
tmpSubArray := subArray.([]float32)
csubArray = slicePtr(tmpSubArray)
case reflect.Float64:
// Create subArray void*
tmpSubArray := subArray.([]float64)
csubArray = slicePtr(tmpSubArray)
case reflect.Bool:
// Create subArray void*
tmpSubArray := subArray.([]bool)
csubArray = slicePtr(tmpSubArray)
default:
return fmt.Errorf("unrecognized subArray type passed: %s", subArrayType.String())
}
ret := C.tiledb_subarray_set_subarray(sa.context.tiledbContext, sa.subarray, csubArray)
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting subarray: %w", sa.context.LastError())
}
return nil
}
// SetCoalesceRanges sets coalesce_ranges property on a TileDB subarray object.
// Intended to be used just after array.NewSubarray to replace the initial coalesce_ranges == true with coalesce_ranges = false if needed.
func (sa *Subarray) SetCoalesceRanges(b bool) error {
var coalesce C.int
if b {
coalesce = 1
}
ret := C.tiledb_subarray_set_coalesce_ranges(sa.context.tiledbContext, sa.subarray, coalesce)
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting coalesce ranges on subarray: %w", sa.context.LastError())
}
return nil
}
// AddRange adds a range along a subarray dimension. It checks the types of range and dimension and
// if the datatype of the range is not the same as the type of the dimension it returns an error.
func (sa *Subarray) AddRange(dimIdx uint32, r Range) error {
dt, isVar, err := datatypeOfDimensionFromIndex(sa.array, dimIdx)
if err != nil {
return err
}
if err := r.assertCompatibility(dt, isVar); err != nil {
return err
}
var ret C.int32_t
if isVar {
startSlice := []byte(r.start.(string))
endSlice := []byte(r.end.(string))
ret = C.tiledb_subarray_add_range_var(sa.context.tiledbContext, sa.subarray, C.uint32_t(dimIdx),
slicePtr(startSlice), C.uint64_t(len(startSlice)), slicePtr(endSlice), C.uint64_t(len(endSlice)))
runtime.KeepAlive(startSlice)
runtime.KeepAlive(endSlice)
} else {
startValue := addressableValue(r.start)
endValue := addressableValue(r.end)
ret = C.tiledb_subarray_add_range(sa.context.tiledbContext, sa.subarray, C.uint32_t(dimIdx),
startValue.UnsafePointer(), endValue.UnsafePointer(), nil)
runtime.KeepAlive(startValue)
runtime.KeepAlive(endValue)
}
if ret != C.TILEDB_OK {
return fmt.Errorf("error adding subarray range: %w", sa.context.LastError())
}
return nil
}
// AddRangeByName adds a range along a subarray dimension. It checks the types of range and dimension and
// if the datatype of the range is not the same as the type of the dimension it returns an error.
func (sa *Subarray) AddRangeByName(dimName string, r Range) error {
dt, isVar, err := datatypeOfDimensionFromName(sa.array, dimName)
if err != nil {
return err
}
if err := r.assertCompatibility(dt, isVar); err != nil {
return err
}
cDimName := C.CString(dimName)
defer C.free(unsafe.Pointer(cDimName))
var ret C.int32_t
if isVar {
startSlice := []byte(r.start.(string))
endSlice := []byte(r.end.(string))
ret = C.tiledb_subarray_add_range_var_by_name(sa.context.tiledbContext, sa.subarray, cDimName,
slicePtr(startSlice), C.uint64_t(len(startSlice)), slicePtr(endSlice), C.uint64_t(len(endSlice)))
runtime.KeepAlive(startSlice)
runtime.KeepAlive(endSlice)
} else {
startValue := addressableValue(r.start)
endValue := addressableValue(r.end)
ret = C.tiledb_subarray_add_range_by_name(sa.context.tiledbContext, sa.subarray, cDimName,
startValue.UnsafePointer(), endValue.UnsafePointer(), nil)
runtime.KeepAlive(startValue)
runtime.KeepAlive(endValue)
}
if ret != C.TILEDB_OK {
return fmt.Errorf("error adding subarray range: %w", sa.context.LastError())
}
return nil
}
// GetRangeNum retrieves the number of ranges of the query subarray along a given dimension index.
func (sa *Subarray) GetRangeNum(dimIdx uint32) (uint64, error) {
var rangeNum uint64
ret := C.tiledb_subarray_get_range_num(sa.context.tiledbContext, sa.subarray, C.uint32_t(dimIdx), (*C.uint64_t)(unsafe.Pointer(&rangeNum)))
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("error retrieving subarray range num: %w", sa.context.LastError())
}
return rangeNum, nil
}
// GetRangeNum retrieves the number of ranges of the query subarray along a given dimension name.
func (sa *Subarray) GetRangeNumFromName(dimName string) (uint64, error) {
var rangeNum uint64
cDimName := C.CString(dimName)
defer C.free(unsafe.Pointer(cDimName))
ret := C.tiledb_subarray_get_range_num_from_name(sa.context.tiledbContext, sa.subarray, cDimName, (*C.uint64_t)(unsafe.Pointer(&rangeNum)))
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("error retrieving subarray range num: %w", sa.context.LastError())
}
return rangeNum, nil
}
// GetRanges gets the number of dimensions from the array under current subarray
// and builds an array of dimensions that have as memmbers arrays of ranges.
func (s *Subarray) GetRanges() (map[string][]Range, error) {
// We need to infer the datatype of the dimension represented by index
// dimIdx. That said:
// Get array schema
schema, err := s.array.Schema()
if err != nil {
return nil, err
}
defer schema.Free()
// Get the domain object
domain, err := schema.Domain()
if err != nil {
return nil, err
}
defer domain.Free()
// Use the index to retrieve the dimension object
nDim, err := domain.NDim()
if err != nil {
return nil, err
}
var dimIdx uint
rangeMap := make(map[string][]Range)
for dimIdx = 0; dimIdx < nDim; dimIdx++ {
err = func() error {
// Get dimension object
dimension, err := domain.DimensionFromIndex(dimIdx)
if err != nil {
return err
}
defer dimension.Free()
// Get name from dimension
name, err := dimension.Name()
if err != nil {
return err
}
// Get number of renges to iterate
numOfRanges, err := s.GetRangeNum(uint32(dimIdx))
if err != nil {
return err
}
var I uint64
rangeArray := make([]Range, 0)
for I = 0; I < numOfRanges; I++ {
r, err := s.GetRange(uint32(dimIdx), I)
if err != nil {
return err
}
// Append range to range Array
rangeArray = append(rangeArray, r)
}
// key: name (string), value: rangeArray ([]RangeLimits)
rangeMap[name] = rangeArray
return nil
}()
if err != nil {
return nil, err
}
}
return rangeMap, err
}
// GetRange retrieves a specific range of the subarray along a given dimension index.
func (sa *Subarray) GetRange(dimIdx uint32, rangeNum uint64) (Range, error) {
dt, isVar, err := datatypeOfDimensionFromIndex(sa.array, dimIdx)
if err != nil {
return Range{}, err
}
var r Range
var ret C.int32_t
if isVar {
var startSize, endSize uint64
ret = C.tiledb_subarray_get_range_var_size(sa.context.tiledbContext, sa.subarray, C.uint32_t(dimIdx), C.uint64_t(rangeNum),
(*C.uint64_t)(unsafe.Pointer(&startSize)), (*C.uint64_t)(unsafe.Pointer(&endSize)))
if startSize == 0 && endSize == 0 {
r.start = ""
r.end = ""
} else if ret == C.TILEDB_OK {
startData := make([]byte, int(startSize))
sp := slicePtr(startData)
endData := make([]byte, int(endSize))
ep := slicePtr(endData)
ret = C.tiledb_subarray_get_range_var(sa.context.tiledbContext, sa.subarray,
C.uint32_t(dimIdx), C.uint64_t(rangeNum), sp, ep)
if ret == C.TILEDB_OK {
r.start = string(startData)
r.end = string(endData)
}
}
} else {
var startPointer, endPointer, stridePointer unsafe.Pointer
ret = C.tiledb_subarray_get_range(sa.context.tiledbContext, sa.subarray,
C.uint32_t(dimIdx), C.uint64_t(rangeNum), &startPointer, &endPointer, &stridePointer)
typ := dt.ReflectType()
if ret == C.TILEDB_OK {
r.start = reflect.NewAt(typ, startPointer).Elem().Interface()
r.end = reflect.NewAt(typ, endPointer).Elem().Interface()
}
}
if ret != C.TILEDB_OK {
return Range{}, fmt.Errorf("error retrieving subarray range for dimension %d and range num %d: %w", dimIdx, rangeNum, sa.context.LastError())
}
return r, err
}
// GetRangeFromName retrieves a specific range of the subarray along a given dimension name.
func (sa *Subarray) GetRangeFromName(dimName string, rangeNum uint64) (Range, error) {
dt, isVar, err := datatypeOfDimensionFromName(sa.array, dimName)
if err != nil {
return Range{}, err
}
cDimName := C.CString(dimName)
defer C.free(unsafe.Pointer(cDimName))
var r Range
var ret C.int32_t
if isVar {
var startSize, endSize uint64
ret = C.tiledb_subarray_get_range_var_size_from_name(sa.context.tiledbContext, sa.subarray, cDimName, C.uint64_t(rangeNum),
(*C.uint64_t)(unsafe.Pointer(&startSize)), (*C.uint64_t)(unsafe.Pointer(&endSize)))
if startSize == 0 && endSize == 0 {
r.start = ""
r.end = ""
} else if ret == C.TILEDB_OK {
startData := make([]byte, int(startSize))
sp := slicePtr(startData)
endData := make([]byte, int(endSize))
ep := slicePtr(endData)
ret = C.tiledb_subarray_get_range_var_from_name(sa.context.tiledbContext, sa.subarray,
cDimName, C.uint64_t(rangeNum), sp, ep)
if ret == C.TILEDB_OK {
r.start = string(startData)
r.end = string(endData)
}
}
} else {
var startPointer, endPointer, stridePointer unsafe.Pointer
ret = C.tiledb_subarray_get_range_from_name(sa.context.tiledbContext, sa.subarray,
cDimName, C.uint64_t(rangeNum), &startPointer, &endPointer, &stridePointer)
typ := dt.ReflectType()
if ret == C.TILEDB_OK {
r.start = reflect.NewAt(typ, startPointer).Elem().Interface()
r.end = reflect.NewAt(typ, endPointer).Elem().Interface()
}
}
if ret != C.TILEDB_OK {
return Range{}, fmt.Errorf("error retrieving subarray range for dimension %s and range num %d: %w", dimName, rangeNum, sa.context.LastError())
}
return r, err
}