1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mavibot.btree;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.UUID;
29
30 import org.apache.commons.io.FileUtils;
31 import org.apache.directory.mavibot.btree.serializer.IntSerializer;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.TemporaryFolder;
37
38
39
40
41
42
43 public class PersistedSubBtreeKeyCursorTest
44 {
45 private BTree<Integer, Integer> btree = null;
46
47 private RecordManager recordManager = null;
48
49 @Rule
50 public TemporaryFolder tempFolder = new TemporaryFolder();
51
52 private File dataDir = null;
53
54
55 @Before
56 public void createBTree() throws IOException
57 {
58 dataDir = tempFolder.newFolder( UUID.randomUUID().toString() );
59
60
61 recordManager = new RecordManager( dataDir.getAbsolutePath() );
62
63 try
64 {
65 PersistedBTreeConfiguration<Integer, Integer> configuration = new PersistedBTreeConfiguration<Integer, Integer>();
66 configuration.setAllowDuplicates( false );
67 configuration.setKeySerializer( IntSerializer.INSTANCE );
68 configuration.setValueSerializer( IntSerializer.INSTANCE );
69 configuration.setName( "sub-btree" );
70 configuration.setBtreeType( BTreeTypeEnum.PERSISTED_SUB );
71
72 btree = BTreeFactory.createPersistedBTree( configuration );
73
74 recordManager.manage( btree, RecordManager.INTERNAL_BTREE );
75 }
76 catch ( Exception e )
77 {
78 throw new RuntimeException( e );
79 }
80 }
81
82
83 @After
84 public void cleanup() throws IOException
85 {
86 dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
87
88 btree.close();
89
90 if ( dataDir.exists() )
91 {
92 FileUtils.deleteDirectory( dataDir );
93 }
94
95 recordManager.close();
96 assertTrue( recordManager.isContextOk() );
97 }
98
99 @Test
100 public void testBrowseKeys() throws Exception
101 {
102 for( int i=0; i< 10; i++ )
103 {
104
105 btree.insert( i, i );
106 }
107
108 KeyCursor<Integer> cursor = btree.browseKeys();
109
110 for( int i=0; i< 10; i++ )
111 {
112 assertTrue( cursor.hasNext() );
113 assertEquals( String.valueOf( i ), String.valueOf( cursor.next() ) );
114 }
115
116 assertFalse( cursor.hasNext() );
117
118 cursor.afterLast();
119
120 for( int i=9; i>= 0; i-- )
121 {
122 assertTrue( cursor.hasPrev() );
123 assertEquals( String.valueOf( i ), String.valueOf( cursor.prev() ) );
124 }
125
126 assertFalse( cursor.hasPrev() );
127 cursor.close();
128 }
129 }