Java Code Examples for com.googlecode.cqengine.IndexedCollection#add()

The following examples show how to use com.googlecode.cqengine.IndexedCollection#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeduplicationTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeduplication_Materialize() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.add(new Car(1, "Ford", "Focus", BLUE, 5, 1000.0, Collections.<String>emptyList(), Collections.emptyList()));
    cars.addIndex(HashIndex.onAttribute(Car.COLOR));
    cars.addIndex(HashIndex.onAttribute(Car.MANUFACTURER));

    Query<Car> query = or(
            equal(COLOR, BLUE),
            equal(MANUFACTURER, "Ford")
    );
    ResultSet<Car> results;
    results = cars.retrieve(query);
    assertEquals(2, results.size());

    DeduplicationOption deduplicate = deduplicate(DeduplicationStrategy.MATERIALIZE);
    results = cars.retrieve(query, queryOptions(deduplicate));
    assertEquals(1, results.size());
}
 
Example 2
Source File: DeduplicationTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeduplication_Logical() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.add(new Car(1, "Ford", "Focus", BLUE, 5, 1000.0, Collections.<String>emptyList(), Collections.emptyList()));
    cars.addIndex(HashIndex.onAttribute(Car.COLOR));
    cars.addIndex(HashIndex.onAttribute(Car.MANUFACTURER));

    Query<Car> query = or(
            equal(COLOR, BLUE),
            equal(MANUFACTURER, "Ford")
    );
    ResultSet<Car> results;
    results = cars.retrieve(query);
    assertEquals(2, results.size());

    DeduplicationOption deduplicate = deduplicate(DeduplicationStrategy.LOGICAL_ELIMINATION);
    results = cars.retrieve(query, queryOptions(deduplicate));
    assertEquals(1, results.size());
}
 
Example 3
Source File: Replace.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates how to concurrently replace a Car in a collection, using multi-version concurrency control.
 * <p/>
 * Prints:
 * <pre>
 * The only car in the collection, before the replacement: Car{carId=1, name='Ford Focus', version=1}
 * Collection contains 2 cars, but we filtered the duplicate: Car{carId=1, name='New Ford Focus', version=2}
 * Collection contains 1 car again: Car{carId=1, name='New Ford Focus', version=2}
 * </pre>
 *
 * @param args Not used
 */
public static void main(String[] args) {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    // Add a car with carId 1...
    cars.add(new Car(1, "Ford Focus"));

    // Test the retrieval...
    Car carFound = retrieveOnlyOneVersion(cars, 1);
    System.out.println("The only car in the collection, before the replacement: " + carFound);

    // Update the name of the Car with carId 1, by replacing it using MVCC...
    Car oldVersion = cars.retrieve(equal(Car.CAR_ID, 1)).uniqueResult(); // Retrieve the existing version
    Car newVersion = new Car(1, "New Ford Focus"); // Create a new car, same carId, different version
    cars.add(newVersion); // Collection now contains two versions of the same car

    // Test the retrieval (collection contains both versions, should retrieve only one of them)...
    carFound = retrieveOnlyOneVersion(cars, 1);
    System.out.println("Collection contains " + cars.size() + " cars, but we filtered the duplicate: " + carFound);

    cars.remove(oldVersion); // Remove the old version, collection now only contains new version

    // Test the retrieval...
    carFound = retrieveOnlyOneVersion(cars, 1);
    System.out.println("Collection contains " + cars.size() + " car again: " + carFound);
}
 
Example 4
Source File: UniqueIndexTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testUniqueIndex() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    // Add some indexes...
    cars.addIndex(UniqueIndex.onAttribute(Car.CAR_ID));
    cars.addIndex(HashIndex.onAttribute(Car.CAR_ID));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford focus", "great condition, low mileage", Arrays.asList("spare tyre", "sunroof")));
    cars.add(new Car(2, "ford taurus", "dirty and unreliable, flat tyre", Arrays.asList("spare tyre", "radio")));
    cars.add(new Car(3, "honda civic", "has a flat tyre and high mileage", Arrays.asList("radio")));

    Query<Car> query = equal(Car.CAR_ID, 2);
    ResultSet<Car> rs = cars.retrieve(query);
    Assert.assertEquals("should prefer unique index over hash index", UniqueIndex.INDEX_RETRIEVAL_COST, rs.getRetrievalCost());

    Assert.assertEquals("should retrieve car 2", 2, rs.uniqueResult().carId);
}
 
