Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create experimental and non-experimental makeContext functions. #225

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package tiledb
#cgo LDFLAGS: -ltiledb
#cgo linux LDFLAGS: -ldl
#include <tiledb/tiledb.h>
#include <tiledb/tiledb_experimental.h>
#include <stdlib.h>
*/
import "C"
Expand All @@ -26,28 +27,20 @@ type Context struct {
// 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 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())
context, err := makeContext(config)
if err != nil {
return nil, err
}

// Set finalizer for free C pointer on gc
runtime.SetFinalizer(&context, func(context *Context) {
context.Free()
})

err := context.setDefaultTags()
if err != nil {
if err := context.setDefaultTags(); err != nil {
return nil, fmt.Errorf("error creating tiledb context: %w", err)
}

return &context, nil
return context, nil
}

// NewContextFromMap creates a TileDB context with the given configuration.
Expand Down
30 changes: 30 additions & 0 deletions context_new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build !experimental

package tiledb

/*
#cgo LDFLAGS: -ltiledb
#cgo linux LDFLAGS: -ldl
#include <tiledb/tiledb.h>
#include <stdlib.h>
*/
import "C"
import "fmt"

// makeContext wraps the internals of context making. It is separated out
// so the functions which use experimental APIs are separate from those
// that do not.
func makeContext(config *Config) (*Context, error) {
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())
}
return context, nil
}
39 changes: 39 additions & 0 deletions context_new_experimental.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build experimental

package tiledb

/*
#cgo LDFLAGS: -ltiledb
#cgo linux LDFLAGS: -ldl
#include <tiledb/tiledb.h>
#include <tiledb/tiledb_experimental.h>
#include <stdlib.h>
*/
import "C"
import "fmt"

func makeContext(config *Config) (*Context, error) {
context := &Context{}
var ret C.int32_t
var tdbErr *C.tiledb_error_t
if config != nil {
ret = C.tiledb_ctx_alloc_with_error(config.tiledbConfig, &context.tiledbContext, &tdbErr)
} else {
ret = C.tiledb_ctx_alloc_with_error(nil, &context.tiledbContext, &tdbErr)
}
if ret != C.TILEDB_OK {
// If the error isn't null report this
if tdbErr != nil {
var msg *C.char
C.tiledb_error_message(tdbErr, &msg)
defer C.tiledb_error_free(&tdbErr)
return nil, fmt.Errorf("error creating tiledb context: %s", C.GoString(msg))
}
// If the context is not null see if the error exists there
if context.tiledbContext != nil {
return nil, fmt.Errorf("error creating tiledb context: %w", context.LastError())
}
return nil, fmt.Errorf("error creating tiledb context: unknown error")
}
return context, nil
}