Rework InMemoryDOMDataStoreConfigProperties 30/71430/5
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 26 Apr 2018 16:09:42 +0000 (18:09 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 2 May 2018 09:04:31 +0000 (09:04 +0000)
Rework InMemoryDOMDataStoreConfigProperties to use immutables.org,
i.e. generate a builder. Also include debug transaction flag in
properties.

This cleans up InMemoryDOMDataStoreFactory, making it more
straightforward.

Change-Id: Ifae57b87fe8a87d183958e4826afc1e17c173273
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
dom/mdsal-dom-inmemory-datastore/pom.xml
dom/mdsal-dom-inmemory-datastore/src/main/java/org/opendaylight/mdsal/dom/store/inmemory/InMemoryDOMDataStoreConfigProperties.java
dom/mdsal-dom-inmemory-datastore/src/main/java/org/opendaylight/mdsal/dom/store/inmemory/InMemoryDOMDataStoreFactory.java

index dbc5c27603bedd760624e6608f41995c8d05b01d..e3ad524f491ca914344c0e5cc359e0c3ece81bcd 100644 (file)
             <artifactId>org.osgi.core</artifactId>
         </dependency>
 
+        <dependency>
+          <groupId>org.immutables</groupId>
+          <artifactId>value</artifactId>
+        </dependency>
+
         <!-- Test Dependencies -->
         <dependency>
             <groupId>junit</groupId>
index f23d66daf4e3fb4bfffb6e0593028f7c3c6b03e8..ba701d468420a5ea6acd268806a93b754d5ac186 100644 (file)
@@ -5,9 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.mdsal.dom.store.inmemory;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.immutables.value.Value;
+import org.immutables.value.Value.Style.ImplementationVisibility;
+
 /**
  * Holds configuration properties when creating an {@link InMemoryDOMDataStore} instance via the
  * {@link InMemoryDOMDataStoreFactory}.
@@ -15,92 +18,78 @@ package org.opendaylight.mdsal.dom.store.inmemory;
  * @author Thomas Pantelis
  * @see InMemoryDOMDataStoreFactory
  */
-public final class InMemoryDOMDataStoreConfigProperties {
+@Value.Immutable
+@Value.Style(visibility = ImplementationVisibility.PRIVATE)
+public abstract class InMemoryDOMDataStoreConfigProperties {
 
     public static final int DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE = 1000;
     public static final int DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE = 20;
     public static final int DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE = 1000;
     public static final int DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE = 5000;
 
-    private static final InMemoryDOMDataStoreConfigProperties DEFAULT =
-            create(DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE,
-                    DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE,
-                    DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE,
-                    DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE);
-
-    private final int maxDataChangeExecutorQueueSize;
-    private final int maxDataChangeExecutorPoolSize;
-    private final int maxDataChangeListenerQueueSize;
-    private final int maxDataStoreExecutorQueueSize;
-
-    private InMemoryDOMDataStoreConfigProperties(final int maxDataChangeExecutorPoolSize,
-            final int maxDataChangeExecutorQueueSize, final int maxDataChangeListenerQueueSize,
-            final int maxDataStoreExecutorQueueSize) {
-        this.maxDataChangeExecutorQueueSize = maxDataChangeExecutorQueueSize;
-        this.maxDataChangeExecutorPoolSize = maxDataChangeExecutorPoolSize;
-        this.maxDataChangeListenerQueueSize = maxDataChangeListenerQueueSize;
-        this.maxDataStoreExecutorQueueSize = maxDataStoreExecutorQueueSize;
-    }
+    private static final @NonNull InMemoryDOMDataStoreConfigProperties DEFAULT = builder().build();
 
     /**
-     * Constructs an instance with the given property values.
+     * Returns the InMemoryDOMDataStoreConfigProperties instance with default values.
      *
-     * @param maxDataChangeExecutorPoolSize
-     *            maximum thread pool size for the data change notification executor.
-     * @param maxDataChangeExecutorQueueSize
-     *            maximum queue size for the data change notification executor.
-     * @param maxDataChangeListenerQueueSize
-     *            maximum queue size for the data change listeners.
-     * @param maxDataStoreExecutorQueueSize
-     *            maximum queue size for the data store executor.
+     * @return the InMemoryDOMDataStoreConfigProperties instance with default values.
      */
-    public static InMemoryDOMDataStoreConfigProperties create(final int maxDataChangeExecutorPoolSize,
-            final int maxDataChangeExecutorQueueSize, final int maxDataChangeListenerQueueSize,
-            final int maxDataStoreExecutorQueueSize) {
-        return new InMemoryDOMDataStoreConfigProperties(maxDataChangeExecutorPoolSize,
-                maxDataChangeExecutorQueueSize, maxDataChangeListenerQueueSize,
-                maxDataStoreExecutorQueueSize);
+    public static @NonNull InMemoryDOMDataStoreConfigProperties getDefault() {
+        return DEFAULT;
     }
 
-    public static InMemoryDOMDataStoreConfigProperties create(final int maxDataChangeExecutorPoolSize,
-            final int maxDataChangeExecutorQueueSize, final int maxDataChangeListenerQueueSize) {
-        return new InMemoryDOMDataStoreConfigProperties(maxDataChangeExecutorPoolSize,
-                maxDataChangeExecutorQueueSize, maxDataChangeListenerQueueSize,
-                DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE);
+    /**
+     * Returns a new {@link InMemoryDOMDataStoreConfigPropertiesBuilder}.
+     *
+     * @return a new {@link InMemoryDOMDataStoreConfigPropertiesBuilder}.
+     */
+    public static @NonNull InMemoryDOMDataStoreConfigPropertiesBuilder builder() {
+        return new InMemoryDOMDataStoreConfigPropertiesBuilder();
     }
 
     /**
-     * Returns the InMemoryDOMDataStoreConfigProperties instance with default values.
+     * Returns true if transaction allocation debugging should be enabled.
+     *
+     * @return true if transaction allocation debugging should be enabled.
      */
-    public static InMemoryDOMDataStoreConfigProperties getDefault() {
-        return DEFAULT;
+    @Value.Default
+    public boolean getDebugTransactions() {
+        return false;
     }
 
     /**
      * Returns the maximum queue size for the data change notification executor.
+     *
+     * @return the maximum queue size for the data change notification executor.
      */
+    @Value.Default
     public int getMaxDataChangeExecutorQueueSize() {
-        return maxDataChangeExecutorQueueSize;
+        return DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE;
     }
 
     /**
      * Returns the maximum thread pool size for the data change notification executor.
+     *
+     * @return the maximum thread pool size for the data change notification executor.
      */
+    @Value.Default
     public int getMaxDataChangeExecutorPoolSize() {
-        return maxDataChangeExecutorPoolSize;
+        return DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE;
     }
 
     /**
      * Returns the maximum queue size for the data change listeners.
      */
+    @Value.Default
     public int getMaxDataChangeListenerQueueSize() {
-        return maxDataChangeListenerQueueSize;
+        return DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE;
     }
 
     /**
      * Returns the maximum queue size for the data store executor.
      */
+    @Value.Default
     public int getMaxDataStoreExecutorQueueSize() {
-        return maxDataStoreExecutorQueueSize;
+        return DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE;
     }
 }
index 947137c43faff84549be83a2a1283ee0a740166b..c040b2ce82b9f979bed71f190af0042f0e095828 100644 (file)
@@ -8,7 +8,8 @@
 package org.opendaylight.mdsal.dom.store.inmemory;
 
 import java.util.concurrent.ExecutorService;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
 
@@ -17,62 +18,36 @@ import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
  *
  * @author Thomas Pantelis
  */
+@NonNullByDefault
 public final class InMemoryDOMDataStoreFactory {
 
     private InMemoryDOMDataStoreFactory() {
     }
 
-    public static InMemoryDOMDataStore create(final String name,
-            @Nullable final DOMSchemaService schemaService) {
-        return create(name, schemaService, null);
-    }
-
     /**
-     * Creates an InMemoryDOMDataStore instance.
+     * Creates an InMemoryDOMDataStore instance with default properties.
      *
      * @param name the name of the data store
      * @param schemaService the SchemaService to which to register the data store.
-     * @param properties configuration properties for the InMemoryDOMDataStore instance. If null,
-     *                   default property values are used.
      * @return an InMemoryDOMDataStore instance
      */
-    public static InMemoryDOMDataStore create(final String name,
-            @Nullable final DOMSchemaService schemaService,
-            @Nullable final InMemoryDOMDataStoreConfigProperties properties) {
-        return create(name, schemaService, false, properties);
+    public static InMemoryDOMDataStore create(final String name, final @Nullable DOMSchemaService schemaService) {
+        return create(name, InMemoryDOMDataStoreConfigProperties.getDefault(), schemaService);
     }
 
     /**
      * Creates an InMemoryDOMDataStore instance.
      *
      * @param name the name of the data store
+     * @param properties configuration properties for the InMemoryDOMDataStore instance.
      * @param schemaService the SchemaService to which to register the data store.
-     * @param debugTransactions enable transaction debugging
-     * @param properties configuration properties for the InMemoryDOMDataStore instance. If null,
-     *                   default property values are used.
      * @return an InMemoryDOMDataStore instance
      */
-    public static InMemoryDOMDataStore create(final String name,
-            @Nullable final DOMSchemaService schemaService, final boolean debugTransactions,
-            @Nullable final InMemoryDOMDataStoreConfigProperties properties) {
-
-        InMemoryDOMDataStoreConfigProperties actualProperties = properties;
-        if (actualProperties == null) {
-            actualProperties = InMemoryDOMDataStoreConfigProperties.getDefault();
-        }
-
-        // For DataChangeListener notifications we use an executor that provides the fastest
-        // task execution time to get higher throughput as DataChangeListeners typically provide
-        // much of the business logic for a data model. If the executor queue size limit is reached,
-        // subsequent submitted notifications will block the calling thread.
-        int dclExecutorMaxQueueSize = actualProperties.getMaxDataChangeExecutorQueueSize();
-        int dclExecutorMaxPoolSize = actualProperties.getMaxDataChangeExecutorPoolSize();
-
-        ExecutorService dataChangeListenerExecutor = SpecialExecutors.newBlockingBoundedFastThreadPool(
-                dclExecutorMaxPoolSize, dclExecutorMaxQueueSize, name + "-DCL", InMemoryDOMDataStore.class);
-
+    public static InMemoryDOMDataStore create(final String name, final InMemoryDOMDataStoreConfigProperties properties,
+            @Nullable final DOMSchemaService schemaService) {
+        final ExecutorService dataChangeListenerExecutor = createExecutorService(name, properties);
         final InMemoryDOMDataStore dataStore = new InMemoryDOMDataStore(name, dataChangeListenerExecutor,
-                actualProperties.getMaxDataChangeListenerQueueSize(), debugTransactions);
+            properties.getMaxDataChangeListenerQueueSize(), properties.getDebugTransactions());
 
         if (schemaService != null) {
             schemaService.registerSchemaContextListener(dataStore);
@@ -80,4 +55,15 @@ public final class InMemoryDOMDataStoreFactory {
 
         return dataStore;
     }
+
+    private static ExecutorService createExecutorService(final String name,
+            final InMemoryDOMDataStoreConfigProperties props) {
+        // For DataChangeListener notifications we use an executor that provides the fastest
+        // task execution time to get higher throughput as DataChangeListeners typically provide
+        // much of the business logic for a data model. If the executor queue size limit is reached,
+        // subsequent submitted notifications will block the calling thread.
+        return SpecialExecutors.newBlockingBoundedFastThreadPool(
+            props.getMaxDataChangeExecutorPoolSize(), props.getMaxDataChangeExecutorQueueSize(),
+            name + "-DCL", InMemoryDOMDataStore.class);
+    }
 }