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

The following examples show how to use com.googlecode.cqengine.IndexedCollection#addAll() . 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: IndexOrderingDemo.java    From cqengine with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.addIndex(NavigableIndex.onAttribute(Car.FEATURES));
    cars.addIndex(NavigableIndex.onAttribute(forObjectsMissing(Car.FEATURES)));
    cars.addAll(CarFactory.createCollectionOfCars(100));

    ResultSet<Car> results = cars.retrieve(
            between(Car.CAR_ID, 40, 50),
            queryOptions(
                    orderBy(ascending(missingLast(Car.FEATURES))),
                    applyThresholds(threshold(INDEX_ORDERING_SELECTIVITY, 1.0))
            )
    );
    for (Car car : results) {
        System.out.println(car); // prints cars 40 -> 50, using the index on Car.FEATURES to accelerate ordering
    }
}
 
Example 2
Source File: NavigableIndexTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexQuantization_Between() {
    IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
    collection.addIndex(NavigableIndex.withQuantizerOnAttribute(IntegerQuantizer.withCompressionFactor(10), Car.CAR_ID));
    collection.addAll(CarFactory.createCollectionOfCars(100));

    Query<Car> query = between(Car.CAR_ID, 88, 92);
    assertEquals(5, collection.retrieve(query).size());
    assertEquals(asList(88, 89, 90, 91, 92), retrieveCarIds(collection, query));

    query = between(Car.CAR_ID, 88, true, 92, true);
    assertEquals(5, collection.retrieve(query).size());
    assertEquals(asList(88, 89, 90, 91, 92), retrieveCarIds(collection, query));

    query = between(Car.CAR_ID, 88, false, 92, true);
    assertEquals(4, collection.retrieve(query).size());
    assertEquals(asList(89, 90, 91, 92), retrieveCarIds(collection, query));

    query = between(Car.CAR_ID, 88, true, 92, false);
    assertEquals(4, collection.retrieve(query).size());
    assertEquals(asList(88, 89, 90, 91), retrieveCarIds(collection, query));

    query = between(Car.CAR_ID, 88, false, 92, false);
    assertEquals(3, collection.retrieve(query).size());
    assertEquals(asList(89, 90, 91), retrieveCarIds(collection, query));
}
 
Example 3
Source File: DiskPersistenceTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpand() {
    final long bytesToExpand = 102400;  // Expand by 100KB;
    DiskPersistence<Car, Integer> persistence = DiskPersistence.onPrimaryKey(Car.CAR_ID);
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(persistence);
    cars.addAll(CarFactory.createCollectionOfCars(50));
    persistence.compact();
    long initialBytesUsed = persistence.getBytesUsed();
    Assert.assertTrue("Initial bytes used should be greater than zero: " + initialBytesUsed, initialBytesUsed > 0);
    persistence.expand(bytesToExpand);
    long bytesUsedAfterExpanding = persistence.getBytesUsed();
    Assert.assertTrue("Bytes used after expanding (" + bytesUsedAfterExpanding + ") should have been increased by at least bytes to expand (" + bytesToExpand + ") above initial bytes used (" + initialBytesUsed + ")", bytesUsedAfterExpanding >= (initialBytesUsed + bytesToExpand));
    persistence.compact();
    long bytesUsedAfterCompaction = persistence.getBytesUsed();
    Assert.assertTrue("Bytes used after compaction (" + bytesUsedAfterCompaction + ") should be equal to initial bytes used (" + initialBytesUsed + ")", bytesUsedAfterCompaction == initialBytesUsed);
    Assert.assertTrue("Failed to delete temp file:" + persistence.getFile(), persistence.getFile().delete());
}
 
Example 4
Source File: HashIndexTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetStatisticsForDistinctKeys(){
    IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
    KeyStatisticsIndex<String, Car> MANUFACTURER_INDEX = HashIndex.onAttribute(Car.MANUFACTURER);
    collection.addIndex(MANUFACTURER_INDEX);

    collection.addAll(CarFactory.createCollectionOfCars(20));

    Set<KeyStatistics<String>> keyStatistics = setOf(MANUFACTURER_INDEX.getStatisticsForDistinctKeys(noQueryOptions()));
    Assert.assertEquals(setOf(
                    new KeyStatistics<String>("Ford", 6),
                    new KeyStatistics<String>("Honda", 6),
                    new KeyStatistics<String>("Toyota", 6),
                    new KeyStatistics<String>("BMW", 2)

            ),
            keyStatistics);
}
 
