Skip to content

Commit

Permalink
[CLIENT-2258] Convert AS_BYTES_PYTHON server type to Python bytearray (
Browse files Browse the repository at this point in the history
…#487)

* Docs: data mapping: add headers
  • Loading branch information
juliannguyen4 authored Aug 16, 2023
1 parent 9d224ba commit 1a12877
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
15 changes: 14 additions & 1 deletion doc/data_mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ Python Data Mappings

.. rubric:: How Python types map to server types

Default Behavior
----------------

By default, the :py:class:`~aerospike.Client` maps the supported Python types to Aerospike server \
`types <https://docs.aerospike.com/server/guide/data-types/overview>`_. \
When an unsupported type is encountered by the module, it does not serialize the type and will throw an error.
When an unsupported type is encountered by the module:

1. When sending data to the server, it does not serialize the type and will throw an error.
2. When reading `AS_BYTES_PYTHON` types from the server, it returns the raw bytes as a :class:`bytearray`.
To deserialize this data, the application must use cPickle instead of relying on the client to do it automatically.

Serializers
-----------

However, the functions :func:`~aerospike.set_serializer` and :func:`~aerospike.set_deserializer` \
allow for user-defined functions to handle serialization.
Expand All @@ -31,6 +41,9 @@ instance-level pair of functions that handle serialization.

All versions before ``6.x`` wrote Python booleans as ``AS_BYTES_PYTHON``.

Data Mappings
-------------

The following table shows which Python types map directly to Aerospike server types.

+---------------------------------+------------------------+
Expand Down
1 change: 1 addition & 0 deletions doc/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ lua
namespace
geospatial
serialized
Serializers
deserialized
Aerospike
aerospike
Expand Down
21 changes: 17 additions & 4 deletions src/main/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,23 @@ extern as_status deserialize_based_on_as_bytes_type(AerospikeClient *self,
as_error *error_p)
{
switch (as_bytes_get_type(bytes)) {
case AS_BYTES_PYTHON:
as_error_update(error_p, AEROSPIKE_ERR,
"Unable to deserialize AS_BYTES_PYTHON bytes");
break;
case AS_BYTES_PYTHON:;
// Automatically convert AS_BYTES_PYTHON server types to bytearrays.
// This prevents the client from throwing an exception and
// breaking applications that don't handle the exception
// in case it still fetches AS_BYTES_PYTHON types stored in the server.
// Applications using this client must deserialize the bytearrays
// manually with cPickle.
uint32_t bval_size = as_bytes_size(bytes);
PyObject *py_val = PyByteArray_FromStringAndSize(
(char *)as_bytes_get(bytes), bval_size);
if (!py_val) {
as_error_update(error_p, AEROSPIKE_ERR_CLIENT,
"Unable to deserialize AS_BYTES_PYTHON bytes");
goto CLEANUP;
}
*retval = py_val;
as_error_update(error_p, AEROSPIKE_OK, NULL);
case AS_BYTES_BLOB: {
if (self->user_deserializer_call_info.callback) {
execute_user_callback(&self->user_deserializer_call_info, &bytes,
Expand Down

0 comments on commit 1a12877

Please sign in to comment.