/
vfs.go
398 lines (312 loc) · 10.8 KB
/
vfs.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
package tiledb
/*
#cgo LDFLAGS: -ltiledb
#cgo linux LDFLAGS: -ldl
#include <tiledb/tiledb.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
// VFSfh is a virtual file system file handler
type VFSfh struct {
tiledbVFSfh *C.tiledb_vfs_fh_t
context *Context
}
// Free a tiledb c vfs file handler
func (v *VFSfh) Free() {
if v.tiledbVFSfh != nil {
C.tiledb_vfs_fh_free(&v.tiledbVFSfh)
}
}
// IsClosed checks a vfs file handler to see if it is closed. Return true if
// file handler is closed, false if its not closed and error is non-nil on error
func (v *VFSfh) IsClosed() (bool, error) {
var isClosed C.int32_t
ret := C.tiledb_vfs_fh_is_closed(v.context.tiledbContext, v.tiledbVFSfh, &isClosed)
if ret != C.TILEDB_OK {
return false, fmt.Errorf("Error in checking if vfs file handler is closed")
}
if isClosed == 1 {
return true, nil
}
return false, nil
}
// VFS Implements a virtual filesystem that enables performing directory/file
// operations with a unified API on different filesystems, such as local
// posix/windows, HDFS, AWS S3, etc.
type VFS struct {
tiledbVFS *C.tiledb_vfs_t
context *Context
}
// NewVFS alloc a new context using tiledb_vfs_alloc. This also registers the
// `runtime.SetFinalizer` for handling the free'ing of the c data structure on
// garbage collection
func NewVFS(context *Context, config *Config) (*VFS, error) {
vfs := VFS{context: context}
var err *C.tiledb_error_t
C.tiledb_vfs_alloc(context.tiledbContext, config.tiledbConfig, &vfs.tiledbVFS)
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(&vfs, func(vfs *VFS) {
vfs.Free()
})
return &vfs, nil
}
// Free tiledb_vfs_t c structure that was allocated on the heap
func (v *VFS) Free() {
if v.tiledbVFS != nil {
C.tiledb_vfs_free(&v.tiledbVFS)
}
}
// Config retrieves a copy of the config from vfs
func (v *VFS) Config() (*Config, error) {
config := &Config{}
ret := C.tiledb_vfs_get_config(v.context.tiledbContext, v.tiledbVFS,
&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
}
// CreateBucket creates an object-store bucket with the input URI.
func (v *VFS) CreateBucket(uri string) error {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_vfs_create_bucket(v.context.tiledbContext, v.tiledbVFS, curi)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in creating s3 bucket %s: %s", uri, v.context.LastError())
}
return nil
}
// RemoveBucket deletes an object-store bucket with the input URI.
func (v *VFS) RemoveBucket(uri string) error {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_vfs_remove_bucket(v.context.tiledbContext, v.tiledbVFS, curi)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in removing s3 bucket %s: %s", uri, v.context.LastError())
}
return nil
}
// EmptyBucket empty a bucket
func (v *VFS) EmptyBucket(uri string) error {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_vfs_empty_bucket(v.context.tiledbContext, v.tiledbVFS, curi)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in emptying s3 bucket %s: %s", uri, v.context.LastError())
}
return nil
}
// IsEmptyBucket check if a bucket is empty
func (v *VFS) IsEmptyBucket(uri string) (bool, error) {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
var isEmpty C.int32_t
ret := C.tiledb_vfs_is_empty_bucket(v.context.tiledbContext, v.tiledbVFS, curi, &isEmpty)
if ret != C.TILEDB_OK {
return false, fmt.Errorf("Error in checking if s3 bucket %s is empty: %s", uri, v.context.LastError())
}
if isEmpty == 1 {
return true, nil
}
return false, nil
}
// IsBucket checks if an object-store bucket with the input URI exists.
func (v *VFS) IsBucket(uri string) (bool, error) {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
var isBucket C.int32_t
ret := C.tiledb_vfs_is_bucket(v.context.tiledbContext, v.tiledbVFS, curi, &isBucket)
if ret != C.TILEDB_OK {
return false, fmt.Errorf("Error in checking if %s is a s3 bucket: %s", uri, v.context.LastError())
}
if isBucket == 1 {
return true, nil
}
return false, nil
}
// CreateDir creates a directory with the input URI.
func (v *VFS) CreateDir(uri string) error {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_vfs_create_dir(v.context.tiledbContext, v.tiledbVFS, curi)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in creating directory %s: %s", uri, v.context.LastError())
}
return nil
}
// IsDir checks if a directory with the input URI exists.
func (v *VFS) IsDir(uri string) (bool, error) {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
var isDir C.int32_t
ret := C.tiledb_vfs_is_dir(v.context.tiledbContext, v.tiledbVFS, curi, &isDir)
if ret != C.TILEDB_OK {
return false, fmt.Errorf("Error in checking if %s is a directory: %s", uri, v.context.LastError())
}
if isDir == 1 {
return true, nil
}
return false, nil
}
// RemoveDir removes a directory (recursively) with the input URI.
func (v *VFS) RemoveDir(uri string) error {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_vfs_remove_dir(v.context.tiledbContext, v.tiledbVFS, curi)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in removing directory %s: %s", uri, v.context.LastError())
}
return nil
}
// IsFile checks if a file with the input URI exists.
func (v *VFS) IsFile(uri string) (bool, error) {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
var isFile C.int32_t
ret := C.tiledb_vfs_is_file(v.context.tiledbContext, v.tiledbVFS, curi, &isFile)
if ret != C.TILEDB_OK {
return false, fmt.Errorf("Error in checking if %s is a file: %s", uri, v.context.LastError())
}
if isFile == 1 {
return true, nil
}
return false, nil
}
// RemoveFile deletes a file with the input URI.
func (v *VFS) RemoveFile(uri string) error {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_vfs_remove_file(v.context.tiledbContext, v.tiledbVFS, curi)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in removing file %s: %s", uri, v.context.LastError())
}
return nil
}
// FileSize retrieves the size of a file.
func (v *VFS) FileSize(uri string) (uint64, error) {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
var cfsize C.uint64_t
ret := C.tiledb_vfs_file_size(v.context.tiledbContext, v.tiledbVFS, curi, &cfsize)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("Error in getting file size %s: %s", uri, v.context.LastError())
}
return uint64(cfsize), nil
}
// MoveFile renames a TileDB file from an old URI to a new URI.
func (v *VFS) MoveFile(oldURI string, newURI string) error {
cOldURI := C.CString(oldURI)
defer C.free(unsafe.Pointer(cOldURI))
cNewURI := C.CString(newURI)
defer C.free(unsafe.Pointer(cNewURI))
ret := C.tiledb_vfs_move_file(v.context.tiledbContext, v.tiledbVFS, cOldURI, cNewURI)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in moving file %s to %s: %s", oldURI, newURI, v.context.LastError())
}
return nil
}
// MoveDir menames a TileDB directory from an old URI to a new URI.
func (v *VFS) MoveDir(oldURI string, newURI string) error {
cOldURI := C.CString(oldURI)
defer C.free(unsafe.Pointer(cOldURI))
cNewURI := C.CString(newURI)
defer C.free(unsafe.Pointer(cNewURI))
ret := C.tiledb_vfs_move_dir(v.context.tiledbContext, v.tiledbVFS, cOldURI, cNewURI)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in moving directory %s to %s: %s", oldURI, newURI, v.context.LastError())
}
return nil
}
// Open prepares a file for reading/writing.
func (v *VFS) Open(uri string, mode VFSMode) (*VFSfh, error) {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
fh := &VFSfh{context: v.context}
// Set finalizer for free C pointer on gc
runtime.SetFinalizer(fh, func(fh *VFSfh) {
fh.Free()
})
ret := C.tiledb_vfs_open(v.context.tiledbContext, v.tiledbVFS, curi, C.tiledb_vfs_mode_t(mode), &fh.tiledbVFSfh)
if ret == C.TILEDB_OOM {
return nil, fmt.Errorf("Out of Memory error in VFS.Open: %s", v.context.LastError())
} else if ret != C.TILEDB_OK {
return nil, fmt.Errorf("Unknown error in VFS.Open: %s", v.context.LastError())
}
return fh, nil
}
// Close a file. This is flushes the buffered data into the file when the file
// was opened in write (or append) mode. It is particularly important to be
// called after S3 writes, as otherwise the writes will not take effect.
func (v *VFS) Close(fh *VFSfh) error {
ret := C.tiledb_vfs_close(v.context.tiledbContext, fh.tiledbVFSfh)
if ret != C.TILEDB_OK {
return fmt.Errorf("Unknown error in VFS.Close: %s", v.context.LastError())
}
fh.Free()
return nil
}
// Read part of a file
func (v *VFS) Read(fh *VFSfh, offset uint64, nbytes uint64) ([]byte, error) {
bytes := make([]byte, nbytes)
cbuffer := C.CBytes(bytes)
ret := C.tiledb_vfs_read(v.context.tiledbContext, fh.tiledbVFSfh, C.uint64_t(offset), cbuffer, C.uint64_t(nbytes))
if ret != C.TILEDB_OK {
return []byte{}, fmt.Errorf("Unknown error in VFS.Read: %s", v.context.LastError())
}
bytes = C.GoBytes(cbuffer, C.int32_t(nbytes))
return bytes, nil
}
// Write the contents of a buffer into a file. Note that this function only
// appends data at the end of the file. If the file does not exist,
// it will be created
func (v *VFS) Write(fh *VFSfh, bytes []byte) error {
cbuffer := C.CBytes(bytes)
ret := C.tiledb_vfs_write(v.context.tiledbContext, fh.tiledbVFSfh, cbuffer, C.uint64_t(len(bytes)))
if ret != C.TILEDB_OK {
return fmt.Errorf("Unknown error in VFS.Write: %s", v.context.LastError())
}
return nil
}
// Sync (flushes) a file.
func (v *VFS) Sync(fh *VFSfh) error {
ret := C.tiledb_vfs_sync(v.context.tiledbContext, fh.tiledbVFSfh)
if ret != C.TILEDB_OK {
return fmt.Errorf("Unknown error in VFS.Sync: %s", v.context.LastError())
}
return nil
}
// Touch a file, i.e., creates a new empty file.
func (v *VFS) Touch(uri string) error {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
ret := C.tiledb_vfs_touch(v.context.tiledbContext, v.tiledbVFS, curi)
if ret != C.TILEDB_OK {
return fmt.Errorf("Error in touching %s: %s", uri, v.context.LastError())
}
return nil
}
// DirSize retrieves the size of a directory.
func (v *VFS) DirSize(uri string) (uint64, error) {
curi := C.CString(uri)
defer C.free(unsafe.Pointer(curi))
var cfsize C.uint64_t
ret := C.tiledb_vfs_dir_size(v.context.tiledbContext, v.tiledbVFS, curi, &cfsize)
if ret != C.TILEDB_OK {
return 0, fmt.Errorf("Error in getting dir size %s: %s", uri, v.context.LastError())
}
return uint64(cfsize), nil
}