From 2278740878264362fd0f51e5f782715a89e4964f Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Tue, 19 Sep 2006 06:07:30 +0000 Subject: Removed all cursor synchronization. It just adds overhead and no real (or needed) thread safety. --- .../carbonado/repo/indexed/IndexedCursor.java | 178 ++++++++++----------- .../com/amazon/carbonado/repo/jdbc/JDBCCursor.java | 8 +- 2 files changed, 90 insertions(+), 96 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo') diff --git a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedCursor.java b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedCursor.java index a6df5e0..075df1b 100644 --- a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedCursor.java +++ b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedCursor.java @@ -61,123 +61,117 @@ class IndexedCursor extends AbstractCursor { } public boolean hasNext() throws FetchException { - synchronized (mCursor) { - if (mNext != null) { - return true; - } - while (mCursor.hasNext()) { - final Storable indexEntry = mCursor.next(); - S master = mGenerator.loadMaster(indexEntry); - if (master == null) { - LogFactory.getLog(getClass()).warn - ("Master is missing for index entry: " + indexEntry); - } else { - if (mGenerator.isConsistent(indexEntry, master)) { + if (mNext != null) { + return true; + } + while (mCursor.hasNext()) { + final Storable indexEntry = mCursor.next(); + S master = mGenerator.loadMaster(indexEntry); + if (master == null) { + LogFactory.getLog(getClass()).warn + ("Master is missing for index entry: " + indexEntry); + } else { + if (mGenerator.isConsistent(indexEntry, master)) { + mNext = master; + return true; + } + + // This index entry is stale. Repair is needed. + + // Insert a correct index entry, just to be sure. + try { + final IndexedRepository repo = mStorage.mRepository; + final Storage indexEntryStorage = + repo.getIndexEntryStorageFor(mGenerator.getIndexEntryClass()); + Storable newIndexEntry = indexEntryStorage.prepare(); + mGenerator.setAllProperties(newIndexEntry, master); + + if (newIndexEntry.tryLoad()) { + // Good, the correct index entry exists. We'll see + // the master record eventually, so skip. + } else { + // We have no choice but to return the master, at + // the risk of seeing it multiple times. This is + // better than seeing it never. + LogFactory.getLog(getClass()).warn + ("Inconsistent index entry: " + indexEntry); mNext = master; - return true; } - // This index entry is stale. Repair is needed. - - // Insert a correct index entry, just to be sure. - try { - final IndexedRepository repo = mStorage.mRepository; - final Storage indexEntryStorage = - repo.getIndexEntryStorageFor(mGenerator.getIndexEntryClass()); - Storable newIndexEntry = indexEntryStorage.prepare(); - mGenerator.setAllProperties(newIndexEntry, master); - - if (newIndexEntry.tryLoad()) { - // Good, the correct index entry exists. We'll see - // the master record eventually, so skip. - } else { - // We have no choice but to return the master, at - // the risk of seeing it multiple times. This is - // better than seeing it never. - LogFactory.getLog(getClass()).warn - ("Inconsistent index entry: " + indexEntry); - mNext = master; - } + // Repair the stale index entry. + RepairExecutor.execute(new Runnable() { + public void run() { + Transaction txn = repo.enterTransaction(); + try { + // Reload master and verify inconsistency. + S master = mGenerator.loadMaster(indexEntry); + if (mGenerator.isConsistent(indexEntry, master)) { + return; + } - // Repair the stale index entry. - RepairExecutor.execute(new Runnable() { - public void run() { - Transaction txn = repo.enterTransaction(); + Storable newIndexEntry = indexEntryStorage.prepare(); + mGenerator.setAllProperties(newIndexEntry, master); + + newIndexEntry.tryInsert(); + + indexEntry.tryDelete(); + txn.commit(); + } catch (FetchException fe) { + LogFactory.getLog(IndexedCursor.class).warn + ("Unable to check if repair required for " + + "inconsistent index entry " + + indexEntry, fe); + } catch (PersistException pe) { + LogFactory.getLog(IndexedCursor.class).error + ("Unable to repair inconsistent index entry " + + indexEntry, pe); + } finally { try { - // Reload master and verify inconsistency. - S master = mGenerator.loadMaster(indexEntry); - if (mGenerator.isConsistent(indexEntry, master)) { - return; - } - - Storable newIndexEntry = indexEntryStorage.prepare(); - mGenerator.setAllProperties(newIndexEntry, master); - - newIndexEntry.tryInsert(); - - indexEntry.tryDelete(); - txn.commit(); - } catch (FetchException fe) { - LogFactory.getLog(IndexedCursor.class).warn - ("Unable to check if repair required for " + - "inconsistent index entry " + - indexEntry, fe); + txn.exit(); } catch (PersistException pe) { LogFactory.getLog(IndexedCursor.class).error ("Unable to repair inconsistent index entry " + indexEntry, pe); - } finally { - try { - txn.exit(); - } catch (PersistException pe) { - LogFactory.getLog(IndexedCursor.class).error - ("Unable to repair inconsistent index entry " + - indexEntry, pe); - } } } - }); - } catch (RepositoryException re) { - LogFactory.getLog(getClass()).error - ("Unable to inspect inconsistent index entry " + - indexEntry, re); - } + } + }); + } catch (RepositoryException re) { + LogFactory.getLog(getClass()).error + ("Unable to inspect inconsistent index entry " + + indexEntry, re); + } - if (mNext != null) { - return true; - } + if (mNext != null) { + return true; } } - return false; } + return false; } public S next() throws FetchException { - synchronized (mCursor) { - if (hasNext()) { - S next = mNext; - mNext = null; - return next; - } + if (hasNext()) { + S next = mNext; + mNext = null; + return next; } throw new NoSuchElementException(); } public int skipNext(int amount) throws FetchException { - synchronized (mCursor) { - if (mNext == null) { - return mCursor.skipNext(amount); - } + if (mNext == null) { + return mCursor.skipNext(amount); + } - if (amount <= 0) { - if (amount < 0) { - throw new IllegalArgumentException("Cannot skip negative amount: " + amount); - } - return 0; + if (amount <= 0) { + if (amount < 0) { + throw new IllegalArgumentException("Cannot skip negative amount: " + amount); } - - mNext = null; - return 1 + mCursor.skipNext(amount - 1); + return 0; } + + mNext = null; + return 1 + mCursor.skipNext(amount - 1); } } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java index 7ef2510..f0a3752 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java @@ -52,7 +52,7 @@ class JDBCCursor extends AbstractCursor { mResultSet = statement.executeQuery(); } - public synchronized void close() throws FetchException { + public void close() throws FetchException { if (mResultSet != null) { try { mResultSet.close(); @@ -66,7 +66,7 @@ class JDBCCursor extends AbstractCursor { } } - public synchronized boolean hasNext() throws FetchException { + public boolean hasNext() throws FetchException { ResultSet rs = mResultSet; if (rs == null) { return false; @@ -84,7 +84,7 @@ class JDBCCursor extends AbstractCursor { return mHasNext; } - public synchronized S next() throws FetchException, NoSuchElementException { + public S next() throws FetchException, NoSuchElementException { if (!hasNext()) { throw new NoSuchElementException(); } @@ -97,7 +97,7 @@ class JDBCCursor extends AbstractCursor { } } - public synchronized int skipNext(int amount) throws FetchException { + public int skipNext(int amount) throws FetchException { if (amount <= 0) { if (amount < 0) { throw new IllegalArgumentException("Cannot skip negative amount: " + amount); -- cgit v1.2.3