@Override public StartState call() { return Criteria.where(); }
/** * Translate the {@code criteria} to its Thrift analog. * * @param criteria * @return the analogous TCriteria */ public static TCriteria translateToThriftCriteria(Criteria criteria) { List<TSymbol> symbols = Lists.newArrayList(); for (Symbol symbol : criteria.getSymbols()) { symbols.add(translateToThriftSymbol(symbol)); } return new TCriteria(symbols); }
/** * Add a sub {@code criteria} to the Criteria that is building. A sub * criteria is one that is wrapped in parenthesis. * * @param criteria * @return the builder */ public BuildableStartState group(Criteria criteria) { this.criteria.add(criteria); return new BuildableStartState(this.criteria); }
/** * Translate the {@code tcriteria} to its Java analog. * * @param tcriteria * @return the analogous Java {@link Criteria} */ public static Criteria translateFromThriftCriteria(TCriteria tcriteria) { Criteria criteria = new Criteria(); for (TSymbol tsymbol : tcriteria.getSymbols()) { criteria.add(translateFromThriftSymbol(tsymbol)); } return criteria; }
/** * Return this {@link Criteria} with each expression (e.g. {key} {operator} * {values}) pinned to the specified {@code timestamp}. * * <strong>NOTE:</strong> Any timestamps that are pinned to any expressions * within this Criteria will be replaced by the specified {@code timestamp}. * * @param timestamp the {@link Timestamp} to which the returned * {@link Criteria} is pinned * * @return this {@link Criteria} pinned to {@code timestamp} */ public Criteria at(Timestamp timestamp) { Parser parser = Parsers.create(getCclString()); List<Symbol> symbols = Parsing.groupExpressions(parser.tokenize()); TimestampSymbol ts = new TimestampSymbol(timestamp.getMicros()); symbols.forEach((symbol) -> { if(symbol instanceof Expression) { Expression expression = (Expression) symbol; Reflection.set("timestamp", ts, expression); // (authorized) } }); Criteria criteria = new Criteria(); symbols = Parsing.ungroupExpressions(symbols); criteria.symbols = symbols; return criteria; }
@Test public void testParseCclLocalReferences() { Criteria criteria = Criteria.where().key("name") .operator(Operator.EQUALS).value("Lebron James").build(); String ccl = "name = $name"; Multimap<String, Object> data = LinkedHashMultimap.create(); data.put("name", "Lebron James"); data.put("age", 30); data.put("team", "Cleveland Cavaliers"); Parser parser = Parsers.create(ccl, data); Assert.assertEquals(Parsing.toPostfixNotation(criteria.getSymbols()), parser.order()); }
@Test public void testTimestampPinning() { Criteria criteria = Criteria.where().key("name") .operator(Operator.EQUALS).value("Jeff Nelson").and() .group(Criteria.where().key("company").operator(Operator.EQUALS) .value("Cinchapi").or().key("company") .operator(Operator.EQUALS).value("Blavity")) .build(); Timestamp timestamp = Timestamp.now(); criteria = criteria.at(timestamp); List<Symbol> symbols = Parsing.groupExpressions(criteria.getSymbols()); symbols.forEach((symbol) -> { if(symbol instanceof Expression) { Expression expression = (Expression) symbol; Assert.assertEquals(expression.raw().timestamp(), timestamp.getMicros()); } }); }
@Override public String toString() { return getCclString(); }
@Test public void testParseCcl() { String ccl = "name = jeff AND (company = Cinchapi at 12345 or company = Blavity)"; Criteria criteria = Criteria.parse(ccl); Parser parser1 = Parsers.create(ccl); Parser parser2 = Parsers.create(criteria.getCclString()); Assert.assertEquals(Parsing.groupExpressions(parser1.tokenize()), Parsing.groupExpressions(parser2.tokenize())); }
/** * Start building a new {@link Criteria}. * * @return the Criteria builder */ public static StartState where() { return new StartState(new Criteria()); }
/** * Build and return the {@link Criteria}. * * @return the built Criteria */ public final Criteria build() { criteria.close(); return criteria; }
@Test public void testParseCclBetweenWithBothReferences() { Criteria criteria = Criteria.where().key("age") .operator(Operator.BETWEEN).value(30).value(35).build(); String ccl = "where age bw $age $retireAge"; Multimap<String, Object> data = LinkedHashMultimap.create(); data.put("name", "Lebron James"); data.put("age", 30); data.put("retireAge", 35); data.put("team", "Cleveland Cavaliers"); Parser parser = Parsers.create(ccl, data); Assert.assertEquals(Parsing.toPostfixNotation(criteria.getSymbols()), parser.order()); }
@Test public void testTimestampPinningSomeTimestamps() { Criteria criteria = Criteria.where().key("name") .operator(Operator.EQUALS).value("Jeff Nelson").and() .group(Criteria.where().key("company").operator(Operator.EQUALS) .value("Cinchapi").at(Timestamp.now()).or() .key("company").operator(Operator.EQUALS) .value("Blavity")) .build(); Timestamp timestamp = Timestamp.now(); criteria = criteria.at(timestamp); List<Symbol> symbols = Parsing.groupExpressions(criteria.getSymbols()); symbols.forEach((symbol) -> { if(symbol instanceof Expression) { Expression expression = (Expression) symbol; Assert.assertEquals(expression.raw().timestamp(), timestamp.getMicros()); } }); }
/** * Return a {@link Parser} for the {@code criteria}. * * @param criteria * @return a {@link Parser} */ public static Parser create(Criteria criteria) { return create(criteria.getCclString()); }
/** * Return a {@link Criteria} object that expresses the same as the * {@code ccl} statement. * * @param ccl the CCL statement to parse * @return an equivalanet {@link Criteria} object */ public static Criteria parse(String ccl) { Parser parser = Parsers.create(ccl); Criteria criteria = new Criteria(); try { criteria.symbols = Lists.newArrayList(parser.tokenize()); return criteria; } catch (Exception e) { if(e instanceof SyntaxException || e instanceof IllegalStateException || e.getCause() != null && e .getCause() instanceof com.cinchapi.ccl.v2.generated.ParseException) { throw new ParseException( new com.cinchapi.concourse.thrift.ParseException( e.getMessage())); } else { throw CheckedExceptions.throwAsRuntimeException(e); } } }
@Test public void testParseCclBetweenWithSecondReference() { Criteria criteria = Criteria.where().key("age") .operator(Operator.BETWEEN).value(5).value(30).build(); String ccl = "where age bw 5 $age"; Multimap<String, Object> data = LinkedHashMultimap.create(); data.put("name", "Lebron James"); data.put("age", 30); data.put("team", "Cleveland Cavaliers"); Parser parser = Parsers.create(ccl, data); Assert.assertEquals(Parsing.toPostfixNotation(criteria.getSymbols()), parser.order()); }
@Test public void testRecordRetrievalMatchingId() { Set<Long> set = Sets.newHashSet(); set.add(new Long(35)); Assert.assertEquals(set, client.find(Criteria.where() .key(Constants.JSON_RESERVED_IDENTIFIER_NAME) .operator(Operator.EQUALS).value(35).build())); }
/** * Build a conjunctive clause onto the {@link Criteria} that is building. * * @return the builder */ public StartState and() { criteria.add(ConjunctionSymbol.AND); return new StartState(criteria); }
/** * Return a {@link Parser} for the {@code criteria} that uses the provided * {@code data} for local resolution. * * @param criteria * @param data a dataset * @return a {@link Parser} */ public static Parser create(Criteria criteria, Multimap<String, Object> data) { return create(criteria.getCclString(), data); }