com.googlecode.cqengine.IndexedCollection Java Examples
The following examples show how to use
com.googlecode.cqengine.IndexedCollection.
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: PersistenceIndexingBenchmark.java From cqengine with Apache License 2.0 | 6 votes |
static void populateCollection(IndexedCollection<Car> collection, int total, int batchSize) { int count = 0; long start = System.currentTimeMillis(); Queue<Car> batch = new ArrayBlockingQueue<Car>(batchSize); for (Car next : CarFactory.createIterableOfCars(total)) { if (!batch.offer(next)) { if (BULK_IMPORT_FLAG) { collection.update(Collections.<Car>emptySet(), batch, QueryFactory.queryOptions(enableFlags(SQLiteIndexFlags.BULK_IMPORT))); } else { collection.update(Collections.<Car>emptySet(), batch); } count += batchSize; batch.clear(); batch.add(next); // printProgress(start, count, total); } } count += batch.size(); collection.addAll(batch); // printProgress(start, count, total); // System.out.println(); }
Example #2
Source File: DeduplicationTest.java From cqengine with Apache License 2.0 | 6 votes |
@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 #3
Source File: AttributeMetadataTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testGetFrequencyDistribution() { IndexedCollection<Car> cars = createIndexedCollectionOfCars(20); // Add an unsorted index on Car.MANUFACTURER (a HashIndex)... cars.addIndex(HashIndex.onAttribute(Car.MANUFACTURER)); MetadataEngine<Car> metadataEngine = cars.getMetadataEngine(); // Access metadata for Car.MANUFACTURER attribute. // Because we call getAttributeMetadata() (and not getSortedAttributeMetadata()), // values will be returned in no particular order... AttributeMetadata<String, Car> sortedAttributeMetadata = metadataEngine.getAttributeMetadata(Car.MANUFACTURER); // Call AttributeMetadata.getFrequencyDistribution() to retrieve distinct keys and counts in no particular order. // We retrieve into a Set (not a List), to assert the expected values were returned, without asserting any order... assertEquals( asSet(frequency("Ford", 6), frequency("BMW", 2), frequency("Toyota", 6), frequency("Honda", 6)), sortedAttributeMetadata.getFrequencyDistribution().collect(toSet()) ); }
Example #4
Source File: NavigableIndexTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testIndexQuantization_SpanningTwoBucketsMidRange() { 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 20 because this query spans 2 buckets (each containing 10 objects)... assertEquals(20, collection.retrieve(between(Car.CAR_ID, 47, 53)).getMergeCost()); // 7 objects match the query (between is inclusive)... assertEquals(7, collection.retrieve(between(Car.CAR_ID, 47, 53)).size()); // The matching objects are... List<Integer> carIdsFound = retrieveCarIds(collection, between(Car.CAR_ID, 47, 53)); assertEquals(asList(47, 48, 49, 50, 51, 52, 53), carIdsFound); }
Example #5
Source File: SortedAttributeMetadataTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testGetFrequencyDistributionDescending() { IndexedCollection<Car> cars = createIndexedCollectionOfCars(20); // Add a sorted index on Car.MANUFACTURER (a NavigableIndex)... cars.addIndex(NavigableIndex.onAttribute(Car.MANUFACTURER)); MetadataEngine<Car> metadataEngine = cars.getMetadataEngine(); // Access metadata for Car.MANUFACTURER attribute. // Because we call getSortedAttributeMetadata() values will be returned in ascending order... SortedAttributeMetadata<String, Car> sortedAttributeMetadata = metadataEngine.getSortedAttributeMetadata(Car.MANUFACTURER); // Call AttributeMetadata.getFrequencyDistribution() to retrieve distinct keys and counts in ascending order. // We retrieve into a List in order to assert the ordering of values returned... assertEquals( asList(frequency("Toyota", 6), frequency("Honda", 6), frequency("Ford", 6), frequency("BMW", 2)), sortedAttributeMetadata.getFrequencyDistributionDescending().collect(toList()) ); }
Example #6
Source File: SortedAttributeMetadataTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testGetFrequencyDistribution() { IndexedCollection<Car> cars = createIndexedCollectionOfCars(20); // Add a sorted index on Car.MANUFACTURER (a NavigableIndex)... cars.addIndex(NavigableIndex.onAttribute(Car.MANUFACTURER)); MetadataEngine<Car> metadataEngine = cars.getMetadataEngine(); // Access metadata for Car.MANUFACTURER attribute. // Because we call getSortedAttributeMetadata() values will be returned in ascending order... SortedAttributeMetadata<String, Car> sortedAttributeMetadata = metadataEngine.getSortedAttributeMetadata(Car.MANUFACTURER); // Call AttributeMetadata.getFrequencyDistribution() to retrieve distinct keys and counts in ascending order. // We retrieve into a List in order to assert the ordering of values returned... assertEquals( asList(frequency("BMW", 2), frequency("Ford", 6), frequency("Honda", 6), frequency("Toyota", 6)), sortedAttributeMetadata.getFrequencyDistribution().collect(toList()) ); }
Example #7
Source File: AttributeMetadataTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testGetKeysAndValues() { IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>(); Car car1 = new Car(1, "Ford", "Taurus", Car.Color.GREEN, 4, 1000.0, emptyList(), Collections.emptyList()); Car car2 = new Car(2, "Honda", "Civic", Car.Color.BLUE, 4, 2000.0, emptyList(), Collections.emptyList()); Car car3 = new Car(3, "Honda", "Accord", Car.Color.RED, 4, 3000.0, emptyList(), Collections.emptyList()); cars.addAll(asList(car1, car2, car3)); // Add an unsorted index on Car.MANUFACTURER (a HashIndex)... cars.addIndex(HashIndex.onAttribute(Car.MANUFACTURER)); MetadataEngine<Car> metadataEngine = cars.getMetadataEngine(); // Access metadata for Car.MODEL attribute. // Because we call getAttributeMetadata() (and not getSortedAttributeMetadata()), // values will be returned in no particular order... AttributeMetadata<String, Car> attributeMetadata = metadataEngine.getAttributeMetadata(Car.MANUFACTURER); // Call AttributeMetadata.getDistinctKeys() to retrieve distinct keys in no particular order. // We retrieve into a Set (not a List), to assert the expected values were returned, without asserting any order... assertEquals( asSet(keyValue("Ford", car1), keyValue("Honda", car2), keyValue("Honda", car3)), attributeMetadata.getKeysAndValues().collect(toSet()) ); }
Example #8
Source File: Replace.java From cqengine with Apache License 2.0 | 6 votes |
/** * 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 #9
Source File: IndexOrderingDemo.java From cqengine with Apache License 2.0 | 6 votes |
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 #10
Source File: DiskPersistenceTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test @Ignore public void testReadFromDisk() { Set<Car> collectionOfCars = CarFactory.createCollectionOfCars(50); Set<Integer> expectedCarIds = collectionOfCars.stream().map(Car::getCarId).collect(toSet()); File persistenceFile = new File("cqengine-persisted.dat"); System.out.println("Persistence file: " + persistenceFile.getAbsolutePath()); // Create a collection (it will read from the pre-existing file on disk)... DiskPersistence<Car, Integer> persistence = DiskPersistence.onPrimaryKeyInFile(Car.CAR_ID, persistenceFile); IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(persistence); // Retrieve the cars from disk... Set<Integer> actualCarIds = cars.stream().map(Car::getCarId).collect(toSet()); Assert.assertEquals(expectedCarIds, actualCarIds); System.out.println("Loaded from disk: " + actualCarIds); }
Example #11
Source File: NavigableIndexTest.java From cqengine with Apache License 2.0 | 6 votes |
@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 #12
Source File: OffHeapPersistenceTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testExpand() { final long bytesToExpand = 102400; // Expand by 100KB; OffHeapPersistence<Car, Integer> persistence = OffHeapPersistence.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); }
Example #13
Source File: InTest.java From cqengine with Apache License 2.0 | 6 votes |
@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 #14
Source File: InTest.java From cqengine with Apache License 2.0 | 6 votes |
@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 #15
Source File: NavigableIndexTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testGetDistinctKeysAndCounts() { IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(); SortedKeyStatisticsIndex<String, Car> MODEL_INDEX = NavigableIndex.onAttribute(Car.MODEL); collection.addIndex(MODEL_INDEX); collection.addAll(CarFactory.createCollectionOfCars(20)); Set<String> distinctModels = setOf(MODEL_INDEX.getDistinctKeys(noQueryOptions())); assertEquals(asList("Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius", "Taurus"), new ArrayList<String>(distinctModels)); for (String model : distinctModels) { assertEquals(Integer.valueOf(2), MODEL_INDEX.getCountForKey(model, noQueryOptions())); } Set<String> distinctModelsDescending = setOf(MODEL_INDEX.getDistinctKeysDescending(noQueryOptions())); assertEquals(asList("Taurus", "Prius", "M6", "Insight", "Hilux", "Fusion", "Focus", "Civic", "Avensis", "Accord"), new ArrayList<String>(distinctModelsDescending)); }
Example #16
Source File: CQNDateMathTest.java From cqengine with Apache License 2.0 | 6 votes |
@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 #17
Source File: GenerateAttributeByteCode.java From cqengine with Apache License 2.0 | 6 votes |
@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 #18
Source File: NavigableIndexTest.java From cqengine with Apache License 2.0 | 6 votes |
@Test public void testGetStatisticsForDistinctKeysDescending(){ IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(); SortedKeyStatisticsIndex<String, Car> MANUFACTURER_INDEX = NavigableIndex.onAttribute(Car.MANUFACTURER); collection.addIndex(MANUFACTURER_INDEX); collection.addAll(CarFactory.createCollectionOfCars(20)); Set<KeyStatistics<String>> keyStatistics = setOf(MANUFACTURER_INDEX.getStatisticsForDistinctKeysDescending(noQueryOptions())); Assert.assertEquals(setOf( new KeyStatistics<String>("Toyota", 6), new KeyStatistics<String>("Honda", 6), new KeyStatistics<String>("Ford", 6), new KeyStatistics<String>("BMW", 2) ), keyStatistics); }
Example #19
Source File: CompositePersistenceTest.java From cqengine with Apache License 2.0 | 5 votes |
/** * Tests a configuration where the collection is stored off-heap, one index is on-disk, and one index is on-heap. */ @Test public void testCompositePersistence_EndToEnd() { OffHeapPersistence<Car, Integer> offHeapPersistence = OffHeapPersistence.onPrimaryKey(Car.CAR_ID); DiskPersistence<Car, Integer> diskPersistence = DiskPersistence.onPrimaryKey(Car.CAR_ID); IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(CompositePersistence.of( offHeapPersistence, diskPersistence, singletonList(OnHeapPersistence.onPrimaryKey(Car.CAR_ID)) )); collection.addIndex(DiskIndex.onAttribute(Car.MANUFACTURER)); collection.addIndex(OffHeapIndex.onAttribute(Car.MODEL)); collection.addIndex(NavigableIndex.onAttribute(Car.PRICE)); collection.addAll(CarFactory.createCollectionOfCars(1000)); ResultSet<Car> results = null; try { results = collection.retrieve( and( or( equal(Car.MANUFACTURER, "Ford"), equal(Car.MODEL, "Avensis") ), lessThan(Car.PRICE, 6000.0) ) ); Assert.assertEquals(300, results.size()); Assert.assertTrue(offHeapPersistence.getBytesUsed() > 4096); // example: 163840 Assert.assertTrue(diskPersistence.getBytesUsed() > 4096); // example: 30720 } finally { CloseableRequestResources.closeQuietly(results); collection.clear(); offHeapPersistence.close(); diskPersistence.getFile().delete(); } }
Example #20
Source File: ExistsIn.java From cqengine with Apache License 2.0 | 5 votes |
public ExistsIn(IndexedCollection<F> foreignCollection, Attribute<O, A> localKeyAttribute, Attribute<F, A> foreignKeyAttribute, Query<F> foreignRestrictions) { super(requireNonNull(localKeyAttribute, "The localKeyAttribute cannot be null")); this.foreignCollection = requireNonNull(foreignCollection, "The foreignCollection cannot be null"); this.localKeyAttribute = requireNonNull(localKeyAttribute, "The localKeyAttribute cannot be null"); this.foreignKeyAttribute = requireNonNull(foreignKeyAttribute, "The foreignKeyAttribute cannot be null"); this.foreignRestrictions = foreignRestrictions; // ..this may be null }
Example #21
Source File: ExistsIn.java From cqengine with Apache License 2.0 | 5 votes |
/** * Checks if the given foreign collection contains objects which match the given query. * @param foreignCollection The foreign collection to check * @param query The query to check * @return True if the foreign collection contains one or more objects which match the query, otherwise false */ static <F> boolean foreignCollectionContains(IndexedCollection<F> foreignCollection, Query<F> query) { ResultSet<F> resultSet = foreignCollection.retrieve(query); try { return resultSet.isNotEmpty(); } finally { resultSet.close(); } }
Example #22
Source File: SQLParserTest.java From cqengine with Apache License 2.0 | 5 votes |
@Test public void testRetrieve() { IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(); cars.addAll(CarFactory.createCollectionOfCars(10)); ResultSet<Car> results = parser.retrieve(cars, "SELECT * FROM cars WHERE (manufacturer = 'Honda' AND color <> 'WHITE') ORDER BY carId DESC"); Assert.assertEquals(2, results.size()); Assert.assertEquals(asList(5, 4), extractCarIds(results, new ArrayList<Integer>())); }
Example #23
Source File: MetadataEngineTest.java From cqengine with Apache License 2.0 | 5 votes |
@Test public void testAttemptToAccessMetadataWithoutIndex() { IndexedCollection<Car> cars = createIndexedCollectionOfCars(5); @SuppressWarnings("unchecked") Attribute<Car, Integer> ATTRIBUTE = Mockito.mock(SimpleAttribute.class); Mockito.when(ATTRIBUTE.toString()).thenReturn("ATTRIBUTE"); IllegalStateException expected = null; try { cars.getMetadataEngine().getAttributeMetadata(ATTRIBUTE); } catch (IllegalStateException e) { expected = e; } assertNotNull(expected); assertEquals("A KeyStatisticsAttributeIndex has not been added to the collection, and must be added first, to enable metadata to be examined for attribute: ATTRIBUTE", expected.getMessage()); }
Example #24
Source File: DynamicExample.java From cqengine with Apache License 2.0 | 5 votes |
@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 #25
Source File: OffHeapPersistenceConcurrencyTest.java From cqengine with Apache License 2.0 | 5 votes |
@Test public void testOffHeapPersistenceConcurrency() { final IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(OffHeapPersistence.onPrimaryKey(Car.CAR_ID)); 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); }
Example #26
Source File: DiskPersistenceTest.java From cqengine with Apache License 2.0 | 5 votes |
@Test public void testGetBytesUsed() { DiskPersistence<Car, Integer> persistence = DiskPersistence.onPrimaryKey(Car.CAR_ID); @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(persistence); cars.addAll(CarFactory.createCollectionOfCars(50)); long bytesUsed = persistence.getBytesUsed(); Assert.assertTrue("Bytes used should be greater than zero: " + bytesUsed, bytesUsed > 0); Assert.assertTrue("Failed to delete temp file:" + persistence.getFile(), persistence.getFile().delete()); }
Example #27
Source File: Replace.java From cqengine with Apache License 2.0 | 5 votes |
static Car retrieveOnlyOneVersion(IndexedCollection<Car> cars, int carId) { Query<Car> query = equal(Car.CAR_ID, carId); ResultSet<Car> multipleCarVersions = cars.retrieve(query); // Wrap in a result set which will return only one car per version number... ResultSet<Car> deduplicatedCars = new DeduplicatingResultSet<Car, Integer>(Car.CAR_ID, multipleCarVersions, query, noQueryOptions()); return deduplicatedCars.uniqueResult(); }
Example #28
Source File: NavigableIndexTest.java From cqengine with Apache License 2.0 | 5 votes |
@Test public void testIndexQuantization_LessThan() { IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(); collection.addIndex(NavigableIndex.withQuantizerOnAttribute(IntegerQuantizer.withCompressionFactor(10), Car.CAR_ID)); collection.addAll(CarFactory.createCollectionOfCars(100)); assertEquals(5, collection.retrieve(lessThan(Car.CAR_ID, 5)).size()); assertEquals(15, collection.retrieve(lessThan(Car.CAR_ID, 15)).size()); assertEquals(6, collection.retrieve(lessThanOrEqualTo(Car.CAR_ID, 5)).size()); assertEquals(16, collection.retrieve(lessThanOrEqualTo(Car.CAR_ID, 15)).size()); }
Example #29
Source File: SortedAttributeMetadataTest.java From cqengine with Apache License 2.0 | 5 votes |
@Test public void testGetDistinctKeysDescending() { IndexedCollection<Car> cars = createIndexedCollectionOfCars(20); // the 20 cars will contain 10 distinct models // Add a sorted index on Car.MODEL (a NavigableIndex)... cars.addIndex(NavigableIndex.onAttribute(Car.MODEL)); MetadataEngine<Car> metadataEngine = cars.getMetadataEngine(); // Access metadata for Car.MODEL attribute. // Because we call getSortedAttributeMetadata(), values will be returned in sorted order... SortedAttributeMetadata<String, Car> sortedAttributeMetadata = metadataEngine.getSortedAttributeMetadata(Car.MODEL); // Call SortedAttributeMetadata.getDistinctKeysDescending() to retrieve distinct keys in descending order. // We retrieve into a List in order to assert the ordering of values returned... assertEquals( asList("Taurus", "Prius", "M6", "Insight", "Hilux", "Fusion", "Focus", "Civic", "Avensis", "Accord"), sortedAttributeMetadata.getDistinctKeysDescending().collect(toList()) ); // Test specifying range explicitly... assertEquals( asList("Hilux", "Fusion", "Focus"), sortedAttributeMetadata.getDistinctKeysDescending("Civic", false, "Insight", false).collect(toList()) ); assertEquals( asList("Prius", "M6", "Insight", "Hilux", "Fusion", "Focus", "Civic", "Avensis"), sortedAttributeMetadata.getDistinctKeysDescending("Alpha", false, "Tango", false).collect(toList()) ); }
Example #30
Source File: NoneTest.java From cqengine with Apache License 2.0 | 5 votes |
@Test public void testNone() { IndexedCollection<Integer> indexedCollection = new ConcurrentIndexedCollection<Integer>(); indexedCollection.addAll(asList(1, 2, 3, 4, 5)); IndexedCollection<Integer> collection = indexedCollection; Query<Integer> query = none(Integer.class); ResultSet<Integer> results = collection.retrieve(query); assertEquals(0, results.size()); assertFalse(results.iterator().hasNext()); }