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  import static org.junit.Assert.assertNotNull;
25  
26  import java.io.File;
27  import java.lang.reflect.Method;
28  import java.nio.ByteBuffer;
29  
30  import org.apache.directory.mavibot.btree.PageIO;
31  import org.apache.directory.mavibot.btree.RecordManager;
32  import org.junit.Test;
33  
34  
35  /**
36   * Test the RecordManager.readXXX() methods using reflection
37   * 
38   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
39   */
40  public class ReadTest
41  {
42      /**
43       * Test the readInt method
44       */
45      @Test
46      public void testReadInt() throws Exception
47      {
48          File tempFile = File.createTempFile( "mavibot", ".db" );
49          String tempFileName = tempFile.getAbsolutePath();
50          tempFile.deleteOnExit();
51  
52          // Create page size of 32 only
53          RecordManager recordManager = new RecordManager( tempFileName, 32 );
54          Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, int.class, PageIO[].class );
55          Method readIntMethod = RecordManager.class.getDeclaredMethod( "readInt", PageIO[].class, long.class );
56          storeMethod.setAccessible( true );
57          readIntMethod.setAccessible( true );
58  
59          // Allocate some Pages
60          PageIO[] pageIos = new PageIO[2];
61          pageIos[0] = new PageIO();
62          pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
63          pageIos[1] = new PageIO();
64          pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
65  
66          // Set the int at the beginning
67          storeMethod.invoke( recordManager, 0, 0x12345678, pageIos );
68  
69          // Read it back
70          int readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 0 );
71  
72          assertEquals( 0x12345678, readValue );
73  
74          // Set the int at the end of the first page
75          storeMethod.invoke( recordManager, 16, 0x12345678, pageIos );
76  
77          // Read it back
78          readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 16 );
79  
80          assertEquals( 0x12345678, readValue );
81  
82          // Set the int at the end of the first page and overlapping on the second page
83          // 1 byte overlapping
84          storeMethod.invoke( recordManager, 17, 0x12345678, pageIos );
85  
86          // Read it back
87          readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 17 );
88  
89          assertEquals( 0x12345678, readValue );
90  
91          // Set the int at the end of the first page and overlapping on the second page
92          // 2 bytes overlapping
93          storeMethod.invoke( recordManager, 18, 0x12345678, pageIos );
94  
95          // Read it back
96          readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 18 );
97  
98          assertEquals( 0x12345678, readValue );
99  
100         // Set the int at the end of the first page and overlapping on the second page
101         // 3 bytes overlapping
102         storeMethod.invoke( recordManager, 19, 0x12345678, pageIos );
103 
104         // Read it back
105         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 19 );
106 
107         assertEquals( 0x12345678, readValue );
108 
109         // Set the int at the beginning of the second page
110         storeMethod.invoke( recordManager, 20, 0x12345678, pageIos );
111 
112         // Read it back
113         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 20 );
114     }
115 
116 
117     /**
118      * Test the readLong method
119      */
120     @Test
121     public void testReadLong() throws Exception
122     {
123         File tempFile = File.createTempFile( "mavibot", ".db" );
124         String tempFileName = tempFile.getAbsolutePath();
125         tempFile.deleteOnExit();
126 
127         // Create page size of 32 only
128         RecordManager recordManager = new RecordManager( tempFileName, 32 );
129         Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, long.class, PageIO[].class );
130         Method readLongMethod = RecordManager.class.getDeclaredMethod( "readLong", PageIO[].class, long.class );
131         storeMethod.setAccessible( true );
132         readLongMethod.setAccessible( true );
133 
134         // Allocate some Pages
135         PageIO[] pageIos = new PageIO[2];
136         pageIos[0] = new PageIO();
137         pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
138         pageIos[1] = new PageIO();
139         pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
140 
141         // Set the int at the beginning
142         storeMethod.invoke( recordManager, 0, 0x0123456789ABCDEFL, pageIos );
143 
144         // Read it back
145         long readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 0 );
146 
147         assertEquals( 0x0123456789ABCDEFL, readValue );
148 
149         // Set the int at the end of the first page
150         storeMethod.invoke( recordManager, 12, 0x0123456789ABCDEFL, pageIos );
151 
152         // Read it back
153         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 12 );
154 
155         assertEquals( 0x0123456789ABCDEFL, readValue );
156 
157         // Set the int at the end of the first page and overlapping on the second page
158         // 1 byte overlapping
159         storeMethod.invoke( recordManager, 13, 0x0123456789ABCDEFL, pageIos );
160 
161         // Read it back
162         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 13 );
163 
164         assertEquals( 0x0123456789ABCDEFL, readValue );
165 
166         // Set the int at the end of the first page and overlapping on the second page
167         // 2 bytes overlapping
168         storeMethod.invoke( recordManager, 14, 0x0123456789ABCDEFL, pageIos );
169 
170         // Read it back
171         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 14 );
172 
173         assertEquals( 0x0123456789ABCDEFL, readValue );
174 
175         // Set the int at the end of the first page and overlapping on the second page
176         // 3 bytes overlapping
177         storeMethod.invoke( recordManager, 15, 0x0123456789ABCDEFL, pageIos );
178 
179         // Read it back
180         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 15 );
181 
182         assertEquals( 0x0123456789ABCDEFL, readValue );
183 
184         // Set the int at the end of the first page and overlapping on the second page
185         // 4 bytes overlapping
186         storeMethod.invoke( recordManager, 16, 0x0123456789ABCDEFL, pageIos );
187 
188         // Read it back
189         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 16 );
190 
191         assertEquals( 0x0123456789ABCDEFL, readValue );
192 
193         // Set the int at the end of the first page and overlapping on the second page
194         // 5 bytes overlapping
195         storeMethod.invoke( recordManager, 17, 0x0123456789ABCDEFL, pageIos );
196 
197         // Read it back
198         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 17 );
199 
200         assertEquals( 0x0123456789ABCDEFL, readValue );
201 
202         // Set the int at the end of the first page and overlapping on the second page
203         // 6 bytes overlapping
204         storeMethod.invoke( recordManager, 18, 0x0123456789ABCDEFL, pageIos );
205 
206         // Read it back
207         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 18 );
208 
209         assertEquals( 0x0123456789ABCDEFL, readValue );
210 
211         // Set the int at the end of the first page and overlapping on the second page
212         // 7 bytes overlapping
213         storeMethod.invoke( recordManager, 19, 0x0123456789ABCDEFL, pageIos );
214 
215         // Read it back
216         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 19 );
217 
218         assertEquals( 0x0123456789ABCDEFL, readValue );
219 
220         // Set the int at the beginning of the second page
221         storeMethod.invoke( recordManager, 20, 0x0123456789ABCDEFL, pageIos );
222 
223         // Read it back
224         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 20 );
225     }
226 
227 
228     /**
229      * Test the readBytes() method
230      */
231     @Test
232     public void testReadBytes() throws Exception
233     {
234         File tempFile = File.createTempFile( "mavibot", ".db" );
235         String tempFileName = tempFile.getAbsolutePath();
236         tempFile.deleteOnExit();
237 
238         // We use smaller pages
239         RecordManager recordManager = new RecordManager( tempFileName, 32 );
240         Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, byte[].class, PageIO[].class );
241         Method readBytesMethod = RecordManager.class.getDeclaredMethod( "readBytes", PageIO[].class, long.class );
242         storeMethod.setAccessible( true );
243         readBytesMethod.setAccessible( true );
244 
245         // Allocate some Pages
246         PageIO[] pageIos = new PageIO[4];
247         pageIos[0] = new PageIO();
248         pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
249         pageIos[1] = new PageIO();
250         pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
251         pageIos[2] = new PageIO();
252         pageIos[2].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
253         pageIos[3] = new PageIO();
254         pageIos[3].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
255 
256         // We start with 4 bytes
257         byte[] bytes = new byte[]
258             { 0x01, 0x23, 0x45, 0x67 };
259 
260         // Set the bytes at the beginning
261         long position = ( Long ) storeMethod.invoke( recordManager, 0L, bytes, pageIos );
262 
263         // Read the bytes back
264         byte[] readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 0L );
265 
266         // The byte length
267         int pos = 0;
268         assertNotNull( readBytes );
269         assertEquals( 4, readBytes.length );
270         // The data
271         assertEquals( 0x01, readBytes[pos++] );
272         assertEquals( 0x23, readBytes[pos++] );
273         assertEquals( 0x45, readBytes[pos++] );
274         assertEquals( 0x67, readBytes[pos++] );
275 
276         // Set the bytes at the end of the first page
277         position = ( Long ) storeMethod.invoke( recordManager, 12L, bytes, pageIos );
278 
279         // Read the bytes back
280         readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 12L );
281 
282         // The byte length
283         pos = 0;
284         assertNotNull( readBytes );
285         assertEquals( 4, readBytes.length );
286         // The data
287         assertEquals( 0x01, readBytes[pos++] );
288         assertEquals( 0x23, readBytes[pos++] );
289         assertEquals( 0x45, readBytes[pos++] );
290         assertEquals( 0x67, readBytes[pos++] );
291 
292         // Set A full page of bytes in the first page 
293         bytes = new byte[16];
294 
295         for ( int i = 0; i < 16; i++ )
296         {
297             bytes[i] = ( byte ) ( i + 1 );
298         }
299 
300         position = ( Long ) storeMethod.invoke( recordManager, 0L, bytes, pageIos );
301 
302         // Read the bytes back
303         readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 0L );
304 
305         // The byte length
306         pos = 0;
307         assertNotNull( readBytes );
308         assertEquals( 16, readBytes.length );
309         // The data
310         for ( int i = 0; i < 16; i++ )
311         {
312             assertEquals( i + 1, readBytes[pos++] );
313         }
314 
315         // Write the bytes over 2 pages
316         position = ( Long ) storeMethod.invoke( recordManager, 15L, bytes, pageIos );
317 
318         // Read the bytes back
319         readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 15L );
320 
321         // The byte length
322         pos = 0;
323         assertNotNull( readBytes );
324         assertEquals( 16, readBytes.length );
325         // The data
326         for ( int i = 0; i < 16; i++ )
327         {
328             assertEquals( i + 1, readBytes[pos++] );
329         }
330 
331         // Write the bytes over 4 pages
332         bytes = new byte[80];
333 
334         for ( int i = 0; i < 80; i++ )
335         {
336             bytes[i] = ( byte ) ( i + 1 );
337         }
338 
339         position = ( Long ) storeMethod.invoke( recordManager, 2L, bytes, pageIos );
340 
341         // Read the bytes back
342         readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 2L );
343 
344         // The byte length
345         pos = 0;
346         assertNotNull( readBytes );
347         assertEquals( 80, readBytes.length );
348         // The data
349         for ( int i = 0; i < 80; i++ )
350         {
351             assertEquals( i + 1, readBytes[pos++] );
352         }
353     }
354 }