From 81475fd493bcc3d5fe750b54ce8d8fa9fb812c9c Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 8 Oct 2006 01:26:37 +0000 Subject: Finished support for trigger factories --- .../com/amazon/carbonado/RepositoryBuilder.java | 5 +++++ .../repo/indexed/IndexedRepositoryBuilder.java | 4 ++++ .../carbonado/repo/logging/LoggingRepository.java | 8 ++++++- .../repo/logging/LoggingRepositoryBuilder.java | 7 +++++- .../carbonado/repo/logging/LoggingStorage.java | 2 +- .../carbonado/repo/logging/LoggingTransaction.java | 26 +++++++++++++++++++--- .../replicated/ReplicatedRepositoryBuilder.java | 4 ++++ .../carbonado/spi/AbstractRepositoryBuilder.java | 24 +++++++++----------- .../amazon/carbonado/spi/CommonMethodNames.java | 1 - .../amazon/carbonado/spi/StorableGenerator.java | 1 - .../com/amazon/carbonado/spi/WrappedQuery.java | 1 - .../com/amazon/carbonado/spi/WrappedStorage.java | 7 +++--- .../com/amazon/carbonado/spi/WrappedSupport.java | 1 - 13 files changed, 64 insertions(+), 27 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/amazon/carbonado/RepositoryBuilder.java b/src/main/java/com/amazon/carbonado/RepositoryBuilder.java index 368ed9a..8c64483 100644 --- a/src/main/java/com/amazon/carbonado/RepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/RepositoryBuilder.java @@ -107,4 +107,9 @@ public interface RepositoryBuilder { * opportunity to register custom persistence code for each Storable. */ void addTriggerFactory(TriggerFactory factory); + + /** + * Returns all the TriggerFactories which were added. + */ + Iterable getTriggerFactories(); } diff --git a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepositoryBuilder.java index a1aa45e..2010230 100644 --- a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepositoryBuilder.java @@ -26,6 +26,7 @@ import com.amazon.carbonado.ConfigurationException; import com.amazon.carbonado.Repository; import com.amazon.carbonado.RepositoryBuilder; import com.amazon.carbonado.RepositoryException; +import com.amazon.carbonado.TriggerFactory; import com.amazon.carbonado.spi.AbstractRepositoryBuilder; @@ -58,6 +59,9 @@ public class IndexedRepositoryBuilder extends AbstractRepositoryBuilder { boolean originalOption = mRepoBuilder.isMaster(); try { mRepoBuilder.setMaster(mIsMaster); + for (TriggerFactory factory : getTriggerFactories()) { + mRepoBuilder.addTriggerFactory(factory); + } wrapped = mRepoBuilder.build(rootRef); } finally { mRepoBuilder.setMaster(originalOption); diff --git a/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepository.java b/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepository.java index 3f17d17..99c462c 100644 --- a/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepository.java @@ -30,6 +30,7 @@ import com.amazon.carbonado.Storable; import com.amazon.carbonado.Storage; import com.amazon.carbonado.SupportException; import com.amazon.carbonado.Transaction; +import com.amazon.carbonado.TriggerFactory; import com.amazon.carbonado.capability.Capability; @@ -40,14 +41,19 @@ import com.amazon.carbonado.capability.Capability; */ class LoggingRepository implements Repository, LogAccessCapability { private final AtomicReference mRootRef; + final Iterable mTriggerFactories; private final Repository mRepo; private final Log mLog; // Map of storages by storable class private final Map, LoggingStorage> mStorages; - LoggingRepository(AtomicReference rootRef, Repository actual, Log log) { + LoggingRepository(AtomicReference rootRef, + Iterable triggerFactories, + Repository actual, Log log) + { mRootRef = rootRef; + mTriggerFactories = triggerFactories; mRepo = actual; mLog = log; diff --git a/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepositoryBuilder.java index 956c947..98c1008 100644 --- a/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/repo/logging/LoggingRepositoryBuilder.java @@ -26,6 +26,7 @@ import com.amazon.carbonado.ConfigurationException; import com.amazon.carbonado.Repository; import com.amazon.carbonado.RepositoryBuilder; import com.amazon.carbonado.RepositoryException; +import com.amazon.carbonado.TriggerFactory; import com.amazon.carbonado.spi.AbstractRepositoryBuilder; @@ -83,6 +84,9 @@ public class LoggingRepositoryBuilder extends AbstractRepositoryBuilder { mRepoBuilder.setName("Logging " + mName); } mRepoBuilder.setMaster(master); + for (TriggerFactory factory : getTriggerFactories()) { + mRepoBuilder.addTriggerFactory(factory); + } actual = mRepoBuilder.build(rootRef); } finally { mRepoBuilder.setName(originalName); @@ -93,7 +97,8 @@ public class LoggingRepositoryBuilder extends AbstractRepositoryBuilder { return actual; } - Repository repo = new LoggingRepository(rootRef, actual, mLog); + Repository repo = new LoggingRepository + (rootRef, mRepoBuilder.getTriggerFactories(), actual, mLog); rootRef.set(repo); return repo; } 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 93b5a33..47d6bb1 100644 --- a/src/main/java/com/amazon/carbonado/repo/logging/LoggingStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/logging/LoggingStorage.java @@ -36,7 +36,7 @@ class LoggingStorage extends WrappedStorage { final LoggingRepository mRepo; LoggingStorage(LoggingRepository repo, Storage storage) { - super(storage); + super(storage, repo.mTriggerFactories); mRepo = repo; } diff --git a/src/main/java/com/amazon/carbonado/repo/logging/LoggingTransaction.java b/src/main/java/com/amazon/carbonado/repo/logging/LoggingTransaction.java index 7deac92..b9782c8 100644 --- a/src/main/java/com/amazon/carbonado/repo/logging/LoggingTransaction.java +++ b/src/main/java/com/amazon/carbonado/repo/logging/LoggingTransaction.java @@ -19,6 +19,7 @@ package com.amazon.carbonado.repo.logging; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; import com.amazon.carbonado.IsolationLevel; import com.amazon.carbonado.PersistException; @@ -30,27 +31,39 @@ import com.amazon.carbonado.Transaction; * @author Brian S O'Neill */ class LoggingTransaction implements Transaction { + private static final ThreadLocal mActiveTxn = + new ThreadLocal(); + + private static final AtomicLong mNextID = new AtomicLong(); + + private final LoggingTransaction mParent; private final Log mLog; private final Transaction mTxn; + private final long mID; LoggingTransaction(Log log, Transaction txn) { + mParent = mActiveTxn.get(); mLog = log; mTxn = txn; + mID = mNextID.addAndGet(1); + mActiveTxn.set(this); + mLog.write("Entered transaction: " + idChain()); } public void commit() throws PersistException { - mLog.write("Transaction.commit()"); + mLog.write("Transaction.commit() on " + idChain()); mTxn.commit(); } public void exit() throws PersistException { - mLog.write("Transaction.exit()"); + mLog.write("Transaction.exit() on " + idChain()); mTxn.exit(); + mActiveTxn.set(mParent); } public void setForUpdate(boolean forUpdate) { if (mLog.isEnabled()) { - mLog.write("Transaction.setForUpdate(" + forUpdate + ')'); + mLog.write("Transaction.setForUpdate(" + forUpdate + ") on " + idChain()); } mTxn.setForUpdate(forUpdate); } @@ -66,4 +79,11 @@ class LoggingTransaction implements Transaction { public IsolationLevel getIsolationLevel() { return mTxn.getIsolationLevel(); } + + private String idChain() { + if (mParent == null) { + return String.valueOf(mID); + } + return mParent.idChain() + " > " + mID; + } } diff --git a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepositoryBuilder.java index 2a09c19..3ab2146 100644 --- a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepositoryBuilder.java @@ -28,6 +28,7 @@ import com.amazon.carbonado.ConfigurationException; import com.amazon.carbonado.Repository; import com.amazon.carbonado.RepositoryBuilder; import com.amazon.carbonado.RepositoryException; +import com.amazon.carbonado.TriggerFactory; import com.amazon.carbonado.spi.AbstractRepositoryBuilder; import com.amazon.carbonado.spi.BelatedRepositoryCreator; @@ -64,6 +65,9 @@ public class ReplicatedRepositoryBuilder extends AbstractRepositoryBuilder { boolean originalOption = mReplicaRepositoryBuilder.isMaster(); try { mReplicaRepositoryBuilder.setMaster(false); + for (TriggerFactory factory : getTriggerFactories()) { + mReplicaRepositoryBuilder.addTriggerFactory(factory); + } replica = mReplicaRepositoryBuilder.build(rootRef); } finally { mReplicaRepositoryBuilder.setMaster(originalOption); diff --git a/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java index d74c70a..ed4eaf3 100644 --- a/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java @@ -21,6 +21,7 @@ package com.amazon.carbonado.spi; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -39,10 +40,10 @@ import com.amazon.carbonado.TriggerFactory; * @author Brian S O'Neill */ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder { - private final List mTriggerFactories; + private final Collection mTriggerFactories; protected AbstractRepositoryBuilder() { - mTriggerFactories = new ArrayList(2); + mTriggerFactories = new LinkedHashSet(2); } public Repository build() throws ConfigurationException, RepositoryException { @@ -53,6 +54,14 @@ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder { mTriggerFactories.add(factory); } + public Iterable getTriggerFactories() { + if (mTriggerFactories == null || mTriggerFactories.size() == 0) { + return Collections.emptyList(); + } else { + return new ArrayList(mTriggerFactories); + } + } + /** * Throw a configuration exception if the configuration is not filled out * sufficiently and correctly such that a repository could be instantiated @@ -91,15 +100,4 @@ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder { messages.add("name missing"); } } - - /** - * Returns all the TriggerFactories which were added. - */ - protected Iterable getTriggerFactories() { - if (mTriggerFactories == null || mTriggerFactories.size() == 0) { - return Collections.emptyList(); - } else { - return new ArrayList(mTriggerFactories); - } - } } diff --git a/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java b/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java index c79e502..8a011c9 100644 --- a/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java +++ b/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java @@ -83,6 +83,5 @@ public class CommonMethodNames { EXIT_METHOD_NAME = "exit"; /** WrappedStorage.Support API method name */ - @Deprecated public static final String CREATE_WRAPPED_SUPPORT_METHOD_NAME = "createSupport"; } diff --git a/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java b/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java index f69d4dd..ecd344d 100644 --- a/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java @@ -357,7 +357,6 @@ public final class StorableGenerator { * @throws IllegalArgumentException if type is null */ @SuppressWarnings("unchecked") - @Deprecated public static Class getWrappedClass(Class type) throws IllegalArgumentException { diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java b/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java index 531f38f..ec3ae34 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java @@ -36,7 +36,6 @@ import com.amazon.carbonado.filter.FilterValues; * @author Don Schneider * @author Brian S O'Neill */ -@Deprecated public abstract class WrappedQuery implements Query { // The query to which this query will delegate diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java b/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java index 629cb53..1cefc73 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java @@ -25,6 +25,7 @@ import com.amazon.carbonado.Repository; import com.amazon.carbonado.Storable; import com.amazon.carbonado.Storage; import com.amazon.carbonado.Trigger; +import com.amazon.carbonado.TriggerFactory; import com.amazon.carbonado.filter.Filter; import com.amazon.carbonado.filter.FilterValues; @@ -39,7 +40,6 @@ import com.amazon.carbonado.util.QuickConstructorGenerator; * * @author Brian S O'Neill */ -@Deprecated public abstract class WrappedStorage implements Storage { private final Storage mStorage; private final WrappedStorableFactory mFactory; @@ -48,14 +48,13 @@ public abstract class WrappedStorage implements Storage { /** * @param storage storage to wrap */ - public WrappedStorage(Storage storage) { + public WrappedStorage(Storage storage, Iterable triggerFactories) { mStorage = storage; Class wrappedClass = StorableGenerator .getWrappedClass(storage.getStorableType()); mFactory = QuickConstructorGenerator .getInstance(wrappedClass, WrappedStorableFactory.class); - // FIXME: wrong, just deprecate this class - mTriggerManager = new TriggerManager(null, null); + mTriggerManager = new TriggerManager(storage.getStorableType(), triggerFactories); } public Class getStorableType() { diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java b/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java index fa57c60..24e2c02 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java @@ -27,7 +27,6 @@ import com.amazon.carbonado.Storable; * * @author Brian S O'Neill */ -@Deprecated public interface WrappedSupport extends TriggerSupport { /** * @see Storable#load -- cgit v1.2.3