| 1 |
/** |
| 2 |
* JDBM LICENSE v1.00 |
| 3 |
* |
| 4 |
* Redistribution and use of this software and associated documentation |
| 5 |
* ("Software"), with or without modification, are permitted provided |
| 6 |
* that the following conditions are met: |
| 7 |
* |
| 8 |
* 1. Redistributions of source code must retain copyright |
| 9 |
* statements and notices. Redistributions must also contain a |
| 10 |
* copy of this document. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the |
| 13 |
* above copyright notice, this list of conditions and the |
| 14 |
* following disclaimer in the documentation and/or other |
| 15 |
* materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. The name "JDBM" must not be used to endorse or promote |
| 18 |
* products derived from this Software without prior written |
| 19 |
* permission of Cees de Groot. For written permission, |
| 20 |
* please contact [email protected]. |
| 21 |
* |
| 22 |
* 4. Products derived from this Software may not be called "JDBM" |
| 23 |
* nor may "JDBM" appear in their names without prior written |
| 24 |
* permission of Cees de Groot. |
| 25 |
* |
| 26 |
* 5. Due credit should be given to the JDBM Project |
| 27 |
* (http://jdbm.sourceforge.net/). |
| 28 |
* |
| 29 |
* THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS |
| 30 |
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 31 |
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 33 |
* CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 34 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 35 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 36 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 37 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 38 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 39 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 40 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 41 |
* |
| 42 |
* Copyright 2000 (C) Cees de Groot. All Rights Reserved. |
| 43 |
* Contributions are Copyright (C) 2000 by their associated contributors. |
| 44 |
* |
| 45 |
*/ |
| 46 |
|
| 47 |
package jdbm.btree; |
| 48 |
|
| 49 |
|
| 50 |
import static org.junit.Assert.assertEquals; |
| 51 |
import static org.junit.Assert.assertNotNull; |
| 52 |
import static org.junit.Assert.assertNull; |
| 53 |
import static org.junit.Assert.assertTrue; |
| 54 |
import static org.junit.Assert.fail; |
| 55 |
|
| 56 |
import java.io.File; |
| 57 |
import java.io.IOException; |
| 58 |
import java.io.Serializable; |
| 59 |
import java.util.Iterator; |
| 60 |
import java.util.Map; |
| 61 |
import java.util.TreeMap; |
| 62 |
|
| 63 |
import jdbm.RecordManager; |
| 64 |
import jdbm.RecordManagerFactory; |
| 65 |
import jdbm.helper.ByteArrayComparator; |
| 66 |
import jdbm.helper.IntegerComparator; |
| 67 |
import jdbm.helper.StringComparator; |
| 68 |
import jdbm.helper.Tuple; |
| 69 |
import jdbm.helper.TupleBrowser; |
| 70 |
import jdbm.recman.BaseRecordManager; |
| 71 |
import jdbm.recman.TransactionManager; |
| 72 |
|
| 73 |
import org.junit.Ignore; |
| 74 |
import org.junit.Rule; |
| 75 |
import org.junit.Test; |
| 76 |
import org.junit.rules.TemporaryFolder; |
| 77 |
import org.junit.runner.RunWith; |
| 78 |
|
| 79 |
import com.mycila.junit.concurrent.ConcurrentJunitRunner; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* This class contains all Unit tests for {@link BTree}. |
| 84 |
* |
| 85 |
* @author <a href="mailto:[email protected]">Alex Boisvert</a> |
| 86 |
*/ |
| 87 |
@RunWith(ConcurrentJunitRunner.class) |
| 88 |
public class TestBTree |
| 89 |
{ |
| 90 |
@Rule |
| 91 |
public TemporaryFolder folder = new TemporaryFolder(); |
| 92 |
|
| 93 |
static final boolean DEBUG = false; |
| 94 |
|
| 95 |
// the number of threads to be started in the synchronization test |
| 96 |
static final int THREAD_NUMBER = 5; |
| 97 |
|
| 98 |
// the size of the content of the maps for the synchronization |
| 99 |
// test. Beware that THREAD_NUMBER * THREAD_CONTENT_COUNT < Integer.MAX_VALUE. |
| 100 |
static final int THREAD_CONTENT_SIZE = 150; |
| 101 |
|
| 102 |
// for how long should the threads run. |
| 103 |
static final int THREAD_RUNTIME = 10 * 1000; |
| 104 |
|
| 105 |
|
| 106 |
private String getTemporaryFile( String name ) throws IOException |
| 107 |
{ |
| 108 |
String file = folder.newFile( name ).getAbsolutePath(); |
| 109 |
return file; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
//---------------------------------------------------------------------- |
| 114 |
/** |
| 115 |
* Basic tests |
| 116 |
*/ |
| 117 |
@Test |
| 118 |
public void testBasics() throws IOException |
| 119 |
{ |
| 120 |
RecordManager recman; |
| 121 |
BTree<byte[], byte[]> tree; |
| 122 |
byte[] test0 = "test0".getBytes(); |
| 123 |
byte[] test1 = "test1".getBytes(); |
| 124 |
byte[] test2 = "test2".getBytes(); |
| 125 |
byte[] test3 = "test3".getBytes(); |
| 126 |
byte[] value1 = "value1".getBytes(); |
| 127 |
byte[] value2 = "value2".getBytes(); |
| 128 |
|
| 129 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testBasics" ) ); |
| 130 |
tree = new BTree<byte[], byte[]>( recman, new ByteArrayComparator() ); |
| 131 |
|
| 132 |
tree.insert( test1, value1, false ); |
| 133 |
tree.insert( test2, value2, false ); |
| 134 |
byte[] result = tree.find( test0 ); |
| 135 |
|
| 136 |
assertNull( result ); |
| 137 |
|
| 138 |
result = tree.find( test1 ); |
| 139 |
|
| 140 |
assertNotNull( result ); |
| 141 |
assertEquals( 0, ByteArrayComparator.compareByteArray( result, value1 ) ); |
| 142 |
|
| 143 |
result = tree.find( test2 ); |
| 144 |
|
| 145 |
assertNotNull( result ); |
| 146 |
assertEquals( 0, ByteArrayComparator.compareByteArray( result, value2 ) ); |
| 147 |
|
| 148 |
result = tree.find( test3 ); |
| 149 |
assertNull( result ); |
| 150 |
|
| 151 |
recman.close(); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Basic tests, just use the simple test possibilities of junit (cdaller) |
| 157 |
*/ |
| 158 |
@Test |
| 159 |
public void testBasics2() throws IOException |
| 160 |
{ |
| 161 |
RecordManager recman; |
| 162 |
BTree<byte[], byte[]> tree; |
| 163 |
byte[] test0 = "test0".getBytes(); |
| 164 |
byte[] test1 = "test1".getBytes(); |
| 165 |
byte[] test2 = "test2".getBytes(); |
| 166 |
byte[] test3 = "test3".getBytes(); |
| 167 |
byte[] value1 = "value1".getBytes(); |
| 168 |
byte[] value2 = "value2".getBytes(); |
| 169 |
|
| 170 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testBasics2" ) ); |
| 171 |
tree = new BTree<byte[], byte[]>( recman, new ByteArrayComparator() ); |
| 172 |
|
| 173 |
tree.insert( test1, value1, false ); |
| 174 |
tree.insert( test2, value2, false ); |
| 175 |
|
| 176 |
assertEquals( null, tree.find( test0 ) ); |
| 177 |
assertEquals( 0, ByteArrayComparator.compareByteArray( value1, tree.find( test1 ) ) ); |
| 178 |
assertEquals( 0, ByteArrayComparator.compareByteArray( value2, tree.find( test2 ) ) ); |
| 179 |
assertEquals( null, tree.find( test3 ) ); |
| 180 |
|
| 181 |
recman.close(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
/** |
| 186 |
* Test what happens after the recmanager has been closed but the |
| 187 |
* btree is accessed. WHAT SHOULD HAPPEN??????????? |
| 188 |
* (cdaller) |
| 189 |
*/ |
| 190 |
@Test |
| 191 |
public void testClose() throws IOException |
| 192 |
{ |
| 193 |
RecordManager recman; |
| 194 |
BTree<byte[], byte[]> tree; |
| 195 |
byte[] test0 = "test0".getBytes(); |
| 196 |
byte[] test1 = "test1".getBytes(); |
| 197 |
byte[] test2 = "test2".getBytes(); |
| 198 |
byte[] test3 = "test3".getBytes(); |
| 199 |
byte[] value1 = "value1".getBytes(); |
| 200 |
byte[] value2 = "value2".getBytes(); |
| 201 |
|
| 202 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testClose" ) ); |
| 203 |
tree = new BTree<byte[], byte[]>( recman, new ByteArrayComparator() ); |
| 204 |
|
| 205 |
tree.insert( test1, value1, false ); |
| 206 |
tree.insert( test2, value2, false ); |
| 207 |
|
| 208 |
assertEquals( null, tree.find( test0 ) ); |
| 209 |
assertEquals( 0, ByteArrayComparator.compareByteArray( value1, tree.find( test1 ) ) ); |
| 210 |
assertEquals( 0, ByteArrayComparator.compareByteArray( value2, tree.find( test2 ) ) ); |
| 211 |
assertEquals( null, tree.find( test3 ) ); |
| 212 |
|
| 213 |
recman.close(); |
| 214 |
|
| 215 |
try |
| 216 |
{ |
| 217 |
tree.browse(); |
| 218 |
fail( "Should throw an IllegalStateException on access on not opened btree" ); |
| 219 |
} |
| 220 |
catch ( IllegalStateException except ) |
| 221 |
{ |
| 222 |
// expected |
| 223 |
} |
| 224 |
|
| 225 |
try |
| 226 |
{ |
| 227 |
tree.find( test0 ); |
| 228 |
fail( "Should throw an IllegalStateException on access on not opened btree" ); |
| 229 |
} |
| 230 |
catch ( IllegalStateException except ) |
| 231 |
{ |
| 232 |
// expected |
| 233 |
} |
| 234 |
|
| 235 |
try |
| 236 |
{ |
| 237 |
tree.findGreaterOrEqual( test0 ); |
| 238 |
fail( "Should throw an IllegalStateException on access on not opened btree" ); |
| 239 |
} |
| 240 |
catch ( IllegalStateException except ) |
| 241 |
{ |
| 242 |
// expected |
| 243 |
} |
| 244 |
|
| 245 |
try |
| 246 |
{ |
| 247 |
tree.insert( test2, value2, false ); |
| 248 |
fail( "Should throw an IllegalStateException on access on not opened btree" ); |
| 249 |
} |
| 250 |
catch ( IllegalStateException except ) |
| 251 |
{ |
| 252 |
// expected |
| 253 |
} |
| 254 |
|
| 255 |
try |
| 256 |
{ |
| 257 |
tree.remove( test0 ); |
| 258 |
fail( "Should throw an IllegalStateException on access on not opened btree" ); |
| 259 |
} |
| 260 |
catch ( IllegalStateException except ) |
| 261 |
{ |
| 262 |
// expected |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
/** |
| 268 |
* Test to insert different objects into one btree. (cdaller) |
| 269 |
*/ |
| 270 |
@Test |
| 271 |
public void testInsert() throws IOException |
| 272 |
{ |
| 273 |
RecordManager recman; |
| 274 |
BTree<String, Object> tree; |
| 275 |
|
| 276 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testInsert" ) ); |
| 277 |
tree = new BTree<String, Object>( recman, new StringComparator() ); |
| 278 |
|
| 279 |
// insert different objects and retrieve them |
| 280 |
tree.insert( "test1", "value1", false ); |
| 281 |
tree.insert( "test2", "value2", false ); |
| 282 |
tree.insert( "one", Integer.valueOf( 1 ), false ); |
| 283 |
tree.insert( "two", Long.valueOf( 2 ), false ); |
| 284 |
tree.insert( "myownobject", new ObjectStore( Integer.valueOf( 234 ) ), false ); |
| 285 |
|
| 286 |
assertEquals( "value2", tree.find( "test2" ) ); |
| 287 |
assertEquals( "value1", tree.find( "test1" ) ); |
| 288 |
assertEquals( Integer.valueOf( 1 ), tree.find( "one" ) ); |
| 289 |
assertEquals( Long.valueOf( 2 ), tree.find( "two" ) ); |
| 290 |
|
| 291 |
// what happens here? must not be replaced, does it return anything? |
| 292 |
// probably yes! |
| 293 |
assertEquals( "value1", tree.insert( "test1", "value11", false ) ); |
| 294 |
assertEquals( "value1", tree.find( "test1" ) ); // still the old value? |
| 295 |
assertEquals( "value1", tree.insert( "test1", "value11", true ) ); |
| 296 |
assertEquals( "value11", tree.find( "test1" ) ); // now the new value! |
| 297 |
|
| 298 |
ObjectStore expectedObj = new ObjectStore( Integer.valueOf( 234 ) ); |
| 299 |
ObjectStore btreeObj = ( ObjectStore ) tree.find( "myownobject" ); |
| 300 |
|
| 301 |
assertEquals( expectedObj, btreeObj ); |
| 302 |
|
| 303 |
recman.close(); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* Test to insert many objects into one btree |
| 309 |
*/ |
| 310 |
@Test |
| 311 |
public void testInsertMany() throws IOException |
| 312 |
{ |
| 313 |
BTree<String, String> tree; |
| 314 |
|
| 315 |
RecordManager recordManager = RecordManagerFactory.createRecordManager( getTemporaryFile( "testInsertMany" ) ); |
| 316 |
tree = new BTree<String, String>( recordManager, new StringComparator() ); |
| 317 |
tree.setPageSize( 4 ); |
| 318 |
|
| 319 |
// insert different objects and retrieve them |
| 320 |
tree.insert( "test1", "value1", false ); |
| 321 |
tree.insert( "test2", "value2", false ); |
| 322 |
tree.insert( "test3", "value3", false ); |
| 323 |
tree.insert( "test4", "value4", false ); |
| 324 |
tree.insert( "test5", "value5", false ); |
| 325 |
tree.insert( "test6", "value6", false ); |
| 326 |
|
| 327 |
assertEquals( "value2", tree.find( "test2" ) ); |
| 328 |
assertEquals( "value1", tree.find( "test1" ) ); |
| 329 |
|
| 330 |
recordManager.close(); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
/** |
| 335 |
* Test to remove objects from the btree. (cdaller) |
| 336 |
*/ |
| 337 |
@Test |
| 338 |
public void testRemove() throws IOException |
| 339 |
{ |
| 340 |
RecordManager recman; |
| 341 |
BTree<String, Object> tree; |
| 342 |
|
| 343 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testRemove" ) ); |
| 344 |
tree = new BTree<String, Object>( recman, new StringComparator() ); |
| 345 |
|
| 346 |
tree.insert( "test1", "value1", false ); |
| 347 |
tree.insert( "test2", "value2", false ); |
| 348 |
|
| 349 |
assertEquals( "value1", tree.find( "test1" ) ); |
| 350 |
assertEquals( "value2", tree.find( "test2" ) ); |
| 351 |
|
| 352 |
tree.remove( "test1" ); |
| 353 |
|
| 354 |
assertEquals( null, tree.find( "test1" ) ); |
| 355 |
assertEquals( "value2", tree.find( "test2" ) ); |
| 356 |
|
| 357 |
tree.remove( "test2" ); |
| 358 |
|
| 359 |
assertEquals( null, tree.find( "test2" ) ); |
| 360 |
|
| 361 |
int iterations = 1000; |
| 362 |
|
| 363 |
for ( int count = 0; count < iterations; count++ ) |
| 364 |
{ |
| 365 |
tree.insert( "num" + count, Integer.valueOf( count ), false ); |
| 366 |
} |
| 367 |
|
| 368 |
assertEquals( iterations, tree.size() ); |
| 369 |
|
| 370 |
for ( int count = 0; count < iterations; count++ ) |
| 371 |
{ |
| 372 |
assertEquals( Integer.valueOf( count ), tree.find( "num" + count ) ); |
| 373 |
} |
| 374 |
|
| 375 |
for ( int count = 0; count < iterations; count++ ) |
| 376 |
{ |
| 377 |
tree.remove( "num" + count ); |
| 378 |
} |
| 379 |
|
| 380 |
assertEquals( 0, tree.size() ); |
| 381 |
|
| 382 |
recman.close(); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
/** |
| 387 |
* Test to find differents objects in the btree. (cdaller) |
| 388 |
*/ |
| 389 |
@Test |
| 390 |
public void testFind() throws IOException |
| 391 |
{ |
| 392 |
RecordManager recman; |
| 393 |
BTree<String, String> tree; |
| 394 |
|
| 395 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testFind" ) ); |
| 396 |
tree = new BTree<String, String>( recman, new StringComparator() ); |
| 397 |
|
| 398 |
tree.insert( "test1", "value1", false ); |
| 399 |
tree.insert( "test2", "value2", false ); |
| 400 |
|
| 401 |
Object value = tree.find( "test1" ); |
| 402 |
|
| 403 |
assertTrue( value instanceof String ); |
| 404 |
assertEquals( "value1", value ); |
| 405 |
|
| 406 |
tree.insert( "", "Empty String as key", false ); |
| 407 |
|
| 408 |
assertEquals( "Empty String as key", tree.find( "" ) ); |
| 409 |
assertEquals( null, tree.find( "someoneelse" ) ); |
| 410 |
|
| 411 |
recman.close(); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
/** |
| 416 |
* Test to insert, retrieve and remove a large amount of data. (cdaller) |
| 417 |
*/ |
| 418 |
@Test |
| 419 |
public void testLargeDataAmount() throws IOException |
| 420 |
{ |
| 421 |
RecordManager recman; |
| 422 |
BTree<String, Object> tree; |
| 423 |
|
| 424 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testLargeDataAmount" ) ); |
| 425 |
|
| 426 |
// recman = new jdbm.recman.BaseRecordManager( "test" ); |
| 427 |
tree = new BTree<String, Object>( recman, new StringComparator() ); |
| 428 |
int iterations = 10000; |
| 429 |
|
| 430 |
// insert data |
| 431 |
for ( int count = 0; count < iterations; count++ ) |
| 432 |
{ |
| 433 |
assertEquals( null, tree.insert( "num" + count, Integer.valueOf( count ), false ) ); |
| 434 |
} |
| 435 |
|
| 436 |
// find data |
| 437 |
for ( int count = 0; count < iterations; count++ ) |
| 438 |
{ |
| 439 |
assertEquals( Integer.valueOf( count ), tree.find( "num" + count ) ); |
| 440 |
} |
| 441 |
|
| 442 |
// delete data |
| 443 |
for ( int count = 0; count < iterations; count++ ) |
| 444 |
{ |
| 445 |
assertEquals( Integer.valueOf( count ), tree.remove( "num" + count ) ); |
| 446 |
} |
| 447 |
|
| 448 |
assertEquals( 0, tree.size() ); |
| 449 |
|
| 450 |
recman.close(); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
/** |
| 455 |
* Test access from multiple threads. Assertions only work, when the |
| 456 |
* run() method is overridden and the exceptions of the threads are |
| 457 |
* added to the resultset of the TestCase. see run() and |
| 458 |
* handleException(). |
| 459 |
*/ |
| 460 |
@Test |
| 461 |
public void testMultithreadAccess() throws IOException, InterruptedException |
| 462 |
{ |
| 463 |
RecordManager recman; |
| 464 |
BTree<String, Integer> tree; |
| 465 |
|
| 466 |
recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testMultithreadAccess" ) ); |
| 467 |
tree = new BTree<String, Integer>( recman, new StringComparator() ); |
| 468 |
TestThread<String, Integer>[] threadPool = new TestThread[THREAD_NUMBER]; |
| 469 |
String name; |
| 470 |
Map<String, Integer> content; |
| 471 |
|
| 472 |
// create content for the tree, different content for different threads! |
| 473 |
for ( int threadCount = 0; threadCount < THREAD_NUMBER; threadCount++ ) |
| 474 |
{ |
| 475 |
name = "thread" + threadCount; |
| 476 |
content = new TreeMap<String, Integer>(); |
| 477 |
|
| 478 |
for ( int contentCount = 0; contentCount < THREAD_CONTENT_SIZE; contentCount++ ) |
| 479 |
{ |
| 480 |
// guarantee, that keys and values do not overleap, |
| 481 |
// otherwise one thread removes some keys/values of |
| 482 |
// other threads! |
| 483 |
content.put( name + "_" + contentCount, |
| 484 |
Integer.valueOf( threadCount * THREAD_CONTENT_SIZE + contentCount ) ); |
| 485 |
} |
| 486 |
|
| 487 |
threadPool[threadCount] = new TestThread<String, Integer>( name, tree, content ); |
| 488 |
threadPool[threadCount].start(); |
| 489 |
} |
| 490 |
|
| 491 |
Thread.sleep( THREAD_RUNTIME ); |
| 492 |
|
| 493 |
// stop threads: |
| 494 |
for ( int threadCount = 0; threadCount < THREAD_NUMBER; threadCount++ ) |
| 495 |
{ |
| 496 |
threadPool[threadCount].setStop(); |
| 497 |
} |
| 498 |
|
| 499 |
// wait until the threads really stop: |
| 500 |
try |
| 501 |
{ |
| 502 |
for ( int threadCount = 0; threadCount < THREAD_NUMBER; threadCount++ ) |
| 503 |
{ |
| 504 |
threadPool[threadCount].join(); |
| 505 |
} |
| 506 |
} |
| 507 |
catch ( InterruptedException ignore ) |
| 508 |
{ |
| 509 |
ignore.printStackTrace(); |
| 510 |
} |
| 511 |
|
| 512 |
recman.close(); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
@Test |
| 517 |
@Ignore |
| 518 |
public void testWithTransactionManager() throws IOException |
| 519 |
{ |
| 520 |
BaseRecordManager recman; |
| 521 |
BTree<Integer, String> tree; |
| 522 |
//String file = getTemporaryFile( "testWithTM" ); |
| 523 |
|
| 524 |
String data = "This is a test with 1024 chars " + |
| 525 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 526 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 527 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 528 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 529 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 530 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 531 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 532 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 533 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 534 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 535 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 536 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 537 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 538 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 539 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 540 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 541 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 542 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 543 |
"0123456701234567012345670123456701234567012345670123456701234567" + |
| 544 |
"0123456701234567012345670123456701234567012345670123456701234567"; |
| 545 |
|
| 546 |
int[] intervals = new int[] |
| 547 |
{ 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000 }; |
| 548 |
|
| 549 |
/* |
| 550 |
String file = getTemporaryFile( "testWithTM" ); |
| 551 |
|
| 552 |
for ( int i = 0; i < 10; i++ ) |
| 553 |
{ |
| 554 |
recman = new BaseRecordManager( file ); |
| 555 |
TransactionManager tm = recman.getTransactionManager(); |
| 556 |
tree = new BTree<Integer, String>( recman, new IntegerComparator() ); |
| 557 |
tm.setMaximumTransactionsInLog( 200 ); |
| 558 |
|
| 559 |
long t0 = System.currentTimeMillis(); |
| 560 |
|
| 561 |
for ( int k = 0; k < 10000; k++ ) |
| 562 |
{ |
| 563 |
tree.insert( k, data + k, true ); |
| 564 |
|
| 565 |
if ( i % 200 == 0 ) |
| 566 |
{ |
| 567 |
recman.commit(); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
recman.commit(); |
| 572 |
tm.synchronizeLog(); |
| 573 |
|
| 574 |
long t1 = System.currentTimeMillis(); |
| 575 |
|
| 576 |
System.out.println( "In " + file + " : " + ( t1 - t0 ) + "ms transaction disabled" ); |
| 577 |
|
| 578 |
if ( !new File( file + ".db" ).delete() ) |
| 579 |
{ |
| 580 |
System.out.println( "File not deleted : " + new File( file + ".db" ) ); |
| 581 |
} |
| 582 |
} |
| 583 |
*/ |
| 584 |
|
| 585 |
for ( int i = 0; i < intervals.length; i++ ) |
| 586 |
{ |
| 587 |
String file = getTemporaryFile( "testWithTM" ); |
| 588 |
|
| 589 |
for ( int j = i; j < intervals.length; j++ ) |
| 590 |
{ |
| 591 |
int intervalCommit = intervals[i]; |
| 592 |
int intervalSync = intervals[j]; |
| 593 |
recman = new BaseRecordManager( file ); |
| 594 |
TransactionManager tm = recman.getTransactionManager(); |
| 595 |
tree = new BTree<Integer, String>( recman, new IntegerComparator() ); |
| 596 |
tm.setMaximumTransactionsInLog( intervalSync ); |
| 597 |
|
| 598 |
long t0 = System.currentTimeMillis(); |
| 599 |
|
| 600 |
for ( int k = 0; k < 10000; k++ ) |
| 601 |
{ |
| 602 |
tree.insert( k, data + k, true ); |
| 603 |
|
| 604 |
if ( k % intervalCommit == 0 ) |
| 605 |
{ |
| 606 |
recman.commit(); |
| 607 |
} |
| 608 |
|
| 609 |
if ( k % intervalSync == 0 ) |
| 610 |
{ |
| 611 |
tm.synchronizeLog(); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
tm.synchronizeLog(); |
| 616 |
|
| 617 |
long t1 = System.currentTimeMillis(); |
| 618 |
|
| 619 |
System.out.println( "In " + file + " : " + ( t1 - t0 ) + "ms with CI, Sync [" + intervalCommit + "," |
| 620 |
+ intervalSync + "]" ); |
| 621 |
System.out.println( "File = " + new File( file ).getParent() ); |
| 622 |
|
| 623 |
if ( !new File( file + ".lg" ).delete() ) |
| 624 |
{ |
| 625 |
System.out.println( "File not deleted : " + new File( file + ".lg" ).getParent() ); |
| 626 |
} |
| 627 |
|
| 628 |
if ( !new File( file + ".db" ).delete() ) |
| 629 |
{ |
| 630 |
System.out.println( "File not deleted : " + new File( file + ".db" ).getParent() ); |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* Helper method to 'simulate' the methods of an entry set of the btree. |
| 639 |
*/ |
| 640 |
protected boolean containsValue( Object value, BTree btree ) throws IOException |
| 641 |
{ |
| 642 |
// we must synchronize on the BTree while browsing |
| 643 |
synchronized ( btree ) |
| 644 |
{ |
| 645 |
TupleBrowser browser = btree.browse(); |
| 646 |
Tuple tuple = new Tuple(); |
| 647 |
|
| 648 |
while ( browser.getNext( tuple ) ) |
| 649 |
{ |
| 650 |
if ( tuple.getValue().equals( value ) ) |
| 651 |
{ |
| 652 |
return ( true ); |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
return false; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
/** |
| 662 |
* Helper method to 'simulate' the methods of an entry set of the btree. |
| 663 |
*/ |
| 664 |
protected static boolean contains( Map.Entry entry, BTree btree ) throws IOException |
| 665 |
{ |
| 666 |
Object tree_obj = btree.find( entry.getKey() ); |
| 667 |
|
| 668 |
if ( tree_obj == null ) |
| 669 |
{ |
| 670 |
// can't distinguish, if value is null or not found!!!!!! |
| 671 |
return ( entry.getValue() == null ); |
| 672 |
} |
| 673 |
|
| 674 |
return ( tree_obj.equals( entry.getValue() ) ); |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* Inner class for testing puroposes only (multithreaded access) |
| 679 |
*/ |
| 680 |
class TestThread<K, V> extends Thread |
| 681 |
{ |
| 682 |
Map<K, V> content; |
| 683 |
BTree<K, V> btree; |
| 684 |
volatile boolean stop = true; |
| 685 |
int THREAD_SLEEP_TIME = 50; // in ms |
| 686 |
String name; |
| 687 |
|
| 688 |
|
| 689 |
TestThread( String name, BTree<K, V> btree, Map<K, V> content ) |
| 690 |
{ |
| 691 |
this.content = content; |
| 692 |
this.btree = btree; |
| 693 |
this.name = name; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
public void setStop() |
| 698 |
{ |
| 699 |
stop = true; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
private void action() throws IOException |
| 704 |
{ |
| 705 |
Iterator<Map.Entry<K, V>> iterator = content.entrySet().iterator(); |
| 706 |
Map.Entry<K, V> entry; |
| 707 |
|
| 708 |
while ( iterator.hasNext() ) |
| 709 |
{ |
| 710 |
entry = iterator.next(); |
| 711 |
assertEquals( null, btree.insert( entry.getKey(), entry.getValue(), false ) ); |
| 712 |
} |
| 713 |
|
| 714 |
// as other threads are filling the btree as well, the size |
| 715 |
// of the btree is unknown (but must be at least the size of |
| 716 |
// the content map) |
| 717 |
assertTrue( content.size() <= btree.size() ); |
| 718 |
iterator = content.entrySet().iterator(); |
| 719 |
|
| 720 |
while ( iterator.hasNext() ) |
| 721 |
{ |
| 722 |
entry = iterator.next(); |
| 723 |
assertEquals( entry.getValue(), btree.find( entry.getKey() ) ); |
| 724 |
assertTrue( contains( entry, btree ) ); |
| 725 |
|
| 726 |
assertNotNull( btree.find( entry.getKey() ) ); |
| 727 |
|
| 728 |
assertTrue( containsValue( entry.getValue(), btree ) ); |
| 729 |
} |
| 730 |
|
| 731 |
iterator = content.entrySet().iterator(); |
| 732 |
K key; |
| 733 |
|
| 734 |
while ( iterator.hasNext() ) |
| 735 |
{ |
| 736 |
key = iterator.next().getKey(); |
| 737 |
btree.remove( key ); |
| 738 |
assertNull( btree.find( key ) ); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
public void run() |
| 744 |
{ |
| 745 |
try |
| 746 |
{ |
| 747 |
while ( !stop ) |
| 748 |
{ |
| 749 |
action(); |
| 750 |
|
| 751 |
try |
| 752 |
{ |
| 753 |
Thread.sleep( THREAD_SLEEP_TIME ); |
| 754 |
} |
| 755 |
catch ( InterruptedException except ) |
| 756 |
{ |
| 757 |
except.printStackTrace(); |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
catch ( Throwable t ) |
| 762 |
{ |
| 763 |
} |
| 764 |
} |
| 765 |
} // end of class TestThread |
| 766 |
} |
| 767 |
|
| 768 |
/** |
| 769 |
* class for testing purposes only (store as value in btree) not |
| 770 |
* implemented as inner class, as this prevents Serialization if |
| 771 |
* outer class is not Serializable. |
| 772 |
*/ |
| 773 |
class ObjectStore implements Serializable |
| 774 |
{ |
| 775 |
private static final long serialVersionUID = 1L; |
| 776 |
|
| 777 |
/** |
| 778 |
* |
| 779 |
*/ |
| 780 |
Object content; |
| 781 |
|
| 782 |
|
| 783 |
public ObjectStore( Object content ) |
| 784 |
{ |
| 785 |
this.content = content; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
Object getContent() |
| 790 |
{ |
| 791 |
return content; |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
public boolean equals( Object obj ) |
| 796 |
{ |
| 797 |
if ( !( obj instanceof ObjectStore ) ) |
| 798 |
{ |
| 799 |
return false; |
| 800 |
} |
| 801 |
|
| 802 |
return content.equals( ( ( ObjectStore ) obj ).getContent() ); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
public String toString() |
| 807 |
{ |
| 808 |
return ( "TestObject {content='" + content + "'}" ); |
| 809 |
} |
| 810 |
} // TestObject |