/
serialize.go
478 lines (401 loc) · 16.4 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
package tiledb
/*
#cgo LDFLAGS: -ltiledb
#cgo linux LDFLAGS: -ldl
#include <tiledb/tiledb.h>
#include <tiledb/tiledb_serialization.h>
#include <stdio.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
// SerializeArraySchema serializes an array schema
func SerializeArraySchema(schema *ArraySchema, serializationType SerializationType, clientSide bool) ([]byte, 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: %s", schema.context.LastError())
}
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: %s", 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
}
// SerializeArrayNonEmptyDomain gets and serializes the array nonempty domain
func SerializeArrayNonEmptyDomain(a *Array, serializationType SerializationType) ([]byte, 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, unsafe.Pointer(&tmpDomain[0]), &isEmpty)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Error serializing array nonempty domain: %s", 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, unsafe.Pointer(&tmpDomain[0]), isEmpty, C.tiledb_serialization_type_t(serializationType), cClientSide, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Error serializing array nonempty domain: %s", a.context.LastError())
}
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: %s", 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
}
// SerializeArrayNonEmptyDomainAllDimensions gets and serializes the array nonempty domain
func SerializeArrayNonEmptyDomainAllDimensions(a *Array, serializationType SerializationType) ([]byte, 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: %s", a.context.LastError())
}
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: %s", a.context.LastError())
}
return nil
}
// SerializeArrayMaxBufferSizes gets and serializes the array max buffer sizes for the given subarray
func SerializeArrayMaxBufferSizes(a *Array, subarray interface{}, serializationType SerializationType) ([]byte, error) {
// Create subarray void*
var cSubarray unsafe.Pointer
if reflect.TypeOf(subarray).Kind() != reflect.Slice {
return nil, fmt.Errorf("subarray passed must be a slice, type passed was: %s", reflect.TypeOf(subarray).Kind().String())
}
switch subarray := subarray.(type) {
case []int:
cSubarray = slicePtr(subarray)
case []int8:
cSubarray = slicePtr(subarray)
case []int16:
cSubarray = slicePtr(subarray)
case []int32:
cSubarray = slicePtr(subarray)
case []int64:
cSubarray = slicePtr(subarray)
case []uint:
cSubarray = slicePtr(subarray)
case []uint8:
cSubarray = slicePtr(subarray)
case []uint16:
cSubarray = slicePtr(subarray)
case []uint32:
cSubarray = slicePtr(subarray)
case []uint64:
cSubarray = slicePtr(subarray)
case []float32:
cSubarray = slicePtr(subarray)
case []float64:
cSubarray = slicePtr(subarray)
case []bool:
cSubarray = slicePtr(subarray)
default:
return nil, fmt.Errorf("subarray must be a slice of scalars, not a %T", subarray)
}
buffer := Buffer{context: a.context}
freeOnGC(&buffer)
ret := C.tiledb_serialize_array_max_buffer_sizes(a.context.tiledbContext, a.tiledbArray, cSubarray, C.tiledb_serialization_type_t(serializationType), &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error serializing array max buffer sizes: %s", a.context.LastError())
}
return buffer.Serialize(serializationType)
}
// 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: %s", 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: %s", query.context.LastError())
}
return nil
}
// SerializeArrayMetadata gets and serializes the array metadata
func SerializeArrayMetadata(a *Array, serializationType SerializationType) ([]byte, 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: %s", a.context.LastError())
}
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: %s", a.context.LastError())
}
return nil
}
// SerializeQueryEstResultSizes gets and serializes the query estimated result sizes
func SerializeQueryEstResultSizes(q *Query, serializationType SerializationType, clientSide bool) ([]byte, 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: %s", q.context.LastError())
}
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: %s", q.context.LastError())
}
return nil
}
// SerializeArray serializes an array
func SerializeArray(array *Array, serializationType SerializationType, clientSide bool) ([]byte, 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: %s", array.context.LastError())
}
return buffer.Serialize(serializationType)
}
// DeserializeArray deserializes a new array from the given buffer
func DeserializeArray(buffer *Buffer, serializationType SerializationType, clientSide bool) (*Array, error) {
array := Array{context: buffer.context}
var cClientSide C.int32_t
if clientSide {
cClientSide = 1
} else {
cClientSide = 0
}
ret := C.tiledb_deserialize_array(array.context.tiledbContext, buffer.tiledbBuffer, C.tiledb_serialization_type_t(serializationType), cClientSide, &array.tiledbArray)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error deserializing array: %s", 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
}
// SerializeFragmentInfo serializes fragment info
func SerializeFragmentInfo(fragmentInfo *FragmentInfo, serializationType SerializationType, clientSide bool) ([]byte, 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: %s", fragmentInfo.context.LastError())
}
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: %s", fragmentInfo.context.LastError())
}
return nil
}
// SerializeFragmentInfoRequest serializes fragment info
func SerializeFragmentInfoRequest(fragmentInfo *FragmentInfo, serializationType SerializationType, clientSide bool) ([]byte, 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: %s", fragmentInfo.context.LastError())
}
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: %s", fragmentInfo.context.LastError())
}
return nil
}