-
Notifications
You must be signed in to change notification settings - Fork 9
/
object.go
149 lines (122 loc) · 4.18 KB
/
object.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
package tiledb
/*
#include <tiledb/tiledb.h>
#include <stdlib.h>
#include "clibrary.h"
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
pointer "github.com/mattn/go-pointer"
)
// ObjectType returns the object type
// A TileDB "object" is currently either a TileDB array or a TileDB group.
func ObjectType(tdbCtx *Context, path string) (ObjectTypeEnum, error) {
if tdbCtx == nil {
return TILEDB_INVALID, errors.New("error getting object type, context is nil")
}
var objectTypeEnum C.tiledb_object_t
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.tiledb_object_type(tdbCtx.tiledbContext, cpath, &objectTypeEnum)
if ret != C.TILEDB_OK {
return TILEDB_INVALID, fmt.Errorf("cannot get object type from path %s: %w",
path, tdbCtx.LastError())
}
return ObjectTypeEnum(objectTypeEnum), nil
}
type groupDefinition struct {
objectTypeEnum ObjectTypeEnum
path string
}
// ObjectList defines the value of data returned by object iteration callback
type ObjectList struct {
objectList []groupDefinition
}
//export objectsInPath
func objectsInPath(path *C.cchar_t, objectTypeEnum C.tiledb_object_t, data unsafe.Pointer) int32 {
objectData := pointer.Restore(data).(*ObjectList)
groupDefinition := groupDefinition{
objectTypeEnum: ObjectTypeEnum(objectTypeEnum),
path: C.GoString(path),
}
objectData.objectList = append(objectData.objectList, groupDefinition)
return 1
}
// ObjectWalk (iterates) over the TileDB objects contained in *path*. The traversal
// is done recursively in the order defined by the user. The user provides
// a callback function which is applied on each of the visited TileDB objects.
// The iteration continues for as long the callback returns non-zero, and stops
// when the callback returns 0. Note that this function ignores any object
// (e.g., file or directory) that is not TileDB-related.
func ObjectWalk(tdbCtx *Context, path string, walkOrder WalkOrder) (*ObjectList, error) {
if tdbCtx == nil {
return nil, errors.New("error walking object, context is nil")
}
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
objectList := ObjectList{
objectList: []groupDefinition{},
}
data := pointer.Save(&objectList)
ret := C._tiledb_object_walk(tdbCtx.tiledbContext, cpath,
C.tiledb_walk_order_t(walkOrder), unsafe.Pointer(data))
fmt.Println(objectList)
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("cannot walk in path %s: %w", path,
tdbCtx.LastError())
}
return &objectList, nil
}
// ObjectLs is similar to `tiledb_walk`, but now the function visits only the children
// of `path` (it does not recursively continue to the children directories).
func ObjectLs(tdbCtx *Context, path string) (*ObjectList, error) {
if tdbCtx == nil {
return nil, errors.New("error listing object, context is nil")
}
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
objectList := ObjectList{
objectList: []groupDefinition{},
}
data := pointer.Save(&objectList)
ret := C._tiledb_object_ls(tdbCtx.tiledbContext, cpath,
unsafe.Pointer(data))
if ret != C.TILEDB_OK {
return nil, fmt.Errorf("cannot walk in path %s: %w", path,
tdbCtx.LastError())
}
return &objectList, nil
}
// ObjectMove moves a TileDB resource (group, array, key-value).
// Param path is the new path to move to
func ObjectMove(tdbCtx *Context, path string, newPath string) error {
if tdbCtx == nil {
return errors.New("error moving object, context is nil")
}
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
cnewPath := C.CString(newPath)
defer C.free(unsafe.Pointer(cnewPath))
ret := C.tiledb_object_move(tdbCtx.tiledbContext, cpath, cnewPath)
if ret != C.TILEDB_OK {
return fmt.Errorf("cannot move object from %s to %s: %w", path,
newPath, tdbCtx.LastError())
}
return nil
}
// ObjectRemove deletes a TileDB resource (group, array, key-value).
func ObjectRemove(tdbCtx *Context, path string) error {
if tdbCtx == nil {
return errors.New("error removing object, context is nil")
}
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.tiledb_object_remove(tdbCtx.tiledbContext, cpath)
if ret != C.TILEDB_OK {
return fmt.Errorf("cannot delete object %s: %w", path, tdbCtx.LastError())
}
return nil
}