From 64f320d5259ac229bf1040bab005d1a72b80a194 Mon Sep 17 00:00:00 2001
From: Jesse Morgan <jesse@jesterpm.net>
Date: Tue, 1 May 2012 18:08:27 +0000
Subject: Fixed add trigger exception in subsequent constructions of
 IndexedStorage when the first throws an exception.

---
 .../carbonado/repo/indexed/IndexedStorage.java     | 24 ++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

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

diff --git a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedStorage.java b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedStorage.java
index 7552834..b00b0b4 100644
--- a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedStorage.java
+++ b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedStorage.java
@@ -75,7 +75,7 @@ class IndexedStorage<S extends Storable> implements Storage<S>, StorageAccess<S>
     private final StorableIndexSet<S> mQueryableIndexSet;
 
     private final QueryEngine<S> mQueryEngine;
-
+    
     IndexedStorage(IndexAnalysis<S> analysis) throws RepositoryException {
         mRepository = analysis.repository;
         mMasterStorage = analysis.masterStorage;
@@ -89,14 +89,22 @@ class IndexedStorage<S extends Storable> implements Storage<S>, StorageAccess<S>
             }
         }
 
-        // Okay, now start doing some damage. First, remove unnecessary indexes.
-        for (StorableIndex<S> index : analysis.removeIndexSet) {
-            removeIndex(index);
-        }
+        try {
+            // Okay, now start doing some damage. First, remove unnecessary indexes.
+            for (StorableIndex<S> index : analysis.removeIndexSet) {
+                removeIndex(index);
+            }
 
-        // Now add new indexes.
-        for (StorableIndex<S> index : analysis.addIndexSet) {
-            registerIndex((ManagedIndex) mAllIndexInfoMap.get(index));
+            // Now add new indexes.
+            for (StorableIndex<S> index : analysis.addIndexSet) {
+                registerIndex((ManagedIndex) mAllIndexInfoMap.get(index));
+            }
+        } catch (RepositoryException e) {
+            // Something went wrong. Cleanup the trigger to avoid an exception if we try again.
+            if (analysis.indexesTrigger != null) {
+                removeTrigger(analysis.indexesTrigger);
+            }
+            throw e;
         }
 
         mQueryEngine = new QueryEngine<S>(mMasterStorage.getStorableType(), mRepository);
-- 
cgit v1.2.3