-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontext.go
193 lines (163 loc) · 5.08 KB
/
context.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
package tiledb
/*
#include <tiledb/tiledb.h>
#include <stdlib.h>
*/
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)
// Context A TileDB context wraps a TileDB storage manager “instance.” Most
// objects and functions will require a Context.
// Internal error handling is also defined by the Context;
// the default error handler throws a TileDBError with a specific message.
type Context struct {
tiledbContext *C.tiledb_ctx_t
}
// NewContext creates a TileDB context with the given configuration.
// If the configuration passed is nil, it is created with the default config.
func NewContext(config *Config) (*Context, error) {
var context Context
var ret C.int32_t
if config != nil {
ret = C.tiledb_ctx_alloc(config.tiledbConfig, &context.tiledbContext)
} else {
ret = C.tiledb_ctx_alloc(nil, &context.tiledbContext)
}
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("error creating tiledb context: %w", context.LastError())
}
freeOnGC(&context)
err := context.setDefaultTags()
if err != nil {
return nil, fmt.Errorf("error creating tiledb context: %w", err)
}
if config != nil {
runtime.KeepAlive(config)
}
return &context, nil
}
// NewContextFromMap creates a TileDB context with the given configuration.
// If the configuration passed is nil, it is created with the default config.
// This is a shortcut for creating a *Config from the given map and
// using it to create a new context.
func NewContextFromMap(cfgMap map[string]string) (*Context, error) {
if cfgMap == nil {
return NewContext(nil)
}
config, err := NewConfig()
if err != nil {
return nil, err
}
defer config.Free()
for k, v := range cfgMap {
if err := config.Set(k, v); err != nil {
// The value is not included in the error message in case it is sensitive,
// like a password or access key.
return nil, fmt.Errorf("error setting config value %q: %w", k, err)
}
}
return NewContext(config)
}
// 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 (c *Context) Free() {
if c.tiledbContext != nil {
C.tiledb_ctx_free(&c.tiledbContext)
}
}
// CancelAllTasks cancels all currently executing tasks on the context.
func (c *Context) CancelAllTasks() error {
ret := C.tiledb_ctx_cancel_tasks(c.tiledbContext)
if ret != C.TILEDB_OK {
return errors.New("failed to cancel tasks")
}
return nil
}
// Config retrieves a copy of the config from context.
func (c *Context) Config() (*Config, error) {
config := Config{}
ret := C.tiledb_ctx_get_config(c.tiledbContext, &config.tiledbConfig)
if ret == C.TILEDB_OOM {
return nil, errors.New("out of Memory error in GetConfig")
} else if ret != C.TILEDB_OK {
return nil, errors.New("unknown error in GetConfig")
}
freeOnGC(&config)
return &config, nil
}
// LastError returns the last error from this context.
func (c *Context) LastError() error {
var err *C.tiledb_error_t
ret := C.tiledb_ctx_get_last_error(c.tiledbContext, &err)
if ret == C.TILEDB_OOM {
return errors.New("out of Memory error in tiledb_ctx_get_last_error")
} else if ret != C.TILEDB_OK {
return errors.New("unknown error in tiledb_ctx_get_last_error")
}
if err != nil {
defer C.tiledb_error_free(&err)
return cError(err)
}
return nil
}
// IsSupportedFS returns true if the given filesystem backend is supported.
func (c *Context) IsSupportedFS(fs FS) (bool, error) {
var isSupported C.int32_t
ret := C.tiledb_ctx_is_supported_fs(c.tiledbContext, C.tiledb_filesystem_t(fs), &isSupported)
if ret != C.TILEDB_OK {
return false, errors.New("error in checking FS support")
}
if isSupported == 0 {
return false, nil
}
return true, nil
}
// SetTag sets the context tag.
func (c *Context) SetTag(key string, value string) error {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(cvalue))
ret := C.tiledb_ctx_set_tag(c.tiledbContext, ckey, cvalue)
if ret != C.TILEDB_OK {
return fmt.Errorf("error in setting tag: %w", c.LastError())
}
return nil
}
func (c *Context) setDefaultTags() error {
err := c.SetTag("x-tiledb-api-language", "go")
if err != nil {
return err
}
err = c.SetTag("x-tiledb-api-language-version", "0.8.0")
if err != nil {
return err
}
err = c.SetTag("x-tiledb-api-sys-platform", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
if err != nil {
return err
}
return nil
}
// Stats gets stats for a context as json bytes.
func (c *Context) Stats() ([]byte, error) {
var stats *C.char
if ret := C.tiledb_ctx_get_stats(c.tiledbContext, &stats); ret != C.TILEDB_OK {
return nil, fmt.Errorf("error getting stats from context: %w", c.LastError())
}
s := C.GoString(stats)
if ret := C.tiledb_stats_free_str(&stats); ret != C.TILEDB_OK {
return nil, errors.New("error freeing string from dumping stats to string")
}
if s == "" {
return []byte("{}"), nil
}
return []byte(s), nil
}