From 579aa77f7ead94863d1f0c56d450b3bd2800f8db Mon Sep 17 00:00:00 2001
From: "Brian S. O'Neill" <bronee@gmail.com>
Date: Thu, 16 Nov 2006 23:47:31 +0000
Subject: Rebuild layout metadata if broken.

---
 src/main/java/com/amazon/carbonado/layout/Layout.java        | 12 +++++++++++-
 src/main/java/com/amazon/carbonado/layout/LayoutFactory.java | 10 +++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

(limited to 'src/main/java/com')

diff --git a/src/main/java/com/amazon/carbonado/layout/Layout.java b/src/main/java/com/amazon/carbonado/layout/Layout.java
index f784e84..d127835 100644
--- a/src/main/java/com/amazon/carbonado/layout/Layout.java
+++ b/src/main/java/com/amazon/carbonado/layout/Layout.java
@@ -408,7 +408,17 @@ public class Layout {
             throw new IllegalStateException();
         }
         mStoredLayout.setGeneration(generation);
-        mStoredLayout.insert();
+        if (!mStoredLayout.tryInsert()) {
+            StoredLayout existing = mLayoutFactory.mLayoutStorage.prepare();
+            mStoredLayout.copyPrimaryKeyProperties(existing);
+            try {
+                existing.load();
+            } catch (FetchException e) {
+                throw e.toPersistException();
+            }
+            mStoredLayout.setVersionNumber(existing.getVersionNumber());
+            mStoredLayout.update();
+        }
         for (LayoutProperty property : mAllProperties) {
             property.insert();
         }
diff --git a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java
index 6deb3ee..dc77b75 100644
--- a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java
+++ b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java
@@ -125,6 +125,13 @@ public class LayoutFactory implements LayoutCapability {
                             break loadLayout;
                         }
 
+                        if (knownLayout.getAllProperties().size() == 0) {
+                            // This is clearly wrong. All Storables must have
+                            // at least one property. Assume that layout record
+                            // is corrupt so rebuild it.
+                            break;
+                        }
+
                         // If this point is reached, then there was a hash collision in
                         // the generated layout ID. This should be extremely rare.
                         // Rehash and try again.
@@ -133,7 +140,8 @@ public class LayoutFactory implements LayoutCapability {
                             // No more rehashes to attempt. This should be extremely,
                             // extremely rare, unless there is a bug somewhere.
                             throw new FetchException
-                                ("Unable to generate unique layout identifier");
+                                ("Unable to generate unique layout identifier for " +
+                                 type.getName());
                         }
                     }
 
-- 
cgit v1.2.3