View Javadoc

1   package org.apache.directory.mavibot.btree;
2   
3   
4   /*
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
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   * A class to test multi-threaded operations on the btree
36   *  
37   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
38   */
39  public class InMemoryBTreeTestOps
40  {
41      /** The btree we use */
42      private static BTree<Long, String> btree;
43  
44  
45      /**
46       * Create the btree once
47       * @throws IOException If the creation failed
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       * Close the btree
58       */
59      @AfterClass
60      public static void shutdown() throws IOException
61      {
62          btree.close();
63      }
64  
65  
66      /**
67       * Create a btree with 500 000 elements in it
68       * @throws IOException If the creation failed
69       */
70      private void createTree() throws IOException
71      {
72          Random random = new Random( System.nanoTime() );
73  
74          int nbElems = 500000;
75  
76          // Create a BTree with 500 000 entries
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         // Start the writer
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 }