Example 5
Source File: ThreeWayJoin.java    From cqengine with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    IndexedCollection<User> users = new ConcurrentIndexedCollection<User>();
    users.add(new User(1, "Joe"));
    users.add(new User(2, "Jane"));
    users.add(new User(3, "Jesse"));

    IndexedCollection<Role> roles = new ConcurrentIndexedCollection<Role>();
    roles.add(new Role(1, "CEO"));
    roles.add(new Role(2, "Manager"));
    roles.add(new Role(3, "Employee"));

    IndexedCollection<UserRole> userRoles = new ConcurrentIndexedCollection<UserRole>();
    userRoles.add(new UserRole(1, 3)); // Joe is an Employee
    userRoles.add(new UserRole(2, 2)); // Jane is a Manager
    userRoles.add(new UserRole(3, 2)); // Jesse is a Manager

    // Retrieve Users who are managers...
    Query<User> usersWhoAreManagers =
            existsIn(userRoles, User.USER_ID, UserRole.USER_ID,
                    existsIn(roles, UserRole.ROLE_ID, Role.ROLE_ID, equal(Role.ROLE_NAME, "Manager")));

    for (User u : users.retrieve(usersWhoAreManagers)) {
        System.out.println(u.userName);
    }
    // ..prints: Jane, Jesse
}
 
Example 6
Source File: HasTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testExists() {
    // Create an indexed collection (note: could alternatively use CQEngine.copyFrom() existing collection)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    Attribute<Car, String> NAME = new SimpleNullableAttribute<Car, String>("name") {
        public String getValue(Car car, QueryOptions queryOptions) { return car.name; }
    };
    // Add some indexes...
    cars.addIndex(StandingQueryIndex.onQuery(has(NAME)));
    cars.addIndex(StandingQueryIndex.onQuery(not(has(NAME))));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford focus", "great condition, low mileage", Arrays.asList("spare tyre", "sunroof")));
    cars.add(new Car(2, null, "dirty and unreliable, flat tyre", Arrays.asList("spare tyre", "radio")));
    cars.add(new Car(3, "honda civic", "has a flat tyre and high mileage", Arrays.asList("radio")));

    Assert.assertEquals(cars.retrieve(has(NAME)).size(), 2);
    Assert.assertEquals(cars.retrieve(not(has(NAME))).size(), 1);
}
 
Example 7
Source File: InTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testInNone() {
    // Create an indexed collection (note: could alternatively use CQEngine.copyFrom() existing collection)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    Attribute<Car, String> NAME = new SimpleNullableAttribute<Car, String>("name") {
        public String getValue(Car car, QueryOptions queryOptions) {
            return car.name;
        }
    };
    cars.addIndex(NavigableIndex.onAttribute(NAME));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford", null, null));
    cars.add(new Car(2, "honda", null, null));
    cars.add(new Car(3, "toyota", null, null));

    Assert.assertEquals(cars.retrieve(in(NAME)).size(), 0);
    Assert.assertEquals(cars.retrieve(in(NAME, new ArrayList<String>())).size(), 0);
}
 
Example 8
Source File: ReflectiveAttributeTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testReflectiveAttribute() {
    // Create an indexed collection (note: could alternatively use CQEngine.copyFrom() existing collection)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    // Define an attribute which will use reflection...
    Attribute<Car, String> NAME = ReflectiveAttribute.forField(Car.class, String.class, "name");

    cars.addIndex(HashIndex.onAttribute(NAME));
    // Add some objects to the collection...
    cars.add(new Car(1, "ford focus", "great condition, low mileage", Arrays.asList("spare tyre", "sunroof")));
    cars.add(new Car(2, "ford taurus", "dirty and unreliable, flat tyre", Arrays.asList("spare tyre", "radio")));
    cars.add(new Car(3, "honda civic", "has a flat tyre and high mileage", Arrays.asList("radio")));

    Assert.assertEquals(cars.retrieve(equal(NAME, "honda civic")).size(), 1);
}
 
