From 10b8b1179e4b1c90db455e68ff81c89e4cb3f015 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 14 Mar 2010 20:13:11 +0000 Subject: Allow storable types to differ between client and server. --- .../java/com/amazon/carbonado/layout/Layout.java | 39 ++++++++++++++++-- .../com/amazon/carbonado/layout/LayoutFactory.java | 48 ++++++++++++++++++++++ 2 files changed, 83 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 7aea758..570db8c 100644 --- a/src/main/java/com/amazon/carbonado/layout/Layout.java +++ b/src/main/java/com/amazon/carbonado/layout/Layout.java @@ -19,8 +19,11 @@ package com.amazon.carbonado.layout; import java.io.IOException; +import java.io.OutputStream; + import java.net.InetAddress; import java.net.UnknownHostException; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -135,7 +138,7 @@ public class Layout { private final StoredLayout mStoredLayout; private final LayoutOptions mOptions; - private transient List mAllProperties; + private volatile List mAllProperties; /** * Creates a Layout around an existing storable. @@ -248,7 +251,9 @@ public class Layout { * Returns all the properties of this layout, in their proper order. */ public List getAllProperties() throws FetchException { - if (mAllProperties == null) { + List all = mAllProperties; + + if (all == null) { Cursor cursor = mLayoutFactory.mPropertyStorage .query("layoutID = ?") .with(mStoredLayout.getLayoutID()) @@ -262,13 +267,13 @@ public class Layout { list.add(new LayoutProperty(cursor.next())); } - mAllProperties = Collections.unmodifiableList(list); + mAllProperties = all = Collections.unmodifiableList(list); } finally { cursor.close(); } } - return mAllProperties; + return all; } /** @@ -482,6 +487,32 @@ public class Layout { && getAllProperties().equals(layout.getAllProperties()); } + /** + * Write a layout to be read by {@link LayoutFactory#readLayoutFrom}. + * + * @since 1.2.2 + */ + public void writeTo(OutputStream out) throws IOException, RepositoryException { + mStoredLayout.writeTo(out); + + Cursor cursor = mLayoutFactory.mPropertyStorage + .query("layoutID = ?") + .with(mStoredLayout.getLayoutID()) + .fetch(); + + try { + while (cursor.hasNext()) { + StoredLayoutProperty prop = cursor.next(); + out.write(1); + prop.writeTo(out); + } + } finally { + cursor.close(); + } + + out.write(0); + } + // Assumes caller is in a transaction. void insert(int generation) throws PersistException { if (mAllProperties == null) { diff --git a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java index 54b6933..50218e6 100644 --- a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java +++ b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java @@ -18,6 +18,9 @@ package com.amazon.carbonado.layout; +import java.io.InputStream; +import java.io.IOException; + import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; @@ -266,6 +269,51 @@ public class LayoutFactory implements LayoutCapability { return new Layout(this, storedLayout); } + /** + * Read a layout as written by {@link Layout#writeTo}. + * + * @since 1.2.2 + */ + public Layout readLayoutFrom(InputStream in) throws IOException, RepositoryException { + Transaction txn = mRepository.enterTransaction(); + try { + txn.setForUpdate(true); + StoredLayout storedLayout = mLayoutStorage.prepare(); + storedLayout.readFrom(in); + try { + storedLayout.insert(); + } catch (UniqueConstraintException e) { + StoredLayout existing = mLayoutStorage.prepare(); + storedLayout.copyPrimaryKeyProperties(existing); + if (!existing.tryLoad() || !existing.equalProperties(storedLayout)) { + throw e; + } + } + + int op; + while ((op = in.read()) != 0) { + StoredLayoutProperty storedProperty = mPropertyStorage.prepare(); + storedProperty.readFrom(in); + try { + storedProperty.insert(); + } catch (UniqueConstraintException e) { + StoredLayoutProperty existing = mPropertyStorage.prepare(); + storedProperty.copyPrimaryKeyProperties(existing); + existing.load(); + if (!existing.tryLoad() || !existing.equalProperties(storedProperty)) { + throw e; + } + } + } + + txn.commit(); + + return new Layout(this, storedLayout); + } finally { + txn.exit(); + } + } + synchronized void registerReconstructed (Class reconstructed, Layout layout) { -- cgit v1.2.3