-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds deserializer support and refactor Java handler code.
- Loading branch information
1 parent
d74a428
commit 2630502
Showing
11 changed files
with
311 additions
and
120 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
native-schema-registry/c/include/glue_schema_registry_deserializer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef GLUE_SCHEMA_REGISTRY_DESERIALIZER_H | ||
#define GLUE_SCHEMA_REGISTRY_DESERIALIZER_H | ||
|
||
#include "glue_schema_registry_schema.h" | ||
#include "mutable_byte_array.h" | ||
#include "read_only_byte_array.h" | ||
#include <stdbool.h> | ||
|
||
typedef struct glue_schema_registry_deserializer { | ||
//This is used for storing the instance context. Currently, being used for managing GraalVM instance. | ||
void *instance_context; | ||
} glue_schema_registry_deserializer; | ||
|
||
glue_schema_registry_deserializer *new_glue_schema_registry_deserializer(void); | ||
|
||
void delete_glue_schema_registry_deserializer(glue_schema_registry_deserializer *deserializer); | ||
|
||
mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer *deserializer, | ||
read_only_byte_array *array); | ||
|
||
glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer *deserializer, | ||
read_only_byte_array *array); | ||
|
||
bool glue_schema_registry_deserializer_can_decode(glue_schema_registry_deserializer *deserializer, | ||
read_only_byte_array *array); | ||
|
||
#endif //GLUE_SCHEMA_REGISTRY_DESERIALIZER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
native-schema-registry/c/src/glue_schema_registry_deserializer.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "../include/glue_schema_registry_deserializer.h" | ||
#include "../include/error_handling.h" | ||
#include "../../target/libnativeschemaregistry.h" | ||
#include <stdlib.h> | ||
|
||
glue_schema_registry_deserializer * new_glue_schema_registry_deserializer() { | ||
glue_schema_registry_deserializer *deserializer = NULL; | ||
deserializer = | ||
(glue_schema_registry_deserializer *) malloc(sizeof(glue_schema_registry_deserializer)); | ||
|
||
int ret = graal_create_isolate(NULL, NULL, (graal_isolatethread_t **) &deserializer->instance_context); | ||
if (ret != 0) { | ||
log_error("Failed to initialize GraalVM isolate.", ERR_CODE_GRAALVM_INIT_EXCEPTION); | ||
delete_glue_schema_registry_deserializer(deserializer); | ||
return NULL; | ||
} | ||
//TODO: Handle errors here. | ||
initialize_deserializer(deserializer->instance_context); | ||
return deserializer; | ||
} | ||
|
||
void delete_glue_schema_registry_deserializer(glue_schema_registry_deserializer * deserializer) { | ||
if (deserializer == NULL) { | ||
log_error("Deserializer is NULL", ERR_CODE_NULL_PARAMETERS); | ||
return; | ||
} | ||
if (deserializer->instance_context != NULL) { | ||
int ret = graal_tear_down_isolate(deserializer->instance_context); | ||
if (ret != 0) { | ||
log_error("Error tearing down the graal isolate instance.", ERR_CODE_GRAALVM_TEARDOWN_EXCEPTION); | ||
} | ||
deserializer->instance_context = NULL; | ||
} | ||
|
||
free(deserializer); | ||
} | ||
|
||
mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer * deserializer, read_only_byte_array *array) { | ||
if (deserializer == NULL || deserializer->instance_context == NULL) { | ||
log_error("Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE); | ||
return NULL; | ||
} | ||
|
||
if (array == NULL || array->len == 0) { | ||
log_error("Byte array cannot be null", ERR_CODE_NULL_PARAMETERS); | ||
return NULL; | ||
} | ||
|
||
return decode(deserializer->instance_context, array); | ||
} | ||
|
||
glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer * deserializer, read_only_byte_array *array) { | ||
if (deserializer == NULL || deserializer->instance_context == NULL) { | ||
log_error("Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE); | ||
return NULL; | ||
} | ||
|
||
if (array == NULL || array->len == 0) { | ||
log_error("Byte array cannot be null", ERR_CODE_NULL_PARAMETERS); | ||
return NULL; | ||
} | ||
|
||
glue_schema_registry_schema * schema = decode_schema(deserializer->instance_context, array); | ||
return schema; | ||
} | ||
|
||
bool glue_schema_registry_deserializer_can_decode(glue_schema_registry_deserializer * deserializer, read_only_byte_array *array) { | ||
if (deserializer == NULL || deserializer->instance_context == NULL) { | ||
log_error("Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE); | ||
return NULL; | ||
} | ||
|
||
if (array == NULL || array->len == 0) { | ||
log_error("Byte array cannot be null", ERR_CODE_NULL_PARAMETERS); | ||
return NULL; | ||
} | ||
|
||
return can_decode(deserializer->instance_context, array); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .PyGsrSerDe import * | ||
|
||
__all__ = ['GlueSchemaRegistrySchema', 'GlueSchemaRegistrySerializer'] | ||
__all__ = ['GlueSchemaRegistrySchema', 'GlueSchemaRegistrySerializer', 'GlueSchemaRegistryDeserializer'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
#!/bin/zsh | ||
python_version=$1 | ||
python_cmd="python$python_version" | ||
#TODO: Simple script to test Python bindings, upgrade this to multi-platform compatible build. | ||
rm -rf build/ dist/ wheelhouse/ PyGsrSerDe.egg-info/ | ||
ln -sf $(PWD)/../target $(PWD)/../python/deps | ||
ln -sf $(PWD)/../c $(PWD)/../python/c | ||
python3.9 -m pip wheel -w dist --verbose . | ||
ln -sf $PWD/../target $PWD/../python/deps | ||
ln -sf $PWD/../c $PWD/../python/c | ||
$python_cmd -m pip wheel -w dist --verbose . | ||
cp c/src/swig/GsrSerDe.py ./PyGsrSerDe/ | ||
sed -ie "s/c.src.swig/./" PyGsrSerDe/GsrSerDe.py | ||
$python_cmd -m pip wheel -w dist --verbose . | ||
|
||
LD_LIBRARY_PATH=LD_LIBRARY_PATH:$(PWD)/ && auditwheel repair dist/*.whl --plat 'manylinux2014_x86_64' | ||
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:$PWD/ && auditwheel repair dist/*.whl --plat 'manylinux2014_x86_64' | ||
|
||
python3.9 -m pip install wheelhouse/*.whl --force-reinstall | ||
cd ../ | ||
python3.9 | ||
$python_cmd -m pip install wheelhouse/*.whl --force-reinstall |
41 changes: 41 additions & 0 deletions
41
...hema-registry/src/main/java/com/amazonaws/services/schemaregistry/ByteArrayConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.amazonaws.services.schemaregistry; | ||
|
||
import org.graalvm.nativeimage.c.type.CTypeConversion; | ||
import org.graalvm.word.PointerBase; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import static com.amazonaws.services.schemaregistry.DataTypes.C_MutableByteArray; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.C_ReadOnlyByteArray; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.newMutableByteArray; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.writeToMutableArray; | ||
|
||
/** | ||
* Converts Java Byte arrays to/fro C byte arrays. | ||
*/ | ||
public class ByteArrayConverter { | ||
public static byte[] fromCReadOnlyByteArray(C_ReadOnlyByteArray c_readOnlyByteArray) { | ||
PointerBase cData = c_readOnlyByteArray.getData(); | ||
//This is validated to fit in Integer limits. | ||
int cDataLen = Math.toIntExact(c_readOnlyByteArray.getLen()); | ||
|
||
//Copy the bytebuffer to a byte []. | ||
//TODO: This won't be needed if Java APIs accepted ByteBuffer instead of byte[] | ||
ByteBuffer javaByteBuffer = CTypeConversion.asByteBuffer(cData, cDataLen); | ||
byte[] bytes = new byte[cDataLen]; | ||
javaByteBuffer.get(bytes); | ||
|
||
return bytes; | ||
} | ||
|
||
public static C_MutableByteArray toCMutableByteArray(byte[] bytes) { | ||
int len = bytes.length; | ||
C_MutableByteArray mutableByteArray = newMutableByteArray(len); | ||
|
||
//TODO: Check for performance issues with this. | ||
for (int index = 0; index < len; index++) { | ||
writeToMutableArray(mutableByteArray, index, bytes[index]); | ||
} | ||
return mutableByteArray; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
.../java/com/amazonaws/services/schemaregistry/GlueSchemaRegistryDeserializationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.amazonaws.services.schemaregistry; | ||
|
||
import com.amazonaws.services.schemaregistry.common.Schema; | ||
import com.amazonaws.services.schemaregistry.common.configs.GlueSchemaRegistryConfiguration; | ||
import com.amazonaws.services.schemaregistry.deserializers.GlueSchemaRegistryDeserializer; | ||
import com.amazonaws.services.schemaregistry.utils.AWSSchemaRegistryConstants; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.graalvm.nativeimage.IsolateThread; | ||
import org.graalvm.nativeimage.c.CContext; | ||
import org.graalvm.nativeimage.c.function.CEntryPoint; | ||
import org.graalvm.nativeimage.c.type.CTypeConversion; | ||
|
||
import java.util.Map; | ||
|
||
import static com.amazonaws.services.schemaregistry.ByteArrayConverter.fromCReadOnlyByteArray; | ||
import static com.amazonaws.services.schemaregistry.ByteArrayConverter.toCMutableByteArray; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.C_GlueSchemaRegistrySchema; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.C_MutableByteArray; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.C_ReadOnlyByteArray; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.HandlerDirectives; | ||
import static com.amazonaws.services.schemaregistry.DataTypes.newGlueSchemaRegistrySchema; | ||
|
||
/** | ||
* Entry point class for the serialization methods of GSR shared library. | ||
*/ | ||
@CContext(HandlerDirectives.class) | ||
public class GlueSchemaRegistryDeserializationHandler { | ||
|
||
@CEntryPoint(name = "initialize_deserializer") | ||
public static void initializeDeserializer(IsolateThread isolateThread) { | ||
//TODO: Add GlueSchemaRegistryConfiguration to this method. This is hard-coded for now. | ||
//TODO: Error handling | ||
Map<String, String> configMap = | ||
ImmutableMap.of( | ||
AWSSchemaRegistryConstants.AWS_REGION, | ||
"us-east-1" | ||
); | ||
GlueSchemaRegistryConfiguration glueSchemaRegistryConfiguration = | ||
new GlueSchemaRegistryConfiguration(configMap); | ||
|
||
DeserializerInstance.create(glueSchemaRegistryConfiguration); | ||
} | ||
|
||
@CEntryPoint(name = "decode") | ||
public static C_MutableByteArray decode( | ||
IsolateThread isolateThread, C_ReadOnlyByteArray c_readOnlyByteArray) { | ||
|
||
byte[] bytesToDecode = fromCReadOnlyByteArray(c_readOnlyByteArray); | ||
|
||
//Assuming deserializer instance is already initialized | ||
GlueSchemaRegistryDeserializer glueSchemaRegistryDeserializer = DeserializerInstance.get(); | ||
|
||
byte[] decodedBytes = | ||
glueSchemaRegistryDeserializer.getData(bytesToDecode); | ||
|
||
return toCMutableByteArray(decodedBytes); | ||
} | ||
|
||
@CEntryPoint(name = "decode_schema") | ||
public static C_GlueSchemaRegistrySchema decodeSchema( | ||
IsolateThread isolateThread, C_ReadOnlyByteArray c_readOnlyByteArray) { | ||
byte[] bytesToDecode = fromCReadOnlyByteArray(c_readOnlyByteArray); | ||
|
||
//Assuming serializer instance is already initialized | ||
GlueSchemaRegistryDeserializer glueSchemaRegistryDeserializer = DeserializerInstance.get(); | ||
Schema decodedSchema = | ||
glueSchemaRegistryDeserializer.getSchema(bytesToDecode); | ||
|
||
CTypeConversion.CCharPointerHolder cSchemaNamePointer = | ||
CTypeConversion.toCString(decodedSchema.getSchemaName()); | ||
CTypeConversion.CCharPointerHolder cSchemaDefPointer = | ||
CTypeConversion.toCString(decodedSchema.getSchemaDefinition()); | ||
CTypeConversion.CCharPointerHolder cDataFormatPointer = | ||
CTypeConversion.toCString(decodedSchema.getDataFormat()); | ||
|
||
//TODO: We can potentially expose the C Strings to target language layer to | ||
//prevent copying strings repeatedly. | ||
C_GlueSchemaRegistrySchema c_glueSchemaRegistrySchema = newGlueSchemaRegistrySchema( | ||
cSchemaNamePointer.get(), | ||
cSchemaDefPointer.get(), | ||
cDataFormatPointer.get() | ||
); | ||
//newGlueSchemaRegistrySchema has it's own copy of these attributes. | ||
cDataFormatPointer.close(); | ||
cSchemaDefPointer.close(); | ||
cSchemaNamePointer.close(); | ||
|
||
return c_glueSchemaRegistrySchema; | ||
} | ||
|
||
@CEntryPoint(name = "can_decode") | ||
public static byte canDecode( | ||
IsolateThread isolateThread, C_ReadOnlyByteArray c_readOnlyByteArray) { | ||
byte[] bytesToDecode = fromCReadOnlyByteArray(c_readOnlyByteArray); | ||
|
||
GlueSchemaRegistryDeserializer glueSchemaRegistryDeserializer = DeserializerInstance.get(); | ||
boolean canDeserialize = | ||
glueSchemaRegistryDeserializer.canDeserialize(bytesToDecode); | ||
|
||
return CTypeConversion.toCBoolean(canDeserialize); | ||
} | ||
} |
Oops, something went wrong.