Example 9
Source File: CQNDateMathTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 10
Source File: InTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testInMany() {
    // Create an indexed collection (note: could alternatively use CQEngine.copyFrom() existing collection)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    Attribute<Car, String> NAME = new SimpleNullableAttribute<Car, String>("name") {
        public String getValue(Car car, QueryOptions queryOptions) {
            return car.name;
        }
    };
    cars.addIndex(NavigableIndex.onAttribute(NAME));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford", null, null));
    cars.add(new Car(2, "honda", null, null));
    cars.add(new Car(3, "toyota", null, null));

    Assert.assertEquals(cars.retrieve(in(NAME, "ford", "honda")).size(), 2);
    Assert.assertEquals(cars.retrieve(in(NAME, Arrays.asList("ford", "honda"))).size(), 2);
}
 
Example 11
Source File: InTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testInOne() {
    // Create an indexed collection (note: could alternatively use CQEngine.copyFrom() existing collection)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    Attribute<Car, String> NAME = new SimpleNullableAttribute<Car, String>("name") {
        public String getValue(Car car, QueryOptions queryOptions) {
            return car.name;
        }
    };
    cars.addIndex(NavigableIndex.onAttribute(NAME));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford", null, null));
    cars.add(new Car(2, "honda", null, null));
    cars.add(new Car(3, "toyota", null, null));

    Assert.assertEquals(cars.retrieve(in(NAME, "ford")).size(), 1);
    Assert.assertEquals(cars.retrieve(in(NAME, Collections.singletonList("ford"))).size(), 1);
}
 
Example 12
Source File: UniqueIndexTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test(expected = UniqueIndex.UniqueConstraintViolatedException.class)
public void testDuplicateObjectDetection_MultiValueAttribute() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    // Add some indexes...
    cars.addIndex(UniqueIndex.onAttribute(Car.FEATURES));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford focus", "foo", Arrays.asList("spare tyre", "sunroof")));
    cars.add(new Car(2, "ford taurus", "bar", Arrays.asList("radio", "cd player")));

    // Try to add another car which has a cd player, when one car already has a cd player...
    cars.add(new Car(3, "honda civic", "baz", Arrays.asList("cd player", "bluetooth")));
}
 
Example 13
Source File: UniqueIndexTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test(expected = UniqueIndex.UniqueConstraintViolatedException.class)
public void testDuplicateObjectDetection_SimpleAttribute() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    // Add some indexes...
    cars.addIndex(UniqueIndex.onAttribute(Car.CAR_ID));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford focus", "great condition, low mileage", Arrays.asList("spare tyre", "sunroof")));
    cars.add(new Car(2, "ford taurus", "dirty and unreliable, flat tyre", Arrays.asList("spare tyre", "radio")));
    cars.add(new Car(3, "honda civic", "has a flat tyre and high mileage", Arrays.asList("radio")));

    cars.add(new Car(2, "some other car", "foo", Arrays.asList("bar")));
}
 
Example 14
Source File: WrappingPersistenceTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrappingPersistence() {
    Collection<Car> backingCollection = new LinkedHashSet<Car>();
    backingCollection.addAll(CarFactory.createCollectionOfCars(3)); // CarIds 0, 1, 2

    IndexedCollection<Car> indexedCollection = new ConcurrentIndexedCollection<Car>(
            WrappingPersistence.aroundCollection(backingCollection)
    );

    indexedCollection.addIndex(NavigableIndex.onAttribute(Car.CAR_ID));

    ResultSet<Car> results = indexedCollection.retrieve(greaterThan(Car.CAR_ID, 0));

    // Assert that the index will be used...
    assertNotEquals(Integer.MAX_VALUE, results.getRetrievalCost());

    // Assert correct results are returned...
    Set<Integer> expectedCarIds, actualCarIds;
    expectedCarIds = asSet(1, 2);
    actualCarIds = extractCarIds(results, new HashSet<Integer>());
    assertEquals(expectedCarIds, actualCarIds);

    // Add that a new object added to the IndexedCollection...
    indexedCollection.add(CarFactory.createCar(3));

    // Assert the new object was added to the backing collection...
    expectedCarIds = asSet(0, 1, 2, 3);
    actualCarIds = extractCarIds(backingCollection, new HashSet<Integer>());
    assertEquals(expectedCarIds, actualCarIds);
}
 
