Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSBTreeIndexEngine#OSBTreeIndexCursor does not use nextEntry properly #5541

Closed
kruthar opened this issue Dec 30, 2015 · 3 comments
Closed

OSBTreeIndexEngine#OSBTreeIndexCursor does not use nextEntry properly #5541

kruthar opened this issue Dec 30, 2015 · 3 comments
Assignees

Comments

@kruthar
Copy link

kruthar commented Dec 30, 2015

I am trying to use an iterator over entries in an ODictionary. Small test class looks like this:

public class OrientDBTesting {
    public static void main(String[] args) {
        ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:test").create();
        ODictionary dictionary = db.getDictionary();

        dictionary.put("user0", new ODocument());
        dictionary.put("user1", new ODocument());
        dictionary.put("user2", new ODocument());

        OIndexCursor entries = dictionary.getIndex().iterateEntriesMajor("user0", true, true);
        while (entries.hasNext()) {
            System.out.println(entries.nextEntry().getKey());
        }
    }
}

I'm using this dependency through Maven:

<dependency>
    <groupId>com.orientechnologies</groupId>
    <artifactId>orientdb-client</artifactId>
    <version>2.1.8</version>
</dependency>

The output looks like this:

Dec 30, 2015 1:55:44 PM com.orientechnologies.common.log.OLogManager log
INFO: OrientDB auto-config DISKCACHE=10,695MB (heap=3,641MB os=16,384MB disk=62,946MB)
user1
user2
Exception in thread "main" java.lang.NullPointerException
    at com.kruth.orientdb.OrientDBTesting.main(OrientDBTesting.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

There seems to be two issues here:

  1. The iterator doesn't start at 'user0'.
  2. The iterator runs into nulls at the end instead of ending properly.

From what I can see, both issues stem from OSBTreeIndexEngine#OSBTreeIndexCursor not using nextEntry. This class extends OIndexAbstractCursor which maintains iterator state with nextEntry as you can see here: https://github.com/orientechnologies/orientdb/blob/master/core/src/main/java/com/orientechnologies/orient/core/index/OIndexAbstractCursor.java#L80-L100. So, OSBTreeIndexEngine#OSBTreeIndexCursor should use nextEntry also, or potentially override hasNext() and next().

I did not look to see if any of the other OIndexCursor implementations have the same issue.

@laa
Copy link
Member

laa commented Jan 28, 2016

@kruthar Index cursor is correct, cursor implements 2 interfaces Iterator#hasNext() and Iterator#next() and IndexCursor#nextEntry both those interfaces are not related to each other.

@kruthar
Copy link
Author

kruthar commented Jan 28, 2016

Can you help me explain the behaviour I'm seeing? Were the errors I posted reproducible? Is there obvious errors in what I am attempting to do in the sample?

Perfectly acceptable if the cursor code is correct, but the null pointer still happens with what I assumed was the correct usage.

@laa
Copy link
Member

laa commented Jan 29, 2016

If you change code like this one:

public class OrientDBTesting {
    public static void main(String[] args) {
        ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:test").create();
        ODictionary dictionary = db.getDictionary();

        dictionary.put("user0", new ODocument());
        dictionary.put("user1", new ODocument());
        dictionary.put("user2", new ODocument());

        OIndexCursor entries = dictionary.getIndex().iterateEntriesMajor("user0", true, true);
        while (entries.hasNext()) {
            System.out.println(entries.next());
        }
    }
}

It will perfectly works.

Also if you look at Javadoc of nextEntry() method is will find that it is not related to hasNext() method and designed to return null if there are no more elements inside of cursor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants