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.managed;
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.Tuple;
32  import org.apache.directory.mavibot.btree.TupleCursor;
33  import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
34  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
35  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
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", new LongSerializer(), new StringSerializer(), false );
72          }
73          catch ( Exception e )
74          {
75              throw new RuntimeException( e );
76          }
77      }
78  
79  
80      private void openRecordManagerAndBtree()
81      {
82          try
83          {
84              if ( recordManager1 != null )
85              {
86                  recordManager1.close();
87              }
88  
89              // Now, try to reload the file back
90              recordManager1 = new RecordManager( dataDir.getAbsolutePath() );
91  
92              // load the last created btree
93              if ( btree != null )
94              {
95                  btree = recordManager1.getManagedTree( btree.getName() );
96              }
97          }
98          catch ( Exception e )
99          {
100             throw new RuntimeException( e );
101         }
102     }
103 
104     private int nbElems = 100000;
105 
106 
107     /**
108      * Test the creation of a RecordManager, and that we can read it back.  
109      */
110     @Test
111     public void testRecordManager() throws IOException, BTreeAlreadyManagedException
112     {
113         assertEquals( 1, recordManager1.getNbManagedTrees() );
114 
115         Set<String> managedBTrees = recordManager1.getManagedTrees();
116 
117         assertEquals( 1, managedBTrees.size() );
118         assertTrue( managedBTrees.contains( "test" ) );
119 
120         int nbError = 0;
121 
122         long l1 = System.currentTimeMillis();
123         int n = 0;
124         long delta = l1;
125 
126         for ( int i = 0; i < nbElems; i++ )
127         {
128             Long key = ( long ) i;
129             String value = Long.toString( key );
130 
131             btree.insert( key, value );
132 
133             if ( i % 10000 == 0 )
134             {
135                 if ( n > 0 )
136                 {
137                     long t0 = System.currentTimeMillis();
138                     System.out.println( "Written " + i + " elements in : " + ( t0 - delta ) + "ms" );
139                     delta = t0;
140                 }
141 
142                 n++;
143             }
144         }
145 
146         long l2 = System.currentTimeMillis();
147 
148         System.out.println( "Delta : " + ( l2 - l1 ) + ", nbError = " + nbError
149             + ", Nb insertion per second : " + ( ( nbElems ) / ( l2 - l1 ) ) * 1000 );
150 
151         long length = new File( dataDir, "mavibot.db" ).length();
152         String units = "MB";
153 
154         long size = length / ( 1024 * 1024 );
155 
156         if ( size == 0 )
157         {
158             size = length / 1024;
159             units = "KB";
160         }
161 
162         System.out.println( size + units );
163 
164         openRecordManagerAndBtree();
165 
166         assertEquals( 1, recordManager1.getNbManagedTrees() );
167 
168         assertTrue( nbElems == btree.getNbElems() );
169 
170         TupleCursor<Long, String> cursor = btree.browse();
171 
172         long i = 0;
173         
174         while ( cursor.hasNext() )
175         {
176             Tuple<Long, String> t = cursor.next();
177             assertEquals( ( Long ) i, t.getKey() );
178             assertEquals( String.valueOf( i ), t.getValue() );
179             i++;
180         }
181 
182         cursor.close();
183 
184         assertEquals( nbElems, i );
185     }
186 }