View Javadoc

1   package org.apache.directory.mavibot.btree.memory;
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.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   * A class to test multi-threaded operations on the btree
35   *  
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class InMemoryBTreeTestOps
39  {
40      /** The btree we use */
41      private static BTree<Long, String> btree;
42  
43  
44      /**
45       * Create the btree once
46       * @throws IOException If the creation failed
47       */
48      @BeforeClass
49      public static void setup() throws IOException
50      {
51          btree = new BTree<Long, String>( "test", new LongSerializer(), new StringSerializer() );
52      }
53  
54  
55      /**
56       * Close the btree
57       */
58      @AfterClass
59      public static void shutdown() throws IOException
60      {
61          btree.close();
62      }
63  
64  
65      /**
66       * Create a btree with 500 000 elements in it
67       * @throws IOException If the creation failed
68       */
69      private void createTree() throws IOException
70      {
71          Random random = new Random( System.nanoTime() );
72  
73          int nbElems = 500000;
74  
75          // Create a BTree with 500 000 entries
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         // Start the writer
110         createTree();
111         long t1 = System.currentTimeMillis();
112         System.out.println( "Time to create a tree with 500 000 elements in memory:" + ( ( t1 - t0 ) ) + " milliseconds" );
113     }
114 }