1 package org.apache.directory.mavibot.btree;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.IOException;
24 import java.util.Random;
25
26 import org.apache.directory.mavibot.btree.serializer.LongSerializer;
27 import org.apache.directory.mavibot.btree.serializer.StringSerializer;
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31
32
33
34
35
36
37
38 public class InMemoryBTreeTestOps
39 {
40
41 private static BTree<Long, String> btree;
42
43
44
45
46
47
48 @BeforeClass
49 public static void setup() throws IOException
50 {
51 btree = BTreeFactory.createInMemoryBTree( "test", LongSerializer.INSTANCE, StringSerializer.INSTANCE );
52 }
53
54
55
56
57
58 @AfterClass
59 public static void shutdown() throws IOException
60 {
61 btree.close();
62 }
63
64
65
66
67
68
69 private void createTree() throws IOException
70 {
71 Random random = new Random( System.nanoTime() );
72
73 int nbElems = 50000;
74
75
76 btree.setPageSize( 32 );
77 for ( int i = 0; i < nbElems; i++ )
78 {
79 Long key = ( long ) random.nextLong();
80 String value = Long.toString( key );
81
82 try
83 {
84 btree.insert( key, value );
85
86 if ( i % 100000 == 0 )
87 {
88 System.out.println( "Written " + i + " elements" );
89 }
90 }
91 catch ( Exception e )
92 {
93 e.printStackTrace();
94 System.out.println( btree );
95 System.out.println( "Error while adding " + value );
96 return;
97 }
98 }
99
100 }
101
102
103 @Test
104 public void testCreateTree() throws InterruptedException, IOException
105 {
106
107 long t0 = System.currentTimeMillis();
108
109
110 createTree();
111 long t1 = System.currentTimeMillis();
112 System.out.println( "Time to create a tree with 500 000 elements in memory:" + ( ( t1 - t0 ) )
113 + " milliseconds" );
114 }
115 }