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  
25  import java.io.File;
26  import java.lang.reflect.Method;
27  import java.nio.ByteBuffer;
28  
29  import org.apache.directory.mavibot.btree.PageIO;
30  import org.apache.directory.mavibot.btree.RecordManager;
31  import org.junit.Test;
32  
33  
34  /**
35   * Test the RecordManager.store() method using reflection
36   * 
37   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
38   */
39  public class StoreTest
40  {
41      /**
42       * Test the store( int ) method
43       */
44      @Test
45      public void testInjectInt() throws Exception
46      {
47          File tempFile = File.createTempFile( "mavibot", ".db" );
48          String tempFileName = tempFile.getAbsolutePath();
49          tempFile.deleteOnExit();
50  
51          RecordManager recordManager = new RecordManager( tempFileName, 4 * 1024 );
52          Method method = RecordManager.class.getDeclaredMethod( "store", long.class, int.class, PageIO[].class );
53          method.setAccessible( true );
54  
55          // Allocate some Pages
56          PageIO[] pageIos = new PageIO[2];
57          pageIos[0] = new PageIO();
58          pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
59          pageIos[1] = new PageIO();
60          pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
61  
62          // Set the int at the beginning
63          long position = ( Long ) method.invoke( recordManager, 0, 0x12345678, pageIos );
64  
65          assertEquals( 4, position );
66          int pos = 12;
67          assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
68          assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
69          assertEquals( 0x56, pageIos[0].getData().get( pos++ ) );
70          assertEquals( 0x78, pageIos[0].getData().get( pos++ ) );
71  
72          // Set the int at the end of the first page
73          position = ( Long ) method.invoke( recordManager, 4080, 0x12345678, pageIos );
74  
75          assertEquals( 4084, position );
76          pos = 4092;
77          assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
78          assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
79          assertEquals( 0x56, pageIos[0].getData().get( pos++ ) );
80          assertEquals( 0x78, pageIos[0].getData().get( pos++ ) );
81  
82          // Set the int at the end of the first page and overlapping on the second page
83          // 1 byte overlapping
84          position = ( Long ) method.invoke( recordManager, 4081, 0x12345678, pageIos );
85  
86          assertEquals( 4085, position );
87          pos = 4093;
88          assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
89          assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
90          assertEquals( 0x56, pageIos[0].getData().get( pos++ ) );
91          pos = 8;
92          assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
93  
94          // Set the int at the end of the first page and overlapping on the second page
95          // 2 bytes overlapping
96          position = ( Long ) method.invoke( recordManager, 4082, 0x12345678, pageIos );
97  
98          assertEquals( 4086, position );
99          pos = 4094;
100         assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
101         assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
102         pos = 8;
103         assertEquals( 0x56, pageIos[1].getData().get( pos++ ) );
104         assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
105 
106         // Set the int at the end of the first page and overlapping on the second page
107         // 3 bytes overlapping
108         position = ( Long ) method.invoke( recordManager, 4083, 0x12345678, pageIos );
109 
110         assertEquals( 4087, position );
111         pos = 4095;
112         assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
113         pos = 8;
114         assertEquals( 0x34, pageIos[1].getData().get( pos++ ) );
115         assertEquals( 0x56, pageIos[1].getData().get( pos++ ) );
116         assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
117 
118         // Set the int at the beginning of the second page
119         position = ( Long ) method.invoke( recordManager, 4084, 0x12345678, pageIos );
120 
121         assertEquals( 4088, position );
122         pos = 8;
123         assertEquals( 0x12, pageIos[1].getData().get( pos++ ) );
124         assertEquals( 0x34, pageIos[1].getData().get( pos++ ) );
125         assertEquals( 0x56, pageIos[1].getData().get( pos++ ) );
126         assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
127     }
128 
129 
130     /**
131      * Test the store( long ) method
132      */
133     @Test
134     public void testInjectLong() throws Exception
135     {
136         File tempFile = File.createTempFile( "mavibot", ".db" );
137         String tempFileName = tempFile.getAbsolutePath();
138         tempFile.deleteOnExit();
139 
140         RecordManager recordManager = new RecordManager( tempFileName, 4 * 1024 );
141         Method method = RecordManager.class.getDeclaredMethod( "store", long.class, long.class, PageIO[].class );
142         method.setAccessible( true );
143 
144         // Allocate some Pages
145         PageIO[] pageIos = new PageIO[2];
146         pageIos[0] = new PageIO();
147         pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
148         pageIos[1] = new PageIO();
149         pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
150 
151         // Set the long at the beginning
152         long position = ( Long ) method.invoke( recordManager, 0, 0x0123456789ABCDEFL, pageIos );
153 
154         assertEquals( 8, position );
155         int pos = 12;
156         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
157         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
158         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
159         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
160         assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
161         assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
162         assertEquals( ( byte ) 0xCD, pageIos[0].getData().get( pos++ ) );
163         assertEquals( ( byte ) 0xEF, pageIos[0].getData().get( pos++ ) );
164 
165         // Set the long at the end of the first page
166         position = ( Long ) method.invoke( recordManager, 4076, 0x0123456789ABCDEFL, pageIos );
167 
168         assertEquals( 4084, position );
169         pos = 4088;
170         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
171         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
172         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
173         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
174         assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
175         assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
176         assertEquals( ( byte ) 0xCD, pageIos[0].getData().get( pos++ ) );
177         assertEquals( ( byte ) 0xEF, pageIos[0].getData().get( pos++ ) );
178 
179         // Set the long at the end of the first page and overlapping on the second page
180         // 1 byte overlapping
181         position = ( Long ) method.invoke( recordManager, 4077, 0x0123456789ABCDEFL, pageIos );
182 
183         assertEquals( 4085, position );
184         pos = 4089;
185         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
186         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
187         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
188         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
189         assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
190         assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
191         assertEquals( ( byte ) 0xCD, pageIos[0].getData().get( pos++ ) );
192         pos = 8;
193         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
194 
195         // Set the long at the end of the first page and overlapping on the second page
196         // 2 bytes overlapping
197         position = ( Long ) method.invoke( recordManager, 4078, 0x0123456789ABCDEFL, pageIos );
198 
199         assertEquals( 4086, position );
200         pos = 4090;
201         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
202         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
203         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
204         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
205         assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
206         assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
207         pos = 8;
208         assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
209         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
210 
211         // Set the long at the end of the first page and overlapping on the second page
212         // 3 bytes overlapping
213         position = ( Long ) method.invoke( recordManager, 4079, 0x0123456789ABCDEFL, pageIos );
214 
215         assertEquals( 4087, position );
216         pos = 4091;
217         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
218         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
219         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
220         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
221         assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
222         pos = 8;
223         assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
224         assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
225         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
226 
227         // Set the long at the end of the first page and overlapping on the second page
228         // 4 byte overlapping
229         position = ( Long ) method.invoke( recordManager, 4080, 0x0123456789ABCDEFL, pageIos );
230 
231         assertEquals( 4088, position );
232         pos = 4092;
233         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
234         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
235         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
236         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
237         pos = 8;
238         assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
239         assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
240         assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
241         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
242 
243         // Set the long at the end of the first page and overlapping on the second page
244         // 5 bytes overlapping
245         position = ( Long ) method.invoke( recordManager, 4081, 0x0123456789ABCDEFL, pageIos );
246 
247         assertEquals( 4089, position );
248         pos = 4093;
249         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
250         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
251         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
252         pos = 8;
253         assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
254         assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
255         assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
256         assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
257         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
258 
259         // Set the long at the end of the first page and overlapping on the second page
260         // 6 bytes overlapping
261         position = ( Long ) method.invoke( recordManager, 4082, 0x0123456789ABCDEFL, pageIos );
262 
263         assertEquals( 4090, position );
264         pos = 4094;
265         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
266         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
267         pos = 8;
268         assertEquals( 0x45, pageIos[1].getData().get( pos++ ) );
269         assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
270         assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
271         assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
272         assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
273         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
274 
275         // Set the long at the end of the first page and overlapping on the second page
276         // 7 bytes overlapping
277         position = ( Long ) method.invoke( recordManager, 4083, 0x0123456789ABCDEFL, pageIos );
278 
279         assertEquals( 4091, position );
280         pos = 4095;
281         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
282         pos = 8;
283         assertEquals( 0x23, pageIos[1].getData().get( pos++ ) );
284         assertEquals( 0x45, pageIos[1].getData().get( pos++ ) );
285         assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
286         assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
287         assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
288         assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
289         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
290 
291         // Set the long at the beginning of the second page
292         position = ( Long ) method.invoke( recordManager, 4084, 0x0123456789ABCDEFL, pageIos );
293 
294         assertEquals( 4092, position );
295         pos = 8;
296         assertEquals( 0x01, pageIos[1].getData().get( pos++ ) );
297         assertEquals( 0x23, pageIos[1].getData().get( pos++ ) );
298         assertEquals( 0x45, pageIos[1].getData().get( pos++ ) );
299         assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
300         assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
301         assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
302         assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
303         assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
304     }
305 
306 
307     /**
308      * Test the store( bytes ) method
309      */
310     @Test
311     public void testInjectBytes() throws Exception
312     {
313         File tempFile = File.createTempFile( "mavibot", ".db" );
314         String tempFileName = tempFile.getAbsolutePath();
315         tempFile.deleteOnExit();
316 
317         // We use smaller pages
318         RecordManager recordManager = new RecordManager( tempFileName, 32 );
319         Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, byte[].class, PageIO[].class );
320         storeMethod.setAccessible( true );
321 
322         // Allocate some Pages
323         PageIO[] pageIos = new PageIO[4];
324         pageIos[0] = new PageIO();
325         pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
326         pageIos[1] = new PageIO();
327         pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
328         pageIos[2] = new PageIO();
329         pageIos[2].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
330         pageIos[3] = new PageIO();
331         pageIos[3].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
332 
333         // We start with 4 bytes
334         byte[] bytes = new byte[]
335             { 0x01, 0x23, 0x45, 0x67 };
336 
337         // Set the bytes at the beginning
338         long position = ( Long ) storeMethod.invoke( recordManager, 0L, bytes, pageIos );
339 
340         assertEquals( 8, position );
341         int pos = 12;
342         // The byte length
343         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
344         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
345         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
346         assertEquals( 0x04, pageIos[0].getData().get( pos++ ) );
347         // The data
348         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
349         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
350         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
351         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
352 
353         // Set the bytes at the end of the first page
354         position = ( Long ) storeMethod.invoke( recordManager, 12L, bytes, pageIos );
355 
356         assertEquals( 20, position );
357         pos = 24;
358         // The byte length
359         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
360         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
361         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
362         assertEquals( 0x04, pageIos[0].getData().get( pos++ ) );
363         // The data
364         assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
365         assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
366         assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
367         assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
368 
369         // Set A full page of bytes in the first page 
370         bytes = new byte[16];
371 
372         for ( int i = 0; i < 16; i++ )
373         {
374             bytes[i] = ( byte ) ( i + 1 );
375         }
376 
377         position = ( Long ) storeMethod.invoke( recordManager, 0L, bytes, pageIos );
378 
379         assertEquals( 20, position );
380         pos = 12;
381         // The byte length
382         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
383         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
384         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
385         assertEquals( 0x10, pageIos[0].getData().get( pos++ ) );
386 
387         // The data
388         for ( int i = 0; i < 16; i++ )
389         {
390             assertEquals( ( byte ) ( i + 1 ), pageIos[0].getData().get( pos++ ) );
391         }
392 
393         // Write the bytes over 2 pages
394         position = ( Long ) storeMethod.invoke( recordManager, 15L, bytes, pageIos );
395 
396         assertEquals( 35, position );
397         pos = 27;
398         // The byte length
399         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
400         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
401         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
402         assertEquals( 0x10, pageIos[0].getData().get( pos++ ) );
403 
404         // The data in the first page
405         assertEquals( 1, pageIos[0].getData().get( pos++ ) );
406 
407         // and in the second page
408         pos = 8;
409 
410         for ( int i = 0; i < 15; i++ )
411         {
412             assertEquals( ( byte ) ( i + 2 ), pageIos[1].getData().get( pos++ ) );
413         }
414 
415         // Write the bytes over 4 pages
416         bytes = new byte[80];
417 
418         for ( int i = 0; i < 80; i++ )
419         {
420             bytes[i] = ( byte ) ( i + 1 );
421         }
422 
423         position = ( Long ) storeMethod.invoke( recordManager, 2L, bytes, pageIos );
424 
425         assertEquals( 86, position );
426         pos = 14;
427         // The byte length
428         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
429         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
430         assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
431         assertEquals( 0x50, pageIos[0].getData().get( pos++ ) );
432 
433         // The data in the first page
434         for ( int i = 0; i < 14; i++ )
435         {
436             assertEquals( ( byte ) ( i + 1 ), pageIos[0].getData().get( pos++ ) );
437         }
438 
439         // The data in the second page
440         pos = 8;
441         for ( int i = 14; i < 38; i++ )
442         {
443             assertEquals( ( byte ) ( i + 1 ), pageIos[1].getData().get( pos++ ) );
444         }
445 
446         // The data in the third page
447         pos = 8;
448         for ( int i = 38; i < 62; i++ )
449         {
450             assertEquals( ( byte ) ( i + 1 ), pageIos[2].getData().get( pos++ ) );
451         }
452 
453         // The data in the forth page
454         pos = 8;
455         for ( int i = 62; i < 80; i++ )
456         {
457             assertEquals( ( byte ) ( i + 1 ), pageIos[3].getData().get( pos++ ) );
458         }
459     }
460 }