From 558bb53752e64debc503cf37d15b6800181d5b75 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 14 May 2008 16:10:06 +0000 Subject: Added test for multiple index errors. --- .../carbonado/repo/indexed/TestIndexRepair.java | 144 ++++++++++++++++++++- 1 file changed, 141 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/test/java/com/amazon/carbonado/repo/indexed/TestIndexRepair.java b/src/test/java/com/amazon/carbonado/repo/indexed/TestIndexRepair.java index c9d3f23..852c064 100644 --- a/src/test/java/com/amazon/carbonado/repo/indexed/TestIndexRepair.java +++ b/src/test/java/com/amazon/carbonado/repo/indexed/TestIndexRepair.java @@ -48,10 +48,15 @@ public class TestIndexRepair extends TestCase { super(name); } + private static Repository buildTempRepository() throws Exception { + return TestUtilities.buildTempRepository("indexrepair", 1000000, true); + } + public void test_shouldInsert() throws Exception { - Repository repo = TestUtilities.buildTempRepository(); + Repository repo = buildTempRepository(); test_shouldInsert(repo); repo.close(); + repo = null; } public void test_shouldInsertMap() throws Exception { @@ -100,12 +105,16 @@ public class TestIndexRepair extends TestCase { final long reCount = sumCounts(reCounts); assertEquals(correctCount * 4, reCount); + + // Cleanup to workaround apparent BDB-JE memory leaks. + storage.truncate(); } public void test_shouldDelete() throws Exception { - Repository repo = TestUtilities.buildTempRepository(); + Repository repo = buildTempRepository(); test_shouldDelete(repo); repo.close(); + repo = null; } public void test_shouldDeleteMap() throws Exception { @@ -166,12 +175,16 @@ public class TestIndexRepair extends TestCase { final long reCount = sumCounts(reCounts); assertEquals(newCount * 4, reCount); + + // Cleanup to workaround apparent BDB-JE memory leaks. + storage.truncate(); } public void test_shouldUpdate() throws Exception { - Repository repo = TestUtilities.buildTempRepository(); + Repository repo = buildTempRepository(); test_shouldUpdate(repo); repo.close(); + repo = null; } public void test_shouldUpdateMap() throws Exception { @@ -241,6 +254,131 @@ public class TestIndexRepair extends TestCase { storage.query("stringProp >= ? & doubleProp >= ?").with("").with(0).fetch().toList(); assertEquals(correctCount, list.size()); + + // Cleanup to workaround apparent BDB-JE memory leaks. + storage.truncate(); + } + + public void test_multiple() throws Exception { + Repository repo = buildTempRepository(); + test_multiple(repo); + repo.close(); + repo = null; + } + + public void test_multipleMap() throws Exception { + test_multiple(MapRepositoryBuilder.newRepository()); + } + + private void test_multiple(Repository repo) throws Exception { + // Test multiple index errors. + + Storage storage = + repo.storageFor(StorableTestBasicCompoundIndexed.class); + + final long correctCount = insertRecords(storage); + + IndexEntryAccessCapability cap = + repo.getCapability(IndexEntryAccessCapability.class); + + Random rnd = new Random(545321638); + + List toDelete = new ArrayList(); + + for (IndexEntryAccessor acc + : cap.getIndexEntryAccessors(StorableTestBasicCompoundIndexed.class)) { + Cursor entries = acc.getIndexEntryStorage().query().fetch(); + while (entries.hasNext()) { + Storable entry = entries.next(); + if (rnd.nextInt(100) == 0) { + toDelete.add(entry); + } + } + } + + for (Storable entry : toDelete) { + entry.delete(); + } + + List toDelete2 = + new ArrayList(); + List toAdd = new ArrayList(); + + for (IndexEntryAccessor acc + : cap.getIndexEntryAccessors(StorableTestBasicCompoundIndexed.class)) { + Cursor entries = acc.getIndexEntryStorage().query().fetch(); + while (entries.hasNext()) { + Storable entry = entries.next(); + if (rnd.nextInt(100) == 0) { + StorableTestBasicCompoundIndexed master = storage.prepare(); + acc.copyToMasterPrimaryKey(entry, master); + toDelete2.add(master); + toAdd.add(entry); + } + } + } + + long newCount = correctCount; + + for (StorableTestBasicCompoundIndexed master : toDelete2) { + if (master.tryDelete()) { + newCount--; + } + } + + for (Storable entry : toAdd) { + entry.insert(); + } + + List toUpdate = new ArrayList(); + + for (IndexEntryAccessor acc + : cap.getIndexEntryAccessors(StorableTestBasicCompoundIndexed.class)) { + + // Only muck with alternate key. + String[] names = acc.getPropertyNames(); + if ("id".equals(names[names.length - 1])) { + continue; + } + + Cursor entries = acc.getIndexEntryStorage().query().fetch(); + while (entries.hasNext()) { + Storable entry = entries.next(); + if (rnd.nextInt(100) == 0) { + toUpdate.add(entry); + } + } + } + + // Make index entries point to master ids that won't match. + for (Storable entry : toUpdate) { + Integer id = (Integer) entry.getPropertyValue("id"); + entry.setPropertyValue("id", id + 100000000); + entry.update(); + } + + final long[] brokenCounts = indexCounts(storage); + final long brokenCount = sumCounts(brokenCounts); + + assertFalse(newCount * 4 == brokenCount); + + for (IndexEntryAccessor acc + : cap.getIndexEntryAccessors(StorableTestBasicCompoundIndexed.class)) { + acc.repair(1.0); + } + + final long[] reCounts = indexCounts(storage); + final long reCount = sumCounts(reCounts); + + assertEquals(newCount * 4, reCount); + + List list = + storage.query("stringProp >= ? & doubleProp >= ?").with("").with(0).fetch().toList(); + + assertEquals(newCount, list.size()); + + // Cleanup to workaround apparent BDB-JE memory leaks. + storage.truncate(); } private long insertRecords(Storage storage) -- cgit v1.2.3