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

[CLIENT-2077] Add support for reading key ordered maps #512

Merged
merged 12 commits into from
Feb 22, 2024
14 changes: 13 additions & 1 deletion src/main/conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1662,12 +1662,24 @@ as_status map_to_pyobject(AerospikeClient *self, as_error *err,
const as_map *map, PyObject **py_map)
{
*py_map = PyDict_New();

if (!*py_map) {
return as_error_update(err, AEROSPIKE_ERR_CLIENT,
"Failed to allocate memory for dictionary.");
}

// as_orderedmap has flags set to 1
if (map->flags == 1) {
PyObject *key_ordered_dict_class = AerospikeKeyOrderedDict_Get_Type();
PyObject *py_keyordereddict =
PyObject_CallFunctionObjArgs(key_ordered_dict_class, *py_map, NULL);
Py_DECREF(*py_map);
if (py_keyordereddict == NULL) {
return as_error_update(err, AEROSPIKE_ERR_CLIENT,
"Failed to create KeyOrderedDict instance.");
}
*py_map = py_keyordereddict;
}

conversion_data convd = {
.err = err, .count = 0, .client = self, .udata = *py_map};

Expand Down
6 changes: 3 additions & 3 deletions src/main/key_ordered_dict/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ static PyMethodDef AerospikeKeyOrderedDict_Type_Methods[] = {{NULL}};
* PYTHON TYPE HOOKS
******************************************************************************/

static int AerospikeKeyOrderedDict_Type_Init(AerospikeQuery *self,
PyObject *args, PyObject *kwds)
static int AerospikeKeyOrderedDict_Type_Init(PyObject *self, PyObject *args,
PyObject *kwds)
{
return PyDict_Type.tp_init((PyObject *)self, args, kwds);
return PyDict_Type.tp_init(self, args, kwds);
}

/*******************************************************************************
Expand Down
41 changes: 41 additions & 0 deletions test/new_tests/test_get_put_keyordereddict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from aerospike import KeyOrderedDict
import pytest
from aerospike import exception as e


class TestGetPutOrderedDict:
@pytest.fixture(autouse=True)
def setup(self, request, as_connection):
yield
try:
self.as_connection.remove(self.key)
except e.AerospikeError:
pass

@pytest.mark.parametrize(
"bin_value",
[
KeyOrderedDict({"f": 6, "e": 5, "d": 4}),
[
KeyOrderedDict({"f": 6, "e": 5, "d": 4})
]
],
ids=[
"bin-level",
"nested"
]
)
def test_get_put_keyordereddict(self, bin_value):
bins = {
"dict": bin_value
}
self.key = ("test", "demo", 1)
self.as_connection.put(self.key, bins)

_, _, res = self.as_connection.get(self.key)

assert res["dict"] == bin_value
if type(res["dict"]) == list:
assert type(res["dict"][0]) == KeyOrderedDict
else:
assert type(res["dict"]) == KeyOrderedDict
Loading