@Test public void testDateMath() { // Create a collection of Order objects, with shipDates 2015-08-01, 2015-08-02 and 2015-08-03... IndexedCollection<Order> collection = new ConcurrentIndexedCollection<Order>(); collection.add(createOrder("2015-08-01")); collection.add(createOrder("2015-08-02")); collection.add(createOrder("2015-08-03")); // Create a parser for CQN queries on Order objects... CQNParser<Order> parser = CQNParser.forPojoWithAttributes(Order.class, createAttributes(Order.class)); // Register a DateMathParser which can parse date math expressions // relative to the given date value for "now" ("2015-08-04"). // The custom value for "now" can be omitted to have it always calculate relative to the current // time... parser.registerValueParser(Date.class, new DateMathParser(createDate("2015-08-04"))); // Retrieve orders whose ship date is between 3 days ago and 2 days ago... ResultSet<Order> results = parser.retrieve(collection, "between(\"shipDate\", \"-3DAYS\", \"-2DAYS\")"); // Assert that the following two orders are returned... Assert.assertTrue(results.contains(createOrder("2015-08-01"))); Assert.assertTrue(results.contains(createOrder("2015-08-02"))); Assert.assertEquals(2, results.size()); }
public static void main(String[] args) { IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(); cars.addAll(CarFactory.createCollectionOfCars(100000)); cars.addIndex(NavigableIndex.onAttribute(Car.CAR_ID)); QueryLog queryLog = new QueryLog(System.out); ResultSet<Car> resultSet = cars.retrieve( has(Car.CAR_ID), queryOptions( orderBy(descending(Car.CAR_ID)), queryLog, applyThresholds(threshold(INDEX_ORDERING_SELECTIVITY, 1.0)))); { long start = System.currentTimeMillis(); int count = IteratorUtil.countElements(resultSet); long timeTaken = System.currentTimeMillis() - start; System.out.println("Time taken for " + count + " results: " + timeTaken); } System.out.println(); { long start = System.currentTimeMillis(); int count = IteratorUtil.countElements(resultSet); long timeTaken = System.currentTimeMillis() - start; System.out.println("Time taken for " + count + " results: " + timeTaken); } }