Example 5
Source File: GenerateAttributeByteCode.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    // Generate an attribute from bytecode to read the Car.model field...
    Map<String, ? extends Attribute<Car, ?>> attributes = AttributeBytecodeGenerator.createAttributes(Car.class);
    Attribute<Car, String> MODEL = (Attribute<Car, String>) attributes.get("model");

    // Create a collection of 10 Car objects (Ford Focus, Honda Civic etc.)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.addAll(CarFactory.createCollectionOfCars(10));

    // Retrieve the cars whose Car.model field is "Civic" (i.e. the Honda Civic)...
    ResultSet<Car> results = cars.retrieve(equal(MODEL, "Civic"));
    for (Car car : results) {
        System.out.println(car);
    }
    // ..prints:
    // Car{carId=3, manufacturer='Honda', model='Civic', color=WHITE, doors=5, price=4000.0, features=[grade b]}
}
 
Example 6
Source File: SQLQueryDemo.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    SQLParser<Car> parser = SQLParser.forPojoWithAttributes(Car.class, createAttributes(Car.class));
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.addAll(CarFactory.createCollectionOfCars(10));

    ResultSet<Car> results = parser.retrieve(cars, "SELECT * FROM cars WHERE (" +
                                    "(manufacturer = 'Ford' OR manufacturer = 'Honda') " +
                                    "AND price <= 5000.0 " +
                                    "AND color NOT IN ('GREEN', 'WHITE')) " +
                                    "ORDER BY manufacturer DESC, price ASC");

    results.forEach(System.out::println); // Prints: Honda Accord, Ford Fusion, Ford Focus
}
 
Example 7
Source File: HashIndex_ManufacturerFord.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    this.collection = collection;
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    this.indexedCollection = indexedCollection1;
    this.indexedCollection.addIndex(HashIndex.onAttribute(Car.MANUFACTURER));
}
 
Example 8
Source File: AllTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void testAll() {
    IndexedCollection<Integer> indexedCollection = new ConcurrentIndexedCollection<Integer>();
    indexedCollection.addAll(asList(1, 2, 3, 4, 5));
    IndexedCollection<Integer> collection = indexedCollection;
    Query<Integer> query = all(Integer.class);
    ResultSet<Integer> results = collection.retrieve(query);
    assertEquals(5, results.size());
    assertTrue(results.iterator().hasNext());
}
 
Example 9
Source File: NavigableIndex_PriceBetween.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    this.collection = collection;
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    this.indexedCollection = indexedCollection1;
    this.indexedCollection.addIndex(NavigableIndex.onAttribute(Car.PRICE));
}
 
Example 10
Source File: HashIndex_ModelFocus.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    this.collection = collection;
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    this.indexedCollection = indexedCollection1;
    this.indexedCollection.addIndex(HashIndex.onAttribute(Car.MODEL));
}
 
Example 11
Source File: OffHeapPersistenceTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompact() {
    OffHeapPersistence<Car, Integer> persistence = OffHeapPersistence.onPrimaryKey(Car.CAR_ID);
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(persistence);
    cars.addAll(CarFactory.createCollectionOfCars(100));
    long bytesUsedWhenFullyPopulated = persistence.getBytesUsed();
    Assert.assertTrue("Bytes used when fully populated should be greater than zero: " + bytesUsedWhenFullyPopulated, bytesUsedWhenFullyPopulated > 0);
    cars.removeAll(CarFactory.createCollectionOfCars(100));
    long bytesUsedWhenObjectsRemoved = persistence.getBytesUsed();
    Assert.assertTrue("Bytes used when objects removed (" + bytesUsedWhenObjectsRemoved + ") should remain the same as when fully populated (" + bytesUsedWhenFullyPopulated + ")", bytesUsedWhenObjectsRemoved == bytesUsedWhenFullyPopulated);
    persistence.compact(); // Truncates size of the database, but not to zero as the tables which were created remain (although empty)
    long bytesUsedAfterCompaction = persistence.getBytesUsed();
    Assert.assertTrue("Bytes used after compaction (" + bytesUsedAfterCompaction + ") should be less than when fully populated (" + bytesUsedWhenFullyPopulated + ")", bytesUsedAfterCompaction < bytesUsedWhenFullyPopulated);
}
 
