Madan Jampani

Added asConsistentMap() method to AsyncConsistentMap for creating a new ConsistentMap instance.

Moved default implementation of ConsistentMap to core/api bundle

Change-Id: Idb7457a93247a007a9990dd82ed1dfd1eaf30010
1 +package org.onosproject.store.primitives;
2 +
1 /* 3 /*
2 - * Copyright 2015 Open Networking Laboratory 4 + * Copyright 2016 Open Networking Laboratory
3 * 5 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 7 * you may not use this file except in compliance with the License.
...@@ -13,9 +15,6 @@ ...@@ -13,9 +15,6 @@
13 * See the License for the specific language governing permissions and 15 * See the License for the specific language governing permissions and
14 * limitations under the License. 16 * limitations under the License.
15 */ 17 */
16 -
17 -package org.onosproject.store.primitives.impl;
18 -
19 import java.util.Collection; 18 import java.util.Collection;
20 import java.util.Map; 19 import java.util.Map;
21 import java.util.Map.Entry; 20 import java.util.Map.Entry;
...@@ -30,7 +29,6 @@ import java.util.function.Function; ...@@ -30,7 +29,6 @@ import java.util.function.Function;
30 import java.util.function.Predicate; 29 import java.util.function.Predicate;
31 30
32 import org.onlab.util.Tools; 31 import org.onlab.util.Tools;
33 -import org.onosproject.store.primitives.ConsistentMapBackedJavaMap;
34 import org.onosproject.store.service.AsyncConsistentMap; 32 import org.onosproject.store.service.AsyncConsistentMap;
35 import org.onosproject.store.service.ConsistentMap; 33 import org.onosproject.store.service.ConsistentMap;
36 import org.onosproject.store.service.ConsistentMapException; 34 import org.onosproject.store.service.ConsistentMapException;
...@@ -49,15 +47,15 @@ import com.google.common.base.Throwables; ...@@ -49,15 +47,15 @@ import com.google.common.base.Throwables;
49 */ 47 */
50 public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K, V>> implements ConsistentMap<K, V> { 48 public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K, V>> implements ConsistentMap<K, V> {
51 49
52 - private static final int OPERATION_TIMEOUT_MILLIS = 5000;
53 private static final int MAX_DELAY_BETWEEN_RETY_MILLS = 50; 50 private static final int MAX_DELAY_BETWEEN_RETY_MILLS = 50;
54 -
55 private final AsyncConsistentMap<K, V> asyncMap; 51 private final AsyncConsistentMap<K, V> asyncMap;
52 + private final long operationTimeoutMillis;
56 private Map<K, V> javaMap; 53 private Map<K, V> javaMap;
57 54
58 - public DefaultConsistentMap(AsyncConsistentMap<K, V> asyncMap) { 55 + public DefaultConsistentMap(AsyncConsistentMap<K, V> asyncMap, long operationTimeoutMillis) {
59 super(asyncMap); 56 super(asyncMap);
60 this.asyncMap = asyncMap; 57 this.asyncMap = asyncMap;
58 + this.operationTimeoutMillis = operationTimeoutMillis;
61 } 59 }
62 60
63 @Override 61 @Override
...@@ -203,9 +201,9 @@ public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K ...@@ -203,9 +201,9 @@ public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K
203 return asJavaMap().toString(); 201 return asJavaMap().toString();
204 } 202 }
205 203
206 - private static <T> T complete(CompletableFuture<T> future) { 204 + private <T> T complete(CompletableFuture<T> future) {
207 try { 205 try {
208 - return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 206 + return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
209 } catch (InterruptedException e) { 207 } catch (InterruptedException e) {
210 Thread.currentThread().interrupt(); 208 Thread.currentThread().interrupt();
211 throw new ConsistentMapException.Interrupted(); 209 throw new ConsistentMapException.Interrupted();
......
...@@ -25,6 +25,8 @@ import java.util.function.BiFunction; ...@@ -25,6 +25,8 @@ import java.util.function.BiFunction;
25 import java.util.function.Function; 25 import java.util.function.Function;
26 import java.util.function.Predicate; 26 import java.util.function.Predicate;
27 27
28 +import org.onosproject.store.primitives.DefaultConsistentMap;
29 +
28 /** 30 /**
29 * A distributed, strongly consistent map whose methods are all executed asynchronously. 31 * A distributed, strongly consistent map whose methods are all executed asynchronously.
30 * <p> 32 * <p>
...@@ -315,4 +317,14 @@ public interface AsyncConsistentMap<K, V> extends DistributedPrimitive { ...@@ -315,4 +317,14 @@ public interface AsyncConsistentMap<K, V> extends DistributedPrimitive {
315 * @return future that will be completed when the operation finishes 317 * @return future that will be completed when the operation finishes
316 */ 318 */
317 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener); 319 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener);
320 +
321 + /**
322 + * Returns a new {@link ConsistentMap} that is backed by this instance.
323 + *
324 + * @param timeoutMillis timeout duration for the returned ConsistentMap operations
325 + * @return new {@code ConsistentMap} instance
326 + */
327 + default ConsistentMap<K, V> asConsistentMap(long timeoutMillis) {
328 + return new DefaultConsistentMap<>(this, timeoutMillis);
329 + }
318 } 330 }
......
...@@ -41,6 +41,7 @@ public class DefaultConsistentMapBuilder<K, V> implements ConsistentMapBuilder<K ...@@ -41,6 +41,7 @@ public class DefaultConsistentMapBuilder<K, V> implements ConsistentMapBuilder<K
41 private boolean metering = true; 41 private boolean metering = true;
42 private boolean relaxedReadConsistency = false; 42 private boolean relaxedReadConsistency = false;
43 private final DatabaseManager manager; 43 private final DatabaseManager manager;
44 + private static final long DEFAULT_OPERATION_TIMEOUT_MILLIS = 5000L;
44 45
45 public DefaultConsistentMapBuilder(DatabaseManager manager) { 46 public DefaultConsistentMapBuilder(DatabaseManager manager) {
46 this.manager = manager; 47 this.manager = manager;
...@@ -107,7 +108,7 @@ public class DefaultConsistentMapBuilder<K, V> implements ConsistentMapBuilder<K ...@@ -107,7 +108,7 @@ public class DefaultConsistentMapBuilder<K, V> implements ConsistentMapBuilder<K
107 108
108 @Override 109 @Override
109 public ConsistentMap<K, V> build() { 110 public ConsistentMap<K, V> build() {
110 - return new DefaultConsistentMap<>(buildAndRegisterMap()); 111 + return buildAndRegisterMap().asConsistentMap(DEFAULT_OPERATION_TIMEOUT_MILLIS);
111 } 112 }
112 113
113 @Override 114 @Override
......