Skip to content

Commit

Permalink
Add persist support for building CTX.
Browse files Browse the repository at this point in the history
  • Loading branch information
xorphox committed Jan 24, 2024
1 parent 8c0c820 commit d7baf2a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/include/aerospike/as_cdt_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ as_cdt_ctx_add_list_index(as_cdt_ctx* ctx, int index)
static inline void
as_cdt_ctx_add_list_index_create(as_cdt_ctx* ctx, int index, as_list_order order, bool pad)
{
if (ctx->list.size != 0) {
order &= ~AS_LIST_FLAG_PERSIST_INDEX;
}

as_cdt_ctx_item item;
item.type = AS_CDT_CTX_LIST_INDEX | as_list_order_to_flag(order, pad);
item.val.ival = index;
Expand Down Expand Up @@ -278,6 +282,10 @@ as_cdt_ctx_add_map_key(as_cdt_ctx* ctx, as_val* key)
static inline void
as_cdt_ctx_add_map_key_create(as_cdt_ctx* ctx, as_val* key, as_map_order order)
{
if (ctx->list.size != 0) {
order &= ~AS_MAP_FLAG_PERSIST_INDEX;
}

as_cdt_ctx_item item;
item.type = AS_CDT_CTX_MAP_KEY | as_map_order_to_flag(order);
item.val.pval = key;
Expand Down
31 changes: 25 additions & 6 deletions src/include/aerospike/as_cdt_order.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ typedef enum as_list_order_e {
* List is ordered.
*/
AS_LIST_ORDERED = 1,

/**
* Persist index on server.
*/
AS_LIST_FLAG_PERSIST_INDEX = 0x10

} as_list_order;

/**
Expand All @@ -62,7 +68,12 @@ typedef enum as_map_order_e {
/**
* Order map by key, then value.
*/
AS_MAP_KEY_VALUE_ORDERED = 3
AS_MAP_KEY_VALUE_ORDERED = 3,

/**
* Persist index on server.
*/
AS_MAP_FLAG_PERSIST_INDEX = 0x10
} as_map_order;

/******************************************************************************
Expand All @@ -72,22 +83,30 @@ typedef enum as_map_order_e {
static inline uint32_t
as_list_order_to_flag(as_list_order order, bool pad)
{
return (order == AS_LIST_ORDERED)? 0xc0 : pad ? 0x80 : 0x40;
if ((order & AS_LIST_ORDERED) != 0) {
return ((order & AS_LIST_FLAG_PERSIST_INDEX) != 0) ? 0x1c0 : 0xc0;
}

return (pad ? 0x80 : 0x40) |
(((order & AS_LIST_FLAG_PERSIST_INDEX) != 0) ? 0x100 : 0x0);
}

static inline uint32_t
as_map_order_to_flag(as_map_order order)
{
switch (order) {
switch (order & 0x3) {
default:
case AS_MAP_UNORDERED:
return 0x40;
return 0x40 |
(((order & AS_LIST_FLAG_PERSIST_INDEX) != 0) ? 0x100 : 0x0);

case AS_MAP_KEY_ORDERED:
return 0x80;
return 0x80 |
(((order & AS_LIST_FLAG_PERSIST_INDEX) != 0) ? 0x100 : 0x0);

case AS_MAP_KEY_VALUE_ORDERED:
return 0xc0;
return 0xc0 |
(((order & AS_LIST_FLAG_PERSIST_INDEX) != 0) ? 0x100 : 0x0);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/test/aerospike_list/list_basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -2980,7 +2980,7 @@ TEST(list_persist_index, "test persist index")
// Test ctx create.
as_operations_init(&ops, 1);
as_cdt_ctx_init(&ctx, 1);
as_cdt_ctx_add_list_index_create(&ctx, 0, AS_LIST_UNORDERED | AS_MAP_FLAG_PERSIST_INDEX, false);
as_cdt_ctx_add_list_index_create(&ctx, 0, AS_LIST_UNORDERED | AS_LIST_FLAG_PERSIST_INDEX, false);
as_operations_list_append(&ops, BIN_NAME, &ctx, NULL, (as_val*)as_integer_new(1));

status = aerospike_key_operate(as, &err, NULL, &rkey, &ops, &rec);
Expand Down Expand Up @@ -3010,7 +3010,7 @@ TEST(list_persist_index, "test persist index")

as_operations_init(&ops, 1);
as_cdt_ctx_init(&ctx, 1);
as_cdt_ctx_add_list_index_create(&ctx, 10, AS_LIST_UNORDERED | AS_MAP_FLAG_PERSIST_INDEX, true);
as_cdt_ctx_add_list_index_create(&ctx, 10, AS_LIST_UNORDERED | AS_LIST_FLAG_PERSIST_INDEX, true);
as_operations_list_append(&ops, BIN_NAME, &ctx, NULL, (as_val*)as_integer_new(1));

status = aerospike_key_operate(as, &err, NULL, &rkey, &ops, &rec);
Expand All @@ -3031,15 +3031,17 @@ TEST(list_persist_index, "test persist index")
as_record_destroy(rec);
rec = NULL;

// Test ctx create sub presist.
// Test ctx create sub presist rejection.
status = aerospike_key_remove(as, &err, NULL, &rkey);
assert_true(status == AEROSPIKE_OK);

as_operations_init(&ops, 1);

as_cdt_ctx_init(&ctx, 2);
as_cdt_ctx_add_list_index_create(&ctx, 0, AS_LIST_UNORDERED, false);
as_cdt_ctx_add_list_index_create(&ctx, 0, AS_LIST_ORDERED | AS_MAP_FLAG_PERSIST_INDEX, false);
as_cdt_ctx_add_list_index_create(&ctx, 0, AS_LIST_ORDERED, false);
as_cdt_ctx_item* hack_item = as_vector_get(&ctx.list, ctx.list.size - 1);
hack_item->type |= 0x100; // hack in a persist flag, do not do this normally

as_operations_list_append(&ops, BIN_NAME, &ctx, NULL, (as_val*)as_integer_new(1));

Expand Down
19 changes: 12 additions & 7 deletions src/test/aerospike_map/map_basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -3368,16 +3368,17 @@ TEST(map_persist_index, "Test Map Persist Index")
as_record_destroy(rec);
rec = NULL;

// Test ctx create sub presist.
// Test ctx create sub presist rejection.
status = aerospike_key_remove(as, &err, NULL, &rkey);
assert_true(status == AEROSPIKE_OK);

as_operations_init(&ops, 1);

as_cdt_ctx_init(&ctx, 2);
as_cdt_ctx_add_list_index_create(&ctx, 0, AS_LIST_UNORDERED, false);
as_cdt_ctx_add_map_key_create(&ctx, (as_val*)as_integer_new(0),
AS_MAP_KEY_ORDERED | AS_MAP_FLAG_PERSIST_INDEX);
as_cdt_ctx_add_map_key_create(&ctx, (as_val*)as_integer_new(0), AS_MAP_KEY_ORDERED);
as_cdt_ctx_item* hack_item = as_vector_get(&ctx.list, ctx.list.size - 1);
hack_item->type |= 0x100; // hack in a persist flag, do not do this normally

as_operations_list_append(&ops, BIN_NAME, &ctx, NULL, (as_val*)as_integer_new(1));

Expand All @@ -3388,7 +3389,7 @@ TEST(map_persist_index, "Test Map Persist Index")
as_operations_destroy(&ops);
as_cdt_ctx_destroy(&ctx);

// Test ctx create sub presist 2.
// Test ctx create sub presist rejection 2.
as_cdt_ctx_init(&ctx, 1);
as_cdt_ctx_add_map_key_create(&ctx, (as_val*)as_integer_new(0), AS_MAP_FLAG_PERSIST_INDEX);

Expand All @@ -3402,15 +3403,19 @@ TEST(map_persist_index, "Test Map Persist Index")
as_cdt_ctx_destroy(&ctx);

as_cdt_ctx_init(&ctx, 3);
as_cdt_ctx_add_map_key_create(&ctx, (as_val*)as_integer_new(0), AS_MAP_FLAG_PERSIST_INDEX);
as_cdt_ctx_add_map_key_create(&ctx, (as_val*)as_integer_new(1), AS_MAP_FLAG_PERSIST_INDEX);
as_cdt_ctx_add_map_key_create(&ctx, (as_val*)as_integer_new(1), AS_MAP_FLAG_PERSIST_INDEX);
hack_item = as_vector_get(&ctx.list, ctx.list.size - 1);
hack_item->type |= 0x100; // hack in a persist flag, do not do this normally
as_cdt_ctx_add_map_key_create(&ctx, (as_val*)as_integer_new(2), AS_MAP_FLAG_PERSIST_INDEX);
hack_item = as_vector_get(&ctx.list, ctx.list.size - 1);
hack_item->type |= 0x100; // hack in a persist flag, do not do this normally

as_operations_init(&ops, 1);
as_operations_list_append(&ops, BIN_NAME, &ctx, NULL, (as_val*)as_integer_new(1));

status = aerospike_key_operate(as, &err, NULL, &rkey, &ops, &rec);
assert_int_ne(status, AEROSPIKE_OK); // rejected
assert_int_ne(status, AEROSPIKE_OK); // should be rejected
as_record_destroy(rec);
rec = NULL;
as_operations_destroy(&ops);
Expand Down Expand Up @@ -3446,7 +3451,7 @@ TEST(map_persist_index, "Test Map Persist Index")

as_map_policy pol;
as_map_policy_init(&pol);
as_map_policy_set_flags(&pol, AS_MAP_FLAG_PERSIST_INDEX, 0);
as_map_policy_set_all(&pol, AS_MAP_UNORDERED, 0, true);
as_operations_init(&ops, 1);
as_operations_add_map_set_policy(&ops, BIN_NAME, &pol);

Expand Down

0 comments on commit d7baf2a

Please sign in to comment.