fusesource / leveldbjni

A Java Native Interface to LevelDB
BSD 3-Clause "New" or "Revised" License
536 stars 143 forks source link

Last element missing when iterating backwards #50

Closed Jaap-Jan closed 10 years ago

Jaap-Jan commented 10 years ago

I have attached a simple test case which exhibits this behavior. Version is 1.8.

public class TestIterator {

    static final int COUNT = 4;

    public static void main(String[] args) throws IOException {
        Options options = new Options().createIfMissing(true);
        DB db = factory.open(new File("example"), options);

        try {
            DBIterator it = null;
            StringBuffer sb = new StringBuffer();

            for (byte i = 1; i <= COUNT; i++) {
                db.put(new byte[] { i }, new byte[] {});
            }

            sb.append("Forward iterator : ");
            it = db.iterator();
            for (it.seekToFirst(); it.hasNext();) {
                sb.append(it.next().getKey()[0] + " ");
            }

            sb.append("\nBackward iterator: ");
            it = db.iterator();
            for (it.seekToLast(); it.hasPrev();) {
                sb.append(it.prev().getKey()[0] + " ");
            }

            System.out.println(sb.toString());
        } finally {
            if (null != db)
                db.close();
        }
    }
}

Output:

Forward iterator : 1 2 3 4 
Backward iterator: 3 2 1 
davsclaus commented 10 years ago

Fixed with a new PR - The test now outputs

Forward iterator : 1 2 3 4 
Backward iterator: 4 3 2 1