Example 15
Source File: DynamicExample.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    // Generate attributes dynamically for fields in the given POJO...
    Map<String, Attribute<Car, Comparable>> attributes = DynamicIndexer.generateAttributesForPojo(Car.class);
    // Build indexes on the dynamically generated attributes...
    IndexedCollection<Car> cars = DynamicIndexer.newAutoIndexedCollection(attributes.values());

    // Add some objects to the collection...
    cars.add(new Car(1, "ford", "focus", 4, 9000));
    cars.add(new Car(2, "ford", "mondeo", 5, 10000));
    cars.add(new Car(2, "ford", "fiesta", 3, 2000));
    cars.add(new Car(3, "honda", "civic", 5, 11000));

    Query<Car> query = and(
            equal(attributes.get("manufacturer"), "ford"),
            lessThan(attributes.get("doors"), value(5)),
            greaterThan(attributes.get("horsepower"), value(3000))
    );
    ResultSet<Car> results = cars.retrieve(query);

    System.out.println("Ford cars with less than 5 doors and horsepower greater than 3000:- ");
    System.out.println("Using NavigableIndex: " + (results.getRetrievalCost() == 40));
    for (Car car : results) {
        System.out.println(car);
    }

    // Prints:
    //    Ford cars with less than 5 doors and horsepower greater than 3000:-
    //    Using NavigableIndex: true
    //    Car{carId=1, manufacturer='ford', model='focus', doors=4, horsepower=9000}
}
 
Example 16
Source File: Introduction.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // Create an indexed collection (note: could alternatively use CQEngine.copyFrom() existing collection)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    // Add some indexes...
    cars.addIndex(NavigableIndex.onAttribute(Car.CAR_ID));
    cars.addIndex(ReversedRadixTreeIndex.onAttribute(Car.NAME));
    cars.addIndex(SuffixTreeIndex.onAttribute(Car.DESCRIPTION));
    cars.addIndex(HashIndex.onAttribute(Car.FEATURES));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford focus", "great condition, low mileage", Arrays.asList("spare tyre", "sunroof")));
    cars.add(new Car(2, "ford taurus", "dirty and unreliable, flat tyre", Arrays.asList("spare tyre", "radio")));
    cars.add(new Car(3, "honda civic", "has a flat tyre and high mileage", Arrays.asList("radio")));

    // -------------------------- Run some queries --------------------------
    System.out.println("Cars whose name ends with 'vic' or whose id is less than 2:");
    Query<Car> query1 = or(endsWith(Car.NAME, "vic"), lessThan(Car.CAR_ID, 2));
    cars.retrieve(query1).forEach(System.out::println);

    System.out.println("\nCars whose flat tyre can be replaced:");
    Query<Car> query2 = and(contains(Car.DESCRIPTION, "flat tyre"), equal(Car.FEATURES, "spare tyre"));
    cars.retrieve(query2).forEach(System.out::println);


    System.out.println("\nCars which have a sunroof or a radio but are not dirty:");
    Query<Car> query3 = and(in(Car.FEATURES, "sunroof", "radio"), not(contains(Car.DESCRIPTION, "dirty")));
    cars.retrieve(query3).forEach(System.out::println);
}
 
