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