-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patharray_schema.go
423 lines (368 loc) · 14.6 KB
/
array_schema.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
package tiledb
/*
#include <tiledb/tiledb.h>
#include <tiledb/tiledb_serialization.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)
/*
ArraySchema describes an array.
The schema is an independent description of an array. A schema can be used to create multiple array’s, and stores information about its domain, cell types, and compression details. An array schema is composed of:
A Domain
A set of Attributes
Memory layout definitions: tile and cell
Compression details for Array level factors like offsets and coordinates
*/
type ArraySchema struct {
tiledbArraySchema *C.tiledb_array_schema_t
context *Context
}
// MarshalJSON marshals arraySchema struct to json using tiledb.
func (a *ArraySchema) MarshalJSON() ([]byte, error) {
bs, err := SerializeArraySchema(a, TILEDB_JSON, false)
if err != nil {
return nil, fmt.Errorf("error marshaling json for array schema: %w", a.context.LastError())
}
return bs, nil
}
// Context exposes the internal TileDB context used to initialize the array schema.
func (a *ArraySchema) Context() *Context {
return a.context
}
// UnmarshalJSON marshals arraySchema struct to json using tiledb.
func (a *ArraySchema) UnmarshalJSON(b []byte) error {
var err error
if a.context == nil {
a.context, err = NewContext(nil)
if err != nil {
return err
}
}
// tiledb c expect the byte array to include the null terminator
bytesWithNullTerminator := b
size := len(b)
// Add the null terminator if it is missing
if b[size-1] != 0 {
// If we need to add the null terminator we must first create a copy of the
// byte array, the marshaler does not allow editing the input byte array
bytesWithNullTerminator = make([]byte, size+1)
copy(bytesWithNullTerminator, b)
bytesWithNullTerminator[size] = 0
}
// Wrap the input byte slice in a Buffer (does not copy)
buffer, err := NewBuffer(a.context)
if err != nil {
return fmt.Errorf("error unmarshaling json for array schema: %w", a.context.LastError())
}
err = buffer.SetBuffer(bytesWithNullTerminator)
if err != nil {
return fmt.Errorf("error unmarshaling json for array schema: %w", a.context.LastError())
}
// Deserialize into a new array schema
var newCSchema *C.tiledb_array_schema_t
var cClientSide = C.int32_t(0) // Currently this parameter is unused in libtiledb
ret := C.tiledb_deserialize_array_schema(a.context.tiledbContext, buffer.tiledbBuffer, C.TILEDB_JSON, cClientSide, &newCSchema)
if ret != C.TILEDB_OK {
return fmt.Errorf("error deserializing array schema: %w", a.context.LastError())
}
// Replace the C schema object with the deserialized one.
if a.tiledbArraySchema != nil {
C.tiledb_array_schema_free(&a.tiledbArraySchema)
}
a.tiledbArraySchema = newCSchema
return nil
}
// NewArraySchema allocates a new ArraySchema.
func NewArraySchema(tdbCtx *Context, arrayType ArrayType) (*ArraySchema, error) {
arraySchema := ArraySchema{context: tdbCtx}
ret := C.tiledb_array_schema_alloc(arraySchema.context.tiledbContext, C.tiledb_array_type_t(arrayType), &arraySchema.tiledbArraySchema)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error creating tiledb arraySchema: %w", arraySchema.context.LastError())
}
freeOnGC(&arraySchema)
return &arraySchema, 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 *ArraySchema) Free() {
if a.tiledbArraySchema != nil {
C.tiledb_array_schema_free(&a.tiledbArraySchema)
}
}
// AddAttributes adds one or more attributes to the array.
func (a *ArraySchema) AddAttributes(attributes ...*Attribute) error {
for _, attribute := range attributes {
ret := C.tiledb_array_schema_add_attribute(a.context.tiledbContext, a.tiledbArraySchema, attribute.tiledbAttribute)
if ret != C.TILEDB_OK {
return fmt.Errorf("error adding attributes to tiledb arraySchema: %w", a.context.LastError())
}
}
return nil
}
// AttributeNum returns the number of attributes.
func (a *ArraySchema) AttributeNum() (uint, error) {
var attrNum C.uint32_t
ret := C.tiledb_array_schema_get_attribute_num(a.context.tiledbContext, a.tiledbArraySchema, &attrNum)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("error getting attribute number for tiledb arraySchema: %w", a.context.LastError())
}
return uint(attrNum), nil
}
// AttributeFromIndex gets a copy of an Attribute in the schema by name.
func (a *ArraySchema) AttributeFromIndex(index uint) (*Attribute, error) {
attr := Attribute{context: a.context}
ret := C.tiledb_array_schema_get_attribute_from_index(
a.context.tiledbContext,
a.tiledbArraySchema,
C.uint32_t(index),
&attr.tiledbAttribute)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error getting attribute %d for tiledb arraySchema: %w", index, a.context.LastError())
}
freeOnGC(&attr)
return &attr, nil
}
// AttributeFromName gets a copy of an Attribute in the schema by index.
// Attributes are ordered the same way they were defined when
// constructing the array schema.
func (a *ArraySchema) AttributeFromName(attrName string) (*Attribute, error) {
cAttrName := C.CString(attrName)
defer C.free(unsafe.Pointer(cAttrName))
attr := Attribute{context: a.context}
ret := C.tiledb_array_schema_get_attribute_from_name(a.context.tiledbContext, a.tiledbArraySchema, cAttrName, &attr.tiledbAttribute)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error getting attribute %s for tiledb arraySchema: %w", attrName, a.context.LastError())
}
freeOnGC(&attr)
return &attr, nil
}
// HasAttribute returns true if attribute: `attrName` is part of the schema.
func (a *ArraySchema) HasAttribute(attrName string) (bool, error) {
var hasAttr C.int32_t
cAttrName := C.CString(attrName)
defer C.free(unsafe.Pointer(cAttrName))
ret := C.tiledb_array_schema_has_attribute(a.context.tiledbContext, a.tiledbArraySchema, cAttrName, &hasAttr)
if ret != C.TILEDB_OK {
return false, fmt.Errorf("error finding attribute %s in schema: %w", attrName, a.context.LastError())
}
if hasAttr == 0 {
return false, nil
}
return true, nil
}
// SetAllowsDups sets whether the array can allow coordinate duplicates or not.
// Applicable only to sparse arrays (it errors out if set to `1` for dense
// arrays).
func (a *ArraySchema) SetAllowsDups(allowsDups bool) error {
allowsDupsInt := 0
if allowsDups {
allowsDupsInt = 1
}
ret := C.tiledb_array_schema_set_allows_dups(a.context.tiledbContext, a.tiledbArraySchema, C.int32_t(allowsDupsInt))
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting allows dups for schema: %w", a.context.LastError())
}
return nil
}
// AllowsDups gets whether the array can allow coordinate duplicates or not.
// It should always be `0` for dense arrays.
func (a *ArraySchema) AllowsDups() (bool, error) {
var allowsDups C.int32_t
ret := C.tiledb_array_schema_get_allows_dups(a.context.tiledbContext, a.tiledbArraySchema, &allowsDups)
if ret != C.TILEDB_OK {
return false, fmt.Errorf("error getting allows dups for schema: %w", a.context.LastError())
}
if allowsDups == 0 {
return false, nil
}
return true, nil
}
// Attributes gets all attributes in the array.
func (a *ArraySchema) Attributes() ([]*Attribute, error) {
attributes := make([]*Attribute, 0)
attrNum, err := a.AttributeNum()
if err != nil {
return nil, fmt.Errorf("error getting AttributeNum: %w", err)
}
for i := uint(0); i < attrNum; i++ {
attribute, err := a.AttributeFromIndex(i)
if err != nil {
return nil, fmt.Errorf("error getting Attribute: %w", err)
}
attributes = append(attributes, attribute)
}
return attributes, nil
}
// SetDomain sets the array domain.
func (a *ArraySchema) SetDomain(domain *Domain) error {
ret := C.tiledb_array_schema_set_domain(a.context.tiledbContext, a.tiledbArraySchema, domain.tiledbDomain)
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting domain for tiledb arraySchema: %w", a.context.LastError())
}
return nil
}
// Domain returns the array's domain.
func (a *ArraySchema) Domain() (*Domain, error) {
domain := Domain{context: a.context}
ret := C.tiledb_array_schema_get_domain(a.context.tiledbContext, a.tiledbArraySchema, &domain.tiledbDomain)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error setting domain for tiledb arraySchema: %w", a.context.LastError())
}
freeOnGC(&domain)
return &domain, nil
}
// SetCapacity sets the tile capacity.
func (a *ArraySchema) SetCapacity(capacity uint64) error {
ret := C.tiledb_array_schema_set_capacity(a.context.tiledbContext, a.tiledbArraySchema, C.uint64_t(capacity))
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting capacity for tiledb arraySchema: %w", a.context.LastError())
}
return nil
}
// Capacity returns the tile capacity.
func (a *ArraySchema) Capacity() (uint64, error) {
var capacity C.uint64_t
ret := C.tiledb_array_schema_get_capacity(a.context.tiledbContext, a.tiledbArraySchema, &capacity)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("error getting capacity for tiledb arraySchema: %w", a.context.LastError())
}
return uint64(capacity), nil
}
// SetCellOrder sets the cell order.
func (a *ArraySchema) SetCellOrder(cellOrder Layout) error {
ret := C.tiledb_array_schema_set_cell_order(a.context.tiledbContext, a.tiledbArraySchema, C.tiledb_layout_t(cellOrder))
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting cell order for tiledb arraySchema: %w", a.context.LastError())
}
return nil
}
// CellOrder returns the cell order.
func (a *ArraySchema) CellOrder() (Layout, error) {
var cellOrder C.tiledb_layout_t
ret := C.tiledb_array_schema_get_cell_order(a.context.tiledbContext, a.tiledbArraySchema, &cellOrder)
if ret != C.TILEDB_OK {
return -1, fmt.Errorf("error getting cell order for tiledb arraySchema: %w", a.context.LastError())
}
return Layout(cellOrder), nil
}
// SetTileOrder sets the tile order.
func (a *ArraySchema) SetTileOrder(tileOrder Layout) error {
ret := C.tiledb_array_schema_set_tile_order(a.context.tiledbContext, a.tiledbArraySchema, C.tiledb_layout_t(tileOrder))
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting cell order for tiledb arraySchema: %w", a.context.LastError())
}
return nil
}
// TileOrder returns the tile order.
func (a *ArraySchema) TileOrder() (Layout, error) {
var cellOrder C.tiledb_layout_t
ret := C.tiledb_array_schema_get_tile_order(a.context.tiledbContext, a.tiledbArraySchema, &cellOrder)
if ret != C.TILEDB_OK {
return -1, fmt.Errorf("error getting cell order for tiledb arraySchema: %w", a.context.LastError())
}
return Layout(cellOrder), nil
}
// SetCoordsFilterList sets the filter list used for coordinates.
func (a *ArraySchema) SetCoordsFilterList(filterList *FilterList) error {
ret := C.tiledb_array_schema_set_coords_filter_list(a.context.tiledbContext, a.tiledbArraySchema, filterList.tiledbFilterList)
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting coordinates filter list for tiledb arraySchema: %w", a.context.LastError())
}
return nil
}
// CoordsFilterList returns a copy of the filter list of the coordinates.
func (a *ArraySchema) CoordsFilterList() (*FilterList, error) {
filterList := FilterList{context: a.context}
ret := C.tiledb_array_schema_get_coords_filter_list(a.context.tiledbContext, a.tiledbArraySchema, &filterList.tiledbFilterList)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error getting coordinates filter list for tiledb arraySchema: %w", a.context.LastError())
}
freeOnGC(&filterList)
return &filterList, nil
}
// SetOffsetsFilterList sets the filter list for the offsets of
// variable-length attributes.
func (a *ArraySchema) SetOffsetsFilterList(filterList *FilterList) error {
ret := C.tiledb_array_schema_set_offsets_filter_list(a.context.tiledbContext, a.tiledbArraySchema, filterList.tiledbFilterList)
if ret != C.TILEDB_OK {
return fmt.Errorf("error setting offsets filter list for tiledb arraySchema: %w", a.context.LastError())
}
return nil
}
// OffsetsFilterList returns a copy of the FilterList of the offsets for
// variable-length attributes.
func (a *ArraySchema) OffsetsFilterList() (*FilterList, error) {
filterList := FilterList{context: a.context}
ret := C.tiledb_array_schema_get_offsets_filter_list(a.context.tiledbContext, a.tiledbArraySchema, &filterList.tiledbFilterList)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error getting offsets filter list for tiledb arraySchema: %w", a.context.LastError())
}
freeOnGC(&filterList)
return &filterList, nil
}
// Check validates the schema.
func (a *ArraySchema) Check() error {
ret := C.tiledb_array_schema_check(a.context.tiledbContext, a.tiledbArraySchema)
if ret != C.TILEDB_OK {
return fmt.Errorf("error in checking arraySchema: %w", a.context.LastError())
}
return nil
}
// LoadArraySchema reads a directory for an ArraySchema.
func LoadArraySchema(context *Context, path string) (*ArraySchema, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
a := ArraySchema{context: context}
ret := C.tiledb_array_schema_load(a.context.tiledbContext, cpath, &a.tiledbArraySchema)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error in loading arraySchema from %s: %w", path, a.context.LastError())
}
freeOnGC(&a)
return &a, nil
}
// DumpSTDOUT dumps the array schema in ASCII format to stdout.
func (a *ArraySchema) DumpSTDOUT() error {
ret := C.tiledb_array_schema_dump(a.context.tiledbContext, a.tiledbArraySchema, C.stdout)
if ret != C.TILEDB_OK {
return fmt.Errorf("error dumping array schema to stdout: %w", a.context.LastError())
}
return nil
}
// Dump dumps the array schema in ASCII format to the given path.
func (a *ArraySchema) Dump(path string) error {
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("error path already %s exists", path)
}
// Convert to char *
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
// Set mode as char*
cMode := C.CString("w")
defer C.free(unsafe.Pointer(cMode))
// Open file to get FILE*
cFile := C.fopen(cPath, cMode)
defer C.fclose(cFile)
// Dump array schema to file
ret := C.tiledb_array_schema_dump(a.context.tiledbContext, a.tiledbArraySchema, cFile)
if ret != C.TILEDB_OK {
return fmt.Errorf("error dumping array schema to file %s: %w", path, a.context.LastError())
}
return nil
}
// Type fetches the tiledb array type.
func (a *ArraySchema) Type() (ArrayType, error) {
var arrayType C.tiledb_array_type_t
ret := C.tiledb_array_schema_get_array_type(a.context.tiledbContext, a.tiledbArraySchema, &arrayType)
if ret != C.TILEDB_OK {
return TILEDB_DENSE, fmt.Errorf("error fetching array schema type: %w", a.context.LastError())
}
return ArrayType(arrayType), nil
}