From d7baf2a72cdfb15fdab93f7f877f9b84137caec7 Mon Sep 17 00:00:00 2001 From: Shu Hui Chen Date: Tue, 23 Jan 2024 21:14:17 -0800 Subject: [PATCH] Add persist support for building CTX. --- src/include/aerospike/as_cdt_ctx.h | 8 +++++++ src/include/aerospike/as_cdt_order.h | 31 +++++++++++++++++++++------ src/test/aerospike_list/list_basics.c | 10 +++++---- src/test/aerospike_map/map_basics.c | 19 ++++++++++------ 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/include/aerospike/as_cdt_ctx.h b/src/include/aerospike/as_cdt_ctx.h index 9e5104b78..8993fa237 100644 --- a/src/include/aerospike/as_cdt_ctx.h +++ b/src/include/aerospike/as_cdt_ctx.h @@ -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; @@ -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; diff --git a/src/include/aerospike/as_cdt_order.h b/src/include/aerospike/as_cdt_order.h index c1151fe47..14bc23c47 100644 --- a/src/include/aerospike/as_cdt_order.h +++ b/src/include/aerospike/as_cdt_order.h @@ -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; /** @@ -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; /****************************************************************************** @@ -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); } } diff --git a/src/test/aerospike_list/list_basics.c b/src/test/aerospike_list/list_basics.c index 14029fb2d..9b87ea654 100644 --- a/src/test/aerospike_list/list_basics.c +++ b/src/test/aerospike_list/list_basics.c @@ -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); @@ -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); @@ -3031,7 +3031,7 @@ 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); @@ -3039,7 +3039,9 @@ TEST(list_persist_index, "test persist index") 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)); diff --git a/src/test/aerospike_map/map_basics.c b/src/test/aerospike_map/map_basics.c index c448ed0b5..2f6bc20c5 100644 --- a/src/test/aerospike_map/map_basics.c +++ b/src/test/aerospike_map/map_basics.c @@ -3368,7 +3368,7 @@ 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); @@ -3376,8 +3376,9 @@ TEST(map_persist_index, "Test Map Persist Index") 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)); @@ -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); @@ -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); @@ -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);