Skip to content

Commit eb02cef

Browse files
Merge pull request #367 from teo-tsirpanis/new-apis
Add standalone functions for certain operations on arrays and groups.
2 parents e9f5f9f + d8e3964 commit eb02cef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+281
-406
lines changed

array.go

+60-40
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,59 @@ func newArrayFromHandle(tdbCtx *Context, arrayHandle arrayHandle) *Array {
6969
return &Array{context: tdbCtx, tiledbArray: arrayHandle}
7070
}
7171

72+
// ConsolidateArray consolidates the fragments of an array into a single fragment.
73+
// You must first finalize all queries to the array before consolidation can
74+
// begin (as consolidation temporarily acquires an exclusive lock on the array).
75+
func ConsolidateArray(tdbCtx *Context, uri string, config *Config) error {
76+
if config == nil {
77+
return errors.New("Config must not be nil for Consolidate")
78+
}
79+
80+
curi := C.CString(uri)
81+
defer C.free(unsafe.Pointer(curi))
82+
ret := C.tiledb_array_consolidate(tdbCtx.tiledbContext.Get(), curi, config.tiledbConfig.Get())
83+
runtime.KeepAlive(tdbCtx)
84+
runtime.KeepAlive(config)
85+
if ret != C.TILEDB_OK {
86+
return fmt.Errorf("error consolidating tiledb array: %w", tdbCtx.LastError())
87+
}
88+
89+
runtime.KeepAlive(config)
90+
return nil
91+
}
92+
93+
// CreateArray creates a new TileDB array given a context, URI and schema.
94+
func CreateArray(tdbCtx *Context, uri string, arraySchema *ArraySchema) error {
95+
curi := C.CString(uri)
96+
defer C.free(unsafe.Pointer(curi))
97+
ret := C.tiledb_array_create(tdbCtx.tiledbContext.Get(), curi, arraySchema.tiledbArraySchema.Get())
98+
runtime.KeepAlive(tdbCtx)
99+
runtime.KeepAlive(arraySchema)
100+
if ret != C.TILEDB_OK {
101+
return fmt.Errorf("error creating tiledb array: %w", tdbCtx.LastError())
102+
}
103+
return nil
104+
}
105+
106+
// VacuumArray cleans up an array, such as consolidated fragments and array metadata.
107+
func VacuumArray(tdbCtx *Context, uri string, config *Config) error {
108+
if config == nil {
109+
return errors.New("Config must not be nil for Vacuum")
110+
}
111+
112+
curi := C.CString(uri)
113+
defer C.free(unsafe.Pointer(curi))
114+
ret := C.tiledb_array_vacuum(tdbCtx.tiledbContext.Get(), curi, config.tiledbConfig.Get())
115+
runtime.KeepAlive(tdbCtx)
116+
runtime.KeepAlive(config)
117+
if ret != C.TILEDB_OK {
118+
return fmt.Errorf("error vacuuming tiledb array: %w", tdbCtx.LastError())
119+
}
120+
121+
runtime.KeepAlive(config)
122+
return nil
123+
}
124+
72125
// NewArray allocates a new array.
73126
// If the provided Context is nil, a default context is allocated and used.
74127
func NewArray(tdbCtx *Context, uri string) (*Array, error) {
@@ -222,56 +275,23 @@ func (a *Array) Close() error {
222275
}
223276

224277
// Create creates a new TileDB array given an input schema.
278+
// Deprecated: Use CreateArray instead.
225279
func (a *Array) Create(arraySchema *ArraySchema) error {
226-
curi := C.CString(a.uri)
227-
defer C.free(unsafe.Pointer(curi))
228-
ret := C.tiledb_array_create(a.context.tiledbContext.Get(), curi, arraySchema.tiledbArraySchema.Get())
229-
runtime.KeepAlive(a)
230-
runtime.KeepAlive(arraySchema)
231-
if ret != C.TILEDB_OK {
232-
return fmt.Errorf("error creating tiledb array: %w", a.context.LastError())
233-
}
234-
return nil
280+
return CreateArray(a.context, a.uri, arraySchema)
235281
}
236282

237-
// Consolidate consolidates the fragments of an array into a single fragment.
283+
// Consolidate consolidates the fragments of the array into a single fragment.
238284
// You must first finalize all queries to the array before consolidation can
239285
// begin (as consolidation temporarily acquires an exclusive lock on the array).
286+
// Deprecated: Use ConsolidateArray instead.
240287
func (a *Array) Consolidate(config *Config) error {
241-
if config == nil {
242-
return errors.New("Config must not be nil for Consolidate")
243-
}
244-
245-
curi := C.CString(a.uri)
246-
defer C.free(unsafe.Pointer(curi))
247-
ret := C.tiledb_array_consolidate(a.context.tiledbContext.Get(), curi, config.tiledbConfig.Get())
248-
runtime.KeepAlive(a)
249-
runtime.KeepAlive(config)
250-
if ret != C.TILEDB_OK {
251-
return fmt.Errorf("error consolidating tiledb array: %w", a.context.LastError())
252-
}
253-
254-
runtime.KeepAlive(config)
255-
return nil
288+
return ConsolidateArray(a.context, a.uri, config)
256289
}
257290

258291
// Vacuum cleans up the array, such as consolidated fragments and array metadata.
292+
// Deprecated: Use VacuumArray instead.
259293
func (a *Array) Vacuum(config *Config) error {
260-
if config == nil {
261-
return errors.New("Config must not be nil for Vacuum")
262-
}
263-
264-
curi := C.CString(a.uri)
265-
defer C.free(unsafe.Pointer(curi))
266-
ret := C.tiledb_array_vacuum(a.context.tiledbContext.Get(), curi, config.tiledbConfig.Get())
267-
runtime.KeepAlive(a)
268-
runtime.KeepAlive(config)
269-
if ret != C.TILEDB_OK {
270-
return fmt.Errorf("error vacuuming tiledb array: %w", a.context.LastError())
271-
}
272-
273-
runtime.KeepAlive(config)
274-
return nil
294+
return VacuumArray(a.context, a.uri, config)
275295
}
276296

277297
// Schema returns the ArraySchema for the array.

array_experimental_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ func create1DTestArray(t *testing.T) *Array {
5858

5959
// Create array on disk
6060
tmpArrayPath := t.TempDir()
61+
err = CreateArray(context, tmpArrayPath, arraySchema)
62+
require.NoError(t, err)
63+
6164
array, err := NewArray(context, tmpArrayPath)
6265
require.NoError(t, err)
6366
assert.NotNil(t, array)
64-
err = array.Create(arraySchema)
65-
require.NoError(t, err)
66-
6767
return array
6868
}
6969

@@ -192,7 +192,7 @@ func TestConsolidateFragments(t *testing.T) {
192192
require.EqualValues(t, numFrags, fragToVacuumNum)
193193
require.Equal(t, uint32(1), fragInfoNum)
194194

195-
err = array.Vacuum(config)
195+
err = VacuumArray(array.context, array.uri, config)
196196
require.NoError(t, err)
197197

198198
// Check for one fragment after vacuum.

array_schema_evolution_experimental_test.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,7 @@ func TestArraySchemaEvolution(t *testing.T) {
5151
// tmpArrayPath is the array URI
5252
tmpArrayPath := t.TempDir()
5353

54-
array, err := NewArray(context, tmpArrayPath)
55-
require.NoError(t, err)
56-
assert.NotNil(t, array)
57-
58-
require.NoError(t, array.Create(arraySchema))
59-
60-
require.NoError(t, array.Close())
54+
require.NoError(t, CreateArray(context, tmpArrayPath, arraySchema))
6155

6256
arraySchemaEvolution, err := NewArraySchemaEvolution(context)
6357
require.NoError(t, err)
@@ -99,7 +93,7 @@ func TestArraySchemaEvolution(t *testing.T) {
9993
// Prepare the array for reading
10094
arr, err := NewArray(ctx, tmpArrayPath)
10195
require.NoError(t, err)
102-
defer array.Free()
96+
defer arr.Free()
10397

10498
require.NoError(t, arr.Open(TILEDB_READ))
10599

array_test.go

+16-22
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,7 @@ func ExampleNewArray() {
7070
return
7171
}
7272

73-
array, err := NewArray(context, "my_array")
74-
if err != nil {
75-
// Handle error
76-
return
77-
}
78-
79-
err = array.Create(arraySchema)
73+
err = CreateArray(context, "my_array", arraySchema)
8074
if err != nil {
8175
// Handle error
8276
return
@@ -134,16 +128,16 @@ func TestArray(t *testing.T) {
134128

135129
// create temp group name
136130
tmpArrayPath := t.TempDir()
131+
arraySchema := buildArraySchema(context, t)
132+
133+
// Create array on disk
134+
require.NoError(t, CreateArray(context, tmpArrayPath, arraySchema))
135+
137136
// Create new array struct
138137
array, err := NewArray(context, tmpArrayPath)
139138
require.NoError(t, err)
140139
assert.NotNil(t, array)
141140

142-
arraySchema := buildArraySchema(context, t)
143-
144-
// Create array on disk
145-
require.NoError(t, array.Create(arraySchema))
146-
147141
// Get array URI
148142
uri, err := array.URI()
149143
require.NoError(t, err)
@@ -224,17 +218,17 @@ func TestArrayEncryption(t *testing.T) {
224218

225219
// create temp group name
226220
tmpArrayPath := t.TempDir()
227-
// Create new array struct
228-
array, err := NewArray(context, tmpArrayPath)
229-
require.NoError(t, err)
230-
assert.NotNil(t, array)
231-
232221
arraySchema := buildArraySchema(context, t)
233222

234223
// Create array on disk
235-
require.NoError(t, array.Create(arraySchema))
224+
require.NoError(t, CreateArray(context, tmpArrayPath, arraySchema))
236225
assert.Nil(t, err)
237226

227+
// Create new array struct
228+
array, err := NewArray(context, tmpArrayPath)
229+
require.NoError(t, err)
230+
assert.NotNil(t, array)
231+
238232
//err = array.Consolidate()
239233
//require.NoError(t, err)
240234

@@ -573,14 +567,14 @@ func newTestArray(t *testing.T) (*Array, error) {
573567
// create temp group name
574568
tmpArrayPath := t.TempDir()
575569

576-
array, err := NewArray(context, tmpArrayPath)
570+
arraySchema := buildArraySchema(context, t)
571+
// Create array on disk
572+
err = CreateArray(context, tmpArrayPath, arraySchema)
577573
if err != nil {
578574
return nil, err
579575
}
580576

581-
arraySchema := buildArraySchema(context, t)
582-
// Create array on disk
583-
err = array.Create(arraySchema)
577+
array, err := NewArray(context, tmpArrayPath)
584578
if err != nil {
585579
return nil, err
586580
}

dimension_label_experimental_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ func TestDimensionLabelQuery(t *testing.T) {
1515

1616
// create the array
1717
uri := t.TempDir()
18-
array, err := NewArray(tdbCtx, uri)
19-
require.NoError(t, err)
20-
require.NoError(t, array.Create(schema))
18+
require.NoError(t, CreateArray(tdbCtx, uri, schema))
2119

2220
// initialize the labels
21+
array, err := NewArray(tdbCtx, uri)
22+
require.NoError(t, err)
2323
require.NoError(t, array.Open(TILEDB_WRITE))
2424

2525
q, err := NewQuery(tdbCtx, array)
@@ -83,9 +83,7 @@ func TestDimensionLabelSchema(t *testing.T) {
8383

8484
// create the array with the schema and read it back to verify the value
8585
uri := t.TempDir()
86-
memArray, err := NewArray(tdbCtx, uri)
87-
require.NoError(t, err)
88-
require.NoError(t, memArray.Create(schema))
86+
require.NoError(t, CreateArray(tdbCtx, uri, schema))
8987

9088
diskArray, err := NewArray(tdbCtx, uri)
9189
require.NoError(t, err)

enumeration_experimental_test.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ func TestEnumerationAndSchema(t *testing.T) {
126126
require.NoError(t, err)
127127

128128
arrayPath := t.TempDir()
129+
require.NoError(t, CreateArray(tdbCtx, arrayPath, schema))
129130
array, err := NewArray(tdbCtx, arrayPath)
130131
require.NoError(t, err)
131-
require.NoError(t, array.Create(schema))
132132
require.NoError(t, array.Open(TILEDB_READ))
133133
t.Cleanup(func() { array.Close() })
134134

@@ -178,9 +178,7 @@ func TestEnumerationQueryCondition(t *testing.T) {
178178
require.NoError(t, err)
179179

180180
arrayPath := t.TempDir()
181-
array, err := NewArray(tdbCtx, arrayPath)
182-
require.NoError(t, err)
183-
require.NoError(t, array.Create(schema))
181+
require.NoError(t, CreateArray(tdbCtx, arrayPath, schema))
184182

185183
//=====
186184
// write to the array. Each cell gets the row order rank.
@@ -190,7 +188,7 @@ func TestEnumerationQueryCondition(t *testing.T) {
190188
// 8 9 10 11
191189
// 12 13 14 15
192190

193-
array, err = NewArray(tdbCtx, arrayPath)
191+
array, err := NewArray(tdbCtx, arrayPath)
194192
require.NoError(t, err)
195193
require.NoError(t, array.Open(TILEDB_WRITE))
196194
wQuery, err := NewQuery(tdbCtx, array)
@@ -316,9 +314,7 @@ func TestEnumerationEmpty(t *testing.T) {
316314
require.NoError(t, err)
317315

318316
arrayPath := t.TempDir()
319-
array, err := NewArray(tdbCtx, arrayPath)
320-
require.NoError(t, err)
321-
require.NoError(t, array.Create(schema))
317+
require.NoError(t, CreateArray(tdbCtx, arrayPath, schema))
322318
}
323319

324320
func TestEnumerationEvolution(t *testing.T) {
@@ -330,9 +326,7 @@ func TestEnumerationEvolution(t *testing.T) {
330326
require.NoError(t, err)
331327

332328
arrayPath := t.TempDir()
333-
array, err := NewArray(tdbCtx, arrayPath)
334-
require.NoError(t, err)
335-
require.NoError(t, array.Create(schema))
329+
require.NoError(t, CreateArray(tdbCtx, arrayPath, schema))
336330

337331
//=====
338332
// write to the array. Each cell gets the row order rank + 10
@@ -342,7 +336,7 @@ func TestEnumerationEvolution(t *testing.T) {
342336
// 18 19 20 21
343337
// 22 23 24 25
344338

345-
array, err = NewArray(tdbCtx, arrayPath)
339+
array, err := NewArray(tdbCtx, arrayPath)
346340
require.NoError(t, err)
347341
require.NoError(t, array.Open(TILEDB_WRITE))
348342
wQuery, err := NewQuery(tdbCtx, array)

examples_lib/array_metadata.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,8 @@ func createArrayMetadataArray(dir string) {
4545
err = schema.AddAttributes(a)
4646
checkError(err)
4747

48-
// Create the (empty) array on disk.
49-
array, err := tiledb.NewArray(ctx, dir)
50-
checkError(err)
51-
52-
err = array.Create(schema)
48+
err = tiledb.CreateArray(ctx, dir, schema)
5349
checkError(err)
54-
55-
array.Free()
5650
}
5751

5852
func writeArrayMetadata(dir string) {
@@ -165,7 +159,7 @@ func readArrayMetadata(dir string) {
165159
err = config.Set("sm.consolidation.mode", "array_meta")
166160
checkError(err)
167161

168-
err = array.Consolidate(config)
162+
err = tiledb.ConsolidateArray(ctx, dir, config)
169163
checkError(err)
170164

171165
metadataMap, err := array.GetMetadataMap()

examples_lib/dimensions_labels.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func createArrayWithDimensionLabels(uri string) {
3333
checkError(schema.AddAttributes(checkedValue(tiledb.NewAttribute(tdbCtx, "v", tiledb.TILEDB_UINT32))))
3434

3535
// create the array
36-
checkError(checkedValue(tiledb.NewArray(tdbCtx, uri)).Create(schema))
36+
checkError(tiledb.CreateArray(tdbCtx, uri, schema))
3737

3838
// set the dimension labels
3939
array := checkedValue(tiledb.NewArray(tdbCtx, uri))

examples_lib/encryption.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ func createEncryptedArray(dir string) {
6060
checkError(err)
6161

6262
// Create the (empty) encrypted array with AES-256-GCM.
63-
array, err := tiledb.NewArray(ctx, dir)
64-
checkError(err)
65-
defer array.Free()
66-
67-
err = array.Create(schema)
63+
err = tiledb.CreateArray(ctx, dir, schema)
6864
checkError(err)
6965
}
7066

0 commit comments

Comments
 (0)