From 778cdf89223fa9a797108c887550e28ec4ba39f7 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 31 Oct 2012 21:46:12 +0000 Subject: Allow equivalent layout generation to be defined. --- .../java/com/amazon/carbonado/layout/Layout.java | 101 +++++++++++++++------ .../carbonado/layout/StoredLayoutEquivalence.java | 62 +++++++++++++ 2 files changed, 135 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/amazon/carbonado/layout/StoredLayoutEquivalence.java (limited to 'src/main/java/com/amazon/carbonado') 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 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 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; } /** diff --git a/src/main/java/com/amazon/carbonado/layout/StoredLayoutEquivalence.java b/src/main/java/com/amazon/carbonado/layout/StoredLayoutEquivalence.java new file mode 100644 index 0000000..9a86761 --- /dev/null +++ b/src/main/java/com/amazon/carbonado/layout/StoredLayoutEquivalence.java @@ -0,0 +1,62 @@ +/* + * Copyright 2012 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.amazon.carbonado.layout; + +import com.amazon.carbonado.Alias; +import com.amazon.carbonado.Independent; +import com.amazon.carbonado.Nullable; +import com.amazon.carbonado.PrimaryKey; +import com.amazon.carbonado.Storable; +import com.amazon.carbonado.Version; + +/** + * Maps layout generations which are equivalent. + * + * @author Brian S O'Neill + */ +@PrimaryKey({"storableTypeName", "generation"}) +@Independent +@Alias("CARBONADO_LAYOUT_EQUIV") +public interface StoredLayoutEquivalence extends Storable, Unevolvable { + /** + * Storable type name is a fully qualified Java class name. + */ + String getStorableTypeName(); + void setStorableTypeName(String typeName); + + /** + * Generation of storable, where 0 represents the first generation. + */ + int getGeneration(); + void setGeneration(int generation); + + /** + * Generation of matched layout. + */ + int getMatchedGeneration(); + void setMatchedGeneration(int generation); + + @Version + int getVersionNumber(); + void setVersionNumber(int version); + + @Nullable + byte[] getExtraData(); + void setExtraData(byte[] data); +} -- cgit v1.2.3