From 987e5b79304062308a5d304a57dabcce97654867 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Tue, 17 Oct 2006 00:08:26 +0000 Subject: More work on automatic triggers. --- .../com/amazon/carbonado/RepositoryBuilder.java | 12 +++++++++- .../java/com/amazon/carbonado/TriggerFactory.java | 2 +- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 2 +- .../carbonado/repo/logging/LoggingStorage.java | 3 ++- .../carbonado/repo/sleepycat/BDBStorage.java | 5 ++++- .../carbonado/spi/AbstractRepositoryBuilder.java | 25 +++++++++++++++------ .../com/amazon/carbonado/spi/TriggerManager.java | 26 ++++++++++++++++------ .../com/amazon/carbonado/spi/WrappedStorage.java | 5 ++++- 8 files changed, 60 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/main/java/com/amazon/carbonado/RepositoryBuilder.java b/src/main/java/com/amazon/carbonado/RepositoryBuilder.java index 8c64483..f1c3bd0 100644 --- a/src/main/java/com/amazon/carbonado/RepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/RepositoryBuilder.java @@ -105,8 +105,18 @@ public interface RepositoryBuilder { * initial trigger for each Storable type that the Repository supports. The * primary purpose of this method is to allow decorator repositories the * opportunity to register custom persistence code for each Storable. + * + * @return true if TriggerFactory was added, false if TriggerFactory was + * not added because an equal TriggerFactory is already registered + */ + boolean addTriggerFactory(TriggerFactory factory); + + /** + * Remove a TriggerFactory which was added earlier. + * + * @return true if TriggerFactory instance was removed, false if not added */ - void addTriggerFactory(TriggerFactory factory); + boolean removeTriggerFactory(TriggerFactory factory); /** * Returns all the TriggerFactories which were added. diff --git a/src/main/java/com/amazon/carbonado/TriggerFactory.java b/src/main/java/com/amazon/carbonado/TriggerFactory.java index 5193267..f4a7a0f 100644 --- a/src/main/java/com/amazon/carbonado/TriggerFactory.java +++ b/src/main/java/com/amazon/carbonado/TriggerFactory.java @@ -34,5 +34,5 @@ public interface TriggerFactory { * @param type Storable type requesting an automatic trigger * @return trigger instance or null if not applicable */ - Trigger triggerFor(Class type); + Trigger triggerFor(Class type) throws RepositoryException; } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index df61ecf..b06bf99 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -96,7 +96,7 @@ class JDBCStorage extends StandardQueryFactory final TriggerManager mTriggerManager; JDBCStorage(JDBCRepository repository, JDBCStorableInfo info) - throws SupportException + throws SupportException, RepositoryException { super(info.getStorableType()); mRepository = repository; diff --git a/src/main/java/com/amazon/carbonado/repo/logging/LoggingStorage.java b/src/main/java/com/amazon/carbonado/repo/logging/LoggingStorage.java index 47d6bb1..3f807c2 100644 --- a/src/main/java/com/amazon/carbonado/repo/logging/LoggingStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/logging/LoggingStorage.java @@ -22,6 +22,7 @@ import com.amazon.carbonado.FetchException; import com.amazon.carbonado.PersistException; import com.amazon.carbonado.Query; import com.amazon.carbonado.Repository; +import com.amazon.carbonado.RepositoryException; import com.amazon.carbonado.Storable; import com.amazon.carbonado.Storage; @@ -35,7 +36,7 @@ import com.amazon.carbonado.spi.WrappedStorage; class LoggingStorage extends WrappedStorage { final LoggingRepository mRepo; - LoggingStorage(LoggingRepository repo, Storage storage) { + LoggingStorage(LoggingRepository repo, Storage storage) throws RepositoryException { super(storage, repo.mTriggerFactories); mRepo = repo; } diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java index b885399..20ab89f 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java @@ -131,7 +131,7 @@ abstract class BDBStorage implements Storage, Storag mRepository = repository; mType = type; mRawSupport = new Support(repository, this); - mTriggerManager = new TriggerManager(type, repository.mTriggerFactories); + mTriggerManager = new TriggerManager(); try { // Ask if any lobs via static method first, to prevent stack // overflow that occurs when creating BDBStorage instances for @@ -466,6 +466,9 @@ abstract class BDBStorage implements Storage, Storag mPrimaryDatabase = primaryDatabase; mQueryEngine = new QueryEngine(getStorableType(), mRepository); + + // Don't install automatic triggers until we're completely ready. + mTriggerManager.addTriggers(getStorableType(), mRepository.mTriggerFactories); } protected S instantiate(byte[] key, byte[] value) throws FetchException { diff --git a/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java index 6193e50..b73a9ce 100644 --- a/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; +import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import com.amazon.carbonado.ConfigurationException; @@ -37,7 +38,7 @@ import com.amazon.carbonado.TriggerFactory; * @author Brian S O'Neill */ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder { - private final Collection mTriggerFactories; + private final Set mTriggerFactories; protected AbstractRepositoryBuilder() { mTriggerFactories = new LinkedHashSet(2); @@ -47,15 +48,25 @@ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder { return build(new AtomicReference()); } - public void addTriggerFactory(TriggerFactory factory) { - mTriggerFactories.add(factory); + public boolean addTriggerFactory(TriggerFactory factory) { + synchronized (mTriggerFactories) { + return mTriggerFactories.add(factory); + } + } + + public boolean removeTriggerFactory(TriggerFactory factory) { + synchronized (mTriggerFactories) { + return mTriggerFactories.remove(factory); + } } public Iterable getTriggerFactories() { - if (mTriggerFactories == null || mTriggerFactories.size() == 0) { - return Collections.emptyList(); - } else { - return new ArrayList(mTriggerFactories); + synchronized (mTriggerFactories) { + if (mTriggerFactories == null || mTriggerFactories.size() == 0) { + return Collections.emptyList(); + } else { + return new ArrayList(mTriggerFactories); + } } } diff --git a/src/main/java/com/amazon/carbonado/spi/TriggerManager.java b/src/main/java/com/amazon/carbonado/spi/TriggerManager.java index ea7ae4f..147d1ed 100644 --- a/src/main/java/com/amazon/carbonado/spi/TriggerManager.java +++ b/src/main/java/com/amazon/carbonado/spi/TriggerManager.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Arrays; import com.amazon.carbonado.PersistException; +import com.amazon.carbonado.RepositoryException; import com.amazon.carbonado.Storable; import com.amazon.carbonado.Trigger; import com.amazon.carbonado.TriggerFactory; @@ -94,18 +95,18 @@ public class TriggerManager { private volatile ForUpdate mForUpdate; private volatile ForDelete mForDelete; + public TriggerManager() { + } + /** * @param triggerFactories TriggerFactories which will be called upon to * optionally return a trigger to initially register */ - public TriggerManager(Class type, Iterable triggerFactories) { + public TriggerManager(Class type, Iterable triggerFactories) + throws RepositoryException + { if (triggerFactories != null) { - for (TriggerFactory factory : triggerFactories) { - Trigger trigger = factory.triggerFor(type); - if (trigger != null) { - addTrigger(trigger); - } - } + addTriggers(type, triggerFactories); } } @@ -211,6 +212,17 @@ public class TriggerManager { return retValue; } + public void addTriggers(Class type, Iterable triggerFactories) + throws RepositoryException + { + for (TriggerFactory factory : triggerFactories) { + Trigger trigger = factory.triggerFor(type); + if (trigger != null) { + addTrigger(trigger); + } + } + } + /** * Determines which operations the given trigger overrides. */ diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java b/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java index ebe0c19..17db288 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java @@ -22,6 +22,7 @@ import com.amazon.carbonado.FetchException; import com.amazon.carbonado.PersistException; import com.amazon.carbonado.Query; import com.amazon.carbonado.Repository; +import com.amazon.carbonado.RepositoryException; import com.amazon.carbonado.Storable; import com.amazon.carbonado.Storage; import com.amazon.carbonado.Trigger; @@ -46,7 +47,9 @@ public abstract class WrappedStorage implements Storage { /** * @param storage storage to wrap */ - public WrappedStorage(Storage storage, Iterable triggerFactories) { + public WrappedStorage(Storage storage, Iterable triggerFactories) + throws RepositoryException + { mStorage = storage; Class wrappedClass = StorableGenerator .getWrappedClass(storage.getStorableType()); -- cgit v1.2.3