Example 17
Source File: SqlExists.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // Create an indexed collection of cars...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.add(new Car(1, "Ford Focus", "great condition, low mileage", asList("spare tyre", "sunroof")));
    cars.add(new Car(2, "Ford Taurus", "dirty and unreliable, flat tyre", asList("spare tyre", "radio")));
    cars.add(new Car(3, "Honda Civic", "has a flat tyre and high mileage", asList("radio")));
    cars.add(new Car(4, "BMW M3", "2013 model", asList("radio", "convertible")));

    // Create an indexed collection of garages...
    final IndexedCollection<Garage> garages = new ConcurrentIndexedCollection<Garage>();
    garages.add(new Garage(1, "Joe's garage", "London", asList("Ford Focus", "Honda Civic")));
    garages.add(new Garage(2, "Jane's garage", "Dublin", asList("BMW M3")));
    garages.add(new Garage(3, "John's garage", "Dublin", asList("Ford Focus", "Ford Taurus")));
    garages.add(new Garage(4, "Jill's garage", "Dublin", asList("Ford Focus")));

    // Query: Cars which are convertible or which have a sunroof, which can be serviced in Dublin...
    Query<Car> carsQuery = and(
            in(Car.FEATURES, "sunroof", "convertible"),
            existsIn(garages,
                    Car.NAME,
                    Garage.BRANDS_SERVICED,
                    equal(Garage.LOCATION, "Dublin")
            )
    );

    for (Car car : cars.retrieve(carsQuery)) {
        System.out.println(car.name + " has a sunroof or is convertible, and can be serviced in Dublin");
    }
    /* ..prints:

        BMW M3 has a sunroof or is convertible, and can be serviced in Dublin
        Ford Focus has a sunroof or is convertible, and can be serviced in Dublin
     */
}
 
Example 18
Source File: SqlExistsBasedJoin.java    From cqengine with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    // Create an indexed collection of cars...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.add(new Car(1, "Ford Focus", "great condition, low mileage", asList("spare tyre", "sunroof")));
    cars.add(new Car(2, "Ford Taurus", "dirty and unreliable, flat tyre", asList("spare tyre", "radio")));
    cars.add(new Car(3, "Honda Civic", "has a flat tyre and high mileage", asList("radio")));
    cars.add(new Car(4, "BMW M3", "2013 model", asList("radio", "convertible")));

    // Create an indexed collection of garages...
    final IndexedCollection<Garage> garages = new ConcurrentIndexedCollection<Garage>();
    garages.add(new Garage(1, "Joe's garage", "London", asList("Ford Focus", "Honda Civic")));
    garages.add(new Garage(2, "Jane's garage", "Dublin", asList("BMW M3")));
    garages.add(new Garage(3, "John's garage", "Dublin", asList("Ford Focus", "Ford Taurus")));
    garages.add(new Garage(4, "Jill's garage", "Dublin", asList("Ford Focus")));

    // Query: Cars which are convertible or which have a sunroof, which can be serviced in Dublin...
    Query<Car> carsQuery = and(
            in(Car.FEATURES, "sunroof", "convertible"),
            existsIn(garages,
                    Car.NAME,
                    Garage.BRANDS_SERVICED,
                    equal(Garage.LOCATION, "Dublin")
            )
    );

    for (Car car : cars.retrieve(carsQuery)) {
        Query<Garage> garagesWhichServiceThisCarInDublin
                = and(equal(Garage.BRANDS_SERVICED, car.name), equal(Garage.LOCATION, "Dublin"));
        boolean first = true;
        for (Garage garage : garages.retrieve(garagesWhichServiceThisCarInDublin)) {
            if (first) {
                // Print this only when we have actually retrieved the first garage, in case the
                // collection was modified removing the first garage before the inner loop :)...
                System.out.println(car.name + " has a sunroof or is convertible, " +
                        "and can be serviced in Dublin at the following garages:- " );
                first = false;
            }
            System.out.println("---> " + garage);
        }
        System.out.println();
    }
    /* ..prints:

        BMW M3 has a sunroof or is convertible, and can be serviced in Dublin at the following garages:-
        ---> Garage{garageId=2, name='Jane's garage', location='Dublin', brandsServiced=[BMW M3]}

        Ford Focus has a sunroof or is convertible, and can be serviced in Dublin at the following garages:-
        ---> Garage{garageId=3, name='John's garage', location='Dublin', brandsServiced=[Ford Focus, Ford Taurus]}
        ---> Garage{garageId=4, name='Jill's garage', location='Dublin', brandsServiced=[Ford Focus]}
     */
}
 
