summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/layout
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/amazon/carbonado/layout')
-rw-r--r--src/main/java/com/amazon/carbonado/layout/Layout.java39
-rw-r--r--src/main/java/com/amazon/carbonado/layout/LayoutFactory.java48
2 files changed, 83 insertions, 4 deletions
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<LayoutProperty> mAllProperties;
+ private volatile List<LayoutProperty> 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<LayoutProperty> getAllProperties() throws FetchException {
- if (mAllProperties == null) {
+ List<LayoutProperty> all = mAllProperties;
+
+ if (all == null) {
Cursor <StoredLayoutProperty> 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 <StoredLayoutProperty> 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<? extends Storable> reconstructed, Layout layout)
{