Java源码示例:com.googlecode.cqengine.ConcurrentIndexedCollection
示例1
@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);
}
示例2
@SuppressWarnings("unchecked")
public void readFromNBT(NBTTagCompound nbt) {
long start = System.currentTimeMillis();
Kryo kryo = ValkyrienSkiesMod.INSTANCE.getKryo();
Input input = new Input(nbt.getByteArray(NBT_STORAGE_KEY));
try {
allShips = kryo.readObject(input, ConcurrentIndexedCollection.class);
} catch (Exception e) {
// Error reading allShips from memory, just make a new empty one.
e.printStackTrace();
allShips = new ConcurrentIndexedCollection<>();
}
if (allShips == null) {
// This should NEVER EVER happen! So I don't feel bad crashing the game, for now.
throw new IllegalStateException(
"Kryo read allships as null! Making a new empty allships instance");
}
System.out.println("Price of read: " + (System.currentTimeMillis() - start) + "ms");
}
示例3
@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);
}
示例4
@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());
}
示例5
@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());
}
示例6
public static void main(String[] args) {
final int NUM_ITERATIONS = 1000;
final int[] numObjects = {10000, 10000, 100000};
final double[] selectivityThreshold = {0.0, 0.5, 1.0};
IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
cars.addAll(CarFactory.createCollectionOfCars(1000000));
cars.addIndex(NavigableIndex.onAttribute(Car.CAR_ID));
cars.addIndex(NavigableIndex.onAttribute(Car.COLOR));
for (int n : numObjects) {
for (double s : selectivityThreshold) {
long start = System.currentTimeMillis();
long count = 0;
for (int i = 0; i < NUM_ITERATIONS; i++) {
count = countRetrievedResults(cars, n, s);
}
long timeTaken = System.currentTimeMillis() - start;
System.out.println("Number: " + n + ", selectivity threshold: " + s + ", time taken per iteration: " + (timeTaken / (double)NUM_ITERATIONS) + " (count=" + count + ")");
}
}
}
示例7
@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);
}
示例8
@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));
}
示例9
@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));
}
示例10
/**
* 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);
}
示例11
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
}
}
示例12
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
}
示例13
@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]}
}
示例14
@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);
}
示例15
@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())
);
}
示例16
@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());
}
示例17
@Test
public void testQueryToString2() {
IndexedCollection<Garage> garages = new ConcurrentIndexedCollection<Garage>();
Query<Car> query = and(
in(Car.DOORS, 2, 4),
existsIn(garages,
Car.MANUFACTURER,
Garage.BRANDS_SERVICED,
equal(Garage.LOCATION, "Dublin")
)
);
// Note: QueryFactory expands 'in' queries to an 'or' of multiple 'equals' queries (logically equivalent)...
Assert.assertEquals(
"and(" +
"in(\"doors\", [2, 4]), " +
"existsIn(IndexedCollection<Garage>, " +
"\"manufacturer\", " +
"\"brandsServiced\", " +
"equal(\"location\", \"Dublin\")" +
")" +
")",
query.toString()
);
}
示例18
@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);
}
示例19
@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);
}
示例20
@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);
}
示例21
@Test
public void testGetStatisticsForDistinctKeys(){
IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
KeyStatisticsIndex<String, Car> MANUFACTURER_INDEX = NavigableIndex.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);
}
示例22
@Test
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "StatementWithEmptyBody"})
public void testOrQueryCollectionScan() {
IterationCountingSet<Car> iterationCountingSet = new IterationCountingSet<Car>(CarFactory.createCollectionOfCars(10));
IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(WrappingPersistence.aroundCollection(iterationCountingSet));
Query<Car> query = or(equal(Car.COLOR, Car.Color.BLUE), equal(Car.MANUFACTURER, "Honda"));
ResultSet<Car> resultSet = collection.retrieve(query);
for (Car car : resultSet) {
// No op
}
// The two-branch or() query should have been evaluated by scanning the collection only once...
Assert.assertEquals(iterationCountingSet.size(), iterationCountingSet.getItemsIteratedCount());
}
示例23
@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);
}
示例24
@Test
public void testIndexQuantization_ComplexQuery() {
IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
collection.addIndex(NavigableIndex.withQuantizerOnAttribute(IntegerQuantizer.withCompressionFactor(10), Car.CAR_ID));
collection.addAll(CarFactory.createCollectionOfCars(100));
Query<Car> query = and(between(Car.CAR_ID, 96, 98), greaterThan(Car.CAR_ID, 95));
// Merge cost should be 10, because objects matching this query are in a single bucket...
assertEquals(10, collection.retrieve(query).getMergeCost());
// 3 objects match the query...
assertEquals(3, collection.retrieve(query).size());
List<Integer> carIdsFound = retrieveCarIds(collection, query);
assertEquals(asList(96, 97, 98), carIdsFound);
}
示例25
@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());
}
示例26
@Test @Ignore
public void testSaveToDisk() {
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 initially be empty if the file does not exist)...
DiskPersistence<Car, Integer> persistence = DiskPersistence.onPrimaryKeyInFile(Car.CAR_ID, persistenceFile);
IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(persistence);
// Populate the collection (saving it to disk)...
cars.addAll(collectionOfCars);
// Sanity check that we saved the cars correctly...
Set<Integer> actualCarIds = cars.stream().map(Car::getCarId).collect(toSet());
Assert.assertEquals(expectedCarIds, actualCarIds);
System.out.println("Saved to disk: " + actualCarIds);
}
示例27
@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);
}
示例28
@Test
public void testAsCollection() throws Exception {
IndexedCollection<String> indexedCollection = new ConcurrentIndexedCollection<String>();
indexedCollection.addAll(asList("how", "now", "brown", "cow"));
ResultSet<String> resultSet = indexedCollection.retrieve(all(String.class));
Collection<String> collection = ResultSets.asCollection(resultSet);
assertEquals(resultSet.size(), collection.size());
assertEquals(resultSet.size(), IteratorUtil.countElements(collection));
assertEquals(resultSet.isEmpty(), collection.isEmpty());
assertTrue(collection.contains("now"));
assertFalse(collection.contains("baz"));
}
示例29
@Test
public void testGetCountOfDistinctKeys(){
IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
KeyStatisticsIndex<String, Car> MANUFACTURER_INDEX = NavigableIndex.onAttribute(Car.MANUFACTURER);
collection.addIndex(MANUFACTURER_INDEX);
collection.addAll(CarFactory.createCollectionOfCars(20));
Assert.assertEquals(Integer.valueOf(4), MANUFACTURER_INDEX.getCountOfDistinctKeys(noQueryOptions()));
}
示例30
@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);
}