directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Lécharny <elecha...@gmail.com>
Subject [Mavibot] Cursor usage
Date Fri, 06 Dec 2013 13:37:50 GMT
Hi guys,

today, while I was working on the in-memory btree cursor, I looked at
the testDuplicateKey() test.

There are a few things that I disagree with in this test, or more
specifically on the semantic of next and prev methods. At the beginning
of the test, we start to browse the tree containing 2 elements, 1 and 2 :

    public void testDuplicateKey() throws IOException
    {
        BTreeConfiguration<Integer, Integer> config = new
BTreeConfiguration<Integer, Integer>();
        config.setAllowDuplicates( true );
        config.setName( "master" );
        config.setSerializers( serializer, serializer );
        BTree<Integer, Integer> btree = new BTree<Integer, Integer>(
config );

        btree.insert( 1, 1 );
        btree.insert( 1, 2 );

        TupleCursor<Integer, Integer> cursor = btree.browse();


At this point, we are BEFORE the first element, so a call to next()
should return <1, 1>, and it does :

        assertTrue( cursor.hasNext() );

        Tuple<Integer, Integer> t = cursor.next();

        assertEquals( Integer.valueOf( 1 ), t.getKey() );
        assertEquals( Integer.valueOf( 1 ), t.getValue() );


If we call next() a second time, we should get <1,2>, and we do :

        assertTrue( cursor.hasNext() );

        t = cursor.next();

        assertEquals( Integer.valueOf( 1 ), t.getKey() );
        assertEquals( Integer.valueOf( 2 ), t.getValue() );


There are no next value :

        assertFalse( cursor.hasNext() );


Now, the test is moving backward, and here, I disagree. The test is
expercing that doing a prev, we get again the same tuple <1,2> which
makes no sense to me :

        // test backward move
        assertTrue( cursor.hasPrev() );

        t = cursor.prev();

        assertEquals( Integer.valueOf( 1 ), t.getKey() );
        assertEquals( Integer.valueOf( 2 ), t.getValue() );

We should get instead <1,1>

o--[1]--[2]--x
^
|
+-- btreeBrowse()


o--[1]--[2]--x
    ^
    |
    +-- next()  : we are on 1


o--[1]--[2]--x
         ^
         |
         +-- next()  : we are on 2


At this point, a call to prev should move back to [1]



o--[1]--[2]--x
    ^
    |
    +-- prev()  : we are on 1


My point is that a next or a prev operation should move forward or
backward and return the current element *after* the move, not the opposite.

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com 


Mime
View raw message