Example 12
Source File: NavigableIndexTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndexQuantization_LastBucket() {
    IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
    collection.addIndex(NavigableIndex.withQuantizerOnAttribute(IntegerQuantizer.withCompressionFactor(10), Car.CAR_ID));
    collection.addAll(CarFactory.createCollectionOfCars(100));

    // Merge cost should be 10, because objects matching this query are in a single bucket...
    assertEquals(10, collection.retrieve(between(Car.CAR_ID, 96, 98)).getMergeCost());

    // 3 objects match the query...
    assertEquals(3, collection.retrieve(between(Car.CAR_ID, 96, 98)).size());

    List<Integer> carIdsFound = retrieveCarIds(collection, between(Car.CAR_ID, 96, 98));
    assertEquals(asList(96, 97, 98), carIdsFound);
}
 
Example 13
Source File: NoneTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoneOr() {
    IndexedCollection<Integer> indexedCollection = new ConcurrentIndexedCollection<Integer>();
    indexedCollection.addAll(asList(1, 2, 3, 4, 5));
    IndexedCollection<Integer> collection = indexedCollection;
    final Or<Integer> query = or(
            none(Integer.class),
            lessThan(selfAttribute(Integer.class), 3)
    );
    ResultSet<Integer> results = collection.retrieve(query);
    assertEquals(2, results.size());
    assertTrue(results.iterator().hasNext());
}
 
Example 14
Source File: HashIndexTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDistinctKeysAndCounts() {
    IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
    KeyStatisticsIndex<String, Car> MODEL_INDEX = HashIndex.onAttribute(Car.MODEL);
    collection.addIndex(MODEL_INDEX);

    collection.addAll(CarFactory.createCollectionOfCars(20));

    Set<String> distinctModels = setOf(MODEL_INDEX.getDistinctKeys(noQueryOptions()));
    Assert.assertEquals(setOf("Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius", "Taurus"), distinctModels);
    for (String model : distinctModels) {
        Assert.assertEquals(Integer.valueOf(2), MODEL_INDEX.getCountForKey(model, noQueryOptions()));
    }
}
 
Example 15
Source File: UniqueIndex_CarId.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    indexedCollection = indexedCollection1;
}
 
Example 16
Source File: HashIndex_Model.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    indexedCollection = indexedCollection1;
}
 
Example 17
Source File: CompoundIndex_ManufacturerColorDoors.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    indexedCollection = indexedCollection1;
}
 
Example 18
Source File: RadixTreeIndex_Model.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    indexedCollection = indexedCollection1;
}
 
Example 19
Source File: HashIndex_CarId.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Collection<Car> collection) {
    IndexedCollection<Car> indexedCollection1 = new ConcurrentIndexedCollection<Car>();
    indexedCollection1.addAll(collection);
    indexedCollection = indexedCollection1;
}
 
Example 20
Source File: DiskPersistenceConcurrencyTest.java    From cqengine with Apache License 2.0 4 votes vote down vote up
@Test
public void testDiskPersistenceConcurrency_JournalModeDelete() {
    File tempFile = DiskPersistence.createTempFile();
    final IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(
            DiskPersistence.onPrimaryKeyInFileWithProperties(
                Car.CAR_ID,
                tempFile,
                new Properties() {{
                    setProperty("journal_mode", "DELETE");
                }}
            )
    );
    collection.addAll(CarFactory.createCollectionOfCars(100));

    final List<String> sequenceLog = Collections.synchronizedList(new ArrayList<String>());
    ExecutorService executorService = Executors.newCachedThreadPool();

    executorService.submit(new ReadingTask("ReadingTask 1", collection, 1500, sequenceLog)); // start immediately, pause for 1 second, then resume to completion
    sleep(500);
    executorService.submit(new ReadingTask("ReadingTask 2", collection, 1500, sequenceLog)); // start immediately, pause for 1 second, then resume to completion
    sleep(500);
    executorService.submit(new WritingTask("WritingTask 1", collection, sequenceLog)); // start this task after waiting 500ms

    executorService.shutdown();
    awaitTermination(executorService);

    List<String> expected = Arrays.asList(
            "ReadingTask 1 started and about to access collection",
            "ReadingTask 1 pausing mid-read", // Successfully acquired the first read lock
            "ReadingTask 2 started and about to access collection",
            "ReadingTask 2 pausing mid-read", // Successfully acquired a second read lock concurrently
            "WritingTask 1 started and about to access collection", // Should block until both reads finish
            "ReadingTask 1 resuming read",
            "ReadingTask 1 finished reading 20 items",
            "ReadingTask 2 resuming read",
            "ReadingTask 2 finished reading 20 items",
            "WritingTask 1 finished removing 1 item" // Finally was granted write lock

    );
    Assert.assertEquals(expected, sequenceLog);
    tempFile.delete();
}