Added a builder for TransactionContexts
Change-Id: I22b467ed0b1e691f3df4ccc5e414b8f4c3bf18f4
Showing
4 changed files
with
76 additions
and
6 deletions
| ... | @@ -63,9 +63,9 @@ public interface StorageService { | ... | @@ -63,9 +63,9 @@ public interface StorageService { |
| 63 | AtomicCounterBuilder atomicCounterBuilder(); | 63 | AtomicCounterBuilder atomicCounterBuilder(); |
| 64 | 64 | ||
| 65 | /** | 65 | /** |
| 66 | - * Creates a new transaction context. | 66 | + * Creates a new transaction context builder. |
| 67 | * | 67 | * |
| 68 | - * @return transaction context | 68 | + * @return a builder for a transaction context. |
| 69 | */ | 69 | */ |
| 70 | - TransactionContext createTransactionContext(); | 70 | + TransactionContextBuilder transactionContextBuilder(); |
| 71 | } | 71 | } | ... | ... |
| 1 | +package org.onosproject.store.service; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * Interface definition for a transaction context builder. | ||
| 5 | + */ | ||
| 6 | +public interface TransactionContextBuilder { | ||
| 7 | + | ||
| 8 | + /** | ||
| 9 | + * Disables distribution of map entries across multiple database partitions. | ||
| 10 | + * <p> | ||
| 11 | + * When partitioning is disabled, the returned map will have a single | ||
| 12 | + * partition that spans the entire cluster. Furthermore, the changes made to | ||
| 13 | + * the map are ephemeral and do not survive a full cluster restart. | ||
| 14 | + * </p> | ||
| 15 | + * <p> | ||
| 16 | + * Note: By default, partitions are enabled. This feature is intended to | ||
| 17 | + * simplify debugging. | ||
| 18 | + * </p> | ||
| 19 | + * | ||
| 20 | + * @return this TransactionalContextBuilder | ||
| 21 | + */ | ||
| 22 | + public TransactionContextBuilder withPartitionsDisabled(); | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * Builds a TransactionContext based on configuration options supplied to this | ||
| 26 | + * builder. | ||
| 27 | + * | ||
| 28 | + * @return a new TransactionalContext | ||
| 29 | + * @throws java.lang.RuntimeException if a mandatory parameter is missing | ||
| 30 | + */ | ||
| 31 | + public TransactionContext build(); | ||
| 32 | +} |
| ... | @@ -59,7 +59,7 @@ import org.onosproject.store.service.SetBuilder; | ... | @@ -59,7 +59,7 @@ import org.onosproject.store.service.SetBuilder; |
| 59 | import org.onosproject.store.service.StorageAdminService; | 59 | import org.onosproject.store.service.StorageAdminService; |
| 60 | import org.onosproject.store.service.StorageService; | 60 | import org.onosproject.store.service.StorageService; |
| 61 | import org.onosproject.store.service.Transaction; | 61 | import org.onosproject.store.service.Transaction; |
| 62 | -import org.onosproject.store.service.TransactionContext; | 62 | +import org.onosproject.store.service.TransactionContextBuilder; |
| 63 | import org.slf4j.Logger; | 63 | import org.slf4j.Logger; |
| 64 | 64 | ||
| 65 | import java.io.File; | 65 | import java.io.File; |
| ... | @@ -217,8 +217,9 @@ public class DatabaseManager implements StorageService, StorageAdminService { | ... | @@ -217,8 +217,9 @@ public class DatabaseManager implements StorageService, StorageAdminService { |
| 217 | } | 217 | } |
| 218 | 218 | ||
| 219 | @Override | 219 | @Override |
| 220 | - public TransactionContext createTransactionContext() { | 220 | + public TransactionContextBuilder transactionContextBuilder() { |
| 221 | - return new DefaultTransactionContext(partitionedDatabase, transactionIdGenerator.getNewId()); | 221 | + return new DefaultTransactionContextBuilder( |
| 222 | + inMemoryDatabase, partitionedDatabase, transactionIdGenerator.getNewId()); | ||
| 222 | } | 223 | } |
| 223 | 224 | ||
| 224 | @Override | 225 | @Override | ... | ... |
| 1 | +package org.onosproject.store.consistent.impl; | ||
| 2 | + | ||
| 3 | +import org.onosproject.store.service.TransactionContext; | ||
| 4 | +import org.onosproject.store.service.TransactionContextBuilder; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * The default implementation of a transaction context builder. This builder | ||
| 8 | + * generates a {@link DefaultTransactionContext}. | ||
| 9 | + */ | ||
| 10 | +public class DefaultTransactionContextBuilder implements TransactionContextBuilder { | ||
| 11 | + | ||
| 12 | + private boolean partitionsEnabled = true; | ||
| 13 | + private final Database partitionedDatabase; | ||
| 14 | + private final Database inMemoryDatabase; | ||
| 15 | + private final long transactionId; | ||
| 16 | + | ||
| 17 | + public DefaultTransactionContextBuilder( | ||
| 18 | + Database inMemoryDatabase, Database partitionedDatabase, long transactionId) { | ||
| 19 | + this.partitionedDatabase = partitionedDatabase; | ||
| 20 | + this.inMemoryDatabase = inMemoryDatabase; | ||
| 21 | + this.transactionId = transactionId; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + @Override | ||
| 25 | + public TransactionContextBuilder withPartitionsDisabled() { | ||
| 26 | + partitionsEnabled = false; | ||
| 27 | + return this; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + @Override | ||
| 31 | + public TransactionContext build() { | ||
| 32 | + return new DefaultTransactionContext( | ||
| 33 | + partitionsEnabled ? partitionedDatabase : inMemoryDatabase, | ||
| 34 | + transactionId); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | +} |
-
Please register or login to post a comment