Skip to content

Commit

Permalink
CLIENT-2585 Support secondary index on a blob in a list/map bin.
Browse files Browse the repository at this point in the history
Requires server version 7.0+.
  • Loading branch information
BrianNichols committed Oct 19, 2023
1 parent b53f3c4 commit 1ac97cb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
51 changes: 44 additions & 7 deletions src/include/aerospike/as_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ extern "C" {
*****************************************************************************/

/**
* Macro for setting setting the STRING_EQUAL predicate.
* Filter on string bins.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_string_equals("abc"));
* ~~~~~~~~~~
*
* @relates as_query
* @ingroup query_object
*/
#define as_string_equals(__val) AS_PREDICATE_EQUAL, AS_INDEX_TYPE_DEFAULT, AS_INDEX_STRING, __val

/**
* Macro for setting setting the BLOB_EQUAL predicate.
* Filter on blob bins.
* Requires server version 7.0+.
*
* ~~~~~~~~~~{.c}
Expand All @@ -54,22 +55,24 @@ extern "C" {
* ~~~~~~~~~~
*
* @relates as_query
* @ingroup query_object
*/
#define as_blob_equals(__val, __size, __free) AS_PREDICATE_EQUAL, AS_INDEX_TYPE_DEFAULT, AS_INDEX_BLOB, __val, __size, __free

/**
* Macro for setting setting the INTEGER_EQUAL predicate.
* Filter on integer bins.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_integer_equals(123));
* ~~~~~~~~~~
*
* @relates as_query
* @ingroup query_object
*/
#define as_integer_equals(__val) AS_PREDICATE_EQUAL, AS_INDEX_TYPE_DEFAULT, AS_INDEX_NUMERIC, (int64_t)__val

/**
* Macro for setting setting the INTEGER_RANGE predicate.
* Ranger filter on integer bins.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_integer_range(1,100));
Expand All @@ -81,7 +84,7 @@ extern "C" {
#define as_integer_range(__min, __max) AS_PREDICATE_RANGE, AS_INDEX_TYPE_DEFAULT, AS_INDEX_NUMERIC, (int64_t)__min, (int64_t)__max

/**
* Macro for setting setting the RANGE predicate.
* Range filter on list/map elements.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_range(LIST,NUMERIC,1,100));
Expand All @@ -93,7 +96,7 @@ extern "C" {
#define as_range(indextype, datatype, __min, __max) AS_PREDICATE_RANGE, AS_INDEX_TYPE_ ##indextype, AS_INDEX_ ##datatype, __min, __max

/**
* Macro for setting setting the CONTAINS predicate.
* Contains filter on list/map elements.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_contains(LIST,STRING,"val"));
Expand All @@ -105,7 +108,21 @@ extern "C" {
#define as_contains(indextype, datatype, __val) AS_PREDICATE_EQUAL, AS_INDEX_TYPE_ ##indextype, AS_INDEX_ ##datatype, __val

/**
* Macro for setting setting the EQUALS predicate.
* Contains blob filter on list/map elements.
* Requires server version 7.0+.
*
* ~~~~~~~~~~{.c}
* // as_blob_contains(type, uint8_t* bytes, uint32_t size, bool free)
* as_query_where(query, "bin1", as_blob_equals(LIST, bytes, size, true));
* ~~~~~~~~~~
*
* @relates as_query
* @ingroup query_object
*/
#define as_blob_contains(indextype, __val, __size, __free) AS_PREDICATE_EQUAL, AS_INDEX_TYPE_ ##indextype, AS_INDEX_BLOB, __val, __size, __free

/**
* Filter specified type on bins.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_equals(NUMERIC,5));
Expand All @@ -116,8 +133,28 @@ extern "C" {
*/
#define as_equals(datatype, __val) AS_PREDICATE_EQUAL, AS_INDEX_TYPE_DEFAULT, AS_INDEX_ ##datatype, __val

/**
* Within filter on GEO bins.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_geo_within(region));
* ~~~~~~~~~~
*
* @relates as_query
* @ingroup query_object
*/
#define as_geo_within(__val) AS_PREDICATE_RANGE, AS_INDEX_TYPE_DEFAULT, AS_INDEX_GEO2DSPHERE, __val

/**
* Contains filter on GEO bins.
*
* ~~~~~~~~~~{.c}
* as_query_where(query, "bin1", as_geo_contains(region));
* ~~~~~~~~~~
*
* @relates as_query
* @ingroup query_object
*/
#define as_geo_contains(__val) AS_PREDICATE_RANGE, AS_INDEX_TYPE_DEFAULT, AS_INDEX_GEO2DSPHERE, __val


Expand Down
44 changes: 44 additions & 0 deletions src/test/aerospike_query/query_foreach.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ query_foreach_create(void)
status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "blob", "idx_blob_test", AS_INDEX_TYPE_DEFAULT, AS_INDEX_BLOB);
index_process_return_code(status, &err, &task);

