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.assertNotNull;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import org.apache.directory.mavibot.btree.BTree;
29  import org.apache.directory.mavibot.btree.BTreeConfiguration;
30  import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
31  import org.apache.directory.mavibot.btree.serializer.IntSerializer;
32  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
33  import org.junit.Test;
34  
35  
36  /**
37   * Test the creation of a BTree with a configuration.
38   * 
39   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
40   */
41  public class BTreeConfigurationTest
42  {
43      // Some values to inject in a btree
44      private static int[] sortedValues = new int[]
45          {
46              0, 1, 2, 4, 5, 6, 8, 9, 11, 12,
47              13, 14, 16, 19, 21, 22, 23, 25, 26, 28,
48              30, 31, 32, 34, 36, 37, 38, 39, 41, 42,
49              44, 45, 47, 50, 52, 53, 54, 55, 56, 58,
50              59, 60, 63, 64, 67, 68, 70, 72, 73, 74,
51              76, 77, 79, 80, 81, 82, 85, 88, 89, 90,
52              92, 93, 95, 97, 98, 100, 101, 102, 103, 104,
53              105, 106, 107, 109, 110, 111, 112, 117, 118, 120,
54              121, 128, 129, 130, 131, 132, 135, 136, 137, 138,
55              139, 140, 141, 142, 143, 146, 147, 148, 149, 150,
56              152, 154, 156, 160, 161, 162, 163, 165, 167, 168,
57              169, 171, 173, 174, 175, 176, 177, 178, 179, 180,
58              181, 182, 183, 189, 190, 193, 194, 195, 199, 200,
59              202, 203, 205, 206, 207, 208, 209, 210, 212, 215,
60              216, 217, 219, 220, 222, 223, 224, 225, 226, 227,
61              228, 230, 231, 235, 236, 238, 239, 241, 242, 243,
62              245, 246, 247, 249, 250, 251, 252, 254, 256, 257,
63              258, 259, 261, 262, 263, 264, 266, 268, 272, 273,
64              274, 276, 277, 278, 279, 282, 283, 286, 289, 290,
65              292, 293, 294, 296, 298, 299, 300, 301, 303, 305,
66              308, 310, 316, 317, 318, 319, 322, 323, 324, 326,
67              327, 329, 331, 333, 334, 335, 336, 337, 338, 339,
68              340, 341, 346, 347, 348, 349, 350, 351, 352, 353,
69              355, 356, 357, 358, 359, 361, 365, 366, 373, 374,
70              375, 379, 380, 381, 382, 384, 385, 387, 388, 389,
71              390, 392, 393, 395, 396, 397, 398, 399, 400, 401,
72              404, 405, 406, 407, 410, 411, 412, 416, 417, 418,
73              420, 421, 422, 424, 426, 427, 428, 430, 431, 432,
74              433, 436, 439, 441, 443, 444, 445, 446, 447, 448,
75              449, 450, 451, 452, 453, 454, 455, 456, 458, 459,
76              464, 466, 469, 470, 471, 472, 475, 477, 478, 482,
77              483, 484, 485, 486, 488, 490, 491, 492, 493, 495,
78              496, 497, 500, 502, 503, 504, 505, 506, 507, 509,
79              510, 514, 516, 518, 520, 521, 523, 524, 526, 527,
80              528, 529, 530, 532, 533, 535, 538, 539, 540, 542,
81              543, 544, 546, 547, 549, 550, 551, 553, 554, 558,
82              559, 561, 563, 564, 566, 567, 568, 569, 570, 571,
83              572, 576, 577, 578, 580, 582, 583, 586, 588, 589,
84              590, 592, 593, 596, 597, 598, 599, 600, 601, 604,
85              605, 606, 607, 609, 610, 613, 615, 617, 618, 619,
86              620, 621, 626, 627, 628, 631, 632, 633, 635, 636,
87              637, 638, 639, 640, 641, 643, 645, 647, 648, 649,
88              650, 651, 652, 653, 655, 656, 658, 659, 660, 662,
89              666, 669, 673, 674, 675, 676, 677, 678, 680, 681,
90              682, 683, 685, 686, 687, 688, 689, 690, 691, 692,
91              693, 694, 696, 698, 699, 700, 701, 705, 708, 709,
92              711, 713, 714, 715, 719, 720, 723, 725, 726, 727,
93              728, 731, 732, 733, 734, 735, 736, 739, 740, 743,
94              744, 745, 746, 747, 749, 750, 752, 753, 762, 763,
95              765, 766, 768, 770, 772, 773, 774, 776, 777, 779,
96              782, 784, 785, 788, 790, 791, 793, 794, 795, 798,
97              799, 800, 801, 803, 804, 805, 808, 810, 812, 813,
98              814, 816, 818, 821, 822, 823, 824, 827, 828, 829,
99              831, 832, 833, 834, 835, 837, 838, 839, 840, 843,
100             846, 847, 849, 852, 853, 854, 856, 857, 859, 860,
101             863, 864, 865, 866, 867, 868, 869, 872, 873, 877,
102             880, 881, 882, 883, 887, 888, 889, 890, 891, 894,
103             895, 897, 898, 899, 902, 904, 905, 907, 908, 910,
104             911, 912, 915, 916, 917, 918, 919, 923, 925, 926,
105             927, 928, 929, 930, 932, 935, 936, 937, 938, 939,
106             944, 945, 947, 952, 953, 954, 955, 956, 957, 958,
107             960, 967, 970, 971, 972, 974, 975, 976, 978, 979,
108             980, 981, 983, 984, 985, 987, 988, 989, 991, 995
109     };
110 
111 
112     /**
113      * Test the creation of a in-memory BTree using the BTreeConfiguration.
114      */
115     @Test
116     public void testConfigurationBasic() throws IOException, KeyNotFoundException
117     {
118         BTreeConfiguration<Integer, String> config = new BTreeConfiguration<Integer, String>();
119         config.setName( "basic" );
120         config.setPageSize( 32 );
121         config.setSerializers( new IntSerializer(), new StringSerializer() );
122 
123         try
124         {
125             // Create the BTree
126             BTree<Integer, String> btree = new BTree<Integer, String>( config );
127 
128             // Inject the values
129             for ( int value : sortedValues )
130             {
131                 String strValue = "V" + value;
132 
133                 btree.insert( value, strValue );
134             }
135 
136             // Check that the tree contains all the values
137             for ( int key : sortedValues )
138             {
139                 String value = btree.get( key );
140 
141                 assertNotNull( value );
142             }
143 
144             btree.close();
145         }
146         finally
147         {
148             // Erase the mavibot file now
149             File mavibotFile = new File( "", "mavibot" );
150 
151             if ( mavibotFile.exists() )
152             {
153                 mavibotFile.delete();
154             }
155 
156             // Erase the journal too
157             File mavibotJournal = new File( "", "mavibot.log" );
158 
159             if ( mavibotJournal.exists() )
160             {
161                 mavibotJournal.delete();
162             }
163         }
164     }
165 
166 
167     /**
168      * Test the creation of a BTree using the BTreeConfiguration, flushing the 
169      * tree on disk, then reloading it in another BTree.
170      */
171     @Test
172     public void testConfigurationFlushReload() throws IOException, KeyNotFoundException
173     {
174         // Create a temporary file
175         File file = File.createTempFile( "testFlush", "data" );
176         file.deleteOnExit();
177         String parent = file.getParent();
178 
179         try
180         {
181             BTreeConfiguration<Integer, String> config = new BTreeConfiguration<Integer, String>();
182             config.setPageSize( 32 );
183             config.setSerializers( new IntSerializer(), new StringSerializer() );
184 
185             config.setFilePath( parent );
186             config.setName( "mavibot" );
187 
188             // Create the BTree
189             BTree<Integer, String> btree = new BTree<Integer, String>( config );
190 
191             // Inject the values
192             for ( int value : sortedValues )
193             {
194                 String strValue = "V" + value;
195 
196                 btree.insert( value, strValue );
197             }
198 
199             // Check that the tree contains all the values
200             for ( int key : sortedValues )
201             {
202                 String value = btree.get( key );
203 
204                 assertNotNull( value );
205             }
206 
207             // Flush the data
208             btree.close();
209 
210             // Now, create a new BTree using the same configuration
211             BTree<Integer, String> btreeCopy = new BTree<Integer, String>( config );
212 
213             // Check that the tree contains all the values
214             for ( int key : sortedValues )
215             {
216                 String value = btreeCopy.get( key );
217 
218                 assertNotNull( value );
219             }
220 
221             btreeCopy.close();
222         }
223         finally
224         {
225             // Erase the mavibot file now
226             File mavibotFile = new File( parent, "mavibot.db" );
227 
228             if ( mavibotFile.exists() )
229             {
230                 mavibotFile.delete();
231             }
232 
233             // Erase the journal too
234             File mavibotJournal = new File( parent, "mavibot.db.log" );
235 
236             if ( mavibotJournal.exists() )
237             {
238                 mavibotJournal.delete();
239             }
240         }
241     }
242 }