summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2006-10-17 00:08:26 +0000
committerBrian S. O'Neill <bronee@gmail.com>2006-10-17 00:08:26 +0000
commit987e5b79304062308a5d304a57dabcce97654867 (patch)
tree25b7adfef1834d5ede463429b89eb4cfaf76f076 /src
parent75edf91be7467ee8cd10f6ad5c95dbd8007453ff (diff)
More work on automatic triggers.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/amazon/carbonado/RepositoryBuilder.java12
-rw-r--r--src/main/java/com/amazon/carbonado/TriggerFactory.java2
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java2
-rw-r--r--src/main/java/com/amazon/carbonado/repo/logging/LoggingStorage.java3
-rw-r--r--src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java5
-rw-r--r--src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java25
-rw-r--r--src/main/java/com/amazon/carbonado/spi/TriggerManager.java26
-rw-r--r--src/main/java/com/amazon/carbonado/spi/WrappedStorage.java5
8 files changed, 60 insertions, 20 deletions
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
*/
- <S> Trigger<? super S> triggerFor(Class<S> type);
+ <S extends Storable> Trigger<? super S> triggerFor(Class<S> 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<S extends Storable> extends StandardQueryFactory<S>
final TriggerManager<S> mTriggerManager;
JDBCStorage(JDBCRepository repository, JDBCStorableInfo<S> 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<S extends Storable> extends WrappedStorage<S> {
final LoggingRepository mRepo;
- LoggingStorage(LoggingRepository repo, Storage<S> storage) {
+ LoggingStorage(LoggingRepository repo, Storage<S> 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<Txn, S extends Storable> implements Storage<S>, Storag
mRepository = repository;
mType = type;
mRawSupport = new Support<Txn, S>(repository, this);
- mTriggerManager = new TriggerManager<S>(type, repository.mTriggerFactories);
+ mTriggerManager = new TriggerManager<S>();
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<Txn, S extends Storable> implements Storage<S>, Storag
mPrimaryDatabase = primaryDatabase;
mQueryEngine = new QueryEngine<S>(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<TriggerFactory> mTriggerFactories;
+ private final Set<TriggerFactory> mTriggerFactories;
protected AbstractRepositoryBuilder() {
mTriggerFactories = new LinkedHashSet<TriggerFactory>(2);
@@ -47,15 +48,25 @@ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder {
return build(new AtomicReference<Repository>());
}
- 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<TriggerFactory> getTriggerFactories() {
- if (mTriggerFactories == null || mTriggerFactories.size() == 0) {
- return Collections.emptyList();
- } else {
- return new ArrayList<TriggerFactory>(mTriggerFactories);
+ synchronized (mTriggerFactories) {
+ if (mTriggerFactories == null || mTriggerFactories.size() == 0) {
+ return Collections.emptyList();
+ } else {
+ return new ArrayList<TriggerFactory>(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<S extends Storable> {
private volatile ForUpdate<S> mForUpdate;
private volatile ForDelete<S> mForDelete;
+ public TriggerManager() {
+ }
+
/**
* @param triggerFactories TriggerFactories which will be called upon to
* optionally return a trigger to initially register
*/
- public TriggerManager(Class<S> type, Iterable<TriggerFactory> triggerFactories) {
+ public TriggerManager(Class<S> type, Iterable<TriggerFactory> triggerFactories)
+ throws RepositoryException
+ {
if (triggerFactories != null) {
- for (TriggerFactory factory : triggerFactories) {
- Trigger<? super S> trigger = factory.triggerFor(type);
- if (trigger != null) {
- addTrigger(trigger);
- }
- }
+ addTriggers(type, triggerFactories);
}
}
@@ -211,6 +212,17 @@ public class TriggerManager<S extends Storable> {
return retValue;
}
+ public void addTriggers(Class<S> type, Iterable<TriggerFactory> triggerFactories)
+ throws RepositoryException
+ {
+ for (TriggerFactory factory : triggerFactories) {
+ Trigger<? super S> 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<S extends Storable> implements Storage<S> {
/**
* @param storage storage to wrap
*/
- public WrappedStorage(Storage<S> storage, Iterable<TriggerFactory> triggerFactories) {
+ public WrappedStorage(Storage<S> storage, Iterable<TriggerFactory> triggerFactories)
+ throws RepositoryException
+ {
mStorage = storage;
Class<? extends S> wrappedClass = StorableGenerator
.getWrappedClass(storage.getStorableType());