Madan Jampani
Committed by Gerrit Code Review

Added DistributedPrimitive interface

Added AsyncDistributedSet that provides async set operations

Change-Id: I83494075a7973694ea6b7445ff4799b7a1a50641
Showing 30 changed files with 661 additions and 141 deletions
1 -/* 1 +/*
2 - * Copyright 2015 Open Networking Laboratory 2 + * Copyright 2015 Open Networking Laboratory
3 - * 3 + *
4 - * Licensed under the Apache License, Version 2.0 (the "License"); 4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License. 5 + * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at 6 + * You may obtain a copy of the License at
7 - * 7 + *
8 - * http://www.apache.org/licenses/LICENSE-2.0 8 + * http://www.apache.org/licenses/LICENSE-2.0
9 - * 9 + *
10 - * Unless required by applicable law or agreed to in writing, software 10 + * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS, 11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and 13 + * See the License for the specific language governing permissions and
14 - * limitations under the License. 14 + * limitations under the License.
15 - */ 15 + */
16 -package org.onosproject.vtnrsc.util; 16 +package org.onosproject.vtnrsc.util;
17 - 17 +
18 -import java.util.Collection; 18 +import java.util.Collection;
19 -import java.util.Map; 19 +import java.util.Map;
20 -import java.util.Set; 20 +import java.util.Set;
21 -import java.util.function.BiFunction; 21 +import java.util.function.BiFunction;
22 - 22 +
23 -import org.onosproject.store.service.EventuallyConsistentMap; 23 +import org.onosproject.store.service.EventuallyConsistentMap;
24 -import org.onosproject.store.service.EventuallyConsistentMapListener; 24 +import org.onosproject.store.service.EventuallyConsistentMapListener;
25 - 25 +
26 -/** 26 +/**
27 - * Testing adapter for EventuallyConsistentMap. 27 + * Testing adapter for EventuallyConsistentMap.
28 - */ 28 + */
29 -public class VtnEventuallyConsistentMapAdapter<K, V> implements EventuallyConsistentMap<K, V> { 29 +public class VtnEventuallyConsistentMapAdapter<K, V> implements EventuallyConsistentMap<K, V> {
30 - @Override 30 +
31 - public int size() { 31 + @Override
32 - return 0; 32 + public String name() {
33 - } 33 + return null;
34 - 34 + }
35 - @Override 35 +
36 - public boolean isEmpty() { 36 + @Override
37 - return false; 37 + public int size() {
38 - } 38 + return 0;
39 - 39 + }
40 - @Override 40 +
41 - public boolean containsKey(K key) { 41 + @Override
42 - return false; 42 + public boolean isEmpty() {
43 - } 43 + return false;
44 - 44 + }
45 - @Override 45 +
46 - public boolean containsValue(V value) { 46 + @Override
47 - return false; 47 + public boolean containsKey(K key) {
48 - } 48 + return false;
49 - 49 + }
50 - @Override 50 +
51 - public V get(K key) { 51 + @Override
52 - return null; 52 + public boolean containsValue(V value) {
53 - } 53 + return false;
54 - 54 + }
55 - @Override 55 +
56 - public void put(K key, V value) { 56 + @Override
57 - 57 + public V get(K key) {
58 - } 58 + return null;
59 - 59 + }
60 - @Override 60 +
61 - public V remove(K key) { 61 + @Override
62 - return null; 62 + public void put(K key, V value) {
63 - } 63 +
64 - 64 + }
65 - @Override 65 +
66 - public void remove(K key, V value) { 66 + @Override
67 - 67 + public V remove(K key) {
68 - } 68 + return null;
69 - 69 + }
70 - @Override 70 +
71 - public V compute(K key, BiFunction<K, V, V> recomputeFunction) { 71 + @Override
72 - return null; 72 + public void remove(K key, V value) {
73 - } 73 +
74 - 74 + }
75 - @Override 75 +
76 - public void putAll(Map<? extends K, ? extends V> m) { 76 + @Override
77 - 77 + public V compute(K key, BiFunction<K, V, V> recomputeFunction) {
78 - } 78 + return null;
79 - 79 + }
80 - @Override 80 +
81 - public void clear() { 81 + @Override
82 - 82 + public void putAll(Map<? extends K, ? extends V> m) {
83 - } 83 +
84 - 84 + }
85 - @Override 85 +
86 - public Set<K> keySet() { 86 + @Override
87 - return null; 87 + public void clear() {
88 - } 88 +
89 - 89 + }
90 - @Override 90 +
91 - public Collection<V> values() { 91 + @Override
92 - return null; 92 + public Set<K> keySet() {
93 - } 93 + return null;
94 - 94 + }
95 - @Override 95 +
96 - public Set<Map.Entry<K, V>> entrySet() { 96 + @Override
97 - return null; 97 + public Collection<V> values() {
98 - } 98 + return null;
99 - 99 + }
100 - @Override 100 +
101 - public void addListener(EventuallyConsistentMapListener<K, V> listener) { 101 + @Override
102 - 102 + public Set<Map.Entry<K, V>> entrySet() {
103 - } 103 + return null;
104 - 104 + }
105 - @Override 105 +
106 - public void removeListener(EventuallyConsistentMapListener<K, V> listener) { 106 + @Override
107 - 107 + public void addListener(EventuallyConsistentMapListener<K, V> listener) {
108 - } 108 +
109 - 109 + }
110 - @Override 110 +
111 - public void destroy() { 111 + @Override
112 - 112 + public void removeListener(EventuallyConsistentMapListener<K, V> listener) {
113 - } 113 +
114 -} 114 + }
115 +
116 + @Override
117 + public void destroy() {
118 +
119 + }
120 +}
......
...@@ -20,7 +20,12 @@ import java.util.concurrent.CompletableFuture; ...@@ -20,7 +20,12 @@ import java.util.concurrent.CompletableFuture;
20 /** 20 /**
21 * An async atomic counter dispenses monotonically increasing values. 21 * An async atomic counter dispenses monotonically increasing values.
22 */ 22 */
23 -public interface AsyncAtomicCounter { 23 +public interface AsyncAtomicCounter extends DistributedPrimitive {
24 +
25 + @Override
26 + default DistributedPrimitive.Type type() {
27 + return DistributedPrimitive.Type.COUNTER;
28 + }
24 29
25 /** 30 /**
26 * Atomically increment by one the current value. 31 * Atomically increment by one the current value.
......
...@@ -27,7 +27,12 @@ import java.util.concurrent.CompletableFuture; ...@@ -27,7 +27,12 @@ import java.util.concurrent.CompletableFuture;
27 * 27 *
28 * @param <V> value type 28 * @param <V> value type
29 */ 29 */
30 -public interface AsyncAtomicValue<V> { 30 +public interface AsyncAtomicValue<V> extends DistributedPrimitive {
31 +
32 + @Override
33 + default DistributedPrimitive.Type type() {
34 + return DistributedPrimitive.Type.VALUE;
35 + }
31 36
32 /** 37 /**
33 * Atomically sets the value to the given updated value if the current value is equal to the expected value. 38 * Atomically sets the value to the given updated value if the current value is equal to the expected value.
......
...@@ -49,7 +49,12 @@ import java.util.function.Predicate; ...@@ -49,7 +49,12 @@ import java.util.function.Predicate;
49 * the returned future will be {@link CompletableFuture#complete completed} when the 49 * the returned future will be {@link CompletableFuture#complete completed} when the
50 * operation finishes. 50 * operation finishes.
51 */ 51 */
52 -public interface AsyncConsistentMap<K, V> { 52 +public interface AsyncConsistentMap<K, V> extends DistributedPrimitive {
53 +
54 + @Override
55 + default DistributedPrimitive.Type type() {
56 + return DistributedPrimitive.Type.CONSISTENT_MAP;
57 + }
53 58
54 /** 59 /**
55 * Returns the number of entries in the map. 60 * Returns the number of entries in the map.
......
1 +/*
2 + * Copyright 2015-2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.service;
17 +
18 +import java.util.Collection;
19 +import java.util.Set;
20 +import java.util.concurrent.CompletableFuture;
21 +
22 +/**
23 + * A distributed collection designed for holding unique elements.
24 + * <p>
25 + * All methods of {@code AsyncDistributedSet} immediately return a {@link CompletableFuture future}.
26 + * The returned future will be {@link CompletableFuture#complete completed} when the operation
27 + * completes.
28 + *
29 + * @param <E> set entry type
30 + */
31 +public interface AsyncDistributedSet<E> extends DistributedPrimitive {
32 +
33 + @Override
34 + default DistributedPrimitive.Type type() {
35 + return DistributedPrimitive.Type.SET;
36 + }
37 +
38 + /**
39 + * Registers the specified listener to be notified whenever
40 + * the set is updated.
41 + *
42 + * @param listener listener to notify about set update events
43 + */
44 + CompletableFuture<Void> addListener(SetEventListener<E> listener);
45 +
46 + /**
47 + * Unregisters the specified listener.
48 + *
49 + * @param listener listener to unregister.
50 + * @return CompletableFuture that is completed when the operation completes
51 + */
52 + CompletableFuture<Void> removeListener(SetEventListener<E> listener);
53 +
54 + /**
55 + * Adds the specified element to this set if it is not already present (optional operation).
56 + * @param element element to add
57 + * @return {@code true} if this set did not already contain the specified element.
58 + */
59 + CompletableFuture<Boolean> add(E element);
60 +
61 + /**
62 + * Removes the specified element to this set if it is present (optional operation).
63 + * @param element element to remove
64 + * @return {@code true} if this set contained the specified element
65 + */
66 + CompletableFuture<Boolean> remove(E element);
67 +
68 + /**
69 + * Returns the number of elements in the set.
70 + * @return size of the set
71 + */
72 + CompletableFuture<Integer> size();
73 +
74 + /**
75 + * Returns if the set is empty.
76 + * @return {@code true} if this set is empty
77 + */
78 + CompletableFuture<Boolean> isEmpty();
79 +
80 + /**
81 + * Removes all elements from the set.
82 + */
83 + CompletableFuture<Void> clear();
84 +
85 + /**
86 + * Returns if this set contains the specified element.
87 + * @param element element to check
88 + * @return {@code true} if this set contains the specified element
89 + */
90 + CompletableFuture<Boolean> contains(E element);
91 +
92 + /**
93 + * Adds all of the elements in the specified collection to this set if they're not
94 + * already present (optional operation).
95 + * @param c collection containing elements to be added to this set
96 + * @return {@code true} if this set contains all elements in the collection
97 + */
98 + CompletableFuture<Boolean> addAll(Collection<? extends E> c);
99 +
100 + /**
101 + * Returns if this set contains all the elements in specified collection.
102 + * @param c collection
103 + * @return {@code true} if this set contains all elements in the collection
104 + */
105 + CompletableFuture<Boolean> containsAll(Collection<? extends E> c);
106 +
107 + /**
108 + * Retains only the elements in this set that are contained in the specified collection (optional operation).
109 + * @param c collection containing elements to be retained in this set
110 + * @return {@code true} if this set changed as a result of the call
111 + */
112 + CompletableFuture<Boolean> retainAll(Collection<? extends E> c);
113 +
114 + /**
115 + * Removes from this set all of its elements that are contained in the specified collection (optional operation).
116 + * If the specified collection is also a set, this operation effectively modifies this set so that its
117 + * value is the asymmetric set difference of the two sets.
118 + * @param c collection containing elements to be removed from this set
119 + * @return {@code true} if this set changed as a result of the call
120 + */
121 + CompletableFuture<Boolean> removeAll(Collection<? extends E> c);
122 +
123 + /**
124 + * Returns the entries as a immutable set. The returned set is a snapshot and will not reflect new changes made to
125 + * this AsyncDistributedSet
126 + * @return immutable set copy
127 + */
128 + CompletableFuture<? extends Set<E>> getAsImmutableSet();
129 +}
...@@ -18,7 +18,12 @@ package org.onosproject.store.service; ...@@ -18,7 +18,12 @@ package org.onosproject.store.service;
18 /** 18 /**
19 * Distributed version of java.util.concurrent.atomic.AtomicLong. 19 * Distributed version of java.util.concurrent.atomic.AtomicLong.
20 */ 20 */
21 -public interface AtomicCounter { 21 +public interface AtomicCounter extends DistributedPrimitive {
22 +
23 + @Override
24 + default DistributedPrimitive.Type type() {
25 + return DistributedPrimitive.Type.COUNTER;
26 + }
22 27
23 /** 28 /**
24 * Atomically increment by one the current value. 29 * Atomically increment by one the current value.
......
...@@ -20,7 +20,7 @@ package org.onosproject.store.service; ...@@ -20,7 +20,7 @@ package org.onosproject.store.service;
20 * 20 *
21 * @param <V> value type 21 * @param <V> value type
22 */ 22 */
23 -public interface AtomicValue<V> { 23 +public interface AtomicValue<V> extends DistributedPrimitive {
24 24
25 /** 25 /**
26 * Atomically sets the value to the given updated value if the current value is equal to the expected value. 26 * Atomically sets the value to the given updated value if the current value is equal to the expected value.
......
...@@ -44,7 +44,7 @@ import java.util.function.Predicate; ...@@ -44,7 +44,7 @@ import java.util.function.Predicate;
44 * (which extends RuntimeException) to indicate failures. 44 * (which extends RuntimeException) to indicate failures.
45 * 45 *
46 */ 46 */
47 -public interface ConsistentMap<K, V> { 47 +public interface ConsistentMap<K, V> extends DistributedPrimitive {
48 48
49 /** 49 /**
50 * Returns the number of entries in the map. 50 * Returns the number of entries in the map.
......
1 +/*
2 + * Copyright 2015-2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.service;
17 +
18 +import org.onosproject.core.ApplicationId;
19 +
20 +/**
21 + * Interface for all distributed primitives.
22 + */
23 +public interface DistributedPrimitive {
24 +
25 + /**
26 + * Type of distributed primitive.
27 + */
28 + public enum Type {
29 + /**
30 + * Map with strong consistency semantics.
31 + */
32 + CONSISTENT_MAP,
33 +
34 + /**
35 + * Map with eventual consistency semantics.
36 + */
37 + EVENTUALLY_CONSISTENT_MAP,
38 +
39 + /**
40 + * distributed set.
41 + */
42 + SET,
43 +
44 + /**
45 + * atomic counter.
46 + */
47 + COUNTER,
48 +
49 + /**
50 + * Atomic value.
51 + */
52 + VALUE,
53 +
54 + /**
55 + * Distributed queue.
56 + */
57 + QUEUE
58 + }
59 +
60 + /**
61 + * Returns the name of this primitive.
62 + * @return name
63 + */
64 + String name();
65 +
66 + /**
67 + * Returns the type of primitive.
68 + * @return primitive type
69 + */
70 + Type type();
71 +
72 + /**
73 + * Returns the application owning this primitive.
74 + */
75 + default ApplicationId applicationId() {
76 + return null;
77 + }
78 +}
...@@ -24,7 +24,7 @@ import java.util.concurrent.CompletableFuture; ...@@ -24,7 +24,7 @@ import java.util.concurrent.CompletableFuture;
24 * 24 *
25 * @param <E> queue entry type 25 * @param <E> queue entry type
26 */ 26 */
27 -public interface DistributedQueue<E> { 27 +public interface DistributedQueue<E> extends DistributedPrimitive {
28 28
29 /** 29 /**
30 * Returns total number of entries in the queue. 30 * Returns total number of entries in the queue.
......
...@@ -22,7 +22,7 @@ import java.util.Set; ...@@ -22,7 +22,7 @@ import java.util.Set;
22 * 22 *
23 * @param <E> set entry type 23 * @param <E> set entry type
24 */ 24 */
25 -public interface DistributedSet<E> extends Set<E> { 25 +public interface DistributedSet<E> extends Set<E>, DistributedPrimitive {
26 26
27 /** 27 /**
28 * Registers the specified listener to be notified whenever 28 * Registers the specified listener to be notified whenever
......
...@@ -129,4 +129,13 @@ public interface DistributedSetBuilder<E> { ...@@ -129,4 +129,13 @@ public interface DistributedSetBuilder<E> {
129 * @throws java.lang.RuntimeException if a mandatory parameter is missing 129 * @throws java.lang.RuntimeException if a mandatory parameter is missing
130 */ 130 */
131 DistributedSet<E> build(); 131 DistributedSet<E> build();
132 +
133 + /**
134 + * Builds an {@link AsyncDistributedSet async set} based on the configuration options
135 + * supplied to this builder.
136 + *
137 + * @return new AsyncDistributedSet
138 + * @throws java.lang.RuntimeException if a mandatory parameter is missing
139 + */
140 + AsyncDistributedSet<E> buildAsyncSet();
132 } 141 }
......
...@@ -39,7 +39,12 @@ import java.util.function.BiFunction; ...@@ -39,7 +39,12 @@ import java.util.function.BiFunction;
39 * Null values are not allowed in this map. 39 * Null values are not allowed in this map.
40 * </p> 40 * </p>
41 */ 41 */
42 -public interface EventuallyConsistentMap<K, V> { 42 +public interface EventuallyConsistentMap<K, V> extends DistributedPrimitive {
43 +
44 + @Override
45 + default DistributedPrimitive.Type type() {
46 + return DistributedPrimitive.Type.EVENTUALLY_CONSISTENT_MAP;
47 + }
43 48
44 /** 49 /**
45 * Returns the number of key-value mappings in this map. 50 * Returns the number of key-value mappings in this map.
......
1 +/*
2 + * Copyright 2015-2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.service;
17 +
18 +/**
19 + * DistributedPrimitive that is a synchronous (blocking) version of
20 + * another.
21 + *
22 + * @param <T> type of DistributedPrimitive
23 + */
24 +public abstract class Synchronous<T extends DistributedPrimitive> implements DistributedPrimitive {
25 +
26 + private final T primitive;
27 +
28 + public Synchronous(T primitive) {
29 + this.primitive = primitive;
30 + }
31 +
32 + @Override
33 + public String name() {
34 + return primitive.name();
35 + }
36 +
37 + @Override
38 + public Type type() {
39 + return primitive.type();
40 + }
41 +}
...@@ -26,6 +26,17 @@ import java.util.function.Predicate; ...@@ -26,6 +26,17 @@ import java.util.function.Predicate;
26 * Testing adapter for the consistent map. 26 * Testing adapter for the consistent map.
27 */ 27 */
28 public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> { 28 public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> {
29 +
30 + @Override
31 + public String name() {
32 + return null;
33 + }
34 +
35 + @Override
36 + public DistributedPrimitive.Type type() {
37 + return DistributedPrimitive.Type.CONSISTENT_MAP;
38 + }
39 +
29 @Override 40 @Override
30 public int size() { 41 public int size() {
31 return 0; 42 return 0;
......
...@@ -20,10 +20,23 @@ import java.util.Map; ...@@ -20,10 +20,23 @@ import java.util.Map;
20 import java.util.Set; 20 import java.util.Set;
21 import java.util.function.BiFunction; 21 import java.util.function.BiFunction;
22 22
23 +import org.onosproject.store.service.DistributedPrimitive.Type;
24 +
23 /** 25 /**
24 * Testing adapter for EventuallyConsistentMap. 26 * Testing adapter for EventuallyConsistentMap.
25 */ 27 */
26 public class EventuallyConsistentMapAdapter<K, V> implements EventuallyConsistentMap<K, V> { 28 public class EventuallyConsistentMapAdapter<K, V> implements EventuallyConsistentMap<K, V> {
29 +
30 + @Override
31 + public String name() {
32 + return null;
33 + }
34 +
35 + @Override
36 + public Type type() {
37 + return Type.EVENTUALLY_CONSISTENT_MAP;
38 + }
39 +
27 @Override 40 @Override
28 public int size() { 41 public int size() {
29 return 0; 42 return 0;
......
...@@ -23,6 +23,16 @@ import java.util.concurrent.atomic.AtomicLong; ...@@ -23,6 +23,16 @@ import java.util.concurrent.atomic.AtomicLong;
23 public final class TestAtomicCounter implements AtomicCounter { 23 public final class TestAtomicCounter implements AtomicCounter {
24 final AtomicLong value; 24 final AtomicLong value;
25 25
26 + @Override
27 + public String name() {
28 + return null;
29 + }
30 +
31 + @Override
32 + public Type type() {
33 + return Type.COUNTER;
34 + }
35 +
26 private TestAtomicCounter() { 36 private TestAtomicCounter() {
27 value = new AtomicLong(); 37 value = new AtomicLong();
28 } 38 }
......
...@@ -28,7 +28,7 @@ import java.util.function.Predicate; ...@@ -28,7 +28,7 @@ import java.util.function.Predicate;
28 import java.util.stream.Collectors; 28 import java.util.stream.Collectors;
29 29
30 import org.onosproject.core.ApplicationId; 30 import org.onosproject.core.ApplicationId;
31 -import static org.onosproject.store.service.MapEvent.Type; 31 +
32 import static org.onosproject.store.service.MapEvent.Type.*; 32 import static org.onosproject.store.service.MapEvent.Type.*;
33 33
34 /** 34 /**
...@@ -37,7 +37,7 @@ import static org.onosproject.store.service.MapEvent.Type.*; ...@@ -37,7 +37,7 @@ import static org.onosproject.store.service.MapEvent.Type.*;
37 public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> { 37 public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
38 38
39 private final List<MapEventListener<K, V>> listeners; 39 private final List<MapEventListener<K, V>> listeners;
40 - private final HashMap<K, V> map; 40 + private final Map<K, V> map;
41 private final String mapName; 41 private final String mapName;
42 private final AtomicLong counter = new AtomicLong(0); 42 private final AtomicLong counter = new AtomicLong(0);
43 43
...@@ -54,7 +54,7 @@ public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> { ...@@ -54,7 +54,7 @@ public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
54 /** 54 /**
55 * Notify all listeners of an event. 55 * Notify all listeners of an event.
56 */ 56 */
57 - private void notifyListeners(String mapName, Type type, 57 + private void notifyListeners(String mapName, MapEvent.Type type,
58 K key, Versioned<V> value) { 58 K key, Versioned<V> value) {
59 MapEvent<K, V> event = new MapEvent<>(mapName, type, key, value); 59 MapEvent<K, V> event = new MapEvent<>(mapName, type, key, value);
60 listeners.forEach( 60 listeners.forEach(
......
...@@ -51,6 +51,11 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter { ...@@ -51,6 +51,11 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
51 } 51 }
52 52
53 @Override 53 @Override
54 + public String name() {
55 + return name;
56 + }
57 +
58 + @Override
54 public CompletableFuture<Long> incrementAndGet() { 59 public CompletableFuture<Long> incrementAndGet() {
55 final MeteringAgent.Context timer = monitor.startTimer(INCREMENT_AND_GET); 60 final MeteringAgent.Context timer = monitor.startTimer(INCREMENT_AND_GET);
56 return addAndGet(1L) 61 return addAndGet(1L)
......
...@@ -55,6 +55,11 @@ public class DefaultAsyncAtomicValue<V> implements AsyncAtomicValue<V> { ...@@ -55,6 +55,11 @@ public class DefaultAsyncAtomicValue<V> implements AsyncAtomicValue<V> {
55 } 55 }
56 56
57 @Override 57 @Override
58 + public String name() {
59 + return name;
60 + }
61 +
62 + @Override
58 public CompletableFuture<Boolean> compareAndSet(V expect, V update) { 63 public CompletableFuture<Boolean> compareAndSet(V expect, V update) {
59 final MeteringAgent.Context newTimer = monitor.startTimer(COMPARE_AND_SET); 64 final MeteringAgent.Context newTimer = monitor.startTimer(COMPARE_AND_SET);
60 CompletableFuture<Boolean> response; 65 CompletableFuture<Boolean> response;
......
...@@ -171,6 +171,7 @@ public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V ...@@ -171,6 +171,7 @@ public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V
171 * Returns this map name. 171 * Returns this map name.
172 * @return map name 172 * @return map name
173 */ 173 */
174 + @Override
174 public String name() { 175 public String name() {
175 return name; 176 return name;
176 } 177 }
...@@ -187,6 +188,7 @@ public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V ...@@ -187,6 +188,7 @@ public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V
187 * Returns the applicationId owning this map. 188 * Returns the applicationId owning this map.
188 * @return application Id 189 * @return application Id
189 */ 190 */
191 + @Override
190 public ApplicationId applicationId() { 192 public ApplicationId applicationId() {
191 return applicationId; 193 return applicationId;
192 } 194 }
......
1 +/*
2 + * Copyright 2015-2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.consistent.impl;
17 +
18 +import java.util.Collection;
19 +import java.util.Map;
20 +import java.util.Objects;
21 +import java.util.Set;
22 +import java.util.concurrent.CompletableFuture;
23 +import java.util.stream.Collectors;
24 +
25 +import org.onlab.util.Tools;
26 +import org.onosproject.store.service.AsyncConsistentMap;
27 +import org.onosproject.store.service.AsyncDistributedSet;
28 +import org.onosproject.store.service.MapEvent;
29 +import org.onosproject.store.service.MapEventListener;
30 +import org.onosproject.store.service.SetEvent;
31 +import org.onosproject.store.service.SetEventListener;
32 +
33 +import com.google.common.collect.ImmutableSet;
34 +import com.google.common.collect.Maps;
35 +import com.google.common.collect.Sets;
36 +
37 +/**
38 + * Implementation of {@link AsyncDistributedSet}.
39 + *
40 + * @param <E> set entry type
41 + */
42 +public class DefaultAsyncDistributedSet<E> implements AsyncDistributedSet<E> {
43 +
44 + private static final String CONTAINS = "contains";
45 + private static final String PRIMITIVE_NAME = "distributedSet";
46 + private static final String SIZE = "size";
47 + private static final String IS_EMPTY = "isEmpty";
48 + private static final String ADD = "add";
49 + private static final String REMOVE = "remove";
50 + private static final String CONTAINS_ALL = "containsAll";
51 + private static final String ADD_ALL = "addAll";
52 + private static final String RETAIN_ALL = "retainAll";
53 + private static final String REMOVE_ALL = "removeAll";
54 + private static final String CLEAR = "clear";
55 + private static final String GET_AS_IMMUTABLE_SET = "getAsImmutableSet";
56 +
57 + private final String name;
58 + private final AsyncConsistentMap<E, Boolean> backingMap;
59 + private final Map<SetEventListener<E>, MapEventListener<E, Boolean>> listenerMapping = Maps.newIdentityHashMap();
60 + private final MeteringAgent monitor;
61 +
62 + public DefaultAsyncDistributedSet(AsyncConsistentMap<E, Boolean> backingMap, String name, boolean meteringEnabled) {
63 + this.backingMap = backingMap;
64 + this.name = name;
65 + monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled);
66 + }
67 +
68 + @Override
69 + public String name() {
70 + return name;
71 + }
72 +
73 + @Override
74 + public CompletableFuture<Integer> size() {
75 + final MeteringAgent.Context timer = monitor.startTimer(SIZE);
76 + return backingMap.size().whenComplete((r, e) -> timer.stop(null));
77 + }
78 +
79 + @Override
80 + public CompletableFuture<Boolean> isEmpty() {
81 + final MeteringAgent.Context timer = monitor.startTimer(IS_EMPTY);
82 + return backingMap.isEmpty().whenComplete((r, e) -> timer.stop(null));
83 + }
84 +
85 + @Override
86 + public CompletableFuture<Boolean> contains(E element) {
87 + final MeteringAgent.Context timer = monitor.startTimer(CONTAINS);
88 + return backingMap.containsKey(element).whenComplete((r, e) -> timer.stop(null));
89 + }
90 +
91 + @Override
92 + public CompletableFuture<Boolean> add(E entry) {
93 + final MeteringAgent.Context timer = monitor.startTimer(ADD);
94 + return backingMap.putIfAbsent(entry, true).thenApply(Objects::isNull).whenComplete((r, e) -> timer.stop(null));
95 + }
96 +
97 + @Override
98 + public CompletableFuture<Boolean> remove(E entry) {
99 + final MeteringAgent.Context timer = monitor.startTimer(REMOVE);
100 + return backingMap.remove(entry, true).whenComplete((r, e) -> timer.stop(null));
101 + }
102 +
103 + @Override
104 + public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
105 + final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_ALL);
106 + return Tools.allOf(c.stream().map(this::contains).collect(Collectors.toList())).thenApply(v ->
107 + v.stream().reduce(Boolean::logicalAnd).orElse(true)).whenComplete((r, e) -> timer.stop(null));
108 + }
109 +
110 + @Override
111 + public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
112 + final MeteringAgent.Context timer = monitor.startTimer(ADD_ALL);
113 + return Tools.allOf(c.stream().map(this::add).collect(Collectors.toList())).thenApply(v ->
114 + v.stream().reduce(Boolean::logicalOr).orElse(false)).whenComplete((r, e) -> timer.stop(null));
115 + }
116 +
117 + @Override
118 + public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
119 + final MeteringAgent.Context timer = monitor.startTimer(RETAIN_ALL);
120 + return backingMap.keySet().thenApply(set -> Sets.difference(set, Sets.newHashSet(c)))
121 + .thenCompose(this::removeAll)
122 + .whenComplete((r, e) -> timer.stop(null));
123 + }
124 +
125 + @Override
126 + public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
127 + final MeteringAgent.Context timer = monitor.startTimer(REMOVE_ALL);
128 + return Tools.allOf(c.stream().map(this::remove).collect(Collectors.toList())).thenApply(v ->
129 + v.stream().reduce(Boolean::logicalOr).orElse(false)).whenComplete((r, e) -> timer.stop(null));
130 + }
131 +
132 + @Override
133 + public CompletableFuture<Void> clear() {
134 + final MeteringAgent.Context timer = monitor.startTimer(CLEAR);
135 + return backingMap.clear().whenComplete((r, e) -> timer.stop(null));
136 + }
137 +
138 + @Override
139 + public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
140 + final MeteringAgent.Context timer = monitor.startTimer(GET_AS_IMMUTABLE_SET);
141 + return backingMap.keySet().thenApply(s -> ImmutableSet.copyOf(s)).whenComplete((r, e) -> timer.stop(null));
142 + }
143 +
144 + @Override
145 + public CompletableFuture<Void> addListener(SetEventListener<E> listener) {
146 + MapEventListener<E, Boolean> mapEventListener = mapEvent -> {
147 + if (mapEvent.type() == MapEvent.Type.INSERT) {
148 + listener.event(new SetEvent<>(name, SetEvent.Type.ADD, mapEvent.key()));
149 + } else if (mapEvent.type() == MapEvent.Type.REMOVE) {
150 + listener.event(new SetEvent<>(name, SetEvent.Type.REMOVE, mapEvent.key()));
151 + }
152 + };
153 + if (listenerMapping.putIfAbsent(listener, mapEventListener) == null) {
154 + return backingMap.addListener(mapEventListener);
155 + }
156 + return CompletableFuture.completedFuture(null);
157 + }
158 +
159 + @Override
160 + public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
161 + MapEventListener<E, Boolean> mapEventListener = listenerMapping.remove(listener);
162 + if (mapEventListener != null) {
163 + return backingMap.removeListener(mapEventListener);
164 + }
165 + return CompletableFuture.completedFuture(null);
166 + }
167 +}
...@@ -18,6 +18,7 @@ package org.onosproject.store.consistent.impl; ...@@ -18,6 +18,7 @@ package org.onosproject.store.consistent.impl;
18 import org.onosproject.store.service.AsyncAtomicCounter; 18 import org.onosproject.store.service.AsyncAtomicCounter;
19 import org.onosproject.store.service.AtomicCounter; 19 import org.onosproject.store.service.AtomicCounter;
20 import org.onosproject.store.service.StorageException; 20 import org.onosproject.store.service.StorageException;
21 +import org.onosproject.store.service.Synchronous;
21 22
22 import java.util.concurrent.CompletableFuture; 23 import java.util.concurrent.CompletableFuture;
23 import java.util.concurrent.ExecutionException; 24 import java.util.concurrent.ExecutionException;
...@@ -30,16 +31,15 @@ import java.util.concurrent.TimeoutException; ...@@ -30,16 +31,15 @@ import java.util.concurrent.TimeoutException;
30 * <p> 31 * <p>
31 * The initial value will be zero. 32 * The initial value will be zero.
32 */ 33 */
33 -public class DefaultAtomicCounter implements AtomicCounter { 34 +public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implements AtomicCounter {
34 35
35 private static final int OPERATION_TIMEOUT_MILLIS = 5000; 36 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
36 37
37 private final AsyncAtomicCounter asyncCounter; 38 private final AsyncAtomicCounter asyncCounter;
38 39
39 - public DefaultAtomicCounter(String name, 40 + public DefaultAtomicCounter(AsyncAtomicCounter asyncCounter) {
40 - Database database, 41 + super(asyncCounter);
41 - boolean meteringEnabled) { 42 + this.asyncCounter = asyncCounter;
42 - asyncCounter = new DefaultAsyncAtomicCounter(name, database, meteringEnabled);
43 } 43 }
44 44
45 @Override 45 @Override
......
...@@ -53,9 +53,7 @@ public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder { ...@@ -53,9 +53,7 @@ public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder {
53 53
54 @Override 54 @Override
55 public AtomicCounter build() { 55 public AtomicCounter build() {
56 - validateInputs(); 56 + return new DefaultAtomicCounter(buildAsyncCounter());
57 - Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase;
58 - return new DefaultAtomicCounter(name, database, metering);
59 } 57 }
60 58
61 @Override 59 @Override
......
...@@ -17,10 +17,12 @@ package org.onosproject.store.consistent.impl; ...@@ -17,10 +17,12 @@ package org.onosproject.store.consistent.impl;
17 17
18 import java.util.concurrent.CompletableFuture; 18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.TimeUnit; 19 import java.util.concurrent.TimeUnit;
20 +
20 import org.onosproject.store.service.AsyncAtomicValue; 21 import org.onosproject.store.service.AsyncAtomicValue;
21 import org.onosproject.store.service.AtomicValue; 22 import org.onosproject.store.service.AtomicValue;
22 import org.onosproject.store.service.AtomicValueEventListener; 23 import org.onosproject.store.service.AtomicValueEventListener;
23 import org.onosproject.store.service.StorageException; 24 import org.onosproject.store.service.StorageException;
25 +import org.onosproject.store.service.Synchronous;
24 26
25 import com.google.common.util.concurrent.Futures; 27 import com.google.common.util.concurrent.Futures;
26 28
...@@ -29,12 +31,13 @@ import com.google.common.util.concurrent.Futures; ...@@ -29,12 +31,13 @@ import com.google.common.util.concurrent.Futures;
29 * 31 *
30 * @param <V> value type 32 * @param <V> value type
31 */ 33 */
32 -public class DefaultAtomicValue<V> implements AtomicValue<V> { 34 +public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> implements AtomicValue<V> {
33 35
34 private static final int OPERATION_TIMEOUT_MILLIS = 5000; 36 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
35 private final AsyncAtomicValue<V> asyncValue; 37 private final AsyncAtomicValue<V> asyncValue;
36 38
37 public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue) { 39 public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue) {
40 + super(asyncValue);
38 this.asyncValue = asyncValue; 41 this.asyncValue = asyncValue;
39 } 42 }
40 43
......
...@@ -28,9 +28,11 @@ import java.util.function.Function; ...@@ -28,9 +28,11 @@ import java.util.function.Function;
28 import java.util.function.Predicate; 28 import java.util.function.Predicate;
29 import java.util.Set; 29 import java.util.Set;
30 30
31 +import org.onosproject.store.service.AsyncConsistentMap;
31 import org.onosproject.store.service.ConsistentMap; 32 import org.onosproject.store.service.ConsistentMap;
32 import org.onosproject.store.service.ConsistentMapException; 33 import org.onosproject.store.service.ConsistentMapException;
33 import org.onosproject.store.service.MapEventListener; 34 import org.onosproject.store.service.MapEventListener;
35 +import org.onosproject.store.service.Synchronous;
34 import org.onosproject.store.service.Versioned; 36 import org.onosproject.store.service.Versioned;
35 37
36 /** 38 /**
...@@ -40,18 +42,15 @@ import org.onosproject.store.service.Versioned; ...@@ -40,18 +42,15 @@ import org.onosproject.store.service.Versioned;
40 * @param <K> type of key. 42 * @param <K> type of key.
41 * @param <V> type of value. 43 * @param <V> type of value.
42 */ 44 */
43 -public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> { 45 +public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K, V>> implements ConsistentMap<K, V> {
44 46
45 private static final int OPERATION_TIMEOUT_MILLIS = 5000; 47 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
46 48
47 private final DefaultAsyncConsistentMap<K, V> asyncMap; 49 private final DefaultAsyncConsistentMap<K, V> asyncMap;
48 private Map<K, V> javaMap; 50 private Map<K, V> javaMap;
49 51
50 - public String name() {
51 - return asyncMap.name();
52 - }
53 -
54 public DefaultConsistentMap(DefaultAsyncConsistentMap<K, V> asyncMap) { 52 public DefaultConsistentMap(DefaultAsyncConsistentMap<K, V> asyncMap) {
53 + super(asyncMap);
55 this.asyncMap = asyncMap; 54 this.asyncMap = asyncMap;
56 } 55 }
57 56
......
...@@ -19,12 +19,14 @@ import com.google.common.collect.Sets; ...@@ -19,12 +19,14 @@ import com.google.common.collect.Sets;
19 import com.google.common.util.concurrent.Futures; 19 import com.google.common.util.concurrent.Futures;
20 20
21 import org.onlab.util.SharedExecutors; 21 import org.onlab.util.SharedExecutors;
22 +import org.onosproject.store.service.DistributedPrimitive;
22 import org.onosproject.store.service.DistributedQueue; 23 import org.onosproject.store.service.DistributedQueue;
23 import org.onosproject.store.service.Serializer; 24 import org.onosproject.store.service.Serializer;
24 25
25 import java.util.List; 26 import java.util.List;
26 import java.util.Set; 27 import java.util.Set;
27 import java.util.concurrent.CompletableFuture; 28 import java.util.concurrent.CompletableFuture;
29 +
28 import static com.google.common.base.Preconditions.checkNotNull; 30 import static com.google.common.base.Preconditions.checkNotNull;
29 import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.QUEUE_PUSH; 31 import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.QUEUE_PUSH;
30 32
...@@ -108,10 +110,16 @@ public class DefaultDistributedQueue<E> implements DistributedQueue<E> { ...@@ -108,10 +110,16 @@ public class DefaultDistributedQueue<E> implements DistributedQueue<E> {
108 .whenComplete((r, e) -> timer.stop(e))); 110 .whenComplete((r, e) -> timer.stop(e)));
109 } 111 }
110 112
113 + @Override
111 public String name() { 114 public String name() {
112 return name; 115 return name;
113 } 116 }
114 117
118 + @Override
119 + public DistributedPrimitive.Type type() {
120 + return DistributedPrimitive.Type.QUEUE;
121 + }
122 +
115 protected void tryPoll() { 123 protected void tryPoll() {
116 Set<CompletableFuture<E>> completedFutures = Sets.newHashSet(); 124 Set<CompletableFuture<E>> completedFutures = Sets.newHashSet();
117 for (CompletableFuture<E> future : pendingFutures) { 125 for (CompletableFuture<E> future : pendingFutures) {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.store.consistent.impl; 16 package org.onosproject.store.consistent.impl;
17 17
18 import org.onosproject.core.ApplicationId; 18 import org.onosproject.core.ApplicationId;
19 +import org.onosproject.store.service.AsyncDistributedSet;
19 import org.onosproject.store.service.ConsistentMapBuilder; 20 import org.onosproject.store.service.ConsistentMapBuilder;
20 import org.onosproject.store.service.DistributedSet; 21 import org.onosproject.store.service.DistributedSet;
21 import org.onosproject.store.service.Serializer; 22 import org.onosproject.store.service.Serializer;
...@@ -88,6 +89,11 @@ public class DefaultDistributedSetBuilder<E> implements DistributedSetBuilder<E> ...@@ -88,6 +89,11 @@ public class DefaultDistributedSetBuilder<E> implements DistributedSetBuilder<E>
88 89
89 @Override 90 @Override
90 public DistributedSet<E> build() { 91 public DistributedSet<E> build() {
91 - return new DefaultDistributedSet<E>(name, metering, mapBuilder.build()); 92 + return new DefaultDistributedSet<E>(buildAsyncSet());
93 + }
94 +
95 + @Override
96 + public AsyncDistributedSet<E> buildAsyncSet() {
97 + return new DefaultAsyncDistributedSet<E>(mapBuilder.buildAsyncMap(), name, metering);
92 } 98 }
93 } 99 }
......
...@@ -273,6 +273,11 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -273,6 +273,11 @@ public class EventuallyConsistentMapImpl<K, V>
273 } 273 }
274 274
275 @Override 275 @Override
276 + public String name() {
277 + return mapName;
278 + }
279 +
280 + @Override
276 public int size() { 281 public int size() {
277 checkState(!destroyed, destroyedMessage); 282 checkState(!destroyed, destroyedMessage);
278 // TODO: Maintain a separate counter for tracking live elements in map. 283 // TODO: Maintain a separate counter for tracking live elements in map.
......