Skip to content

Commit

Permalink
Changes to support $eq comparison operator. (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshrajamani authored Jan 28, 2023
1 parent 7039dad commit 61164e7
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/stargate/sgv3/docsapi/StargateDocsV3Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"""
{
"findOne": {
"filter": {"location": "London"}
"filter": {"location": "London", "race.competitors" : {"$eq" : 100}}
}
}
"""),
Expand All @@ -69,7 +69,7 @@
"""
{
"find": {
"filter": {"location": "London"},
"filter": {"location": "London", "race.competitors" : {"$eq" : 100}},
"options": {"limit" : 1000, "pageSize": 25, "pagingState" : "Next paging state got from previous page call"}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public record ComparisonExpression(
@Valid @NotEmpty List<FilterOperation<?>> filterOperations) {

/**
* Shortcut to create equals against a literal
* Shortcut to create equals against a literal, mare condition cannot be added using add().
*
* <p>e.g. {"username" : "aaron"}
*
Expand All @@ -35,6 +35,19 @@ public static ComparisonExpression eq(String path, Object value) {
List.of(new ValueComparisonOperation<>(ValueComparisonOperator.EQ, getLiteral(value))));
}

/**
* Adds a comparison operation
*
* <p>e.g. {"username" : "aaron"}
*
* @param path Json node path
* @param value Value returned by the deserializer
* @return {@link ComparisonExpression} with equal operator
*/
public void add(ValueComparisonOperator operator, Object value) {
filterOperations.add(new ValueComparisonOperation<>(operator, getLiteral(value)));
}

/**
* Create Typed JsonLiteral object for the value
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
package io.stargate.sgv3.docsapi.api.model.command.clause.filter;

/** List of value operator that can be used in Filter clause */
import io.stargate.sgv3.docsapi.exception.DocsException;
import io.stargate.sgv3.docsapi.exception.ErrorCode;
import java.util.HashMap;
import java.util.Map;

/**
* List of value operator that can be used in Filter clause Have commented the unsupported
* operators, will add it as we support them
*/
public enum ValueComparisonOperator implements FilterOperator {
EQ,
GT,
GTE,
LT,
LTE,
NE
EQ("$eq");
/*GT("$gt"),
GTE("$gte"),
LT("$lt"),
LTE("$lte"),
NE("$ne");*/

private String operator;

ValueComparisonOperator(String operator) {
this.operator = operator;
}

private static final Map<String, ValueComparisonOperator> operatorMap = new HashMap<>();

static {
for (ValueComparisonOperator filterOperator : ValueComparisonOperator.values()) {
operatorMap.put(filterOperator.operator, filterOperator);
}
}

public static ValueComparisonOperator getComparisonOperator(String operator) {
final ValueComparisonOperator valueComparisonOperator = operatorMap.get(operator);
if (valueComparisonOperator == null)
throw new DocsException(
ErrorCode.UNSUPPORTED_FILTER_OPERATION, "Unsupported filter operation " + operator);

return valueComparisonOperator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.ComparisonExpression;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.ValueComparisonOperator;
import io.stargate.sgv3.docsapi.exception.DocsException;
import io.stargate.sgv3.docsapi.exception.ErrorCode;
import java.io.IOException;
Expand All @@ -22,7 +23,10 @@ public FilterClauseDeserializer() {
super(FilterClause.class);
}

/** {@inheritDoc} */
/**
* {@inheritDoc} Filter clause can follow short cut {"field" : "value"} instead of {"field" :
* {"$eq" : "value"}}
*/
@Override
public FilterClause deserialize(
JsonParser jsonParser, DeserializationContext deserializationContext)
Expand All @@ -35,13 +39,42 @@ public FilterClause deserialize(
Map.Entry<String, JsonNode> entry = fieldIter.next();
// TODO: Does not handle logical expressions, they are out of scope
JsonNode operatorExpression = entry.getValue();
if (!operatorExpression.isValueNode())
throw new DocsException(ErrorCode.UNSUPPORTED_FILTER_DATA_TYPE);
expressionList.add(ComparisonExpression.eq(entry.getKey(), jsonNodeValue(entry.getValue())));
if (operatorExpression.isObject()) {
expressionList.add(createComparisonExpression(entry));
} else {
// @TODO: Need to add array value type to this condition
expressionList.add(
ComparisonExpression.eq(entry.getKey(), jsonNodeValue(entry.getValue())));
}
}
return new FilterClause(expressionList);
}

/**
* The filter clause is entry will have field path as key and object type as value. The value can
* have multiple operator and condition values.
*
* <p>Eg 1: {"field" : {"$eq" : "value"}}
*
* <p>Eg 2: {"field" : {"$gt" : 10, "$lt" : 50}}
*
* @param entry
* @return
*/
private ComparisonExpression createComparisonExpression(Map.Entry<String, JsonNode> entry) {
ComparisonExpression expression = new ComparisonExpression(entry.getKey(), new ArrayList<>());
final Iterator<Map.Entry<String, JsonNode>> fields = entry.getValue().fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> updateField = fields.next();
ValueComparisonOperator operator =
ValueComparisonOperator.getComparisonOperator(updateField.getKey());
JsonNode value = updateField.getValue();
// @TODO: Need to add array and sub-document value type to this condition
expression.add(operator, jsonNodeValue(value));
}
return expression;
}

private static Object jsonNodeValue(JsonNode node) {
switch (node.getNodeType()) {
case BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public ReadOperation apply(CommandContext commandContext, T command) {
.filter(Optional::isPresent)
.map(Optional::get) // unwraps the Optional from the resolver function.
.findFirst()
.orElseThrow(() -> new DocsException(ErrorCode.FILTER_UNRESOLVABLE));
.orElseThrow(
() ->
new DocsException(
ErrorCode.FILTER_UNRESOLVABLE,
"Filter type not supported, unable to resolve to a filtering strategy"));
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import org.junit.jupiter.api.Nested;
Expand All @@ -26,6 +27,20 @@ public void stringValueComparisonExpression() throws Exception {
assertThat(result).isEqualTo(expectedResult);
}

@Test
public void multiValueComparisonExpression() throws Exception {
final ComparisonExpression expectedResult =
new ComparisonExpression(
"username",
List.of(
new ValueComparisonOperation(
ValueComparisonOperator.EQ, new JsonLiteral("abc", JsonType.STRING))));

ComparisonExpression result = new ComparisonExpression("username", new ArrayList<>());
result.add(ValueComparisonOperator.EQ, "abc");
assertThat(result).isEqualTo(expectedResult);
}

@Test
public void numberValueComparisonExpression() throws Exception {
final ComparisonExpression expectedResult =
Expand Down Expand Up @@ -95,10 +110,6 @@ public void matchTest() throws Exception {
comparisonExpression.match(
"path", EnumSet.of(ValueComparisonOperator.EQ), JsonType.STRING);
assertThat(match).hasSize(0);

match =
comparisonExpression.match("path", EnumSet.of(ValueComparisonOperator.GT), JsonType.NULL);
assertThat(match).hasSize(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ public void happyPath() throws Exception {
assertThat(filterClause.comparisonExpressions()).hasSize(1).contains(expectedResult);
}

@Test
public void eqComparisonOperator() throws Exception {
String json = """
{"username": {"$eq" : "aaron"}}
""";

FilterClause filterClause = objectMapper.readValue(json, FilterClause.class);
final ComparisonExpression expectedResult =
new ComparisonExpression(
"username",
List.of(
new ValueComparisonOperation(
ValueComparisonOperator.EQ, new JsonLiteral("aaron", JsonType.STRING))));
assertThat(filterClause).isNotNull();
assertThat(filterClause.comparisonExpressions()).hasSize(1).contains(expectedResult);
}

@Test
public void mustHandleNull() throws Exception {
String json = "null";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.http.ContentType;
Expand Down Expand Up @@ -137,6 +138,7 @@ public void findById() {
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.count", is(1))
.body("data.docs[0]", jsonEquals(expected));
}

Expand All @@ -160,9 +162,56 @@ public void findByColumn() {
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.count", is(1))
.body("data.docs[0]", jsonEquals(expected));
}

@Test
@Order(2)
public void findWithEqComparisonOperator() {
String json =
"""
{
"find": {
"filter" : {"username" : {"$eq" : "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.count", is(1))
.body("data.docs[0]", jsonEquals(expected));
}

@Test
@Order(2)
public void findWithNEComparisonOperator() {
String json =
"""
{
"find": {
"filter" : {"username" : {"$ne" : "user1"}}
}
}
""";
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("errors[1].message", startsWith("Unsupported filter operation $ne"));
}

@Test
@Order(2)
public void findByBooleanColumn() {
Expand Down Expand Up @@ -280,6 +329,7 @@ public void findOneById() {
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.count", is(1))
.body("data.docs[0]", jsonEquals(expected));
}

Expand All @@ -303,6 +353,7 @@ public void findOneByColumn() {
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.count", is(1))
.body("data.docs[0]", jsonEquals(expected));
}
}
Expand Down

0 comments on commit 61164e7

Please sign in to comment.