Skip to content

Commit

Permalink
feat: Support for comparison filters in search/read (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
adilansari authored Jul 27, 2022
1 parent 1f317b0 commit 46e2a2c
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

enum ComparisonOperator {
EQUALS("$eq"),
LT("$lt"),
LTE("$lte"),
GT("$gt"),
GTE("$gte"),
NONE("");

private final String operator;
Expand Down
176 changes: 176 additions & 0 deletions client/src/main/java/com/tigrisdata/db/client/Filters.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,182 @@ public static SelectorFilter<UUID> eq(String key, UUID value) {
return new SelectorFilter<>(ComparisonOperator.EQUALS, key, value);
}

/**
* Creates less than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Integer> lt(String key, int value) {
return new SelectorFilter<>(ComparisonOperator.LT, key, value);
}

/**
* Creates less than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Long> lt(String key, long value) {
return new SelectorFilter<>(ComparisonOperator.LT, key, value);
}

/**
* Creates less than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Float> lt(String key, float value) {
return new SelectorFilter<>(ComparisonOperator.LT, key, value);
}

/**
* Creates less than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Double> lt(String key, double value) {
return new SelectorFilter<>(ComparisonOperator.LT, key, value);
}

/**
* Creates less than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Integer> lte(String key, int value) {
return new SelectorFilter<>(ComparisonOperator.LTE, key, value);
}

/**
* Creates less than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Long> lte(String key, long value) {
return new SelectorFilter<>(ComparisonOperator.LTE, key, value);
}

/**
* Creates less than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Float> lte(String key, float value) {
return new SelectorFilter<>(ComparisonOperator.LTE, key, value);
}

/**
* Creates less than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Double> lte(String key, double value) {
return new SelectorFilter<>(ComparisonOperator.LTE, key, value);
}

/**
* Creates greater than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Integer> gt(String key, int value) {
return new SelectorFilter<>(ComparisonOperator.GT, key, value);
}

/**
* Creates greater than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Long> gt(String key, long value) {
return new SelectorFilter<>(ComparisonOperator.GT, key, value);
}

/**
* Creates greater than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Float> gt(String key, float value) {
return new SelectorFilter<>(ComparisonOperator.GT, key, value);
}

/**
* Creates greater than filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Double> gt(String key, double value) {
return new SelectorFilter<>(ComparisonOperator.GT, key, value);
}

/**
* Creates greater than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Integer> gte(String key, int value) {
return new SelectorFilter<>(ComparisonOperator.GTE, key, value);
}

/**
* Creates greater than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Long> gte(String key, long value) {
return new SelectorFilter<>(ComparisonOperator.GTE, key, value);
}

/**
* Creates greater than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Integer}
*/
public static SelectorFilter<Float> gte(String key, float value) {
return new SelectorFilter<>(ComparisonOperator.GTE, key, value);
}

/**
* Creates greater than equals filter for given key and value
*
* @param key field key
* @param value field value
* @return constructed {@link SelectorFilter} of type {@link Long}
*/
public static SelectorFilter<Double> gte(String key, double value) {
return new SelectorFilter<>(ComparisonOperator.GTE, key, value);
}

/**
* Creates a composite logical OR filter from input filters
*
Expand Down
24 changes: 18 additions & 6 deletions client/src/main/java/com/tigrisdata/db/client/SelectorFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

class SelectorFilter<T> implements TigrisFilter {
final class SelectorFilter<T> implements TigrisFilter {

private final ComparisonOperator comparisonOperator;
private final String key;
Expand All @@ -34,12 +35,23 @@ class SelectorFilter<T> implements TigrisFilter {

@Override
public String toJSON(ObjectMapper objectMapper) {
if (comparisonOperator == ComparisonOperator.NONE) {
// filters nothing
return "{}";
}
Map<String, Object> map = new LinkedHashMap<>();
map.put(key, val);
switch (comparisonOperator) {
case NONE:
return "{}";
case EQUALS:
map.put(key, val);
break;
default:
Map<String, Object> nestedMap =
new HashMap<String, Object>() {
{
put(comparisonOperator.getOperator(), val);
}
};
map.put(key, nestedMap);
}

try {
return objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
Expand Down
Loading

0 comments on commit 46e2a2c

Please sign in to comment.