-
Notifications
You must be signed in to change notification settings - Fork 6
/
context.go
153 lines (125 loc) · 3.72 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
package tiledb
/*
#cgo LDFLAGS: -ltiledb
#cgo linux LDFLAGS: -ldl
#include <tiledb/tiledb.h>
#include <stdlib.h>
*/
import "C"
import (
"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 null it is created with default config
func NewContext(config *Config) (*Context, error) {
var context Context
var err *C.tiledb_error_t
if config != nil {
C.tiledb_ctx_alloc(config.tiledbConfig, &context.tiledbContext)
} else {
C.tiledb_ctx_alloc(nil, &context.tiledbContext)
}
if err != nil {
var msg *C.char
defer C.free(unsafe.Pointer(msg))
C.tiledb_error_message(err, &msg)
defer C.tiledb_error_free(&err)
return nil, fmt.Errorf("Error creating tiledb context: %s", C.GoString(msg))
}
// Set finalizer for free C pointer on gc
runtime.SetFinalizer(&context, func(context *Context) {
context.Free()
})
err1 := context.setDefaultTags()
if err != nil {
return nil, fmt.Errorf("Error creating tiledb context: %s", err1.Error())
}
return &context, nil
}
// Free tiledb_ctx_t that was allocated on heap in c
func (c *Context) Free() {
if c.tiledbContext != nil {
C.tiledb_ctx_free(&c.tiledbContext)
}
}
// 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, fmt.Errorf("Out of Memory error in GetConfig")
} else if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Unknown error in GetConfig")
}
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 fmt.Errorf("Out of Memory error in tiledb_ctx_get_last_error")
} else if ret != C.TILEDB_OK {
return fmt.Errorf("Unknown error in tiledb_ctx_get_last_error")
}
if err != nil {
var msg *C.char
defer C.tiledb_error_free(&err)
ret := C.tiledb_error_message(err, &msg)
if ret == C.TILEDB_OOM {
return fmt.Errorf("Out of Memory error in tiledb_error_message")
} else if ret != C.TILEDB_OK {
return fmt.Errorf("Unknown error in tiledb_error_message")
}
return fmt.Errorf("%s", C.GoString(msg))
}
return nil
}
// IsSupportedFS Return 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, fmt.Errorf("Error in checking FS support")
}
if isSupported == 0 {
return false, nil
}
return true, nil
}
// SetTag, sets 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")
}
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
}