Example 19
Source File: GeneralFunctionalTest.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Test
public void testGeneralFunctionality() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    cars.addIndex(HashIndex.onAttribute(Car.MODEL));
    cars.addIndex(HashIndex.onAttribute(Car.COLOR));
    cars.addIndex(NavigableIndex.onAttribute(Car.DOORS));
    cars.addIndex(RadixTreeIndex.onAttribute(Car.MODEL));
    cars.addIndex(ReversedRadixTreeIndex.onAttribute(Car.MODEL));
    cars.addIndex(InvertedRadixTreeIndex.onAttribute(Car.MODEL));
    cars.addIndex(SuffixTreeIndex.onAttribute(Car.MODEL));

    cars.add(new Car(1, "Ford",   "Focus",  Car.Color.BLUE,  5, 9000.50, Collections.<String>emptyList(), Collections.emptyList()));
    cars.add(new Car(2, "Ford",   "Fiesta", Car.Color.BLUE,  2, 5000.00, Collections.<String>emptyList(), Collections.emptyList()));
    cars.add(new Car(3, "Ford",   "F-150",  Car.Color.RED,   2, 9500.00, Collections.<String>emptyList(), Collections.emptyList()));
    cars.add(new Car(4, "Honda",  "Civic",  Car.Color.RED,   5, 5000.00, Collections.<String>emptyList(), Collections.emptyList()));
    cars.add(new Car(5, "Toyota", "Prius",  Car.Color.BLACK, 3, 9700.00, Collections.<String>emptyList(), Collections.emptyList()));

    // Ford cars...
    assertThat(carIdsIn(cars.retrieve(equal(Car.MANUFACTURER, "Ford"))), is(setOf(1, 2, 3)));

    // 3-door cars...
    assertThat(carIdsIn(cars.retrieve(equal(Car.DOORS, 3))), is(setOf(5)));

    // 2 or 3-door cars...
    assertThat(carIdsIn(cars.retrieve(between(Car.DOORS, 2, 3))), is(setOf(2, 3, 5)));

    // 2 or 5-door cars...
    assertThat(carIdsIn(cars.retrieve(in(Car.DOORS, 2, 5))), is(setOf(1, 2, 3, 4)));

    // Blue Ford cars...
    assertThat(carIdsIn(cars.retrieve(and(equal(Car.COLOR, Car.Color.BLUE), equal(Car.MANUFACTURER, "Ford")))), is(setOf(1, 2)));

    // NOT 3-door cars...
    assertThat(carIdsIn(cars.retrieve(not(equal(Car.DOORS, 3)))), is(setOf(1, 2, 3, 4)));

    // Cars which have 5 doors and which are not red...
    assertThat(carIdsIn(cars.retrieve(and(equal(Car.DOORS, 5), not(equal(Car.COLOR, Car.Color.RED))))), is(setOf(1)));

    // Cars whose model starts with 'F'...
    assertThat(carIdsIn(cars.retrieve(startsWith(Car.MODEL, "F"))), is(setOf(1, 2, 3)));

    // Cars whose model ends with 's'...
    assertThat(carIdsIn(cars.retrieve(endsWith(Car.MODEL, "s"))), is(setOf(1, 5)));

    // Cars whose model contains 'i'...
    assertThat(carIdsIn(cars.retrieve(contains(Car.MODEL, "i"))), is(setOf(2, 4, 5)));

    // Cars whose model is contained in 'Banana, Focus, Civic, Foobar'...
    assertThat(carIdsIn(cars.retrieve(isContainedIn(Car.MODEL, "Banana, Focus, Civic, Foobar"))), is(setOf(1, 4)));

    // NOT 3-door cars, sorted by doors ascending...
    assertThat(
            carIdsIn(cars.retrieve(not(equal(Car.DOORS, 3)), queryOptions(orderBy(ascending(Car.DOORS))))).toString(),
            is(equalTo(setOf(2, 3, 1, 4).toString()))
    );

    // NOT 3-door cars, sorted by doors ascending then price descending...
    assertThat(
            carIdsIn(
                    cars.retrieve(
                            not(equal(Car.DOORS, 3)),
                            queryOptions(
                                    orderBy(ascending(Car.DOORS),
                                            descending(Car.PRICE))
                            )
                    )
            ).toString(),
            is(equalTo(setOf(3, 2, 1, 4).toString()))
    );
}
 
