/
buffer_list.go
109 lines (88 loc) · 3.31 KB
/
buffer_list.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
package tiledb
/*
#cgo LDFLAGS: -ltiledb
#cgo linux LDFLAGS: -ldl
#include <tiledb/tiledb.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"runtime"
)
// BufferList A list of TileDB BufferList objects
type BufferList struct {
tiledbBufferList *C.tiledb_buffer_list_t
context *Context
}
// NewBufferList Allocs a new buffer list
func NewBufferList(context *Context) (*BufferList, error) {
bufferList := BufferList{context: context}
ret := C.tiledb_buffer_list_alloc(bufferList.context.tiledbContext, &bufferList.tiledbBufferList)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Error creating tiledb buffer list: %s", bufferList.context.LastError())
}
// Set finalizer for free C pointer on gc
runtime.SetFinalizer(&bufferList, func(bufferList *BufferList) {
bufferList.Free()
})
return &bufferList, 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 (b *BufferList) Free() {
if b.tiledbBufferList != nil {
C.tiledb_buffer_list_free(&b.tiledbBufferList)
}
}
// Context exposes the internal TileDB context used to initialize the buffer list
func (b *BufferList) Context() *Context {
return b.context
}
// NumBuffers returns number of buffers in the list
func (b *BufferList) NumBuffers() (uint64, error) {
var numBuffers C.uint64_t
ret := C.tiledb_buffer_list_get_num_buffers(b.context.tiledbContext, b.tiledbBufferList, &numBuffers)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("Error getting tiledb bufferList num buffers: %s", b.context.LastError())
}
return uint64(numBuffers), nil
}
// GetBuffer returns a Buffer at the given index in the list
func (b *BufferList) GetBuffer(bufferIndex uint) (*Buffer, error) {
buffer := Buffer{context: b.context}
// Set finalizer for free C pointer on gc
runtime.SetFinalizer(&buffer, func(buffer *Buffer) {
buffer.Free()
})
ret := C.tiledb_buffer_list_get_buffer(b.context.tiledbContext, b.tiledbBufferList, C.uint64_t(bufferIndex), &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Error getting tiledb buffer index %d from buffer list: %s", bufferIndex, b.context.LastError())
}
return &buffer, nil
}
// TotalSize returns total number of bytes in the buffers in the list
func (b *BufferList) TotalSize() (uint64, error) {
var totalSize C.uint64_t
ret := C.tiledb_buffer_list_get_total_size(b.context.tiledbContext, b.tiledbBufferList, &totalSize)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("Error getting tiledb bufferList num buffers: %s", b.context.LastError())
}
return uint64(totalSize), nil
}
// Flatten copies and concatenates all buffers in the list into a new buffer
func (b *BufferList) Flatten() (*Buffer, error) {
buffer := Buffer{context: b.context}
// Set finalizer for free C pointer on gc
runtime.SetFinalizer(&buffer, func(buffer *Buffer) {
buffer.Free()
})
ret := C.tiledb_buffer_list_flatten(b.context.tiledbContext, b.tiledbBufferList, &buffer.tiledbBuffer)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Error getting tiledb bufferList num buffers: %s", b.context.LastError())
}
return &buffer, nil
}