From fcfc6ff914a9f9c821c5aced72037dd9b9799e0b Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Mon, 2 Dec 2024 18:11:31 +0200 Subject: [PATCH] Move all serialization functions to `serialize.go`. --- group.go | 48 ------------ serialize.go | 109 ++++++++++++++++++++++++++++ serialize_array_schema_evolution.go | 75 ------------------- 3 files changed, 109 insertions(+), 123 deletions(-) delete mode 100644 serialize_array_schema_evolution.go diff --git a/group.go b/group.go index 74fb370..87cfff1 100644 --- a/group.go +++ b/group.go @@ -8,10 +8,6 @@ import ( ) /* - - - #include - #include #include */ import "C" @@ -436,50 +432,6 @@ func (g *Group) Dump(recurse bool) (string, error) { return C.GoString(cOutput), nil } -// SerializeGroupMetadata gets and serializes the group metadata and returns a Buffer object containing the payload -func SerializeGroupMetadataToBuffer(g *Group, serializationType SerializationType) (*Buffer, error) { - buffer := Buffer{context: g.context} - freeOnGC(&buffer) - - ret := C.tiledb_serialize_group_metadata(g.context.tiledbContext, g.group, C.tiledb_serialization_type_t(serializationType), &buffer.tiledbBuffer) - if ret != C.TILEDB_OK { - return nil, fmt.Errorf("Error serializing group metadata: %s", g.context.LastError()) - } - - return &buffer, nil -} - -// SerializeGroupMetadata gets and serializes the group metadata -// -// Deprecated: Use SerializeGroupMetadataToBuffer instead. -func SerializeGroupMetadata(g *Group, serializationType SerializationType) ([]byte, error) { - buffer, err := SerializeGroupMetadataToBuffer(g, serializationType) - if err != nil { - return nil, err - } - - return buffer.Serialize(serializationType) -} - -// DeserializeGroupMetadata deserializes group metadata -func DeserializeGroupMetadata(g *Group, buffer *Buffer, serializationType SerializationType) error { - b, err := buffer.dataCopy() - if err != nil { - return errors.New("failed to retrieve bytes from buffer") - } - // cstrings are null terminated. Go's are not, add it as a suffix - if err := buffer.SetBuffer(append(b, []byte("\u0000")...)); err != nil { - return errors.New("failed to add null terminator to buffer") - } - - ret := C.tiledb_deserialize_group_metadata(g.context.tiledbContext, g.group, C.tiledb_serialization_type_t(serializationType), buffer.tiledbBuffer) - if ret != C.TILEDB_OK { - return fmt.Errorf("Error deserializing group metadata: %s", g.context.LastError()) - } - - return nil -} - // GetIsRelativeURIByName returns whether a named member of the group has a uri relative to the group func (g *Group) GetIsRelativeURIByName(name string) (bool, error) { cName := C.CString(name) diff --git a/serialize.go b/serialize.go index 3eac671..84ecfd0 100644 --- a/serialize.go +++ b/serialize.go @@ -9,6 +9,7 @@ package tiledb import "C" import ( + "errors" "fmt" "runtime" "unsafe" @@ -70,6 +71,70 @@ func DeserializeArraySchema(buffer *Buffer, serializationType SerializationType, return &schema, nil } +// SerializeArraySchemaEvolution serializes the given array schema evolution and serializes the group metadata and returns a Buffer object containing the payload. +func SerializeArraySchemaEvolutionToBuffer(arraySchemaEvolution *ArraySchemaEvolution, serializationType SerializationType, clientSide bool) (*Buffer, error) { + var cClientSide C.int32_t + if clientSide { + cClientSide = 1 + } else { + cClientSide = 0 + } + + buffer := Buffer{context: arraySchemaEvolution.context} + freeOnGC(&buffer) + + ret := C.tiledb_serialize_array_schema_evolution( + arraySchemaEvolution.context.tiledbContext, + arraySchemaEvolution.tiledbArraySchemaEvolution, + C.tiledb_serialization_type_t(serializationType), + cClientSide, &buffer.tiledbBuffer) + if ret != C.TILEDB_OK { + return nil, fmt.Errorf("Error serializing array schem evolution: %s", + arraySchemaEvolution.context.LastError()) + } + + return &buffer, nil +} + +// SerializeArraySchemaEvolution serializes the given array schema evolution. +// +// Deprecated: Use SerializeArraySchemaEvolutionToBuffer instead. +func SerializeArraySchemaEvolution(arraySchemaEvolution *ArraySchemaEvolution, serializationType SerializationType, clientSide bool) ([]byte, error) { + buffer, err := SerializeArraySchemaEvolutionToBuffer(arraySchemaEvolution, serializationType, clientSide) + if err != nil { + return nil, err + } + + return buffer.Serialize(serializationType) +} + +// DeserializeArraySchemaEvolution deserializes a new array schema evolution object from the given buffer. +func DeserializeArraySchemaEvolution(buffer *Buffer, serializationType SerializationType, clientSide bool) (*ArraySchemaEvolution, error) { + arraySchemaEvolution := ArraySchemaEvolution{context: buffer.context} + + var cClientSide C.int32_t + if clientSide { + cClientSide = 1 + } else { + cClientSide = 0 + } + + ret := C.tiledb_deserialize_array_schema_evolution( + arraySchemaEvolution.context.tiledbContext, buffer.tiledbBuffer, + C.tiledb_serialization_type_t(serializationType), + cClientSide, &arraySchemaEvolution.tiledbArraySchemaEvolution) + if ret != C.TILEDB_OK { + return nil, fmt.Errorf("Error deserializing array schema evolution: %s", arraySchemaEvolution.context.LastError()) + } + + // This needs to happen *after* the tiledb_deserialize_array_schema_evolution + // call because that may leave the schemaEvolution with a non-nil pointer + // to already-freed memory. + freeOnGC(&arraySchemaEvolution) + + return &arraySchemaEvolution, nil +} + // SerializeArrayNonEmptyDomainToBuffer gets and serializes the array nonempty domain and returns a Buffer object containing the payload. func SerializeArrayNonEmptyDomainToBuffer(a *Array, serializationType SerializationType) (*Buffer, error) { schema, err := a.Schema() @@ -560,6 +625,50 @@ func DeserializeQueryAndArray(context *Context, buffer *Buffer, serializationTyp return array, query, nil } +// SerializeGroupMetadata gets and serializes the group metadata and returns a Buffer object containing the payload +func SerializeGroupMetadataToBuffer(g *Group, serializationType SerializationType) (*Buffer, error) { + buffer := Buffer{context: g.context} + freeOnGC(&buffer) + + ret := C.tiledb_serialize_group_metadata(g.context.tiledbContext, g.group, C.tiledb_serialization_type_t(serializationType), &buffer.tiledbBuffer) + if ret != C.TILEDB_OK { + return nil, fmt.Errorf("Error serializing group metadata: %s", g.context.LastError()) + } + + return &buffer, nil +} + +// SerializeGroupMetadata gets and serializes the group metadata +// +// Deprecated: Use SerializeGroupMetadataToBuffer instead. +func SerializeGroupMetadata(g *Group, serializationType SerializationType) ([]byte, error) { + buffer, err := SerializeGroupMetadataToBuffer(g, serializationType) + if err != nil { + return nil, err + } + + return buffer.Serialize(serializationType) +} + +// DeserializeGroupMetadata deserializes group metadata +func DeserializeGroupMetadata(g *Group, buffer *Buffer, serializationType SerializationType) error { + b, err := buffer.dataCopy() + if err != nil { + return errors.New("failed to retrieve bytes from buffer") + } + // cstrings are null terminated. Go's are not, add it as a suffix + if err := buffer.SetBuffer(append(b, []byte("\u0000")...)); err != nil { + return errors.New("failed to add null terminator to buffer") + } + + ret := C.tiledb_deserialize_group_metadata(g.context.tiledbContext, g.group, C.tiledb_serialization_type_t(serializationType), buffer.tiledbBuffer) + if ret != C.TILEDB_OK { + return fmt.Errorf("Error deserializing group metadata: %s", g.context.LastError()) + } + + return nil +} + // HandleLoadArraySchemaRequest Passes the array and serialized LoadArraySchemaRequest to core which returns the // serialized LoadArraySchemaResponse. The request contains a TileDB Config used to load the schema, the response // contains the latest array schema loaded and a map of all array schemas. diff --git a/serialize_array_schema_evolution.go b/serialize_array_schema_evolution.go deleted file mode 100644 index a864e5b..0000000 --- a/serialize_array_schema_evolution.go +++ /dev/null @@ -1,75 +0,0 @@ -package tiledb - -/* -#include -#include -*/ -import "C" - -import ( - "fmt" -) - -// SerializeArraySchemaEvolution serializes the given array schema evolution and serializes the group metadata and returns a Buffer object containing the payload. -func SerializeArraySchemaEvolutionToBuffer(arraySchemaEvolution *ArraySchemaEvolution, serializationType SerializationType, clientSide bool) (*Buffer, error) { - var cClientSide C.int32_t - if clientSide { - cClientSide = 1 - } else { - cClientSide = 0 - } - - buffer := Buffer{context: arraySchemaEvolution.context} - freeOnGC(&buffer) - - ret := C.tiledb_serialize_array_schema_evolution( - arraySchemaEvolution.context.tiledbContext, - arraySchemaEvolution.tiledbArraySchemaEvolution, - C.tiledb_serialization_type_t(serializationType), - cClientSide, &buffer.tiledbBuffer) - if ret != C.TILEDB_OK { - return nil, fmt.Errorf("Error serializing array schem evolution: %s", - arraySchemaEvolution.context.LastError()) - } - - return &buffer, nil -} - -// SerializeArraySchemaEvolution serializes the given array schema evolution. -// -// Deprecated: Use SerializeArraySchemaEvolutionToBuffer instead. -func SerializeArraySchemaEvolution(arraySchemaEvolution *ArraySchemaEvolution, serializationType SerializationType, clientSide bool) ([]byte, error) { - buffer, err := SerializeArraySchemaEvolutionToBuffer(arraySchemaEvolution, serializationType, clientSide) - if err != nil { - return nil, err - } - - return buffer.Serialize(serializationType) -} - -// DeserializeArraySchemaEvolution deserializes a new array schema evolution object from the given buffer. -func DeserializeArraySchemaEvolution(buffer *Buffer, serializationType SerializationType, clientSide bool) (*ArraySchemaEvolution, error) { - arraySchemaEvolution := ArraySchemaEvolution{context: buffer.context} - - var cClientSide C.int32_t - if clientSide { - cClientSide = 1 - } else { - cClientSide = 0 - } - - ret := C.tiledb_deserialize_array_schema_evolution( - arraySchemaEvolution.context.tiledbContext, buffer.tiledbBuffer, - C.tiledb_serialization_type_t(serializationType), - cClientSide, &arraySchemaEvolution.tiledbArraySchemaEvolution) - if ret != C.TILEDB_OK { - return nil, fmt.Errorf("Error deserializing array schema evolution: %s", arraySchemaEvolution.context.LastError()) - } - - // This needs to happen *after* the tiledb_deserialize_array_schema_evolution - // call because that may leave the schemaEvolution with a non-nil pointer - // to already-freed memory. - freeOnGC(&arraySchemaEvolution) - - return &arraySchemaEvolution, nil -}