From 7bb2ef6f61fb8f180c70e854344550a13bdb3361 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 24 Jul 2010 23:14:20 +0000 Subject: Allow new layouts if BDB is read-only. --- .../java/com/amazon/carbonado/layout/Layout.java | 6 +++++- .../com/amazon/carbonado/layout/LayoutFactory.java | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/layout') diff --git a/src/main/java/com/amazon/carbonado/layout/Layout.java b/src/main/java/com/amazon/carbonado/layout/Layout.java index 0c7ab83..98df22c 100644 --- a/src/main/java/com/amazon/carbonado/layout/Layout.java +++ b/src/main/java/com/amazon/carbonado/layout/Layout.java @@ -518,13 +518,17 @@ public class Layout { } // Assumes caller is in a transaction. - void insert(int generation) throws PersistException { + void insert(boolean readOnly, int generation) throws PersistException { if (mAllProperties == null) { throw new IllegalStateException(); } mStoredLayout.setGeneration(generation); + if (readOnly) { + return; + } + try { mStoredLayout.insert(); } catch (UniqueConstraintException e) { diff --git a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java index 92099dd..7b7953b 100644 --- a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java +++ b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java @@ -101,6 +101,21 @@ public class LayoutFactory implements LayoutCapability { */ public Layout layoutFor(Class type, LayoutOptions options) throws FetchException, PersistException + { + return layoutFor(false, type, options); + } + + /** + * Returns the layout matching the current definition of the given type. + * + * @param readOnly if true, don't attempt to persist new generation because + * underlying repository is read-only + * @throws PersistException if type represents a new generation, but + * persisting this information failed + */ + public Layout layoutFor(boolean readOnly, + Class type, LayoutOptions options) + throws FetchException, PersistException { if (options != null) { // Make side-effect consistently applied. @@ -132,7 +147,7 @@ public class LayoutFactory implements LayoutCapability { txn = mRepository.enterTransaction(IsolationLevel.READ_COMMITTED); } - txn.setForUpdate(true); + txn.setForUpdate(!readOnly); try { // If type represents a new generation, then a new layout needs to // be inserted. @@ -207,7 +222,7 @@ public class LayoutFactory implements LayoutCapability { cursor.close(); } - newLayout.insert(generation); + newLayout.insert(readOnly, generation); layout = newLayout; txn.commit(); @@ -247,7 +262,7 @@ public class LayoutFactory implements LayoutCapability { } } - if (resyncCap != null) { + if (!readOnly && resyncCap != null) { // Make sure that all layout records are sync'd. try { resyncCap.resync(StoredLayoutProperty.class, 1.0, null); -- cgit v1.2.3