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