| 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 |
package jdbm.btree; |
| 47 |
|
| 48 |
|
| 49 |
import jdbm.RecordManager; |
| 50 |
import jdbm.RecordManagerFactory; |
| 51 |
import jdbm.helper.StringComparator; |
| 52 |
import jdbm.btree.BTree; |
| 53 |
|
| 54 |
import java.io.File; |
| 55 |
import java.io.IOException; |
| 56 |
|
| 57 |
import org.junit.Test; |
| 58 |
|
| 59 |
/** |
| 60 |
* Contributed test case for BTree by Christof Dallermassl ([email protected]): |
| 61 |
* |
| 62 |
* -= quote from original message posted on jdbm-general =- |
| 63 |
* <pre> |
| 64 |
* |
| 65 |
* I tried to insert a couple of elements into a BTree and then remove |
| 66 |
* them one by one. After a number or removals, there is always (if more |
| 67 |
* than 20 elements in btree) a java.io.StreamCorruptedException thrown. |
| 68 |
* |
| 69 |
* The strange thing is, that on 50 elements, the exception is thrown |
| 70 |
* after removing 22, on 200 it is thrown after 36, on 1000 it is thrown |
| 71 |
* after 104, on 10000 it is thrown after 1003.... |
| 72 |
* |
| 73 |
* The full stackTrace is here: |
| 74 |
* ---------------------- snip ------- snap ------------------------- |
| 75 |
* java.io.StreamCorruptedException: Caught EOFException while reading the |
| 76 |
* stream header |
| 77 |
* at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:845) |
| 78 |
* at java.io.ObjectInputStream.<init>(ObjectInputStream.java:168) |
| 79 |
* at jdbm.recman.RecordManager.byteArrayToObject(RecordManager.java:296) |
| 80 |
* at jdbm.recman.RecordManager.fetchObject(RecordManager.java:239) |
| 81 |
* at jdbm.helper.ObjectCache.fetchObject(ObjectCache.java:104) |
| 82 |
* at jdbm.btree.BPage.loadBPage(BPage.java:670) |
| 83 |
* at jdbm.btree.BPage.remove(BPage.java:492) |
| 84 |
* at jdbm.btree.BPage.remove(BPage.java:437) |
| 85 |
* at jdbm.btree.BTree.remove(BTree.java:313) |
| 86 |
* at JDBMTest.main(JDBMTest.java:41) |
| 87 |
* |
| 88 |
* </pre> |
| 89 |
* |
| 90 |
* @author <a href="mailto:[email protected]">Christof Dallermassl</a> |
| 91 |
*/ |
| 92 |
public class StreamCorrupted |
| 93 |
{ |
| 94 |
public final static String testFileName = "test"; |
| 95 |
|
| 96 |
public static void deleteFile( String filename ) |
| 97 |
{ |
| 98 |
File file = new File( filename ); |
| 99 |
|
| 100 |
if ( file.exists() ) { |
| 101 |
try |
| 102 |
{ |
| 103 |
file.delete(); |
| 104 |
} |
| 105 |
catch ( Exception except ) |
| 106 |
{ |
| 107 |
except.printStackTrace(); |
| 108 |
} |
| 109 |
|
| 110 |
if ( file.exists() ) |
| 111 |
{ |
| 112 |
System.out.println( "WARNING: Cannot delete file: " + file ); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
public static void deleteTestFile() |
| 119 |
{ |
| 120 |
System.gc(); |
| 121 |
deleteFile( testFileName); |
| 122 |
deleteFile( testFileName + ".db" ); |
| 123 |
deleteFile( testFileName + ".lg" ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Basic tests |
| 129 |
*/ |
| 130 |
@Test |
| 131 |
public void testStreamCorrupted() throws IOException |
| 132 |
{ |
| 133 |
RecordManager recman; |
| 134 |
BTree<String, Integer> btree; |
| 135 |
int iterations; |
| 136 |
|
| 137 |
iterations = 100; // 23 works :-((((( |
| 138 |
|
| 139 |
// open database |
| 140 |
recman = RecordManagerFactory.createRecordManager( testFileName ); |
| 141 |
|
| 142 |
// create a new B+Tree data structure |
| 143 |
btree = new BTree<String, Integer>( recman, new StringComparator() ); |
| 144 |
recman.setNamedObject( "testbtree", btree.getRecordId() ); |
| 145 |
|
| 146 |
// action: |
| 147 |
// insert data |
| 148 |
for( int count = 0; count < iterations; count++ ) |
| 149 |
{ |
| 150 |
btree.insert( "num" + count, Integer.valueOf( count ), true ); |
| 151 |
} |
| 152 |
|
| 153 |
// delete data |
| 154 |
for( int count = 0; count < iterations; count++ ) |
| 155 |
{ |
| 156 |
btree.remove( "num" + count ); |
| 157 |
} |
| 158 |
|
| 159 |
// close database |
| 160 |
recman.close(); |
| 161 |
recman = null; |
| 162 |
} |
| 163 |
} |