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.RecordManager;
33  import org.apache.directory.mavibot.btree.Tuple;
34  import org.apache.directory.mavibot.btree.TupleCursor;
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.After;
39  import org.junit.Before;
40  import org.junit.Test;
41  
42  
43  /**
44   * test the RecordManager's free page management
45   * 
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  public class RecordManagerFreePageTest
49  {
50      private BTree<Long, String> btree = null;
51  
52      private RecordManager recordManager1 = null;
53  
54      private File dataDir = null;
55  
56  
57      @Before
58      public void createBTree() throws IOException
59      {
60          dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
61  
62          if ( dataDir.exists() )
63          {
64              FileUtils.deleteDirectory( dataDir );
65          }
66  
67          dataDir.mkdirs();
68  
69          openRecordManagerAndBtree();
70  
71          try
72          {
73              // Create a new BTree
74              btree = recordManager1.addBTree( "test", new LongSerializer(), new StringSerializer(), false );
75          }
76          catch ( Exception e )
77          {
78              throw new RuntimeException( e );
79          }
80      }
81  
82  
83      @After
84      public void cleanup() throws IOException
85      {
86          dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
87  
88          btree.close();
89  
90          if ( dataDir.exists() )
91          {
92              FileUtils.deleteDirectory( dataDir );
93          }
94  
95          recordManager1.close();
96      }
97  
98  
99      private void openRecordManagerAndBtree()
100     {
101         try
102         {
103             if ( recordManager1 != null )
104             {
105                 recordManager1.close();
106             }
107 
108             // Now, try to reload the file back
109             recordManager1 = new RecordManager( dataDir.getAbsolutePath() );
110 
111             // load the last created btree
112             if ( btree != null )
113             {
114                 btree = recordManager1.getManagedTree( btree.getName() );
115             }
116         }
117         catch ( Exception e )
118         {
119             throw new RuntimeException( e );
120         }
121     }
122 
123     private int nbElems = 100000;
124 
125 
126     /**
127      * Test the creation of a RecordManager, and that we can read it back.  
128      */
129     @Test
130     public void testRecordManager() throws IOException, BTreeAlreadyManagedException
131     {
132         assertEquals( 1, recordManager1.getNbManagedTrees() );
133 
134         Set<String> managedBTrees = recordManager1.getManagedTrees();
135 
136         assertEquals( 1, managedBTrees.size() );
137         assertTrue( managedBTrees.contains( "test" ) );
138 
139         int nbError = 0;
140 
141         long l1 = System.currentTimeMillis();
142         int n = 0;
143         long delta = l1;
144 
145         for ( int i = 0; i < nbElems; i++ )
146         {
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 }