diff --git a/src/include/conversions.h b/src/include/conversions.h index 91504bf629..c979ae9637 100644 --- a/src/include/conversions.h +++ b/src/include/conversions.h @@ -167,9 +167,36 @@ as_status string_and_pyuni_from_pystring(PyObject *py_string, PyObject **pyuni_r, char **c_str_ptr, as_error *err); -as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, - PyObject *op_dict, bool *ctx_in_use, - as_static_pool *static_pool, int serializer_type); +// This takes in a Python "list of contexts" argument from an API, +// and returns the as_cdt_ctx* value to be passed to the C client API +// +// This assumes that the C client API's ctx parameter is optional and can accept a NULL value +// If this call succeeds and returns a non-NULL as_cdt_ctx reference, the user is responsible for destroying it. +// Sets err on error. +// The API calls that take in a Python list of contexts doesn't check the parameter's type, so we check it here. +as_cdt_ctx *as_cdt_ctx_create_from_pyobject(AerospikeClient *self, + as_error *err, + PyObject *py_ctx_list, + as_static_pool *static_pool, + int serializer_type); + +// See docstring for as_cdt_ctx_create_from_pyobject +// +// The cdt_ctx argument *must* point to an uninitialized as_cdt_ctx object in order to initialize it. +// This method is only used for stack allocated as_cdt_ctx objects where the C client API only needs to use it once. +as_cdt_ctx *as_cdt_ctx_init_from_pyobject(AerospikeClient *self, as_error *err, + as_cdt_ctx *cdt_ctx, + PyObject *py_ctx_list, + as_static_pool *static_pool, + int serializer_type); + +// This takes in a Python operations dictionary created from aerospike_helpers.operations, +// and returns the as_cdt_ctx* value to be passed to the C client API that adds operations +// +// See docstring for as_cdt_ctx_init_from_pyobject +as_cdt_ctx *get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, + PyObject *py_op_dict, as_static_pool *static_pool, int serializer_type); // allow_base64_encoded_exprs: can the Python object also be a Python unicode object (base64 encoded)? // if false, the Python object should only be a compiled Python expression object from aerospike_helpers diff --git a/src/main/client/cdt_list_operate.c b/src/main/client/cdt_list_operate.c index ca83b51c52..811b02a351 100644 --- a/src/main/client/cdt_list_operate.c +++ b/src/main/client/cdt_list_operate.c @@ -434,7 +434,7 @@ static as_status add_op_list_get_by_index(AerospikeClient *self, as_error *err, { int64_t index; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -446,18 +446,19 @@ static as_status add_op_list_get_by_index(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_get_by_index(ops, bin, (ctx_in_use ? &ctx : NULL), - index, return_type)) { + if (!as_operations_list_get_by_index(ops, bin, ctx_ref, index, + return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add get_by_list_index operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -475,7 +476,7 @@ add_op_list_get_by_index_range(AerospikeClient *self, as_error *err, char *bin, bool range_specified = false; bool success = false; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -493,19 +494,19 @@ add_op_list_get_by_index_range(AerospikeClient *self, as_error *err, char *bin, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (range_specified) { success = as_operations_list_get_by_index_range( - ops, bin, (ctx_in_use ? &ctx : NULL), index, (uint64_t)count, - return_type); + ops, bin, ctx_ref, index, (uint64_t)count, return_type); } else { success = as_operations_list_get_by_index_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), index, return_type); + ops, bin, ctx_ref, index, return_type); } if (!success) { @@ -513,7 +514,7 @@ add_op_list_get_by_index_range(AerospikeClient *self, as_error *err, char *bin, "Failed to add get_by_list_index_range operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -529,7 +530,7 @@ static as_status add_op_list_get_by_rank(AerospikeClient *self, as_error *err, { int64_t rank; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -541,18 +542,18 @@ static as_status add_op_list_get_by_rank(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_get_by_rank(ops, bin, (ctx_in_use ? &ctx : NULL), - rank, return_type)) { + if (!as_operations_list_get_by_rank(ops, bin, ctx_ref, rank, return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add get_by_list_index operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -570,7 +571,7 @@ add_op_list_get_by_rank_range(AerospikeClient *self, as_error *err, char *bin, bool range_specified = false; bool success = false; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -587,19 +588,19 @@ add_op_list_get_by_rank_range(AerospikeClient *self, as_error *err, char *bin, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (range_specified) { success = as_operations_list_get_by_rank_range( - ops, bin, (ctx_in_use ? &ctx : NULL), rank, (uint64_t)count, - return_type); + ops, bin, ctx_ref, rank, (uint64_t)count, return_type); } else { success = as_operations_list_get_by_rank_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), rank, return_type); + ops, bin, ctx_ref, rank, return_type); } if (!success) { @@ -607,7 +608,7 @@ add_op_list_get_by_rank_range(AerospikeClient *self, as_error *err, char *bin, "Failed to add list_get_by_rank_range operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -622,7 +623,7 @@ static as_status add_op_list_get_by_value(AerospikeClient *self, as_error *err, { as_val *val = NULL; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -634,19 +635,19 @@ static as_status add_op_list_get_by_value(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to convert ctx list"); } - if (!as_operations_list_get_by_value(ops, bin, (ctx_in_use ? &ctx : NULL), - val, return_type)) { + if (!as_operations_list_get_by_value(ops, bin, ctx_ref, val, return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_get_by_value operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -661,7 +662,7 @@ add_op_list_get_by_value_list(AerospikeClient *self, as_error *err, char *bin, { as_list *value_list = NULL; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -673,22 +674,23 @@ add_op_list_get_by_value_list(AerospikeClient *self, as_error *err, char *bin, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { /* Failed to add the operation, we need to destroy the list of values*/ as_val_destroy(value_list); return err->code; } - if (!as_operations_list_get_by_value_list( - ops, bin, (ctx_in_use ? &ctx : NULL), value_list, return_type)) { + if (!as_operations_list_get_by_value_list(ops, bin, ctx_ref, value_list, + return_type)) { /* Failed to add the operation, we need to destroy the list of values*/ as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_get_by_value_list operation"); as_val_destroy(value_list); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -703,9 +705,9 @@ add_op_list_get_by_value_range(AerospikeClient *self, as_error *err, char *bin, { as_val *val_begin = NULL; as_val *val_end = NULL; - bool ctx_in_use = false; - as_cdt_ctx ctx; + as_cdt_ctx ctx; + as_cdt_ctx *ctx_ref = NULL; int return_type = AS_LIST_RETURN_VALUE; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -722,20 +724,20 @@ add_op_list_get_by_value_range(AerospikeClient *self, as_error *err, char *bin, goto error; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { goto error; } - if (!as_operations_list_get_by_value_range( - ops, bin, (ctx_in_use ? &ctx : NULL), val_begin, val_end, - return_type)) { + if (!as_operations_list_get_by_value_range(ops, bin, ctx_ref, val_begin, + val_end, return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_get_by_value_range operation"); goto error; } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -751,7 +753,7 @@ add_op_list_get_by_value_range(AerospikeClient *self, as_error *err, char *bin, as_val_destroy(val_end); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -766,7 +768,7 @@ add_op_list_remove_by_index(AerospikeClient *self, as_error *err, char *bin, { int64_t index; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -778,18 +780,19 @@ add_op_list_remove_by_index(AerospikeClient *self, as_error *err, char *bin, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_remove_by_index( - ops, bin, (ctx_in_use ? &ctx : NULL), index, return_type)) { + if (!as_operations_list_remove_by_index(ops, bin, ctx_ref, index, + return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add remove_by_list_index operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -806,7 +809,7 @@ static as_status add_op_list_remove_by_index_range( bool range_specified = false; bool success = false; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -824,19 +827,19 @@ static as_status add_op_list_remove_by_index_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (range_specified) { success = as_operations_list_remove_by_index_range( - ops, bin, (ctx_in_use ? &ctx : NULL), index, (uint64_t)count, - return_type); + ops, bin, ctx_ref, index, (uint64_t)count, return_type); } else { success = as_operations_list_remove_by_index_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), index, return_type); + ops, bin, ctx_ref, index, return_type); } if (!success) { @@ -844,7 +847,7 @@ static as_status add_op_list_remove_by_index_range( "Failed to add remove_by_list_index_range operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -859,7 +862,7 @@ add_op_list_remove_by_rank(AerospikeClient *self, as_error *err, char *bin, { int64_t rank; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -871,18 +874,19 @@ add_op_list_remove_by_rank(AerospikeClient *self, as_error *err, char *bin, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_remove_by_rank(ops, bin, (ctx_in_use ? &ctx : NULL), - rank, return_type)) { + if (!as_operations_list_remove_by_rank(ops, bin, ctx_ref, rank, + return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_remove_by_rank operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -899,7 +903,7 @@ static as_status add_op_list_remove_by_rank_range( bool range_specified = false; bool success = false; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -916,19 +920,19 @@ static as_status add_op_list_remove_by_rank_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (range_specified) { success = as_operations_list_remove_by_rank_range( - ops, bin, (ctx_in_use ? &ctx : NULL), rank, (uint64_t)count, - return_type); + ops, bin, ctx_ref, rank, (uint64_t)count, return_type); } else { success = as_operations_list_remove_by_rank_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), rank, return_type); + ops, bin, ctx_ref, rank, return_type); } if (!success) { @@ -936,7 +940,7 @@ static as_status add_op_list_remove_by_rank_range( "Failed to add list_remove_by_rank_range operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -951,7 +955,7 @@ add_op_list_remove_by_value(AerospikeClient *self, as_error *err, char *bin, { as_val *val = NULL; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -963,19 +967,20 @@ add_op_list_remove_by_value(AerospikeClient *self, as_error *err, char *bin, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_val_destroy(val); return err->code; } - if (!as_operations_list_remove_by_value( - ops, bin, (ctx_in_use ? &ctx : NULL), val, return_type)) { + if (!as_operations_list_remove_by_value(ops, bin, ctx_ref, val, + return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_remove_by_value operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -989,7 +994,7 @@ static as_status add_op_list_remove_by_value_list( { as_list *value_list = NULL; int return_type = AS_LIST_RETURN_VALUE; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -1001,22 +1006,23 @@ static as_status add_op_list_remove_by_value_list( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { /* Failed to convert ctx, we need to destroy the list of values*/ as_val_destroy(value_list); return err->code; } - if (!as_operations_list_remove_by_value_list( - ops, bin, (ctx_in_use ? &ctx : NULL), value_list, return_type)) { + if (!as_operations_list_remove_by_value_list(ops, bin, ctx_ref, value_list, + return_type)) { /* Failed to add the operation, we need to destroy the list of values*/ as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_get_by_value_list operation"); as_val_destroy(value_list); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1029,8 +1035,9 @@ static as_status add_op_list_remove_by_value_range( { as_val *val_begin = NULL; as_val *val_end = NULL; - bool ctx_in_use = false; + as_cdt_ctx ctx; + as_cdt_ctx *ctx_ref = NULL; int return_type = AS_LIST_RETURN_VALUE; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -1047,20 +1054,20 @@ static as_status add_op_list_remove_by_value_range( goto error; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { goto error; } - if (!as_operations_list_remove_by_value_range( - ops, bin, (ctx_in_use ? &ctx : NULL), val_begin, val_end, - return_type)) { + if (!as_operations_list_remove_by_value_range(ops, bin, ctx_ref, val_begin, + val_end, return_type)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_remove_by_value_range operation"); goto error; } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1076,7 +1083,7 @@ static as_status add_op_list_remove_by_value_range( as_val_destroy(val_end); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1090,7 +1097,7 @@ static as_status add_op_list_set_order(AerospikeClient *self, as_error *err, int serializer_type) { int64_t order_type_int; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_int64_t(err, AS_PY_LIST_ORDER, op_dict, &order_type_int) != @@ -1098,19 +1105,20 @@ static as_status add_op_list_set_order(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_set_order(ops, bin, (ctx_in_use ? &ctx : NULL), + if (!as_operations_list_set_order(ops, bin, ctx_ref, (as_list_order)order_type_int)) { as_cdt_ctx_destroy(&ctx); return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_set_order operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1124,7 +1132,7 @@ static as_status add_op_list_sort(AerospikeClient *self, as_error *err, int serializer_type) { int64_t sort_flags; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_int64_t(err, AS_PY_LIST_SORT_FLAGS, op_dict, &sort_flags) != @@ -1132,19 +1140,20 @@ static as_status add_op_list_sort(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_sort(ops, bin, (ctx_in_use ? &ctx : NULL), + if (!as_operations_list_sort(ops, bin, ctx_ref, (as_list_sort_flags)sort_flags)) { as_cdt_ctx_destroy(&ctx); return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_sort operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1159,7 +1168,7 @@ static as_status add_op_list_create(AerospikeClient *self, as_error *err, { int64_t order_type_int; as_cdt_ctx ctx; - bool ctx_in_use = false; + bool pad, persist_index; if (get_int64_t(err, AS_PY_LIST_ORDER, op_dict, &order_type_int) != @@ -1176,16 +1185,16 @@ static as_status add_op_list_create(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } bool add_op_successful = as_operations_list_create_all( - ops, bin, (ctx_in_use ? &ctx : NULL), (as_list_order)order_type_int, - pad, persist_index); + ops, bin, ctx_ref, (as_list_order)order_type_int, pad, persist_index); - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1207,7 +1216,7 @@ static as_status add_op_list_append(AerospikeClient *self, as_error *err, as_val *val = NULL; as_list_policy list_policy; bool policy_in_use = false; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_policy(err, op_dict, &list_policy, &policy_in_use, @@ -1220,22 +1229,22 @@ static as_status add_op_list_append(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_val_destroy(val); return err->code; } - if (!as_operations_list_append(ops, bin, (ctx_in_use ? &ctx : NULL), - (policy_in_use ? &list_policy : NULL), - val)) { + if (!as_operations_list_append( + ops, bin, ctx_ref, (policy_in_use ? &list_policy : NULL), val)) { as_val_destroy(val); as_cdt_ctx_destroy(&ctx); return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_append operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1251,7 +1260,7 @@ static as_status add_op_list_append_items(AerospikeClient *self, as_error *err, as_list *items_list = NULL; as_list_policy list_policy; bool policy_in_use = false; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_policy(err, op_dict, &list_policy, &policy_in_use, @@ -1264,13 +1273,14 @@ static as_status add_op_list_append_items(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_val_destroy(items_list); return err->code; } - if (!as_operations_list_append_items(ops, bin, (ctx_in_use ? &ctx : NULL), + if (!as_operations_list_append_items(ops, bin, ctx_ref, (policy_in_use ? &list_policy : NULL), items_list)) { as_val_destroy(items_list); @@ -1279,7 +1289,7 @@ static as_status add_op_list_append_items(AerospikeClient *self, as_error *err, "Failed to add list_append_items operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1296,7 +1306,7 @@ static as_status add_op_list_insert(AerospikeClient *self, as_error *err, int64_t index; as_list_policy list_policy; bool policy_in_use = false; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_int64_t(err, AS_PY_INDEX_KEY, op_dict, &index) != AEROSPIKE_OK) { @@ -1313,13 +1323,14 @@ static as_status add_op_list_insert(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_val_destroy(val); return err->code; } - if (!as_operations_list_insert(ops, bin, (ctx_in_use ? &ctx : NULL), + if (!as_operations_list_insert(ops, bin, ctx_ref, (policy_in_use ? &list_policy : NULL), index, val)) { as_val_destroy(val); @@ -1328,7 +1339,7 @@ static as_status add_op_list_insert(AerospikeClient *self, as_error *err, "Failed to add list_insert operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1345,7 +1356,7 @@ static as_status add_op_list_insert_items(AerospikeClient *self, as_error *err, int64_t index; as_list_policy list_policy; bool policy_in_use = false; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_int64_t(err, AS_PY_INDEX_KEY, op_dict, &index) != AEROSPIKE_OK) { @@ -1362,13 +1373,14 @@ static as_status add_op_list_insert_items(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_val_destroy(items_list); return err->code; } - if (!as_operations_list_insert_items(ops, bin, (ctx_in_use ? &ctx : NULL), + if (!as_operations_list_insert_items(ops, bin, ctx_ref, (policy_in_use ? &list_policy : NULL), index, items_list)) { as_val_destroy(items_list); @@ -1377,7 +1389,7 @@ static as_status add_op_list_insert_items(AerospikeClient *self, as_error *err, "Failed to add list_insert_items operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1394,7 +1406,7 @@ static as_status add_op_list_increment(AerospikeClient *self, as_error *err, int64_t index; as_list_policy list_policy; bool policy_in_use = false; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_policy(err, op_dict, &list_policy, &policy_in_use, @@ -1411,13 +1423,14 @@ static as_status add_op_list_increment(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_val_destroy(incr); return err->code; } - if (!as_operations_list_increment(ops, bin, (ctx_in_use ? &ctx : NULL), + if (!as_operations_list_increment(ops, bin, ctx_ref, (policy_in_use ? &list_policy : NULL), index, incr)) { as_val_destroy(incr); @@ -1426,7 +1439,7 @@ static as_status add_op_list_increment(AerospikeClient *self, as_error *err, "Failed to add list_increment operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1440,7 +1453,7 @@ static as_status add_op_list_pop(AerospikeClient *self, as_error *err, int serializer_type) { int64_t index; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -1448,17 +1461,18 @@ static as_status add_op_list_pop(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_pop(ops, bin, (ctx_in_use ? &ctx : NULL), index)) { + if (!as_operations_list_pop(ops, bin, ctx_ref, index)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_pop operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1473,7 +1487,7 @@ static as_status add_op_list_pop_range(AerospikeClient *self, as_error *err, { int64_t index; int64_t count; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -1486,18 +1500,19 @@ static as_status add_op_list_pop_range(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_pop_range(ops, bin, (ctx_in_use ? &ctx : NULL), - index, (uint64_t)count)) { + if (!as_operations_list_pop_range(ops, bin, ctx_ref, index, + (uint64_t)count)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to list_pop_range operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1511,27 +1526,27 @@ static as_status add_op_list_remove(AerospikeClient *self, as_error *err, int serializer_type) { int64_t index; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_int64_t(err, AS_PY_INDEX_KEY, op_dict, &index) != AEROSPIKE_OK) { return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { ; return err->code; } - if (!as_operations_list_remove(ops, bin, (ctx_in_use ? &ctx : NULL), - index)) { + if (!as_operations_list_remove(ops, bin, ctx_ref, index)) { as_cdt_ctx_destroy(&ctx); return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_remove operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1546,7 +1561,7 @@ static as_status add_op_list_remove_range(AerospikeClient *self, as_error *err, { int64_t index; int64_t count; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -1559,18 +1574,19 @@ static as_status add_op_list_remove_range(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_remove_range(ops, bin, (ctx_in_use ? &ctx : NULL), - index, (uint64_t)count)) { + if (!as_operations_list_remove_range(ops, bin, ctx_ref, index, + (uint64_t)count)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to list_remove_range operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1583,21 +1599,22 @@ static as_status add_op_list_clear(AerospikeClient *self, as_error *err, as_static_pool *static_pool, int serializer_type) { - bool ctx_in_use = false; + as_cdt_ctx ctx; - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_clear(ops, bin, (ctx_in_use ? &ctx : NULL))) { + if (!as_operations_list_clear(ops, bin, ctx_ref)) { as_cdt_ctx_destroy(&ctx); return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_clear operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1614,7 +1631,7 @@ static as_status add_op_list_set(AerospikeClient *self, as_error *err, int64_t index; as_list_policy list_policy; bool policy_in_use = false; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_policy(err, op_dict, &list_policy, &policy_in_use, @@ -1631,13 +1648,14 @@ static as_status add_op_list_set(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { as_val_destroy(val); return err->code; } - if (!as_operations_list_set(ops, bin, (ctx_in_use ? &ctx : NULL), + if (!as_operations_list_set(ops, bin, ctx_ref, (policy_in_use ? &list_policy : NULL), index, val)) { as_val_destroy(val); @@ -1646,7 +1664,7 @@ static as_status add_op_list_set(AerospikeClient *self, as_error *err, "Failed to add list_set operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1660,25 +1678,26 @@ static as_status add_op_list_get(AerospikeClient *self, as_error *err, int serializer_type) { int64_t index; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_int64_t(err, AS_PY_INDEX_KEY, op_dict, &index) != AEROSPIKE_OK) { return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_get(ops, bin, (ctx_in_use ? &ctx : NULL), index)) { + if (!as_operations_list_get(ops, bin, ctx_ref, index)) { as_cdt_ctx_destroy(&ctx); return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_get operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1693,7 +1712,7 @@ static as_status add_op_list_get_range(AerospikeClient *self, as_error *err, { int64_t index; int64_t count; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -1706,18 +1725,19 @@ static as_status add_op_list_get_range(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_get_range(ops, bin, (ctx_in_use ? &ctx : NULL), - index, (uint64_t)count)) { + if (!as_operations_list_get_range(ops, bin, ctx_ref, index, + (uint64_t)count)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to list_get_range operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1732,7 +1752,7 @@ static as_status add_op_list_trim(AerospikeClient *self, as_error *err, { int64_t index; int64_t count; - bool ctx_in_use = false; + as_cdt_ctx ctx; /* Get the index*/ @@ -1745,18 +1765,18 @@ static as_status add_op_list_trim(AerospikeClient *self, as_error *err, return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_trim(ops, bin, (ctx_in_use ? &ctx : NULL), index, - (uint64_t)count)) { + if (!as_operations_list_trim(ops, bin, ctx_ref, index, (uint64_t)count)) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to list_trim operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1769,21 +1789,22 @@ static as_status add_op_list_size(AerospikeClient *self, as_error *err, as_static_pool *static_pool, int serializer_type) { - bool ctx_in_use = false; + as_cdt_ctx ctx; - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } - if (!as_operations_list_size(ops, bin, (ctx_in_use ? &ctx : NULL))) { + if (!as_operations_list_size(ops, bin, ctx_ref)) { as_cdt_ctx_destroy(&ctx); return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to add list_size operation"); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1799,7 +1820,7 @@ static as_status add_add_op_list_remove_by_value_rel_rank_range( int return_type; int64_t rank; as_val *value = NULL; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -1820,15 +1841,15 @@ static as_status add_add_op_list_remove_by_value_rel_rank_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (count_present) { if (!as_operations_list_remove_by_value_rel_rank_range( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - (uint64_t)count, return_type)) { + ops, bin, ctx_ref, value, rank, (uint64_t)count, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -1837,8 +1858,7 @@ static as_status add_add_op_list_remove_by_value_rel_rank_range( } else { if (!as_operations_list_remove_by_value_rel_rank_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - return_type)) { + ops, bin, ctx_ref, value, rank, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -1846,7 +1866,7 @@ static as_status add_add_op_list_remove_by_value_rel_rank_range( } } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -1863,7 +1883,7 @@ static as_status add_add_op_list_get_by_value_rel_rank_range( int return_type; int64_t rank; as_val *value = NULL; - bool ctx_in_use = false; + as_cdt_ctx ctx; if (get_list_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -1884,15 +1904,15 @@ static as_status add_add_op_list_get_by_value_rel_rank_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (count_present) { if (!as_operations_list_get_by_value_rel_rank_range( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - (uint64_t)count, return_type)) { + ops, bin, ctx_ref, value, rank, (uint64_t)count, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -1901,8 +1921,7 @@ static as_status add_add_op_list_get_by_value_rel_rank_range( } else { if (!as_operations_list_get_by_value_rel_rank_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - return_type)) { + ops, bin, ctx_ref, value, rank, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -1910,7 +1929,7 @@ static as_status add_add_op_list_get_by_value_rel_rank_range( } } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } diff --git a/src/main/client/cdt_map_operate.c b/src/main/client/cdt_map_operate.c index b3f850adde..bb89986568 100644 --- a/src/main/client/cdt_map_operate.c +++ b/src/main/client/cdt_map_operate.c @@ -104,7 +104,6 @@ static as_status add_op_map_remove_by_value_rel_rank_range( int return_type = AS_MAP_RETURN_VALUE; int64_t rank; as_val *value = NULL; - bool ctx_in_use = false; as_cdt_ctx ctx; if (get_map_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -125,15 +124,15 @@ static as_status add_op_map_remove_by_value_rel_rank_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (count_present) { if (!as_operations_map_remove_by_value_rel_rank_range( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - (uint64_t)count, return_type)) { + ops, bin, ctx_ref, value, rank, (uint64_t)count, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -142,8 +141,7 @@ static as_status add_op_map_remove_by_value_rel_rank_range( } else { if (!as_operations_map_remove_by_value_rel_rank_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - return_type)) { + ops, bin, ctx_ref, value, rank, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -151,7 +149,7 @@ static as_status add_op_map_remove_by_value_rel_rank_range( } } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -167,7 +165,6 @@ static as_status add_op_map_get_by_value_rel_rank_range( int return_type = AS_MAP_RETURN_VALUE; int64_t rank; as_val *value = NULL; - bool ctx_in_use = false; as_cdt_ctx ctx; if (get_map_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -188,15 +185,15 @@ static as_status add_op_map_get_by_value_rel_rank_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (count_present) { if (!as_operations_map_get_by_value_rel_rank_range( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - (uint64_t)count, return_type)) { + ops, bin, ctx_ref, value, rank, (uint64_t)count, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -205,8 +202,7 @@ static as_status add_op_map_get_by_value_rel_rank_range( } else { if (!as_operations_map_get_by_value_rel_rank_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), value, rank, - return_type)) { + ops, bin, ctx_ref, value, rank, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -214,7 +210,7 @@ static as_status add_op_map_get_by_value_rel_rank_range( } } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -230,7 +226,6 @@ static as_status add_op_map_remove_by_key_rel_index_range( int return_type = AS_MAP_RETURN_VALUE; int64_t rank; as_val *key = NULL; - bool ctx_in_use = false; as_cdt_ctx ctx; if (get_map_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -251,15 +246,15 @@ static as_status add_op_map_remove_by_key_rel_index_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (count_present) { if (!as_operations_map_remove_by_key_rel_index_range( - ops, bin, (ctx_in_use ? &ctx : NULL), key, rank, - (uint64_t)count, return_type)) { + ops, bin, ctx_ref, key, rank, (uint64_t)count, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -268,7 +263,7 @@ static as_status add_op_map_remove_by_key_rel_index_range( } else { if (!as_operations_map_remove_by_key_rel_index_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), key, rank, return_type)) { + ops, bin, ctx_ref, key, rank, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -276,7 +271,7 @@ static as_status add_op_map_remove_by_key_rel_index_range( } } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } @@ -292,7 +287,6 @@ static as_status add_op_map_get_by_key_rel_index_range( int return_type = AS_MAP_RETURN_VALUE; int64_t rank; as_val *key = NULL; - bool ctx_in_use = false; as_cdt_ctx ctx; if (get_map_return_type(err, op_dict, &return_type) != AEROSPIKE_OK) { @@ -313,15 +307,15 @@ static as_status add_op_map_get_by_key_rel_index_range( return err->code; } - if (get_cdt_ctx(self, err, &ctx, op_dict, &ctx_in_use, static_pool, - serializer_type) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + self, err, &ctx, op_dict, static_pool, serializer_type); + if (err->code != AEROSPIKE_OK) { return err->code; } if (count_present) { if (!as_operations_map_get_by_key_rel_index_range( - ops, bin, (ctx_in_use ? &ctx : NULL), key, rank, - (uint64_t)count, return_type)) { + ops, bin, ctx_ref, key, rank, (uint64_t)count, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -330,7 +324,7 @@ static as_status add_op_map_get_by_key_rel_index_range( } else { if (!as_operations_map_get_by_key_rel_index_range_to_end( - ops, bin, (ctx_in_use ? &ctx : NULL), key, rank, return_type)) { + ops, bin, ctx_ref, key, rank, return_type)) { as_cdt_ctx_destroy(&ctx); return as_error_update( err, AEROSPIKE_ERR_CLIENT, @@ -338,7 +332,7 @@ static as_status add_op_map_get_by_key_rel_index_range( } } - if (ctx_in_use) { //add these spaces + if (ctx_ref) { //add these spaces as_cdt_ctx_destroy(&ctx); } diff --git a/src/main/client/get_cdtctx_base64.c b/src/main/client/get_cdtctx_base64.c index 5b3e2cc25e..0edff4b22d 100644 --- a/src/main/client/get_cdtctx_base64.c +++ b/src/main/client/get_cdtctx_base64.c @@ -45,9 +45,7 @@ PyObject *AerospikeClient_GetCDTCTXBase64(AerospikeClient *self, PyObject *args, // function args PyObject *py_cdtctx = NULL; as_cdt_ctx ctx; - bool ctx_in_use = false; - PyObject *op_dict = NULL; char *base64 = NULL; PyObject *py_response = NULL; @@ -75,30 +73,16 @@ PyObject *AerospikeClient_GetCDTCTXBase64(AerospikeClient *self, PyObject *args, as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); - // Convert Python cdt_ctx to C version - // Pass in ctx into a dict so we can use helper function - op_dict = PyDict_New(); - if (op_dict == NULL) { - as_error_update( - &err, AEROSPIKE_ERR, - "unable to convert Python cdtctx to it's C client counterpart"); - goto CLEANUP; - } - int retval = PyDict_SetItemString(op_dict, "ctx", py_cdtctx); - if (retval == -1) { - as_error_update( - &err, AEROSPIKE_ERR, - "unable to convert Python cdtctx to it's C client counterpart"); - goto CLEANUP; - } - if (get_cdt_ctx(self, &err, &ctx, op_dict, &ctx_in_use, &static_pool, - SERIALIZER_PYTHON) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = as_cdt_ctx_init_from_pyobject( + self, &err, &ctx, py_cdtctx, &static_pool, SERIALIZER_PYTHON); + if (err.code != AEROSPIKE_OK) { goto CLEANUP; } - if (!ctx_in_use) { + else if (ctx_ref == NULL) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "cdt ctx must be valid " ", generated by aerospike cdt context helper"); + goto CLEANUP; } //convert cdtctx to base64 @@ -108,17 +92,15 @@ PyObject *AerospikeClient_GetCDTCTXBase64(AerospikeClient *self, PyObject *args, if (!rv) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "failed to convert cdt ctx to base64 "); - goto CLEANUP; + goto CLEANUP2; } py_response = PyUnicode_FromString((const char *)base64); -CLEANUP: - - if (ctx_in_use) { - as_cdt_ctx_destroy(&ctx); - } +CLEANUP2: + as_cdt_ctx_destroy(&ctx); +CLEANUP: if (base64 != NULL) { cf_free(base64); } @@ -128,7 +110,5 @@ PyObject *AerospikeClient_GetCDTCTXBase64(AerospikeClient *self, PyObject *args, return NULL; } - Py_XDECREF(op_dict); - return py_response; } diff --git a/src/main/client/operate.c b/src/main/client/operate.c index b743252b83..0db2662992 100644 --- a/src/main/client/operate.c +++ b/src/main/client/operate.c @@ -107,8 +107,9 @@ static inline bool isExprOp(int op); } #define CONVERT_PY_CTX_TO_AS_CTX() \ - if (get_cdt_ctx(self, err, &ctx, py_operation_dict, &ctx_in_use, \ - static_pool, SERIALIZER_PYTHON) != AEROSPIKE_OK) { \ + ctx_ref = get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( \ + self, err, &ctx, py_operation_dict, static_pool, SERIALIZER_PYTHON); \ + if (err->code != AEROSPIKE_OK) { \ return err->code; \ } @@ -319,7 +320,6 @@ as_status add_op(AerospikeClient *self, as_error *err, as_val *put_range = NULL; as_cdt_ctx ctx; as_cdt_ctx *ctx_ref = NULL; - bool ctx_in_use = false; char *bin = NULL; char *val = NULL; long offset = 0; @@ -421,7 +421,6 @@ as_status add_op(AerospikeClient *self, as_error *err, } else if (strcmp(name, "ctx") == 0) { CONVERT_PY_CTX_TO_AS_CTX(); - ctx_ref = (ctx_in_use ? &ctx : NULL); } else if (strcmp(name, "map_order") == 0) { py_map_order = value; @@ -879,7 +878,7 @@ as_status add_op(AerospikeClient *self, as_error *err, if (mod_exp) { as_exp_destroy(mod_exp); } - if (ctx_in_use) { + if (ctx_ref) { as_cdt_ctx_destroy(&ctx); } diff --git a/src/main/client/sec_index.c b/src/main/client/sec_index.c index 1b22db2257..5b0a5acbbc 100644 --- a/src/main/client/sec_index.c +++ b/src/main/client/sec_index.c @@ -224,9 +224,6 @@ PyObject *AerospikeClient_Index_Cdt_Create(AerospikeClient *self, PyObject *py_ctx = NULL; as_cdt_ctx ctx; - bool ctx_in_use = false; - PyObject *py_ctx_dict = NULL; - PyObject *py_obj = NULL; as_index_datatype data_type; as_index_type index_type; @@ -252,25 +249,14 @@ PyObject *AerospikeClient_Index_Cdt_Create(AerospikeClient *self, goto CLEANUP; } - // TODO: this should be refactored by using a new helper function to parse a ctx list instead of get_cdt_ctx() - // which only parses a dictionary containing a ctx list - py_ctx_dict = PyDict_New(); - if (!py_ctx_dict) { - as_error_update(&err, AEROSPIKE_ERR_CLIENT, CTX_PARSE_ERROR_MESSAGE); - goto CLEANUP; - } - int retval = PyDict_SetItemString(py_ctx_dict, "ctx", py_ctx); - if (retval == -1) { - Py_DECREF(py_ctx_dict); - as_error_update(&err, AEROSPIKE_ERR_CLIENT, CTX_PARSE_ERROR_MESSAGE); - goto CLEANUP; - } - as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); - if (get_cdt_ctx(self, &err, &ctx, py_ctx_dict, &ctx_in_use, &static_pool, - SERIALIZER_PYTHON) != AEROSPIKE_OK) { + as_cdt_ctx *ctx_ref = as_cdt_ctx_init_from_pyobject( + self, &err, &ctx, py_ctx, &static_pool, SERIALIZER_PYTHON); + if (ctx_ref == NULL) { + as_error_update(&err, AEROSPIKE_ERR_PARAM, + "ctx is a required argument and must not be None"); goto CLEANUP; } @@ -283,7 +269,6 @@ PyObject *AerospikeClient_Index_Cdt_Create(AerospikeClient *self, as_cdt_ctx_destroy(&ctx); CLEANUP: - Py_XDECREF(py_ctx_dict); if (err.code != AEROSPIKE_OK) { raise_exception_base(&err, Py_None, Py_None, Py_None, Py_None, py_name); diff --git a/src/main/conversions.c b/src/main/conversions.c index 23065d51c7..147d6baeae 100644 --- a/src/main/conversions.c +++ b/src/main/conversions.c @@ -2415,34 +2415,50 @@ as_status string_and_pyuni_from_pystring(PyObject *py_string, return as_error_update(err, AEROSPIKE_ERR_PARAM, "String value required"); } -// This function converts a list of cdt_ctx from aerospike_helpers.ctx to -// an as_cdt_ctx object for use with the c-client. the cdt_ctx parameter should be an uninitialized as_cdt_ctx -// object. This function will initilaise it, and free it IF an error occurs, otherwise, the caller must destroy -// the as_cdt_ctx when it is done. -as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, - PyObject *op_dict, bool *ctx_in_use, - as_static_pool *static_pool, int serializer_type) +as_cdt_ctx *as_cdt_ctx_create_from_pyobject(AerospikeClient *self, + as_error *err, + PyObject *py_ctx_list, + as_static_pool *static_pool, + int serializer_type) { - PyObject *py_ctx_list = PyDict_GetItemString(op_dict, CTX_KEY); + if (!py_ctx_list || Py_IsNone(py_ctx_list)) { + return NULL; + } - if (!py_ctx_list) { - return AEROSPIKE_OK; + as_cdt_ctx *cdt_ctx = cf_malloc(sizeof(as_cdt_ctx)); + + as_cdt_ctx_init_from_pyobject(self, err, cdt_ctx, py_ctx_list, static_pool, + serializer_type); + if (err->code != AEROSPIKE_OK) { + cf_free(cdt_ctx); + return NULL; + } + + return cdt_ctx; +} + +as_cdt_ctx *as_cdt_ctx_init_from_pyobject(AerospikeClient *self, as_error *err, + as_cdt_ctx *cdt_ctx, + PyObject *py_ctx_list, + as_static_pool *static_pool, + int serializer_type) +{ + if (!py_ctx_list || Py_IsNone(py_ctx_list)) { + goto RETURN_NULL; + } + else if (!PyList_Check(py_ctx_list)) { + as_error_update(err, AEROSPIKE_ERR_PARAM, "Failed to convert %s", + CTX_KEY); + goto RETURN_NULL; } long int_val = 0; as_val *val = NULL; - as_status status = 0; PyObject *py_id = NULL; PyObject *py_value = NULL; PyObject *py_extra_args = NULL; - if (!PyList_Check(py_ctx_list)) { - status = as_error_update(err, AEROSPIKE_ERR_PARAM, - "Failed to convert %s", CTX_KEY); - goto CLEANUP5; - } - Py_ssize_t py_list_size = PyList_Size(py_ctx_list); as_cdt_ctx_init(cdt_ctx, (int)py_list_size); @@ -2451,30 +2467,29 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, py_id = PyObject_GetAttrString(py_cdt_ctx, "id"); if (PyErr_Occurred()) { - status = as_error_update(err, AEROSPIKE_ERR_PARAM, - "Failed to convert %s, id", CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Failed to convert %s, id", CTX_KEY); goto CLEANUP4; } py_value = PyObject_GetAttrString(py_cdt_ctx, "value"); if (PyErr_Occurred()) { - status = as_error_update(err, AEROSPIKE_ERR_PARAM, - "Failed to convert %s, value", CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Failed to convert %s, value", CTX_KEY); goto CLEANUP3; } py_extra_args = PyObject_GetAttrString(py_cdt_ctx, "extra_args"); if (PyErr_Occurred()) { - status = as_error_update(err, AEROSPIKE_ERR_PARAM, - "Failed to convert %s", CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, "Failed to convert %s", + CTX_KEY); goto CLEANUP2; } uint64_t item_type = PyLong_AsUnsignedLongLong(py_id); if (PyErr_Occurred()) { - status = as_error_update(err, AEROSPIKE_ERR_PARAM, - "Failed to convert %s, id to uint64_t", - CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Failed to convert %s, id to uint64_t", CTX_KEY); goto CLEANUP1; } @@ -2482,9 +2497,8 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, if (requires_int(item_type)) { int_val = PyLong_AsLong(py_value); if (PyErr_Occurred()) { - status = as_error_update(err, AEROSPIKE_ERR_PARAM, - "Failed to convert %s, value to long", - CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Failed to convert %s, value to long", CTX_KEY); goto CLEANUP1; } switch (item_type) { @@ -2510,9 +2524,9 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, pad); break; default: - status = as_error_update( - err, AEROSPIKE_ERR_PARAM, - "Failed to convert, unknown ctx operation %s", CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Failed to convert, unknown ctx operation %s", + CTX_KEY); goto CLEANUP1; } } @@ -2525,14 +2539,13 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, int retval = PyDict_GetItemStringRef( py_extra_args, _CDT_CTX_FILTER_EXPR_KEY, &py_expr); if (retval != 1) { - status = as_error_update(err, AEROSPIKE_ERR_PARAM, - "Invalid cdt_ctx_exp"); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Invalid cdt_ctx_exp"); goto CLEANUP1; } as_exp *expr = NULL; - status = - as_exp_new_from_pyobject(self, py_expr, &expr, err, false); + as_exp_new_from_pyobject(self, py_expr, &expr, err, false); Py_DECREF(py_expr); if (err->code != AEROSPIKE_OK) { goto CLEANUP1; @@ -2546,9 +2559,9 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, else { if (as_val_new_from_pyobject(self, err, py_value, &val, static_pool, serializer_type) != AEROSPIKE_OK) { - status = as_error_update( - err, AEROSPIKE_ERR_PARAM, - "Failed to convert %s, value to as_val", CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Failed to convert %s, value to as_val", + CTX_KEY); goto CLEANUP1; } @@ -2569,9 +2582,9 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, as_cdt_ctx_add_map_key_create(cdt_ctx, val, map_order); break; default: - status = as_error_update( - err, AEROSPIKE_ERR_PARAM, - "Failed to convert, unknown ctx operation %s", CTX_KEY); + as_error_update(err, AEROSPIKE_ERR_PARAM, + "Failed to convert, unknown ctx operation %s", + CTX_KEY); goto CLEANUP1; } } @@ -2581,8 +2594,7 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, Py_DECREF(py_extra_args); } - *ctx_in_use = true; - return AEROSPIKE_OK; + return cdt_ctx; CLEANUP1: Py_DECREF(py_extra_args); @@ -2592,8 +2604,17 @@ as_status get_cdt_ctx(AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, Py_DECREF(py_id); CLEANUP4: as_cdt_ctx_destroy(cdt_ctx); -CLEANUP5: - return status; +RETURN_NULL: + return NULL; +} + +as_cdt_ctx *get_optional_cdt_ctx_from_py_dict_and_as_cdt_ctx_init( + AerospikeClient *self, as_error *err, as_cdt_ctx *cdt_ctx, + PyObject *py_op_dict, as_static_pool *static_pool, int serializer_type) +{ + PyObject *py_ctx_list = PyDict_GetItemString(py_op_dict, CTX_KEY); + return as_cdt_ctx_init_from_pyobject(self, err, cdt_ctx, py_ctx_list, + static_pool, serializer_type); } static bool requires_int(uint64_t op) diff --git a/src/main/convert_expressions.c b/src/main/convert_expressions.c index 8bf1caf07f..22fc2358d0 100644 --- a/src/main/convert_expressions.c +++ b/src/main/convert_expressions.c @@ -1769,7 +1769,6 @@ as_status as_exp_new_from_pyobject(AerospikeClient *self, PyObject *py_expr, int processed_exp_count = 0; int size_to_alloc = 0; - bool ctx_in_use = false; PyObject *py_expr_tuple = NULL; PyObject *py_list_policy_p = NULL; PyObject *py_map_policy_p = NULL; @@ -1790,7 +1789,6 @@ as_status as_exp_new_from_pyobject(AerospikeClient *self, PyObject *py_expr, for (int i = 0; i < size; ++i) { memset(&temp_expr, 0, sizeof(intermediate_expr)); - ctx_in_use = false; // Reset flag for next temp expr being built is_ctx_initialized = false; @@ -1837,20 +1835,12 @@ as_status as_exp_new_from_pyobject(AerospikeClient *self, PyObject *py_expr, //TODO Could it be moved somewhere else? py_ctx_list_p = PyDict_GetItemString(temp_expr.pydict, CTX_KEY); if (py_ctx_list_p != NULL) { - temp_expr.ctx = malloc(sizeof(as_cdt_ctx)); - if (temp_expr.ctx == NULL) { - as_error_update(err, AEROSPIKE_ERR, - "Could not malloc mem for temp_expr.ctx."); - goto CLEANUP; - } - - if (get_cdt_ctx(self, err, temp_expr.ctx, temp_expr.pydict, - &ctx_in_use, &static_pool, - SERIALIZER_PYTHON) != AEROSPIKE_OK) { + temp_expr.ctx = as_cdt_ctx_create_from_pyobject( + self, err, py_ctx_list_p, &static_pool, SERIALIZER_PYTHON); + if (err->code != AEROSPIKE_OK) { goto CLEANUP; } } - is_ctx_initialized = true; py_list_policy_p = PyDict_GetItemString(temp_expr.pydict, AS_PY_LIST_POLICY); diff --git a/src/main/query/where.c b/src/main/query/where.c index f9eb0e37d5..2f0e35f8f2 100644 --- a/src/main/query/where.c +++ b/src/main/query/where.c @@ -66,35 +66,16 @@ static int AerospikeQuery_Where_Add(AerospikeQuery *self, PyObject *py_ctx, memset(&static_pool, 0, sizeof(static_pool)); as_cdt_ctx *pctx = NULL; - bool ctx_in_use = false; - // Used to pass ctx into get_cdt_ctx() helper - // Declared here to make cleanup logic simpler - PyObject *py_ctx_dict = NULL; - - // Ctx is an optional parameter - if (py_ctx && !Py_IsNone(py_ctx)) { - // If user wanted to pass in an actual ctx - - // Glue code to pass into get_cdt_ctx() - py_ctx_dict = PyDict_New(); - if (!py_ctx_dict) { - as_error_update(&err, AEROSPIKE_ERR_CLIENT, - CTX_PARSE_ERROR_MESSAGE); - goto error; - } - int retval = PyDict_SetItemString(py_ctx_dict, "ctx", py_ctx); - if (retval == -1) { - as_error_update(&err, AEROSPIKE_ERR_CLIENT, - CTX_PARSE_ERROR_MESSAGE); - goto CLEANUP_PY_CTX_DICT_ON_ERROR; - } - pctx = cf_malloc(sizeof(as_cdt_ctx)); - memset(pctx, 0, sizeof(as_cdt_ctx)); + if (py_ctx) { + // TODO: does static pool go out of scope? + as_static_pool static_pool; + memset(&static_pool, 0, sizeof(static_pool)); - if (get_cdt_ctx(self->client, &err, pctx, py_ctx_dict, &ctx_in_use, - &static_pool, SERIALIZER_PYTHON) != AEROSPIKE_OK) { - goto CLEANUP_AS_CTX_ON_ERROR; + pctx = as_cdt_ctx_create_from_pyobject(self->client, &err, py_ctx, + &static_pool, SERIALIZER_PYTHON); + if (err.code != AEROSPIKE_OK) { + goto error; } } @@ -286,7 +267,7 @@ static int AerospikeQuery_Where_Add(AerospikeQuery *self, PyObject *py_ctx, goto CLEANUP_VALUES_ON_ERROR; } - if (ctx_in_use) { + if (pctx) { self->query.where.entries[0].ctx_free = true; } if (exp_list) { @@ -313,16 +294,11 @@ static int AerospikeQuery_Where_Add(AerospikeQuery *self, PyObject *py_ctx, CLEANUP_AS_CTX_ON_ERROR: // The ctx ends up not being used by as_query - if (ctx_in_use) { - as_cdt_ctx_destroy(pctx); - } if (pctx) { + as_cdt_ctx_destroy(pctx); cf_free(pctx); } -CLEANUP_PY_CTX_DICT_ON_ERROR: - Py_XDECREF(py_ctx_dict); - error: return 1; } diff --git a/test/new_tests/test_cdt_index.py b/test/new_tests/test_cdt_index.py index b894b3d647..480a684235 100644 --- a/test/new_tests/test_cdt_index.py +++ b/test/new_tests/test_cdt_index.py @@ -731,8 +731,6 @@ def test_neg_cdtindex_with_no_paramters(self): "ctx", [ None, - # Invalid type - {"ctx": 1} ] ) def test_neg_cdtindex_with_invalid_ctx(self, ctx): diff --git a/test/new_tests/test_query.py b/test/new_tests/test_query.py index c65cbada91..87adf35d33 100644 --- a/test/new_tests/test_query.py +++ b/test/new_tests/test_query.py @@ -306,14 +306,22 @@ def teardown(): request.addfinalizer(teardown) - def test_query_with_correct_parameters_hi(self): + @pytest.mark.parametrize( + "extra_args", + [ + # ctx + [], + [None] + # Empty context list raises an InvalidRequest exception + ] + ) + def test_query_with_correct_parameters_hi(self, extra_args): """ Invoke query() with correct arguments """ query = self.as_connection.query("test", "demo") query.select("name", "test_age") - # Here we explicitly test that ctx accepts None - query.where(p.equals("test_age", 1), None) + query.where(p.equals("test_age", 1), *extra_args) records = []