Skip to content

Instantly share code, notes, and snippets.

@jacobg
Last active September 22, 2016 15:45
Show Gist options
  • Save jacobg/4d06f172206ea7a5b76489e00ae5f21c to your computer and use it in GitHub Desktop.
Save jacobg/4d06f172206ea7a5b76489e00ae5f21c to your computer and use it in GitHub Desktop.
Unit test demonstrating issue removing item with mutated indexable value
package com.googlecode.cqengine;
import com.googlecode.cqengine.attribute.SimpleAttribute;
import com.googlecode.cqengine.index.navigable.NavigableIndex;
import com.googlecode.cqengine.index.unique.UniqueIndex;
import com.googlecode.cqengine.quantizer.DoubleQuantizer;
import com.googlecode.cqengine.query.Query;
import com.googlecode.cqengine.query.option.QueryOptions;
import com.googlecode.cqengine.resultset.ResultSet;
import org.testng.annotations.Test;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static com.googlecode.cqengine.query.QueryFactory.and;
import static com.googlecode.cqengine.query.QueryFactory.greaterThanOrEqualTo;
import static com.googlecode.cqengine.query.QueryFactory.lessThanOrEqualTo;
import static org.testng.Assert.*;
public class IndexedCollectionTest {
@Test
public void testReplace() {
IndexedCollection<Item> indexedCollection = createIndexedCollection();
Item item = new Item("SOME_ID", 40d);
indexedCollection.add(item);
/**
* changing score is what causes the duplicate issue
*/
item.setScore(50d);
indexedCollection.add(item);
// The following assertion always succeeds
assertEquals(indexedCollection.size(), 1, "There should be one item, and not a dupe");
// But this assertion fails
assertEquals(getItemsByScore(indexedCollection, 0, 100).size(), 1,
"No items should be returned");
}
@Test
public void testRemove() {
IndexedCollection<Item> indexedCollection = createIndexedCollection();
Item item = new Item("SOME_ID", 40d);
indexedCollection.add(item);
/**
* changing score is what causes the removal issue
*/
item.setScore(50d);
indexedCollection.add(item);
indexedCollection.remove(item);
// The following assertion always succeeds
assertTrue(indexedCollection.isEmpty(), "Collection should be empty");
// But this assertion fails
assertEquals(getItemsByScore(indexedCollection, 0, 100).size(), 0,
"No items should be returned");
}
private static class Item {
private final String _id;
private Double _score;
public Item(String id, Double score) {
_id = id;
_score = score;
}
public String getId() {
return _id;
}
public Double getScore() {
return _score;
}
public void setScore(Double score) {
_score = score;
}
}
private List<Item> getItemsByScore(IndexedCollection<Item> indexedCollection,
double minScore, double maxScore)
{
Query<Item> query = and(
greaterThanOrEqualTo(SCORE, minScore),
lessThanOrEqualTo(SCORE, maxScore));
return asStream(indexedCollection.retrieve(query))
.collect(Collectors.toList());
}
private static <O> Stream<O> asStream(ResultSet<O> rs) {
return StreamSupport.stream(rs.spliterator(), false);
}
private static IndexedCollection<Item> createIndexedCollection() {
IndexedCollection<Item> indexedCollection = new ConcurrentIndexedCollection<>();
indexedCollection.addIndex(UniqueIndex.onAttribute(ID));
indexedCollection.addIndex(NavigableIndex
.withQuantizerOnAttribute(DoubleQuantizer.withCompressionFactor(1), SCORE));
return indexedCollection;
}
private static final SimpleAttribute<Item, String> ID =
new SimpleAttribute<Item, String>("id") {
public String getValue(Item item, QueryOptions queryOptions) {
return item.getId();
}
};
private static final SimpleAttribute<Item, Double> SCORE =
new SimpleAttribute<Item, Double>("score") {
public Double getValue(Item item, QueryOptions queryOptions) {
return item.getScore();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment