diff --git a/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java b/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java index d922b8fd01..8c880a5102 100644 --- a/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java +++ b/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java @@ -89,7 +89,8 @@ public class DistributedDhcpStore implements DhcpStore { freeIPPool = storageService.setBuilder() .withName("onos-dhcp-freeIP") .withSerializer(Serializer.using(KryoNamespaces.API)) - .build(); + .build() + .asDistributedSet(); log.info("Started"); } diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java index 2d1aa0b8a2..48da48a168 100644 --- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java @@ -56,7 +56,8 @@ public class SetTestAddCommand extends AbstractShellCommand { set = storageService.setBuilder() .withName(setName) .withSerializer(serializer) - .build(); + .build() + .asDistributedSet(); // Add a single element to the set if (values.length == 1) { diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java index 74c52c166b..7417cf9567 100644 --- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java @@ -61,7 +61,8 @@ public class SetTestGetCommand extends AbstractShellCommand { set = storageService.setBuilder() .withName(setName) .withSerializer(serializer) - .build(); + .build() + .asDistributedSet(); // Print the set size if (size) { diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java index 1fa073f32b..86b7dc7192 100644 --- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java @@ -64,7 +64,8 @@ public class SetTestRemoveCommand extends AbstractShellCommand { set = storageService.setBuilder() .withName(setName) .withSerializer(serializer) - .build(); + .build() + .asDistributedSet(); if (clear) { set.clear(); diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/classifier/impl/ClassifierManager.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/classifier/impl/ClassifierManager.java index a12d622113..de78fdf3f2 100644 --- a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/classifier/impl/ClassifierManager.java +++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/classifier/impl/ClassifierManager.java @@ -52,7 +52,8 @@ public class ClassifierManager implements ClassifierService { classifierList = storageService.setBuilder() .withName("classifier") .withSerializer(Serializer.using(KryoNamespaces.API)) - .build(); + .build() + .asDistributedSet(); log.info("Started"); } diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultDistributedSet.java b/core/api/src/main/java/org/onosproject/store/primitives/DefaultDistributedSet.java similarity index 93% rename from core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultDistributedSet.java rename to core/api/src/main/java/org/onosproject/store/primitives/DefaultDistributedSet.java index 01a341d327..e719fe947d 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultDistributedSet.java +++ b/core/api/src/main/java/org/onosproject/store/primitives/DefaultDistributedSet.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.onosproject.store.primitives.impl; +package org.onosproject.store.primitives; import java.lang.reflect.Array; import java.util.Collection; @@ -37,30 +37,14 @@ import org.onosproject.store.service.Synchronous; */ public class DefaultDistributedSet extends Synchronous> implements DistributedSet { - private static final long OPERATION_TIMEOUT_MILLIS = 5000; + private final long operationTimeoutMillis; private final AsyncDistributedSet asyncSet; - public DefaultDistributedSet(AsyncDistributedSet asyncSet) { + public DefaultDistributedSet(AsyncDistributedSet asyncSet, long operationTimeoutMillis) { super(asyncSet); this.asyncSet = asyncSet; - } - - private static T complete(CompletableFuture future) { - try { - return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new StorageException.Interrupted(); - } catch (TimeoutException e) { - throw new StorageException.Timeout(); - } catch (ExecutionException e) { - if (e.getCause() instanceof StorageException) { - throw (StorageException) e.getCause(); - } else { - throw new StorageException(e.getCause()); - } - } + this.operationTimeoutMillis = operationTimeoutMillis; } @Override @@ -149,4 +133,21 @@ public class DefaultDistributedSet extends Synchronous public void removeListener(SetEventListener listener) { complete(asyncSet.removeListener(listener)); } + + private T complete(CompletableFuture future) { + try { + return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new StorageException.Interrupted(); + } catch (TimeoutException e) { + throw new StorageException.Timeout(); + } catch (ExecutionException e) { + if (e.getCause() instanceof StorageException) { + throw (StorageException) e.getCause(); + } else { + throw new StorageException(e.getCause()); + } + } + } } diff --git a/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveBuilder.java b/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveBuilder.java index 2792ac556a..7ed8e81c43 100644 --- a/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveBuilder.java +++ b/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveBuilder.java @@ -24,7 +24,8 @@ import org.onosproject.store.service.Serializer; * * @param distributed primitive type */ -public abstract class DistributedPrimitiveBuilder { +public abstract class DistributedPrimitiveBuilder, + T extends DistributedPrimitive> { private DistributedPrimitive.Type type; private String name; @@ -32,6 +33,8 @@ public abstract class DistributedPrimitiveBuilder withName(String name) { + public B withName(String name) { this.name = name; - return this; + return (B) this; } /** @@ -54,9 +57,9 @@ public abstract class DistributedPrimitiveBuilder withSerializer(Serializer serializer) { + public B withSerializer(Serializer serializer) { this.serializer = serializer; - return this; + return (B) this; } /** @@ -65,9 +68,9 @@ public abstract class DistributedPrimitiveBuilder withApplicationId(ApplicationId applicationId) { + public B withApplicationId(ApplicationId applicationId) { this.applicationId = applicationId; - return this; + return (B) this; } /** @@ -77,9 +80,9 @@ public abstract class DistributedPrimitiveBuilder withPartitionsDisabled() { + public B withPartitionsDisabled() { this.partitionsDisabled = true; - return this; + return (B) this; } /** @@ -88,9 +91,27 @@ public abstract class DistributedPrimitiveBuilder withMeteringDisabled() { + public B withMeteringDisabled() { this.meteringDisabled = true; - return this; + return (B) this; + } + + /** + * Disables state changing operations on the returned distributed primitive. + * @return this builder + */ + public B withUpdatesDisabled() { + this.readOnly = true; + return (B) this; + } + + /** + * Turns on relaxed consistency for read operations. + * @return this builder + */ + public B withRelaxedReadConsistency() { + this.relaxedReadConsistency = true; + return (B) this; } /** @@ -111,6 +132,24 @@ public abstract class DistributedPrimitiveBuilder @@ -122,6 +124,26 @@ public interface AsyncDistributedSet extends DistributedPrimitive { */ CompletableFuture removeAll(Collection c); + + /** + * Returns a new {@link DistributedSet} that is backed by this instance. + * + * @return new {@code DistributedSet} instance + */ + default DistributedSet asDistributedSet() { + return asDistributedSet(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS); + } + + /** + * Returns a new {@link DistributedSet} that is backed by this instance. + * + * @param timeoutMillis timeout duration for the returned DistributedSet operations + * @return new {@code DistributedSet} instance + */ + default DistributedSet asDistributedSet(long timeoutMillis) { + return new DefaultDistributedSet<>(this, timeoutMillis); + } + /** * Returns the entries as a immutable set. The returned set is a snapshot and will not reflect new changes made to * this AsyncDistributedSet diff --git a/core/api/src/main/java/org/onosproject/store/service/AtomicCounterBuilder.java b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterBuilder.java index 0728b20a3f..29f7d7354f 100644 --- a/core/api/src/main/java/org/onosproject/store/service/AtomicCounterBuilder.java +++ b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterBuilder.java @@ -20,7 +20,8 @@ import org.onosproject.store.primitives.DistributedPrimitiveBuilder; /** * Builder for AtomicCounter. */ -public abstract class AtomicCounterBuilder extends DistributedPrimitiveBuilder { +public abstract class AtomicCounterBuilder + extends DistributedPrimitiveBuilder { public AtomicCounterBuilder() { super(DistributedPrimitive.Type.COUNTER); } diff --git a/core/api/src/main/java/org/onosproject/store/service/AtomicValueBuilder.java b/core/api/src/main/java/org/onosproject/store/service/AtomicValueBuilder.java index 81c484f884..ba855113bd 100644 --- a/core/api/src/main/java/org/onosproject/store/service/AtomicValueBuilder.java +++ b/core/api/src/main/java/org/onosproject/store/service/AtomicValueBuilder.java @@ -22,7 +22,8 @@ import org.onosproject.store.primitives.DistributedPrimitiveBuilder; * * @param atomic value type */ -public abstract class AtomicValueBuilder extends DistributedPrimitiveBuilder> { +public abstract class AtomicValueBuilder + extends DistributedPrimitiveBuilder, AsyncAtomicValue> { public AtomicValueBuilder() { super(DistributedPrimitive.Type.VALUE); diff --git a/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java b/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java index 466c8f14fb..060aa8f967 100644 --- a/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java +++ b/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java @@ -15,7 +15,7 @@ */ package org.onosproject.store.service; -import org.onosproject.core.ApplicationId; +import org.onosproject.store.primitives.DistributedPrimitiveBuilder; /** * Builder for {@link ConsistentMap} instances. @@ -23,115 +23,32 @@ import org.onosproject.core.ApplicationId; * @param type for map key * @param type for map value */ -public interface ConsistentMapBuilder { +public abstract class ConsistentMapBuilder + extends DistributedPrimitiveBuilder, ConsistentMap> { + + private boolean purgeOnUninstall = false; + + public ConsistentMapBuilder() { + super(DistributedPrimitive.Type.CONSISTENT_MAP); + } /** - * Sets the name of the map. - *

- * Each map is identified by a unique map name. Different instances with the same name are all backed by the - * same backend state. - *

- *

- * Note: This is a mandatory parameter. - *

+ * Clears map contents when the owning application is uninstalled. * - * @param name name of the map - * @return this ConsistentMapBuilder + * return this builder */ - ConsistentMapBuilder withName(String name); + public ConsistentMapBuilder withPurgeOnUninstall() { + purgeOnUninstall = true; + return this; + } /** - * Sets the identifier of the application that owns this map instance. - *

- * Note: If {@code purgeOnUninstall} option is enabled, applicationId - * must be specified. - *

- * - * @param id applicationId owning the consistent map - * @return this ConsistentMapBuilder + * Returns if map entries need to be cleared when owning application is uninstalled. + * @return {@code true} if yes; {@code false} otherwise. */ - ConsistentMapBuilder withApplicationId(ApplicationId id); - - /** - * Sets a serializer that can be used to serialize - * both the keys and values inserted into the map. The serializer - * builder should be pre-populated with any classes that will be - * put into the map. - *

- * Note: This is a mandatory parameter. - *

- * - * @param serializer serializer - * @return this ConsistentMapBuilder - */ - ConsistentMapBuilder withSerializer(Serializer serializer); - - /** - * Disables distribution of map entries across multiple database partitions. - *

- * When partitioning is disabled, the returned map will have a single partition - * that spans the entire cluster. Furthermore, the changes made to the map are - * ephemeral and do not survive a full cluster restart. - *

- *

- * Disabling partitions is more appropriate when the returned map is used for - * coordination activities such as leader election and not for long term data persistence. - *

- *

- * Note: By default partitions are enabled and entries in the map are durable. - *

- * @return this ConsistentMapBuilder - */ - ConsistentMapBuilder withPartitionsDisabled(); - - /** - * Disables map updates. - *

- * Attempt to update the built map will throw {@code UnsupportedOperationException}. - * - * @return this ConsistentMapBuilder - */ - ConsistentMapBuilder withUpdatesDisabled(); - - /** - * Purges map contents when the application owning the map is uninstalled. - *

- * When this option is enabled, the caller must provide a applicationId via - * the {@code withAppliationId} builder method. - *

- * By default map entries will NOT be purged when owning application is uninstalled. - * - * @return this ConsistentMapBuilder - */ - ConsistentMapBuilder withPurgeOnUninstall(); - - /** - * Instantiates Metering service to gather usage and performance metrics. - * By default, usage data will be stored. - * - * @return this ConsistentMapBuilder - */ - ConsistentMapBuilder withMeteringDisabled(); - - /** - * Provides weak consistency for map gets. - *

- * While this can lead to improved read performance, it can also make the behavior - * heard to reason. Only turn this on if you know what you are doing. By default - * reads are strongly consistent. - * - * @return this ConsistentMapBuilder - */ - ConsistentMapBuilder withRelaxedReadConsistency(); - - /** - * Builds an consistent map based on the configuration options - * supplied to this builder. - * - * @return new consistent map - * @throws java.lang.RuntimeException if a mandatory parameter is missing - */ - ConsistentMap build(); + public boolean purgeOnUninstall() { + return purgeOnUninstall; + } /** * Builds an async consistent map based on the configuration options @@ -140,5 +57,5 @@ public interface ConsistentMapBuilder { * @return new async consistent map * @throws java.lang.RuntimeException if a mandatory parameter is missing */ - AsyncConsistentMap buildAsyncMap(); + public abstract AsyncConsistentMap buildAsyncMap(); } diff --git a/core/api/src/main/java/org/onosproject/store/service/DistributedSetBuilder.java b/core/api/src/main/java/org/onosproject/store/service/DistributedSetBuilder.java index 09ba79493d..d05182a379 100644 --- a/core/api/src/main/java/org/onosproject/store/service/DistributedSetBuilder.java +++ b/core/api/src/main/java/org/onosproject/store/service/DistributedSetBuilder.java @@ -15,127 +15,37 @@ */ package org.onosproject.store.service; -import org.onosproject.core.ApplicationId; +import org.onosproject.store.primitives.DistributedPrimitiveBuilder; /** * Builder for distributed set. * * @param type set elements. */ -public interface DistributedSetBuilder { +public abstract class DistributedSetBuilder extends DistributedPrimitiveBuilder, + AsyncDistributedSet> { + + private boolean purgeOnUninstall = false; + + public DistributedSetBuilder() { + super(DistributedPrimitive.Type.SET); + } /** - * Sets the name of the set. - *

- * Each set is identified by a unique name. - *

- *

- * Note: This is a mandatory parameter. - *

+ * Enables clearing set contents when the owning application is uninstalled. * - * @param name name of the set - * @return this DistributedSetBuilder + * return this builder */ - DistributedSetBuilder withName(String name); + public DistributedSetBuilder withPurgeOnUninstall() { + purgeOnUninstall = true; + return this; + } /** - * Sets the owner applicationId for the set. - *

- * Note: If {@code purgeOnUninstall} option is enabled, applicationId - * must be specified. - *

- * - * @param id applicationId owning the set - * @return this DistributedSetBuilder + * Returns if set contents need to be cleared when owning application is uninstalled. + * @return {@code true} if yes; {@code false} otherwise. */ - DistributedSetBuilder withApplicationId(ApplicationId id); - - /** - * Sets a serializer that can be used to serialize - * the elements add to the set. The serializer - * builder should be pre-populated with any classes that will be - * put into the set. - *

- * Note: This is a mandatory parameter. - *

- * - * @param serializer serializer - * @return this DistributedSetBuilder - */ - DistributedSetBuilder withSerializer(Serializer serializer); - - /** - * Disables set updates. - *

- * Attempt to update the built set will throw {@code UnsupportedOperationException}. - * - * @return this DistributedSetBuilder - */ - DistributedSetBuilder withUpdatesDisabled(); - - /** - * Provides weak consistency for set reads. - *

- * While this can lead to improved read performance, it can also make the behavior - * heard to reason. Only turn this on if you know what you are doing. By default - * reads are strongly consistent. - * - * @return this DistributedSetBuilder - */ - DistributedSetBuilder withRelaxedReadConsistency(); - - /** - * Disables distribution of set entries across multiple database partitions. - *

- * When partitioning is disabled, the returned set will have a single partition - * that spans the entire cluster. Furthermore, the changes made to the set are - * ephemeral and do not survive a full cluster restart. - *

- *

- * Disabling partitions is more appropriate when the returned set is used for - * simple coordination activities and not for long term data persistence. - *

- *

- * Note: By default partitions are enabled and entries in the set are durable. - *

- * @return this DistributedSetBuilder - */ - DistributedSetBuilder withPartitionsDisabled(); - - /** - * Instantiate Metrics service to gather usage and performance metrics. - * By default usage information is enabled - * @return this DistributedSetBuilder - */ - DistributedSetBuilder withMeteringDisabled(); - - /** - * Purges set contents when the application owning the set is uninstalled. - *

- * When this option is enabled, the caller must provide a applicationId via - * the {@code withAppliationId} builder method. - *

- * By default set contents will NOT be purged when owning application is uninstalled. - * - * @return this DistributedSetBuilder - */ - DistributedSetBuilder withPurgeOnUninstall(); - - /** - * Builds an set based on the configuration options - * supplied to this builder. - * - * @return new set - * @throws java.lang.RuntimeException if a mandatory parameter is missing - */ - DistributedSet build(); - - /** - * Builds an {@link AsyncDistributedSet async set} based on the configuration options - * supplied to this builder. - * - * @return new AsyncDistributedSet - * @throws java.lang.RuntimeException if a mandatory parameter is missing - */ - AsyncDistributedSet buildAsyncSet(); + public boolean purgeOnUninstall() { + return purgeOnUninstall; + } } diff --git a/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java b/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java index 8a4399553d..2242badae8 100644 --- a/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java +++ b/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java @@ -20,7 +20,8 @@ import org.onosproject.store.primitives.DistributedPrimitiveBuilder; /** * Abstract base class for a transaction context builder. */ -public abstract class TransactionContextBuilder extends DistributedPrimitiveBuilder { +public abstract class TransactionContextBuilder + extends DistributedPrimitiveBuilder { public TransactionContextBuilder() { super(DistributedPrimitive.Type.TRANSACTION_CONTEXT); diff --git a/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java b/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java index f7857adcef..0efdd4ae71 100644 --- a/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java +++ b/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java @@ -29,7 +29,6 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; -import org.onosproject.core.ApplicationId; import org.onosproject.store.primitives.ConsistentMapBackedJavaMap; import com.google.common.base.Objects; @@ -293,53 +292,11 @@ public final class TestConsistentMap extends ConsistentMapAdapter { return new Builder(); } - public static class Builder implements ConsistentMapBuilder { - String mapName = "map"; - - @Override - public ConsistentMapBuilder withName(String mapName) { - this.mapName = mapName; - return this; - } - - @Override - public ConsistentMapBuilder withApplicationId(ApplicationId id) { - return this; - } - - @Override - public ConsistentMapBuilder withSerializer(Serializer serializer) { - return this; - } - - @Override - public ConsistentMapBuilder withPartitionsDisabled() { - return this; - } - - @Override - public ConsistentMapBuilder withUpdatesDisabled() { - return this; - } - - @Override - public ConsistentMapBuilder withPurgeOnUninstall() { - return this; - } - - @Override - public ConsistentMapBuilder withRelaxedReadConsistency() { - return this; - } - - @Override - public ConsistentMapBuilder withMeteringDisabled() { - return this; - } + public static class Builder extends ConsistentMapBuilder { @Override public ConsistentMap build() { - return new TestConsistentMap<>(mapName); + return new TestConsistentMap<>(name()); } @Override diff --git a/core/store/dist/src/main/java/org/onosproject/store/device/impl/ECDeviceStore.java b/core/store/dist/src/main/java/org/onosproject/store/device/impl/ECDeviceStore.java index 129fbbe556..e1e1d2e592 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/device/impl/ECDeviceStore.java +++ b/core/store/dist/src/main/java/org/onosproject/store/device/impl/ECDeviceStore.java @@ -237,7 +237,8 @@ public class ECDeviceStore .withSerializer(Serializer.using(KryoNamespaces.API)) .withPartitionsDisabled() .withRelaxedReadConsistency() - .build(); + .build() + .asDistributedSet(); deviceDescriptions.addListener(deviceUpdateListener); portDescriptions.addListener(portUpdateListener); diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultConsistentMapBuilder.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultConsistentMapBuilder.java index 6f3fadd2ba..5a81ca6868 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultConsistentMapBuilder.java +++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultConsistentMapBuilder.java @@ -15,13 +15,10 @@ */ package org.onosproject.store.primitives.impl; -import org.onosproject.core.ApplicationId; import org.onosproject.store.service.AsyncConsistentMap; import org.onosproject.store.service.ConsistentMap; import org.onosproject.store.service.ConsistentMapBuilder; -import org.onosproject.store.service.Serializer; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; /** @@ -30,85 +27,25 @@ import static com.google.common.base.Preconditions.checkState; * @param type for map key * @param type for map value */ -public class DefaultConsistentMapBuilder implements ConsistentMapBuilder { +public class DefaultConsistentMapBuilder extends ConsistentMapBuilder { - private Serializer serializer; - private String name; - private ApplicationId applicationId; - private boolean purgeOnUninstall = false; - private boolean partitionsEnabled = true; - private boolean readOnly = false; - private boolean metering = true; - private boolean relaxedReadConsistency = false; private final DatabaseManager manager; - private static final long DEFAULT_OPERATION_TIMEOUT_MILLIS = 5000L; public DefaultConsistentMapBuilder(DatabaseManager manager) { this.manager = manager; } - @Override - public ConsistentMapBuilder withName(String name) { - checkArgument(name != null && !name.isEmpty()); - this.name = name; - return this; - } - - @Override - public ConsistentMapBuilder withApplicationId(ApplicationId id) { - checkArgument(id != null); - this.applicationId = id; - return this; - } - - @Override - public ConsistentMapBuilder withPurgeOnUninstall() { - purgeOnUninstall = true; - return this; - } - - @Override - public ConsistentMapBuilder withMeteringDisabled() { - metering = false; - return this; - } - - @Override - public ConsistentMapBuilder withSerializer(Serializer serializer) { - checkArgument(serializer != null); - this.serializer = serializer; - return this; - } - - @Override - public ConsistentMapBuilder withPartitionsDisabled() { - partitionsEnabled = false; - return this; - } - - @Override - public ConsistentMapBuilder withUpdatesDisabled() { - readOnly = true; - return this; - } - - @Override - public ConsistentMapBuilder withRelaxedReadConsistency() { - relaxedReadConsistency = true; - return this; - } - private void validateInputs() { - checkState(name != null, "name must be specified"); - checkState(serializer != null, "serializer must be specified"); - if (purgeOnUninstall) { - checkState(applicationId != null, "ApplicationId must be specified when purgeOnUninstall is enabled"); + checkState(name() != null, "name must be specified"); + checkState(serializer() != null, "serializer must be specified"); + if (purgeOnUninstall()) { + checkState(applicationId() != null, "ApplicationId must be specified when purgeOnUninstall is enabled"); } } @Override public ConsistentMap build() { - return buildAndRegisterMap().asConsistentMap(DEFAULT_OPERATION_TIMEOUT_MILLIS); + return buildAndRegisterMap().asConsistentMap(); } @Override @@ -118,25 +55,25 @@ public class DefaultConsistentMapBuilder implements ConsistentMapBuilder buildAndRegisterMap() { validateInputs(); - Database database = partitionsEnabled ? manager.partitionedDatabase : manager.inMemoryDatabase; - if (relaxedReadConsistency) { + Database database = partitionsDisabled() ? manager.inMemoryDatabase : manager.partitionedDatabase; + if (relaxedReadConsistency()) { return manager.registerMap( - new AsyncCachingConsistentMap<>(name, - applicationId, + new AsyncCachingConsistentMap<>(name(), + applicationId(), database, - serializer, - readOnly, - purgeOnUninstall, - metering)); + serializer(), + readOnly(), + purgeOnUninstall(), + meteringEnabled())); } else { return manager.registerMap( - new DefaultAsyncConsistentMap<>(name, - applicationId, + new DefaultAsyncConsistentMap<>(name(), + applicationId(), database, - serializer, - readOnly, - purgeOnUninstall, - metering)); + serializer(), + readOnly(), + purgeOnUninstall(), + meteringEnabled())); } } } \ No newline at end of file diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultDistributedSetBuilder.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultDistributedSetBuilder.java index 6621b4c9fa..8c3e5f36e8 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultDistributedSetBuilder.java +++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultDistributedSetBuilder.java @@ -20,7 +20,6 @@ import java.util.function.Supplier; import org.onosproject.core.ApplicationId; import org.onosproject.store.service.AsyncDistributedSet; import org.onosproject.store.service.ConsistentMapBuilder; -import org.onosproject.store.service.DistributedSet; import org.onosproject.store.service.Serializer; import org.onosproject.store.service.DistributedSetBuilder; @@ -29,7 +28,7 @@ import org.onosproject.store.service.DistributedSetBuilder; * * @param type for set elements */ -public class DefaultDistributedSetBuilder implements DistributedSetBuilder { +public class DefaultDistributedSetBuilder extends DistributedSetBuilder { private String name; private ConsistentMapBuilder mapBuilder; @@ -90,12 +89,7 @@ public class DefaultDistributedSetBuilder implements DistributedSetBuilder } @Override - public DistributedSet build() { - return new DefaultDistributedSet(buildAsyncSet()); - } - - @Override - public AsyncDistributedSet buildAsyncSet() { + public AsyncDistributedSet build() { return new DefaultAsyncDistributedSet(mapBuilder.buildAsyncMap(), name, metering); } } diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContext.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContext.java index b71902b96f..43cacb3fd5 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContext.java +++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContext.java @@ -82,14 +82,16 @@ public class DefaultTransactionContext implements TransactionContext { checkState(isOpen, TX_NOT_OPEN_ERROR); checkNotNull(mapName); checkNotNull(serializer); - return txMaps.computeIfAbsent(mapName, name -> new DefaultTransactionalMap<>( + return txMaps.computeIfAbsent(mapName, name -> { + ConsistentMapBuilder mapBuilder = (ConsistentMapBuilder) mapBuilderSupplier.get() + .withName(name) + .withSerializer(serializer); + return new DefaultTransactionalMap<>( name, - mapBuilderSupplier.get() - .withName(name) - .withSerializer(serializer) - .buildAsyncMap(), + mapBuilder.buildAsyncMap(), this, - serializer)); + serializer); + }); } @SuppressWarnings("unchecked") diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContextBuilder.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContextBuilder.java index 99d62ca298..91f4bf6e7d 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContextBuilder.java +++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/DefaultTransactionContextBuilder.java @@ -48,7 +48,7 @@ public class DefaultTransactionContextBuilder extends TransactionContextBuilder return new DefaultTransactionContext(transactionId, transactionCommitter, () -> { ConsistentMapBuilder mapBuilder = mapBuilderSupplier.get(); if (partitionsDisabled()) { - mapBuilder = mapBuilder.withPartitionsDisabled(); + mapBuilder = (ConsistentMapBuilder) mapBuilder.withPartitionsDisabled(); } return mapBuilder; });