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 +++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/layout/Layout.java') 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) { -- cgit v1.2.3