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

Add enumerations #269

Merged
merged 4 commits into from
Sep 19, 2023
Merged

Add enumerations #269

merged 4 commits into from
Sep 19, 2023

Conversation

anastasop
Copy link
Contributor

@anastasop anastasop commented Sep 4, 2023

The TileDB enumerations PR can be found at TileDB-Inc/TileDB#4051

A usage example can be found at https://github.com/TileDB-Inc/TileDB/blob/dev/examples/cpp_api/enumerations.cc

@anastasop anastasop marked this pull request as draft September 4, 2023 13:32
@anastasop anastasop force-pushed the add-enumerations branch 3 times, most recently from 0af2936 to 9bad1c8 Compare September 5, 2023 13:02
enumeration.go Outdated
}

// NewEnumeration creates an enumeration with name and ordered or not values.
func NewEnumeration[T EnumerationType](tdbCtx *Context, name string, ordered bool, values []T) (*Enumeration, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of boolean parameters, since it's not clear to the caller what the true in tiledb.NewEnumeration(someCtx, "myEnum", true, ...) might mean. It might be better to either split this into a NewEnumeration/NewOrderedEnumeration pair, or make the ordered parameter its own type so you can say tiledb.NewEnumeration(someCtx, "myEnum", tiledb.EnumOrdered, ...) or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chose 2 methods

Comment on lines +95 to +110
cData = reflect.ValueOf(data).UnsafePointer()
cDataLen = C.uint64_t(dataSize)
cOffsets = reflect.ValueOf(offsets).UnsafePointer()
cOffsetsLen = C.uint64_t(len(values) * int(reflect.TypeOf(uint64(0)).Size()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since data and offsets are no longer referred to after these, they need a defer runtime.KeepAlive(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

cMode := C.CString("w")
defer C.free(unsafe.Pointer(cMode))

cFile := C.fopen(cPath, cMode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you but might it be better to use file, err := os.Create(path); defer file.Close(); C.tiledb_enumeration_dump(..., file.Fd()) (or whatever the function to get the fd number from a File is)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the API requires a C.FILE not a file descriptor. You cannot get this from os package

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I actually didn’t know that a FILE * was not the same as a fd until just now! Thanks for the learning opportunity.

enumeration.go Outdated
if typ != TILEDB_STRING_ASCII {
switch typ {
case TILEDB_BOOL:
return unsafe.Slice((*bool)(cData), cDataSize), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is pointing to data inside of the Enumeration. So if I call

vals, _ := myEnum.Values()
// ... time passes, myEnum is GC’d
thing = vals[whatever]

so vals is now unsafe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make copies for safety

Copy link
Contributor

@thetorpedodog thetorpedodog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course awaiting the release to make sure it works for real, but otherwise this all looks good!

@anastasop anastasop marked this pull request as ready for review September 8, 2023 11:15
@anastasop
Copy link
Contributor Author

@thetorpedodog Setup github actions for 2.17.0-rc0 and works. Merge is pending until we test with a 2.17 release

Spyros Anastasopoulos added 4 commits September 14, 2023 17:14
The TileDB core API return pointer to values which is in unsafe memory
and also it can be changed by the user. This commit adds safety by
copying the values to managed memory.
@anastasop
Copy link
Contributor Author

@thetorpedodog Rebased to use TileDB-2.17.0 release

@thetorpedodog
Copy link
Contributor

still looks good

@anastasop anastasop merged commit 15715f8 into master Sep 19, 2023
11 checks passed
@ihnorton ihnorton deleted the add-enumerations branch October 16, 2023 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants