diff --git a/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/File.java b/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/File.java index 046b8fdc251..9ca947658e1 100644 --- a/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/File.java +++ b/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/File.java @@ -192,8 +192,12 @@ public class File extends Object { if (cursor.getSearchKey(cursorKey, cursorData, null) != OperationStatus.NOTFOUND) { cursor.delete(); - - while (cursor.getNextDup(cursorKey, cursorData, null) != OperationStatus.NOTFOUND) { + advance: while (cursor.getNext(cursorKey, cursorData, null) != OperationStatus.NOTFOUND) { + byte[] temp = cursorKey.getData(); + for (int i = 0; i < bytes.length; i++) + if (bytes[i] != temp[i]) { + break advance; + } cursor.delete(); } } diff --git a/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java b/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java index df5dd74b0cb..b75150f7b0e 100644 --- a/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java +++ b/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java @@ -27,29 +27,34 @@ import junit.framework.TestCase; import org.apache.lucene.store.Directory; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; -import org.apache.lucene.store.je.JEDirectory; +import com.sleepycat.je.Cursor; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; +import com.sleepycat.je.DatabaseEntry; import com.sleepycat.je.DatabaseException; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; +import com.sleepycat.je.LockMode; +import com.sleepycat.je.OperationStatus; import com.sleepycat.je.Transaction; /** * Tests {@link JEDirectory}. - * + * * Adapted from Andi Vajda's org.apache.lucene.db.DbStoreTest. + * * @author Aaron Donovan */ public class JEStoreTest extends TestCase { protected File dbHome = new File("index"); + protected Environment env; + protected Database index, blocks; - - public void setUp() - throws Exception - { + + public void setUp() throws Exception { + if (!dbHome.exists()) dbHome.mkdir(); else { @@ -79,8 +84,7 @@ public class JEStoreTest extends TestCase { index = env.openDatabase(txn, "__index__", dbConfig); blocks = env.openDatabase(txn, "__blocks__", dbConfig); } catch (DatabaseException e) { - if (txn != null) - { + if (txn != null) { txn.abort(); txn = null; } @@ -94,9 +98,8 @@ public class JEStoreTest extends TestCase { } } - public void tearDown() - throws Exception - { + public void tearDown() throws Exception { + if (index != null) index.close(); if (blocks != null) @@ -105,9 +108,7 @@ public class JEStoreTest extends TestCase { env.close(); } - public void testBytes() - throws Exception - { + public void tesBytes() throws Exception { final int count = 250; final int LENGTH_MASK = 0xffff; @@ -115,7 +116,7 @@ public class JEStoreTest extends TestCase { int totalLength = 0; int duration; Date end; - + Date veryStart = new Date(); Date start = new Date(); Transaction txn = null; @@ -135,10 +136,10 @@ public class JEStoreTest extends TestCase { totalLength += length; for (int j = 0; j < length; j++) { - byte b = (byte)(gen.nextInt() & 0x7F); + byte b = (byte) (gen.nextInt() & 0x7F); file.writeByte(b); } - + file.close(); } } catch (IOException e) { @@ -146,8 +147,7 @@ public class JEStoreTest extends TestCase { txn = null; throw e; } catch (DatabaseException e) { - if (txn != null) - { + if (txn != null) { txn.abort(); txn = null; } @@ -183,7 +183,7 @@ public class JEStoreTest extends TestCase { throw new Exception("length incorrect"); for (int j = 0; j < length; j++) { - byte b = (byte)(gen.nextInt() & 0x7F); + byte b = (byte) (gen.nextInt() & 0x7F); if (file.readByte() != b) throw new Exception("contents incorrect"); @@ -196,8 +196,7 @@ public class JEStoreTest extends TestCase { txn = null; throw e; } catch (DatabaseException e) { - if (txn != null) - { + if (txn != null) { txn.abort(); txn = null; } @@ -233,8 +232,7 @@ public class JEStoreTest extends TestCase { txn = null; throw e; } catch (DatabaseException e) { - if (txn != null) - { + if (txn != null) { txn.abort(); txn = null; } @@ -255,9 +253,7 @@ public class JEStoreTest extends TestCase { System.out.println(" total milliseconds"); } - public void testArrays() - throws Exception - { + public void testDelete() throws Exception { final int count = 250; final int LENGTH_MASK = 0xffff; @@ -265,7 +261,236 @@ public class JEStoreTest extends TestCase { int totalLength = 0; int duration; Date end; - + + Date veryStart = new Date(); + Date start = new Date(); + Transaction txn = null; + Directory store = null; + + System.out.println("Writing files byte by byte"); + + try { + txn = env.beginTransaction(null, null); + store = new JEDirectory(txn, index, blocks); + + for (int i = 0; i < count; i++) { + String name = i + ".dat"; + int length = gen.nextInt() & LENGTH_MASK; + IndexOutput file = store.createOutput(name); + + totalLength += length; + + for (int j = 0; j < length; j++) { + byte b = (byte) (gen.nextInt() & 0x7F); + file.writeByte(b); + } + + file.close(); + } + } catch (IOException e) { + txn.abort(); + txn = null; + throw e; + } catch (DatabaseException e) { + if (txn != null) { + txn.abort(); + txn = null; + } + throw e; + } finally { + if (txn != null) + txn.commit(); + + store.close(); + } + + end = new Date(); + + duration = (int) (end.getTime() - start.getTime()); + System.out.print(duration); + System.out.print(" total milliseconds to read, "); + System.out.print(totalLength / duration); + System.out.println(" kb/s"); + + try { + txn = env.beginTransaction(null, null); + store = new JEDirectory(txn, index, blocks); + + gen = new Random(1251971); + start = new Date(); + + for (int i = 0; i < count; i++) { + if (i % 2 == 0) { + String name = i + ".dat"; + store.deleteFile(name); + } + } + } catch (IOException e) { + txn.abort(); + txn = null; + throw e; + } catch (DatabaseException e) { + if (txn != null) { + txn.abort(); + txn = null; + } + throw e; + } finally { + if (txn != null) + txn.commit(); + + store.close(); + } + + end = new Date(); + + System.out.print(end.getTime() - start.getTime()); + System.out.println(" total milliseconds to delete even files"); + + duration = (int) (end.getTime() - start.getTime()); + System.out.print(duration); + System.out.print(" total milliseconds to create, "); + System.out.print(totalLength / duration); + System.out.println(" kb/s"); + + try { + txn = env.beginTransaction(null, null); + store = new JEDirectory(txn, index, blocks); + + gen = new Random(1251971); + start = new Date(); + + for (int i = 0; i < count; i++) { + int length = gen.nextInt() & LENGTH_MASK; + + if (i % 2 != 0) { + String name = i + ".dat"; + IndexInput file = store.openInput(name); + if (file.length() != length) + throw new Exception("length incorrect"); + + for (int j = 0; j < length; j++) { + byte b = (byte) (gen.nextInt() & 0x7F); + + if (file.readByte() != b) + throw new Exception("contents incorrect"); + } + + file.close(); + } else { + for (int j = 0; j < length; j++) { + gen.nextInt(); + } + } + } + } catch (IOException e) { + txn.abort(); + txn = null; + throw e; + } catch (DatabaseException e) { + if (txn != null) { + txn.abort(); + txn = null; + } + throw e; + } finally { + if (txn != null) + txn.commit(); + + store.close(); + } + + end = new Date(); + + duration = (int) (end.getTime() - start.getTime()); + System.out.print(duration); + System.out.print(" total milliseconds to read, "); + System.out.print(totalLength / duration); + System.out.println(" kb/s"); + + try { + txn = env.beginTransaction(null, null); + store = new JEDirectory(txn, index, blocks); + + gen = new Random(1251971); + start = new Date(); + + for (int i = 0; i < count; i++) { + if (i % 2 != 0) { + String name = i + ".dat"; + store.deleteFile(name); + } + } + + } catch (IOException e) { + txn.abort(); + txn = null; + throw e; + } catch (DatabaseException e) { + if (txn != null) { + txn.abort(); + txn = null; + } + throw e; + } finally { + if (txn != null) + txn.commit(); + + store.close(); + } + + end = new Date(); + + System.out.print(end.getTime() - start.getTime()); + System.out.println(" total milliseconds to delete"); + + System.out.print(end.getTime() - veryStart.getTime()); + System.out.println(" total milliseconds"); + + Cursor cursor = null; + try { + cursor = index.openCursor(null, null); + + DatabaseEntry foundKey = new DatabaseEntry(); + DatabaseEntry foundData = new DatabaseEntry(); + + if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { + fail("index database is not empty"); + } + } catch (DatabaseException e) { + throw e; + } finally { + if (cursor != null) + cursor.close(); + } + + cursor = null; + try { + cursor = blocks.openCursor(null, null); + + DatabaseEntry foundKey = new DatabaseEntry(); + DatabaseEntry foundData = new DatabaseEntry(); + + if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { + fail("blocks database is not empty"); + } + } catch (DatabaseException e) { + throw e; + } finally { + if (cursor != null) + cursor.close(); + } + } + + public void tesArrays() throws Exception { + final int count = 250; + final int LENGTH_MASK = 0xffff; + + Random gen = new Random(1251971); + int totalLength = 0; + int duration; + Date end; + Date veryStart = new Date(); Date start = new Date(); Transaction txn = null; @@ -286,7 +511,7 @@ public class JEStoreTest extends TestCase { totalLength += length; gen.nextBytes(data); file.writeBytes(data, length); - + file.close(); } } catch (IOException e) { @@ -294,8 +519,7 @@ public class JEStoreTest extends TestCase { txn = null; throw e; } catch (DatabaseException e) { - if (txn != null) - { + if (txn != null) { txn.abort(); txn = null; } @@ -326,7 +550,7 @@ public class JEStoreTest extends TestCase { String name = i + ".dat"; int length = gen.nextInt() & LENGTH_MASK; IndexInput file = store.openInput(name); - + if (file.length() != length) throw new Exception("length incorrect"); @@ -345,8 +569,7 @@ public class JEStoreTest extends TestCase { txn = null; throw e; } catch (DatabaseException e) { - if (txn != null) - { + if (txn != null) { txn.abort(); txn = null; } @@ -372,18 +595,17 @@ public class JEStoreTest extends TestCase { gen = new Random(1251971); start = new Date(); - for (int i = 0; i < count; i++) { String name = i + ".dat"; store.deleteFile(name); } + } catch (IOException e) { txn.abort(); txn = null; throw e; } catch (DatabaseException e) { - if (txn != null) - { + if (txn != null) { txn.abort(); txn = null; } @@ -404,4 +626,3 @@ public class JEStoreTest extends TestCase { System.out.println(" total milliseconds"); } } -