CollectionQueryEngine implements QueryEngineInternal<O> { @Override public void removeIndex(Index<O> index, QueryOptions queryOptions) { boolean removed; if (index instanceof StandingQueryIndex) { @SuppressWarnings({"unchecked"}) StandingQueryIndex<O> standingQueryIndex = (StandingQueryIndex<O>) index; removed = standingQueryIndexes.remove(standingQueryIndex.getStandingQuery(), standingQueryIndex); } else if (index instanceof CompoundIndex) { @SuppressWarnings({"unchecked"}) CompoundIndex<O> compoundIndex = (CompoundIndex<O>) index; CompoundAttribute<O> compoundAttribute = compoundIndex.getAttribute(); removed = compoundIndexes.remove(compoundAttribute, compoundIndex); } else if (index instanceof AttributeIndex) { @SuppressWarnings({"unchecked"}) AttributeIndex<?, O> attributeIndex = (AttributeIndex<?, O>) index; Attribute<O, ?> indexedAttribute = attributeIndex.getAttribute(); if (indexedAttribute instanceof StandingQueryAttribute) { @SuppressWarnings("unchecked") StandingQueryAttribute<O> standingQueryAttribute = (StandingQueryAttribute<O>) indexedAttribute; Query<O> standingQuery = standingQueryAttribute.getQuery(); removed = standingQueryIndexes.remove(standingQuery, index); } else { Set<Index<O>> indexesOnThisAttribute = attributeIndexes.get(indexedAttribute); removed = indexesOnThisAttribute.remove(attributeIndex); if (attributeIndex instanceof UniqueIndex) { removed = uniqueIndexes.remove(indexedAttribute, attributeIndex) || removed; } if (indexesOnThisAttribute.isEmpty()) { attributeIndexes.remove(indexedAttribute); } } } else { throw new IllegalStateException("Unexpected type of index: " + (index == null ? null : index.getClass().getName())); } if (removed && !index.isMutable()) { immutableIndexes.remove(index); } index.destroy(queryOptions); } CollectionQueryEngine(); @Override void init(final ObjectStore<O> objectStore, final QueryOptions queryOptions); @Override void destroy(QueryOptions queryOptions); @Override void addIndex(Index<O> index, QueryOptions queryOptions); @Override void removeIndex(Index<O> index, QueryOptions queryOptions); @Override Iterable<Index<O>> getIndexes(); @Override ResultSet<O> retrieve(final Query<O> query, final QueryOptions queryOptions); @Override boolean addAll(final ObjectSet<O> objectSet, final QueryOptions queryOptions); @Override boolean removeAll(final ObjectSet<O> objectSet, final QueryOptions queryOptions); @Override void clear(final QueryOptions queryOptions); @Override boolean isMutable(); static final String ROOT_QUERY; }
@Test public void testRemoveIndex() { CollectionQueryEngine<Car> queryEngine = new CollectionQueryEngine<Car>(); queryEngine.init(emptyObjectStore(), queryOptionsWithOnHeapPersistence()); HashIndex<String, Car> index1 = HashIndex.onAttribute(Car.MANUFACTURER); queryEngine.addIndex(index1, noQueryOptions()); UniqueIndex<Integer, Car> index2 = UniqueIndex.onAttribute(Car.CAR_ID); queryEngine.addIndex(index2, noQueryOptions()); StandingQueryIndex<Car> index3 = StandingQueryIndex.onQuery(equal(Car.MODEL, "Focus")); queryEngine.addIndex(index3, noQueryOptions()); CompoundIndex<Car> index4 = CompoundIndex.onAttributes(Car.MANUFACTURER, Car.MODEL); queryEngine.addIndex(index4, noQueryOptions()); HashIndex<Boolean, Car> index5 = HashIndex.onAttribute(forStandingQuery(equal(Car.MANUFACTURER, "Ford"))); queryEngine.addIndex(index5, noQueryOptions()); Assert.assertEquals(5, countElements(queryEngine.getIndexes())); queryEngine.removeIndex(index1, noQueryOptions()); Assert.assertEquals(4, countElements(queryEngine.getIndexes())); queryEngine.removeIndex(index2, noQueryOptions()); Assert.assertEquals(3, countElements(queryEngine.getIndexes())); queryEngine.removeIndex(index3, noQueryOptions()); Assert.assertEquals(2, countElements(queryEngine.getIndexes())); queryEngine.removeIndex(index4, noQueryOptions()); Assert.assertEquals(1, countElements(queryEngine.getIndexes())); queryEngine.removeIndex(index5, noQueryOptions()); Assert.assertEquals(0, countElements(queryEngine.getIndexes())); }