summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/layout/Layout.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/amazon/carbonado/layout/Layout.java')
-rw-r--r--src/main/java/com/amazon/carbonado/layout/Layout.java101
1 files changed, 73 insertions, 28 deletions
diff --git a/src/main/java/com/amazon/carbonado/layout/Layout.java b/src/main/java/com/amazon/carbonado/layout/Layout.java
index df20f3f..ef7f74c 100644
--- a/src/main/java/com/amazon/carbonado/layout/Layout.java
+++ b/src/main/java/com/amazon/carbonado/layout/Layout.java
@@ -43,8 +43,10 @@ import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.Query;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
+import com.amazon.carbonado.Storage;
import com.amazon.carbonado.SupportException;
import com.amazon.carbonado.UniqueConstraintException;
+import com.amazon.carbonado.cursor.FilteredCursor;
import com.amazon.carbonado.info.StorableInfo;
import com.amazon.carbonado.info.StorableProperty;
import com.amazon.carbonado.synthetic.SyntheticKey;
@@ -313,49 +315,92 @@ public class Layout {
* @throws FetchNoneException if generation not found
*/
public Layout getGeneration(int generation) throws FetchNoneException, FetchException {
+ StoredLayout layout;
+ try {
+ layout = getStoredLayoutByGeneration(generation);
+ } catch (FetchNoneException e) {
+ try {
+ Storage<StoredLayoutEquivalence> equivStorage =
+ mLayoutFactory.mRepository.storageFor(StoredLayoutEquivalence.class);
+ StoredLayoutEquivalence equiv = equivStorage.prepare();
+ equiv.setStorableTypeName(getStorableTypeName());
+ equiv.setGeneration(generation);
+ if (equiv.tryLoad()) {
+ layout = getStoredLayoutByGeneration(equiv.getMatchedGeneration());
+ } else {
+ throw e;
+ }
+ } catch (RepositoryException e2) {
+ if (e2 != e) {
+ LogFactory.getLog(Layout.class).error("Unable to determine equivalance: ", e2);
+ }
+ throw e;
+ }
+ }
+
+ return new Layout(mLayoutFactory, layout);
+ }
+
+ private StoredLayout getStoredLayoutByGeneration(int generation)
+ throws FetchNoneException, FetchException
+ {
final String filter = "storableTypeName = ? & generation = ?";
- StoredLayout storedLayout;
+ final FetchNoneException ex;
try {
- storedLayout = mLayoutFactory.mLayoutStorage
+ return mLayoutFactory.mLayoutStorage
.query(filter)
.with(getStorableTypeName()).with(generation)
.loadOne();
} catch (FetchNoneException e) {
- // Try to resync with a master.
- ResyncCapability cap =
- mLayoutFactory.mRepository.getCapability(ResyncCapability.class);
+ ex = e;
+ }
- if (cap == null) {
- throw e;
- }
+ // Index might be inconsistent.
+ Cursor<StoredLayout> c =
+ FilteredCursor.applyFilter(mLayoutFactory.mLayoutStorage.query().fetch(),
+ StoredLayout.class, filter,
+ getStorableTypeName(), generation);
- try {
- cap.resync(mLayoutFactory.mLayoutStorage.getStorableType(), 1.0,
- filter, getStorableTypeName(), generation);
- } catch (RepositoryException e2) {
- // Uh oh, double trouble. Log this one and throw original exception.
- LogFactory.getLog(Layout.class).error("Unable to resync layout", e2);
- throw e;
+ try {
+ if (c.hasNext()) {
+ return c.next();
}
+ } finally {
+ c.close();
+ }
- storedLayout = mLayoutFactory.mLayoutStorage
- .query(filter)
- .with(getStorableTypeName()).with(generation)
- .loadOne();
+ // Try to resync with a master.
+ ResyncCapability cap =
+ mLayoutFactory.mRepository.getCapability(ResyncCapability.class);
+
+ if (cap == null) {
+ throw ex;
+ }
+ try {
+ cap.resync(mLayoutFactory.mLayoutStorage.getStorableType(), 1.0,
+ filter, getStorableTypeName(), generation);
+ } catch (RepositoryException e) {
+ LogFactory.getLog(Layout.class).info("Unable to resync layout: ", e);
+ throw ex;
+ }
+
+ StoredLayout storedLayout = mLayoutFactory.mLayoutStorage
+ .query(filter)
+ .with(getStorableTypeName()).with(generation)
+ .loadOne();
+
+ try {
// Make sure all the properties are re-sync'd too.
- try {
- cap.resync(mLayoutFactory.mPropertyStorage.getStorableType(), 1.0,
- "layoutID = ?", storedLayout.getLayoutID());
- } catch (RepositoryException e2) {
- // Uh oh, double trouble. Log this one and throw original exception.
- LogFactory.getLog(Layout.class).error("Unable to resync layout", e2);
- throw e;
- }
+ cap.resync(mLayoutFactory.mPropertyStorage.getStorableType(), 1.0,
+ "layoutID = ?", storedLayout.getLayoutID());
+ } catch (RepositoryException e) {
+ LogFactory.getLog(Layout.class).error("Unable to resync layout properties", e);
+ throw ex;
}
- return new Layout(mLayoutFactory, storedLayout);
+ return storedLayout;
}
/**