summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/repo/replicated
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/amazon/carbonado/repo/replicated')
-rw-r--r--src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java20
-rw-r--r--src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedStorage.java22
2 files changed, 28 insertions, 14 deletions
diff --git a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java
index b07b853..16a33bd 100644
--- a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java
+++ b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java
@@ -40,6 +40,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.UnsupportedTypeException;
import com.amazon.carbonado.capability.Capability;
import com.amazon.carbonado.capability.IndexInfo;
@@ -143,11 +144,19 @@ class ReplicatedRepository
mName = aName;
mReplicaRepository = aReplicaRepository;
mMasterRepository = aMasterRepository;
+
mStorages = new StorageCollection() {
protected <S extends Storable> Storage<S> createStorage(Class<S> type)
throws SupportException, RepositoryException
{
- return new ReplicatedStorage<S>(ReplicatedRepository.this, type);
+ Storage<S> replicaStorage = mReplicaRepository.storageFor(type);
+
+ try {
+ return new ReplicatedStorage<S>(ReplicatedRepository.this, replicaStorage);
+ } catch (UnsupportedTypeException e) {
+ // Okay, no master.
+ return replicaStorage;
+ }
}
};
}
@@ -324,6 +333,13 @@ class ReplicatedRepository
Object... filterValues)
throws RepositoryException
{
+ ReplicationTrigger<S> trigger;
+ if (storageFor(type) instanceof ReplicatedStorage) {
+ trigger = ((ReplicatedStorage) storageFor(type)).getTrigger();
+ } else {
+ throw new UnsupportedTypeException(type);
+ }
+
Storage<S> replicaStorage, masterStorage;
replicaStorage = mReplicaRepository.storageFor(type);
masterStorage = mMasterRepository.storageFor(type);
@@ -372,7 +388,7 @@ class ReplicatedRepository
try {
Cursor<S> masterCursor = masterQuery.fetch();
try {
- resync(((ReplicatedStorage) storageFor(type)).getTrigger(),
+ resync(trigger,
replicaCursor,
masterCursor,
throttle, desiredSpeed,
diff --git a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedStorage.java b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedStorage.java
index f781279..38c57c4 100644
--- a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedStorage.java
+++ b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedStorage.java
@@ -44,27 +44,25 @@ class ReplicatedStorage<S extends Storable> implements Storage<S> {
final Storage<S> mReplicaStorage;
final ReplicationTrigger<S> mTrigger;
- public ReplicatedStorage(ReplicatedRepository aRepository, Class<S> aType)
- throws SupportException, RepositoryException
+ /**
+ * @throws UnsupportedTypeException if master doesn't support Storable, but
+ * it is marked as Independent
+ */
+ public ReplicatedStorage(ReplicatedRepository aRepository, Storage<S> replicaStorage)
+ throws SupportException, RepositoryException, UnsupportedTypeException
{
- mReplicaStorage = aRepository.getReplicaRepository().storageFor(aType);
+ mReplicaStorage = replicaStorage;
// Create master using BelatedStorageCreator such that we can start up
// and read from replica even if master is down.
Log log = LogFactory.getLog(getClass());
BelatedStorageCreator<S> creator = new BelatedStorageCreator<S>
- (log, aRepository.getMasterRepository(), aType,
+ (log, aRepository.getMasterRepository(), replicaStorage.getStorableType(),
ReplicatedRepositoryBuilder.DEFAULT_RETRY_MILLIS);
- Storage<S> masterStorage;
- try {
- masterStorage = creator.get(ReplicatedRepositoryBuilder.DEFAULT_MASTER_TIMEOUT_MILLIS);
- } catch (UnsupportedTypeException e) {
- // Master doesn't support Storable, but it is marked as Independent.
- masterStorage = null;
- }
-
+ Storage<S> masterStorage =
+ creator.get(ReplicatedRepositoryBuilder.DEFAULT_MASTER_TIMEOUT_MILLIS);
mTrigger = new ReplicationTrigger<S>(aRepository, mReplicaStorage, masterStorage);
addTrigger(mTrigger);
}