status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "blob_list", "idx_list_blob_test", AS_INDEX_TYPE_LIST, AS_INDEX_BLOB);
index_process_return_code(status, &err, &task);

char* buffer = alloca(n_recs * 1024 + 1);
uint32_t the_ttl = AS_RECORD_NO_EXPIRE_TTL;

Expand Down Expand Up @@ -247,6 +250,13 @@ query_foreach_create(void)
uint8_t* blob_ptr = blob;
*(uint32_t*)blob_ptr = 50000 + i;

// Make list of blobs
as_arraylist list3;
as_arraylist_init(&list3, 1, 0);
as_bytes bytes;
as_bytes_init_wrap(&bytes, blob_ptr, 4, false);
as_arraylist_append_bytes(&list3, &bytes);

// We only create the g bin for odd records.
bool create_g_bin = i % 2 == 1;

Expand All @@ -264,6 +274,7 @@ query_foreach_create(void)
as_record_set_list(&r, "x", (as_list *) &list);
as_record_set_map(&r, "y", (as_map *) &map);
as_record_set_list(&r, "z", (as_list *) &list2);
as_record_set_list(&r, "blob_list", (as_list *) &list3);
as_record_set_str(&r, "bigstr", buffer);
as_record_set_rawp(&r, "blob", blob_ptr, sizeof(uint32_t), false);

Expand Down Expand Up @@ -369,6 +380,11 @@ query_foreach_destroy(void)
info("error(%d): %s", err.code, err.message);
}

aerospike_index_remove(as, &err, NULL, NAMESPACE, "idx_list_blob_test");
if (err.code != AEROSPIKE_OK) {
info("error(%d): %s", err.code, err.message);
}

return true;
}

Expand Down Expand Up @@ -1766,6 +1782,33 @@ TEST(query_blob_index, "query blob index")
as_query_destroy(&q);
}

TEST(query_blob_list_index, "query blob list index")
{
as_error err;
as_error_reset(&err);

uint32_t count = 0;

uint8_t blob[4];
*((uint32_t*)blob) = 50003;

as_query q;
as_query_init(&q, NAMESPACE, SET);

as_query_select_inita(&q, 1);
as_query_select(&q, "blob_list");

as_query_where_inita(&q, 1);
as_query_where(&q, "blob_list", as_blob_contains(LIST, &blob, 4, false));

aerospike_query_foreach(as, &err, NULL, &q, query_foreach_count_callback, &count);

assert_int_eq(err.code, 0);
assert_int_eq(count, 1);

as_query_destroy(&q);
}

/******************************************************************************
* TEST SUITE
*****************************************************************************/
Expand Down Expand Up @@ -1851,4 +1894,5 @@ SUITE(query_foreach, "aerospike_query_foreach tests")
suite_add(query_list_ctx_is_string);
suite_add(query_map_ctx_is_string);
suite_add(query_blob_index);
suite_add(query_blob_list_index);
}

0 comments on commit 1ac97cb

Please sign in to comment.