cqengine icon indicating copy to clipboard operation
cqengine copied to clipboard

Shared index causes incorrect results

Open Bakkenrak opened this issue 4 years ago • 0 comments

I ran across some (to me) unexpected behaviour when creating two collections that ended up using the same static instance of an index. I wasn't aware that queries on an index are answered by the backing collection stored in that very same index object. So I ended up having objects from the second collection in my results when running retrieve on the first. I'm not sure if there's a need to add some more explicit info about not sharing indexes between separate collections. It might have helped me and potentially others in the future though.

Here's a test that illustrates what I mean:

    @Test
    void sharedIndexMixesItemsFromDifferentCollections() {
        Attribute<Person, String> nameAttribute = attribute("name", Person::getName);
        HashIndex<String, Person> index = HashIndex.onAttribute(nameAttribute);

        IndexedCollection<Person> persons1 = new ConcurrentIndexedCollection<>();
        persons1.addIndex(index);

        IndexedCollection<Person> persons2 = new ConcurrentIndexedCollection<>();
        persons2.addIndex(index);

        Person p1 = new Person("Tom");
        persons1.add(p1);

        Person p2 = new Person("Tom");
        persons2.add(p2);

        ResultSet<Person> result = persons1.retrieve(equal(nameAttribute, "Tom"));
        assertEquals(2, result.size());  // person named Tom from persons2 also in result set
    }

Bakkenrak avatar Aug 25 '20 08:08 Bakkenrak