Example 20
Source File: RegularMapTest.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Test
public void testMapFunctionality() {
    IndexedCollection<Map> cars = new ConcurrentIndexedCollection<Map>();

    cars.addIndex(HashIndex.onAttribute(COLOR));
    cars.addIndex(NavigableIndex.onAttribute(DOORS));
    cars.addIndex(RadixTreeIndex.onAttribute(MODEL));
    cars.addIndex(ReversedRadixTreeIndex.onAttribute(MODEL));
    cars.addIndex(InvertedRadixTreeIndex.onAttribute(MODEL));
    cars.addIndex(SuffixTreeIndex.onAttribute(MODEL));

    cars.add(buildNewCar(1, "Ford",   "Focus",  Car.Color.BLUE,  5, 9000.50, Collections.<String>emptyList()));
    cars.add(buildNewCar(2, "Ford",   "Fiesta", Car.Color.BLUE,  2, 5000.00, Collections.<String>emptyList()));
    cars.add(buildNewCar(3, "Ford",   "F-150",  Car.Color.RED,   2, 9500.00, Collections.<String>emptyList()));
    cars.add(buildNewCar(4, "Honda",  "Civic",  Car.Color.RED,   5, 5000.00, Collections.<String>emptyList()));
    cars.add(buildNewCar(5, "Toyota", "Prius",  Car.Color.BLACK, 3, 9700.00, Collections.<String>emptyList()));

    // Ford cars...
    assertThat(carIdsIn(cars.retrieve(equal(MANUFACTURER, "Ford"))), is(setOf(1, 2, 3)));

    // 3-door cars...
    assertThat(carIdsIn(cars.retrieve(equal(DOORS, 3))), is(setOf(5)));

    // 2 or 3-door cars...
    assertThat(carIdsIn(cars.retrieve(between(DOORS, 2, 3))), is(setOf(2, 3, 5)));

    // 2 or 5-door cars...
    assertThat(carIdsIn(cars.retrieve(in(DOORS, 2, 5))), is(setOf(1, 2, 3, 4)));

    // Blue Ford cars...
    assertThat(carIdsIn(cars.retrieve(and(equal(COLOR, Car.Color.BLUE),
            equal(MANUFACTURER, "Ford")))), is(setOf(1, 2)));

    // NOT 3-door cars...
    assertThat(carIdsIn(cars.retrieve(not(equal(DOORS, 3)))),
            is(setOf(1, 2, 3, 4)));

    // Cars which have 5 doors and which are not red...
    assertThat(carIdsIn(cars.retrieve(and(equal(DOORS, 5), not(equal(COLOR, Car.Color.RED))))), is(setOf(1)));

    // Cars whose model starts with 'F'...
    assertThat(carIdsIn(cars.retrieve(startsWith(MODEL, "F"))), is(setOf(1, 2, 3)));

    // Cars whose model ends with 's'...
    assertThat(carIdsIn(cars.retrieve(endsWith(MODEL, "s"))), is(setOf(1, 5)));

    // Cars whose model contains 'i'...
    assertThat(carIdsIn(cars.retrieve(contains(MODEL, "i"))), is(setOf(2, 4, 5)));

    // Cars whose model is contained in 'Banana, Focus, Civic, Foobar'...
    assertThat(carIdsIn(cars.retrieve(isContainedIn(MODEL, "Banana, Focus, Civic, Foobar"))), is(setOf(1, 4)));

    // NOT 3-door cars, sorted by doors ascending...
    assertThat(
            carIdsIn(cars.retrieve(not(equal(DOORS, 3)), queryOptions(orderBy(ascending(DOORS), ascending(MODEL))))).toString(),
            is(equalTo(setOf(3, 2, 4, 1).toString()))
    );

    // NOT 3-door cars, sorted by doors ascending then price descending...
    assertThat(
            carIdsIn(
                    cars.retrieve(
                            not(equal(DOORS, 3)),
                            queryOptions(
                                    orderBy(ascending(DOORS),
                                            descending(PRICE))
                            )
                    )
            ),
            is(equalTo(setOf(3, 2, 1, 4)))
    );
}