Skip to content

Commit

Permalink
Boolean index changes # (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshrajamani authored Jan 24, 2023
1 parent 127713b commit 23ca80a
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static Map<QueryOuterClass.Value, QueryOuterClass.Value> getBooleanMapVal
Map<JsonPath, Boolean> from) {
final Map<QueryOuterClass.Value, QueryOuterClass.Value> to = new HashMap<>(from.size());
for (Map.Entry<JsonPath, Boolean> entry : from.entrySet()) {
to.put(Values.of(entry.getKey().toString()), Values.of(entry.getValue()));
to.put(Values.of(entry.getKey().toString()), Values.of((byte) (entry.getValue() ? 1 : 0)));
}
return to;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected QueryOuterClass.Query getCreateTable(String keyspace, String table) {
+ " array_size map<text, int>,"
+ " array_equals map<text, text>,"
+ " array_contains set<text>,"
+ " query_bool_values map<text, boolean>,"
+ " query_bool_values map<text, tinyint>,"
+ " query_dbl_values map<text, decimal>,"
+ " query_text_values map<text, text>, "
+ " query_null_values set<text>, "
Expand Down Expand Up @@ -101,10 +101,10 @@ protected List<QueryOuterClass.Query> getIndexStatements(String keyspace, String

String boolQuery =
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_bool_values ON %s.%s (entries(query_bool_values)) USING 'StorageAttachedIndex'";
/*statements.add(
QueryOuterClass.Query.newBuilder()
.setCql(String.format(boolQuery, table, keyspace, table))
.build());*/
statements.add(
QueryOuterClass.Query.newBuilder()
.setCql(String.format(boolQuery, table, keyspace, table))
.build());

String dblQuery =
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_dbl_values ON %s.%s (entries(query_dbl_values)) USING 'StorageAttachedIndex'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public TextFilter(String path, Operator operator, String value) {
}

/** Filters db documents based on a boolean field value */
public static class BoolFilter extends MapFilterBase<Boolean> {
public static class BoolFilter extends MapFilterBase<Byte> {
public BoolFilter(String path, Operator operator, Boolean value) {
super("query_bool_values", path, operator, value);
super("query_bool_values", path, operator, (byte) (value ? 1 : 0));
}
}

Expand Down Expand Up @@ -240,8 +240,8 @@ private static QueryOuterClass.Value getValue(Object value) {
return Values.of((String) value);
} else if (value instanceof BigDecimal) {
return Values.of((BigDecimal) value);
} else if (value instanceof Boolean) {
return Values.of((Boolean) value);
} else if (value instanceof Byte) {
return Values.of((Byte) value);
}
return Values.of((String) null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void setUp() {
"insertOne": {
"document": {
"_id": "doc1",
"username": "user1"
"username": "user1",
"active_user" : true
}
}
}
Expand Down Expand Up @@ -161,7 +162,7 @@ public void findById() {
}
}
""";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\"}";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\", \"active_user\":true}";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
Expand All @@ -184,7 +185,30 @@ public void findByColumn() {
}
}
""";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\"}";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\", \"active_user\":true}";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.docs[0]", jsonEquals(expected));
}

@Test
@Order(2)
public void findByBooleanColumn() {
String json =
"""
{
"find": {
"filter" : {"active_user" : true}
}
}
""";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\", \"active_user\":true}";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void getBooleanMapValues() {
final Map.Entry<QueryOuterClass.Value, QueryOuterClass.Value> next =
to.entrySet().iterator().next();
assertThat(Values.string(next.getKey())).isEqualTo("field1");
assertThat(Values.bool(next.getValue())).isEqualTo(true);
assertThat(Values.tinyint(next.getValue())).isEqualTo((byte) 1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.stargate.sgv3.docsapi.service.operation.model.impl;

import static org.assertj.core.api.Assertions.assertThat;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.stargate.sgv2.common.testprofiles.NoGlobalResourcesTestProfile;
import io.stargate.sgv3.docsapi.api.model.command.CommandContext;
import io.stargate.sgv3.docsapi.api.model.command.CommandResult;
import io.stargate.sgv3.docsapi.api.model.command.CommandStatus;
import io.stargate.sgv3.docsapi.service.bridge.AbstractValidatingStargateBridgeTest;
import io.stargate.sgv3.docsapi.service.bridge.executor.QueryExecutor;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import javax.inject.Inject;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@QuarkusTest
@TestProfile(NoGlobalResourcesTestProfile.Impl.class)
public class CreateCollectionOperationTest extends AbstractValidatingStargateBridgeTest {
private static final String KEYSPACE_NAME = RandomStringUtils.randomAlphanumeric(16);
private static final String COLLECTION_NAME = RandomStringUtils.randomAlphanumeric(16);
private CommandContext commandContext = new CommandContext(KEYSPACE_NAME, COLLECTION_NAME);

@Inject QueryExecutor queryExecutor;

@Nested
class CreateCollectionOperationsTest {

@Test
public void createCollection() throws Exception {
List<String> queries = getAllQueryString(KEYSPACE_NAME, COLLECTION_NAME);
queries.stream().forEach(query -> withQuery(query).returningNothing());

CreateCollectionOperation createCollectionOperation =
new CreateCollectionOperation(commandContext, COLLECTION_NAME);

final Supplier<CommandResult> execute =
createCollectionOperation.execute(queryExecutor).subscribeAsCompletionStage().get();
CommandResult result = execute.get();
assertThat(result)
.satisfies(
commandResult -> {
assertThat(result.status().get(CommandStatus.OK)).isNotNull();
});
}
}

private List<String> getAllQueryString(String database, String collection) {
List<String> queries = new ArrayList<>();
String create =
"CREATE TABLE IF NOT EXISTS %s.%s ("
+ " key text,"
+ " tx_id timeuuid, "
+ " doc_json text,"
+ " doc_properties map<text, int>,"
+ " exist_keys set<text>,"
+ " sub_doc_equals set<text>,"
+ " array_size map<text, int>,"
+ " array_equals map<text, text>,"
+ " array_contains set<text>,"
+ " query_bool_values map<text, tinyint>,"
+ " query_dbl_values map<text, decimal>,"
+ " query_text_values map<text, text>, "
+ " query_null_values set<text>, "
+ " PRIMARY KEY (key))";
queries.add(create.formatted(database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_doc_properties ON %s.%s (entries(doc_properties)) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_exists_keys ON %s.%s (exist_keys) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_sub_doc_equals ON %s.%s (sub_doc_equals) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_array_size ON %s.%s (entries(array_size)) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_array_equals ON %s.%s (entries(array_equals)) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_array_contains ON %s.%s (array_contains) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_bool_values ON %s.%s (entries(query_bool_values)) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_dbl_values ON %s.%s (entries(query_dbl_values)) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_text_values ON %s.%s (entries(query_text_values)) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
queries.add(
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_null_values ON %s.%s (query_null_values) USING 'StorageAttachedIndex'"
.formatted(collection, database, collection));
return queries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,61 @@ public void findWithDynamic() throws Exception {
});
}

@Test
public void findWithBooleanFilter() throws Exception {
String collectionReadCql =
"SELECT key, tx_id, doc_json FROM \"%s\".\"%s\" WHERE query_bool_values[?] = ? LIMIT 1"
.formatted(KEYSPACE_NAME, COLLECTION_NAME);
String doc1 =
"""
{
"_id": "doc1",
"username": "user1",
"registration_active" : true
}
""";
ValidatingStargateBridge.QueryAssert candidatesAssert =
withQuery(collectionReadCql, Values.of("registration_active"), Values.of((byte) 1))
.withPageSize(1)
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
.setName("key")
.setType(TypeSpecs.VARCHAR)
.build(),
QueryOuterClass.ColumnSpec.newBuilder()
.setName("tx_id")
.setType(TypeSpecs.UUID)
.build(),
QueryOuterClass.ColumnSpec.newBuilder()
.setName("doc_json")
.setType(TypeSpecs.VARCHAR)
.build()))
.returning(
List.of(
List.of(Values.of("doc1"), Values.of(UUID.randomUUID()), Values.of(doc1))));
FindOperation findOperation =
new FindOperation(
commandContext,
List.of(
new FindOperation.BoolFilter(
"registration_active", FindOperation.MapFilterBase.Operator.EQ, true)),
null,
1,
1,
true,
objectMapper);
final Supplier<CommandResult> execute =
findOperation.execute(queryExecutor).subscribeAsCompletionStage().get();
CommandResult result = execute.get();
assertThat(result)
.satisfies(
commandResult -> {
assertThat(result.data()).isNotNull();
assertThat(result.data().docs()).hasSize(1);
});
}

@Test
public void findWithNoResult() throws Exception {
String collectionReadCql =
Expand Down

0 comments on commit 23ca80a

Please sign in to comment.