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.exception.BTreeAlreadyManagedException;
32  import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
33  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
34  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
35  import org.junit.After;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  
40  /**
41   * test the RecordManager's free page management
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class RecordManagerFreePageTest
46  {
47      private BTree<Long, String> btree = null;
48  
49      private RecordManager recordManager1 = null;
50  
51      private File dataDir = null;
52  
53  
54      @Before
55      public void createBTree() throws IOException
56      {
57          dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
58  
59          if ( dataDir.exists() )
60          {
61              FileUtils.deleteDirectory( dataDir );
62          }
63  
64          dataDir.mkdirs();
65  
66          openRecordManagerAndBtree();
67  
68          try
69          {
70              // Create a new BTree
71              btree = recordManager1.addBTree( "test", LongSerializer.INSTANCE, StringSerializer.INSTANCE, false );
72          }
73          catch ( Exception e )
74          {
75              throw new RuntimeException( e );
76          }
77      }
78  
79  
80      @After
81      public void cleanup() throws IOException
82      {
83          dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
84  
85          btree.close();
86  
87          if ( dataDir.exists() )
88          {
89              FileUtils.deleteDirectory( dataDir );
90          }
91  
92          recordManager1.close();
93          
94          assertTrue( recordManager1.isContextOk() );
95      }
96  
97  
98      private void openRecordManagerAndBtree()
99      {
100         try
101         {
102             if ( recordManager1 != null )
103             {
104                 recordManager1.close();
105             }
106 
107             // Now, try to reload the file back
108             recordManager1 = new RecordManager( dataDir.getAbsolutePath() );
109 
110             // load the last created btree
111             if ( btree != null )
112             {
113                 btree = recordManager1.getManagedTree( btree.getName() );
114             }
115         }
116         catch ( Exception e )
117         {
118             throw new RuntimeException( e );
119         }
120     }
121 
122     private int nbElems = 10000;
123 
124 
125     /**
126      * Test the creation of a RecordManager, and that we can read it back.
127      */
128     @Test
129     public void testRecordManager() throws IOException, BTreeAlreadyManagedException, KeyNotFoundException
130     {
131         assertEquals( 1, recordManager1.getNbManagedTrees() );
132 
133         Set<String> managedBTrees = recordManager1.getManagedTrees();
134 
135         assertEquals( 1, managedBTrees.size() );
136         assertTrue( managedBTrees.contains( "test" ) );
137 
138         int nbError = 0;
139 
140         long l1 = System.currentTimeMillis();
141         int n = 0;
142         long delta = l1;
143 
144         for ( int i = 0; i < nbElems; i++ )
145         {
146             // System.out.println( i );
147             Long key = ( long ) i;
148             String value = Long.toString( key );
149 
150             btree.insert( key, value );
151 
152             if ( i % 10000 == 0 )
153             {
154                 if ( n > 0 )
155                 {
156                     long t0 = System.currentTimeMillis();
157                     System.out.println( "Written " + i + " elements in : " + ( t0 - delta ) + "ms" );
158                     delta = t0;
159                 }
160 
161                 n++;
162             }
163         }
164 
165         long l2 = System.currentTimeMillis();
166 
167         System.out.println( "Delta : " + ( l2 - l1 ) + ", nbError = " + nbError
168             + ", Nb insertion per second : " + ( ( nbElems ) / ( l2 - l1 ) ) * 1000 );
169 
170         long length = new File( dataDir, "mavibot.db" ).length();
171         String units = "MB";
172 
173         long size = length / ( 1024 * 1024 );
174 
175         if ( size == 0 )
176         {
177             size = length / 1024;
178             units = "KB";
179         }
180 
181         // System.out.println( size + units );
182 
183         openRecordManagerAndBtree();
184 
185         assertEquals( 1, recordManager1.getNbManagedTrees() );
186 
187         assertTrue( nbElems == btree.getNbElems() );
188 
189         TupleCursor<Long, String> cursor = btree.browse();
190 
191         long i = 0;
192 
193         while ( cursor.hasNext() )
194         {
195             Tuple<Long, String> t = cursor.next();
196             assertEquals( ( Long ) i, t.getKey() );
197             assertEquals( String.valueOf( i ), t.getValue() );
198             i++;
199         }
200 
201         cursor.close();
202 
203         assertEquals( nbElems, i );
204     }
205 }