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