View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.Set;
29  
30  import org.apache.commons.io.FileUtils;
31  import org.apache.directory.mavibot.btree.BTree;
32  import org.apache.directory.mavibot.btree.Cursor;
33  import org.apache.directory.mavibot.btree.RecordManager;
34  import org.apache.directory.mavibot.btree.Tuple;
35  import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
36  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
37  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
38  import org.junit.Before;
39  import org.junit.Test;
40  
41  
42  /**
43   * test the RecordManager's free page management
44   * 
45   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
46   */
47  public class RecordManagerFreePageTest
48  {
49      private BTree<Long, String> btree = null;
50  
51      private RecordManager recordManager1 = null;
52  
53      private File dataDir = null;
54  
55  
56      @Before
57      public void createBTree() throws IOException
58      {
59          dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
60  
61          if ( dataDir.exists() )
62          {
63              FileUtils.deleteDirectory( dataDir );
64          }
65  
66          dataDir.mkdirs();
67  
68          openRecordManagerAndBtree();
69  
70          try
71          {
72              // Create a new BTree
73              btree = recordManager1.addBTree( "test", new LongSerializer(), new StringSerializer(), false );
74          }
75          catch ( Exception e )
76          {
77              throw new RuntimeException( e );
78          }
79      }
80  
81  
82      private void openRecordManagerAndBtree()
83      {
84          try
85          {
86              if ( recordManager1 != null )
87              {
88                  recordManager1.close();
89              }
90  
91              // Now, try to reload the file back
92              recordManager1 = new RecordManager( dataDir.getAbsolutePath() );
93  
94              // load the last created btree
95              if ( btree != null )
96              {
97                  btree = recordManager1.getManagedTree( btree.getName() );
98              }
99          }
100         catch ( Exception e )
101         {
102             throw new RuntimeException( e );
103         }
104     }
105 
106     private int nbElems = 100000;
107 
108 
109     /**
110      * Test the creation of a RecordManager, and that we can read it back.  
111      */
112     @Test
113     public void testRecordManager() throws IOException, BTreeAlreadyManagedException
114     {
115         assertEquals( 1, recordManager1.getNbManagedTrees() );
116 
117         Set<String> managedBTrees = recordManager1.getManagedTrees();
118 
119         assertEquals( 1, managedBTrees.size() );
120         assertTrue( managedBTrees.contains( "test" ) );
121 
122         int nbError = 0;
123 
124         long l1 = System.currentTimeMillis();
125         int n = 0;
126         long delta = l1;
127 
128         for ( int i = 0; i < nbElems; i++ )
129         {
130             Long key = ( long ) i;
131             String value = Long.toString( key );
132 
133             btree.insert( key, value );
134 
135             if ( i % 10000 == 0 )
136             {
137                 if ( n > 0 )
138                 {
139                     long t0 = System.currentTimeMillis();
140                     System.out.println( "Written " + i + " elements in : " + ( t0 - delta ) + "ms" );
141                     delta = t0;
142                 }
143 
144                 n++;
145             }
146         }
147 
148         long l2 = System.currentTimeMillis();
149 
150         System.out.println( "Delta : " + ( l2 - l1 ) + ", nbError = " + nbError
151             + ", Nb insertion per second : " + ( ( nbElems ) / ( l2 - l1 ) ) * 1000 );
152 
153         long length = new File( dataDir, "mavibot.db" ).length();
154         String units = "MB";
155 
156         long size = length / ( 1024 * 1024 );
157 
158         if ( size == 0 )
159         {
160             size = length / 1024;
161             units = "KB";
162         }
163 
164         System.out.println( size + units );
165 
166         openRecordManagerAndBtree();
167 
168         assertEquals( 1, recordManager1.getNbManagedTrees() );
169 
170         assertTrue( nbElems == btree.getNbElems() );
171 
172         Cursor<Long, String> cursor = btree.browse();
173 
174         long i = 0;
175         while ( cursor.hasNext() )
176         {
177             Tuple<Long, String> t = cursor.next();
178             assertEquals( ( Long ) i, t.getKey() );
179             assertEquals( String.valueOf( i ), t.getValue() );
180             i++;
181         }
182 
183         cursor.close();
184 
185         assertEquals( nbElems, i );
186     }
187 }