diff --git a/README.md b/README.md index a7949c9..19ca36f 100644 --- a/README.md +++ b/README.md @@ -36,18 +36,61 @@ When modifying the grammar file you will need to re-generate the ANTLR sources b ## Usage examples -### Filter expression +### Parsing DSL expression + ```java -String input = "$.intBin1 > 100"; -Expression expression = ConditionTranslator.translate(input); +import com.aerospike.dsl.api.DSLParser; +import com.aerospike.dsl.impl.DSLParserImpl; + +// String DSL expression +String input = "$.intBin1 > 100 and $.intBin2 > 100"; +// Instantiating DSL parser +DSLParser parser = new DSLParserImpl(); +// Providing list of existing secondary indexes +// At most only one index will be chosen based on its namespace and binValuesRatio (cardinality) +// If cardinality of multiple indexes is the same, the index is chosen alphabetically +List indexes = List.of( + Index.builder().namespace("namespace").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("namespace2").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build() +); +// Parsing DSL expression +ParsedExpression expression = parser.parseExpression( + // We provide expression context (it may also contain placeholders values as an optional argument) + ExpressionContext.of(input), + // And optional index context with the required namespace and a list of existing indexes + IndexContext.of("namespace", indexes) +); ``` -Will return the following expression: + +### Getting filter expression + ```java -Exp.build( +Expression actualExp = Exp.build( + // Getting filter expression + expression.getResult().getExp() +); + +// Only one filter expression because the first part has secondary index filter +Expression expectedExp = Exp.build( Exp.gt( - Exp.intBin("intBin1"), + Exp.intBin("intBin2"), Exp.val(100)) ); + +assertEquals(actualExp, expectedExp); +``` + +### Getting secondary index filter + +```java +// Getting secondary index filter +// Only one secondary index filter can be created at most +Filter actualFilter = expression.getResult().getFilter(); + +// The second part has filter expression +Filter expectedFilter = Filter.range("intBin1", 101, Long.MAX_VALUE); + +assertEquals(actualFilter, expectedFilter); ``` ## License