@Test public void byteArrayEquality() { IndexedCollection<EntityHandle<Car>> collection = createIndexedCollection(Car.class); SimpleAttribute<Car, byte[]> attr = new SimpleAttribute<Car, byte[]>(Car.class, (Class<EntityHandle<Car>>) ((Class<?>)getClass()), byte[].class, "bytearray") { @Override public byte[] getValue(Car object, QueryOptions queryOptions) { return object.getManufacturer().getBytes(); } }; HashIndex index = onAttribute(attr); index.clear(noQueryOptions()); collection.addIndex(index); collection.addAll(CarFactory.createCollectionOfCars(10)); ResultSet<EntityHandle<Car>> cars = collection.retrieve(equal(attr, "Honda".getBytes())); assertTrue(cars.isNotEmpty()); assertTrue(streamOf(cars).allMatch(car -> car.get().getManufacturer().contentEquals("Honda"))); cars.close(); index.clear(noQueryOptions()); }
@Test public void getDistinctKeysAndCounts() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MODEL_INDEX = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MODEL); MODEL_INDEX.clear(noQueryOptions()); collection.addIndex(MODEL_INDEX); collection.addAll(CarFactory.createCollectionOfCars(20)); Set<String> distinctModels = setOf(MODEL_INDEX.getDistinctKeys(noQueryOptions())); assertEquals(distinctModels, setOf("Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius", "Taurus")); for (String model : distinctModels) { assertEquals(MODEL_INDEX.getCountForKey(model, noQueryOptions()), Integer.valueOf(2)); } MODEL_INDEX.clear(noQueryOptions()); }
@Test public void getCountOfDistinctKeys() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MANUFACTURER_INDEX = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MANUFACTURER); MANUFACTURER_INDEX.clear(noQueryOptions()); collection.addIndex(MANUFACTURER_INDEX); collection.addAll(CarFactory.createCollectionOfCars(20)); assertEquals(MANUFACTURER_INDEX.getCountOfDistinctKeys(noQueryOptions()), Integer.valueOf(4)); MANUFACTURER_INDEX.clear(noQueryOptions()); }
@Test public void retrieveEqual() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MANUFACTURER_INDEX = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MANUFACTURER); MANUFACTURER_INDEX.clear(noQueryOptions()); collection.addIndex(MANUFACTURER_INDEX); collection.addAll(CarFactory.createCollectionOfCars(10)); ResultSet<EntityHandle<Car>> cars = collection.retrieve(equal(Car.MANUFACTURER, "Honda")); assertTrue(cars.isNotEmpty()); assertTrue(streamOf(cars).allMatch(car -> car.get().getManufacturer().contentEquals("Honda"))); cars.close(); MANUFACTURER_INDEX.clear(noQueryOptions()); }
@Test public void getStatisticsForDistinctKeys() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MANUFACTURER_INDEX = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MANUFACTURER); MANUFACTURER_INDEX.clear(noQueryOptions()); collection.addIndex(MANUFACTURER_INDEX); collection.addAll(CarFactory.createCollectionOfCars(20)); Set<KeyStatistics<String>> keyStatistics = setOf( MANUFACTURER_INDEX.getStatisticsForDistinctKeys(noQueryOptions())); assertEquals(keyStatistics, setOf( new KeyStatistics<>("Ford", 6), new KeyStatistics<>("Honda", 6), new KeyStatistics<>("Toyota", 6), new KeyStatistics<>("BMW", 2) ) ); MANUFACTURER_INDEX.clear(noQueryOptions()); }
@Test public void indexExistingData() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MANUFACTURER_INDEX = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MANUFACTURER); MANUFACTURER_INDEX.clear(noQueryOptions()); collection.addAll(CarFactory.createCollectionOfCars(10)); collection.addIndex(MANUFACTURER_INDEX); ResultSet<EntityHandle<Car>> cars = collection.retrieve(equal(Car.MANUFACTURER, "Honda")); assertTrue(cars.isNotEmpty()); assertTrue(streamOf(cars).allMatch(car -> car.get().getManufacturer().contentEquals("Honda"))); cars.close(); MANUFACTURER_INDEX.clear(noQueryOptions()); }
@Test public void retrieveHas() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MANUFACTURER_INDEX = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MANUFACTURER); MANUFACTURER_INDEX.clear(noQueryOptions()); collection.addIndex(MANUFACTURER_INDEX); Set<EntityHandle<Car>> coll = CarFactory.createCollectionOfCars(10); collection.addAll(coll); ResultSet<EntityHandle<Car>> cars = collection.retrieve(has(Car.MANUFACTURER)); assertTrue(cars.isNotEmpty()); assertEquals(cars.size(), coll.size()); cars.close(); MANUFACTURER_INDEX.clear(noQueryOptions()); }
@Test public void reindexData() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MANUFACTURER_INDEX = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MANUFACTURER); MANUFACTURER_INDEX.clear(noQueryOptions()); Set<EntityHandle<Car>> cars = CarFactory.createCollectionOfCars(10); collection.addAll(cars); collection.addIndex(MANUFACTURER_INDEX); IndexedCollection<EntityHandle<Car>> collection1 = new ConcurrentIndexedCollection<>(); KeyStatisticsIndex<String, EntityHandle<Car>> MANUFACTURER_INDEX1 = (KeyStatisticsIndex<String, EntityHandle<Car>>) onAttribute(Car.MANUFACTURER); collection1.addAll(cars); collection1.addIndex(MANUFACTURER_INDEX1); assertEquals((int)MANUFACTURER_INDEX.getCountForKey("Honda", noQueryOptions()), 3); assertEquals((int)MANUFACTURER_INDEX1.getCountForKey("Honda", noQueryOptions()), 3); MANUFACTURER_INDEX.clear(noQueryOptions()); MANUFACTURER_INDEX1.clear(noQueryOptions()); } }