Committed by
Gerrit Code Review
Adding interfaces and single implementation required for build.
Change-Id: I65d2ce71f29e3d0c031ca3a7713835468c7c062c
Showing
3 changed files
with
571 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 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 | + | ||
17 | +package org.onosproject.store.primitives; | ||
18 | + | ||
19 | +import com.google.common.base.Throwables; | ||
20 | +import org.onosproject.store.service.ConsistentMapException; | ||
21 | +import org.onosproject.store.service.AsyncConsistentTreeMap; | ||
22 | +import org.onosproject.store.service.MapEventListener; | ||
23 | +import org.onosproject.store.service.Synchronous; | ||
24 | +import org.onosproject.store.service.ConsistentTreeMap; | ||
25 | +import org.onosproject.store.service.Versioned; | ||
26 | + | ||
27 | +import java.util.Collection; | ||
28 | +import java.util.Map; | ||
29 | +import java.util.NavigableSet; | ||
30 | +import java.util.Set; | ||
31 | +import java.util.concurrent.CompletableFuture; | ||
32 | +import java.util.concurrent.ExecutionException; | ||
33 | +import java.util.concurrent.TimeUnit; | ||
34 | +import java.util.concurrent.TimeoutException; | ||
35 | +import java.util.function.BiFunction; | ||
36 | +import java.util.function.Function; | ||
37 | +import java.util.function.Predicate; | ||
38 | + | ||
39 | +/** | ||
40 | + * Implementation of the {@link ConsistentTreeMap} interface. | ||
41 | + */ | ||
42 | +public class DefaultConsistentTreeMap<K, V> extends Synchronous<AsyncConsistentTreeMap<K, V>> | ||
43 | + implements ConsistentTreeMap<K, V> { | ||
44 | + private static final int MAX_DELAY_BETWEEN_RETRY_MILLIS = 50; | ||
45 | + private final AsyncConsistentTreeMap<K, V> treeMap; | ||
46 | + private final long operationTimeoutMillis; | ||
47 | + private Map<K, V> javaMap; | ||
48 | + | ||
49 | + public DefaultConsistentTreeMap(AsyncConsistentTreeMap<K, V> treeMap, long operationTimeoutMillis) { | ||
50 | + super(treeMap); | ||
51 | + this.treeMap = treeMap; | ||
52 | + this.operationTimeoutMillis = operationTimeoutMillis; | ||
53 | + } | ||
54 | + | ||
55 | + private <T> T complete(CompletableFuture<T> future) { | ||
56 | + try { | ||
57 | + return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); | ||
58 | + } catch (InterruptedException e) { | ||
59 | + Thread.currentThread().interrupt(); | ||
60 | + throw new ConsistentMapException.Interrupted(); | ||
61 | + } catch (ExecutionException e) { | ||
62 | + throw new ConsistentMapException.Timeout(); | ||
63 | + } catch (TimeoutException e) { | ||
64 | + Throwables.propagateIfPossible(e.getCause()); | ||
65 | + throw new ConsistentMapException(e.getCause()); | ||
66 | + } | ||
67 | + } | ||
68 | + | ||
69 | + @Override | ||
70 | + public K firstKey() { | ||
71 | + return complete(treeMap.firstKey()); | ||
72 | + } | ||
73 | + | ||
74 | + @Override | ||
75 | + public K lastKey() { | ||
76 | + return complete(treeMap.lastKey()); | ||
77 | + } | ||
78 | + | ||
79 | + @Override | ||
80 | + public Map.Entry<K, Versioned<V>> ceilingEntry(K key) { | ||
81 | + return complete(treeMap.ceilingEntry(key)); | ||
82 | + } | ||
83 | + | ||
84 | + @Override | ||
85 | + public Map.Entry<K, Versioned<V>> floorEntry(K key) { | ||
86 | + return complete(treeMap.floorEntry(key)); | ||
87 | + } | ||
88 | + | ||
89 | + @Override | ||
90 | + public Map.Entry<K, Versioned<V>> higherEntry(K key) { | ||
91 | + return complete(treeMap.higherEntry(key)); | ||
92 | + } | ||
93 | + | ||
94 | + @Override | ||
95 | + public Map.Entry<K, Versioned<V>> lowerEntry(K key) { | ||
96 | + return complete(treeMap.lowerEntry(key)); | ||
97 | + } | ||
98 | + | ||
99 | + @Override | ||
100 | + public Map.Entry<K, Versioned<V>> firstEntry() { | ||
101 | + return complete(treeMap.firstEntry()); | ||
102 | + } | ||
103 | + | ||
104 | + @Override | ||
105 | + public Map.Entry<K, Versioned<V>> lastEntry() { | ||
106 | + return complete(treeMap.lastEntry()); | ||
107 | + } | ||
108 | + | ||
109 | + @Override | ||
110 | + public Map.Entry<K, Versioned<V>> pollFirstEntry() { | ||
111 | + return complete(treeMap.pollFirstEntry()); | ||
112 | + } | ||
113 | + | ||
114 | + @Override | ||
115 | + public Map.Entry<K, Versioned<V>> pollLastEntry() { | ||
116 | + return complete(treeMap.pollLastEntry()); | ||
117 | + } | ||
118 | + | ||
119 | + @Override | ||
120 | + public K lowerKey(K key) { | ||
121 | + return complete(treeMap.lowerKey(key)); | ||
122 | + } | ||
123 | + | ||
124 | + @Override | ||
125 | + public K floorKey(K key) { | ||
126 | + return complete(treeMap.floorKey(key)); | ||
127 | + } | ||
128 | + | ||
129 | + @Override | ||
130 | + public K ceilingKey(K key) { | ||
131 | + return complete(treeMap.ceilingKey(key)); | ||
132 | + } | ||
133 | + | ||
134 | + @Override | ||
135 | + public K higherKey(K key) { | ||
136 | + return complete(treeMap.higherKey(key)); | ||
137 | + } | ||
138 | + | ||
139 | + | ||
140 | + @Override | ||
141 | + /** | ||
142 | + * {@inheritDoc} | ||
143 | + * <p>This may be a long operation with greater risk of timeout.</p> | ||
144 | + */ | ||
145 | + public NavigableSet<K> navigableKeySet() { | ||
146 | + return complete(treeMap.navigableKeySet()); | ||
147 | + } | ||
148 | + | ||
149 | + @Override | ||
150 | + public int size() { | ||
151 | + return complete(treeMap.size()); | ||
152 | + } | ||
153 | + | ||
154 | + @Override | ||
155 | + public boolean isEmpty() { | ||
156 | + return complete(treeMap.isEmpty()); | ||
157 | + } | ||
158 | + | ||
159 | + @Override | ||
160 | + public boolean containsKey(K key) { | ||
161 | + return complete(treeMap.containsKey(key)); | ||
162 | + } | ||
163 | + | ||
164 | + @Override | ||
165 | + public boolean containsValue(V value) { | ||
166 | + return complete(treeMap.containsValue(value)); | ||
167 | + } | ||
168 | + | ||
169 | + @Override | ||
170 | + public Versioned<V> get(K key) { | ||
171 | + return complete(treeMap.get(key)); | ||
172 | + } | ||
173 | + | ||
174 | + @Override | ||
175 | + public Versioned<V> computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { | ||
176 | + return complete(treeMap.computeIfAbsent(key, mappingFunction)); | ||
177 | + } | ||
178 | + | ||
179 | + @Override | ||
180 | + public Versioned<V> compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | ||
181 | + return complete(treeMap.compute(key, remappingFunction)); | ||
182 | + } | ||
183 | + | ||
184 | + @Override | ||
185 | + public Versioned<V> computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | ||
186 | + return complete(treeMap.computeIfPresent(key, remappingFunction)); | ||
187 | + } | ||
188 | + | ||
189 | + @Override | ||
190 | + public Versioned<V> computeIf(K key, Predicate<? super V> condition, | ||
191 | + BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | ||
192 | + return complete(treeMap.computeIf(key, condition, remappingFunction)); | ||
193 | + } | ||
194 | + | ||
195 | + @Override | ||
196 | + public Versioned<V> put(K key, V value) { | ||
197 | + return complete(treeMap.put(key, value)); | ||
198 | + } | ||
199 | + | ||
200 | + @Override | ||
201 | + public Versioned<V> putAndGet(K key, V value) { | ||
202 | + return complete(treeMap.putAndGet(key, value)); | ||
203 | + } | ||
204 | + | ||
205 | + @Override | ||
206 | + public Versioned<V> remove(K key) { | ||
207 | + return complete(treeMap.remove(key)); | ||
208 | + } | ||
209 | + | ||
210 | + @Override | ||
211 | + public void clear() { | ||
212 | + complete(treeMap.clear()); | ||
213 | + } | ||
214 | + | ||
215 | + @Override | ||
216 | + public Set<K> keySet() { | ||
217 | + return complete(treeMap.keySet()); | ||
218 | + } | ||
219 | + | ||
220 | + @Override | ||
221 | + public Collection<Versioned<V>> values() { | ||
222 | + return complete(treeMap.values()); | ||
223 | + } | ||
224 | + | ||
225 | + @Override | ||
226 | + public Set<Map.Entry<K, Versioned<V>>> entrySet() { | ||
227 | + return complete(treeMap.entrySet()); | ||
228 | + } | ||
229 | + | ||
230 | + @Override | ||
231 | + public Versioned<V> putIfAbsent(K key, V value) { | ||
232 | + return complete(treeMap.putIfAbsent(key, value)); | ||
233 | + } | ||
234 | + | ||
235 | + @Override | ||
236 | + public boolean remove(K key, V value) { | ||
237 | + return complete(treeMap.remove(key, value)); | ||
238 | + } | ||
239 | + | ||
240 | + @Override | ||
241 | + public boolean remove(K key, long version) { | ||
242 | + return complete(treeMap.remove(key, version)); | ||
243 | + } | ||
244 | + | ||
245 | + @Override | ||
246 | + public Versioned<V> replace(K key, V value) { | ||
247 | + return complete(treeMap.replace(key, value)); | ||
248 | + } | ||
249 | + | ||
250 | + @Override | ||
251 | + public boolean replace(K key, V oldValue, V newValue) { | ||
252 | + return complete(treeMap.replace(key, oldValue, newValue)); | ||
253 | + } | ||
254 | + | ||
255 | + @Override | ||
256 | + public boolean replace(K key, long oldVersion, V newValue) { | ||
257 | + return complete(treeMap.replace(key, oldVersion, newValue)); | ||
258 | + } | ||
259 | + | ||
260 | + @Override | ||
261 | + public void addListener(MapEventListener<K, V> listener) { | ||
262 | + complete(treeMap.addListener(listener)); | ||
263 | + } | ||
264 | + | ||
265 | + @Override | ||
266 | + public void removeListener(MapEventListener<K, V> listener) { | ||
267 | + complete(treeMap.removeListener(listener)); | ||
268 | + } | ||
269 | + | ||
270 | + @Override | ||
271 | + public Map<K, V> asJavaMap() { | ||
272 | + synchronized (this) { | ||
273 | + if (javaMap == null) { | ||
274 | + javaMap = new ConsistentMapBackedJavaMap<>(this); | ||
275 | + } | ||
276 | + } | ||
277 | + return javaMap; | ||
278 | + } | ||
279 | +} |
1 | +/* | ||
2 | + * Copyright 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 | + | ||
17 | +package org.onosproject.store.service; | ||
18 | + | ||
19 | +import org.onosproject.store.primitives.DefaultConsistentTreeMap; | ||
20 | + | ||
21 | +import java.util.Map; | ||
22 | +import java.util.NavigableSet; | ||
23 | +import java.util.concurrent.CompletableFuture; | ||
24 | + | ||
25 | +/** | ||
26 | + * API for a distributed tree map implementation. | ||
27 | + */ | ||
28 | +public interface AsyncConsistentTreeMap<K, V> extends AsyncConsistentMap<K, V> { | ||
29 | + | ||
30 | + /** | ||
31 | + * Return the lowest key in the map. | ||
32 | + * | ||
33 | + * @return the key or null if none exist | ||
34 | + */ | ||
35 | + CompletableFuture<K> firstKey(); | ||
36 | + | ||
37 | + /** | ||
38 | + * Return the highest key in the map. | ||
39 | + * | ||
40 | + * @return the key or null if none exist | ||
41 | + */ | ||
42 | + CompletableFuture<K> lastKey(); | ||
43 | + | ||
44 | + /** | ||
45 | + * Returns the entry associated with the least key greater than or equal to the key. | ||
46 | + * | ||
47 | + * @param key the key | ||
48 | + * @return the entry or null | ||
49 | + */ | ||
50 | + CompletableFuture<Map.Entry<K, Versioned<V>>> ceilingEntry(K key); | ||
51 | + | ||
52 | + /** | ||
53 | + * Returns the entry associated with the greatest key less than or equal to key. | ||
54 | + * | ||
55 | + * @param key the key | ||
56 | + * @return the entry or null | ||
57 | + */ | ||
58 | + CompletableFuture<Map.Entry<K, Versioned<V>>> floorEntry(K key); | ||
59 | + | ||
60 | + /** | ||
61 | + * Returns the entry associated with the lest key greater than key. | ||
62 | + * | ||
63 | + * @param key the key | ||
64 | + * @return the entry or null | ||
65 | + */ | ||
66 | + CompletableFuture<Map.Entry<K, Versioned<V>>> higherEntry(K key); | ||
67 | + | ||
68 | + /** | ||
69 | + * Returns the entry associated with the largest key less than key. | ||
70 | + * | ||
71 | + * @param key the key | ||
72 | + * @return the entry or null | ||
73 | + */ | ||
74 | + CompletableFuture<Map.Entry<K, Versioned<V>>> lowerEntry(K key); | ||
75 | + | ||
76 | + /** | ||
77 | + * Return the entry associated with the lowest key in the map. | ||
78 | + * | ||
79 | + * @return the entry or null | ||
80 | + */ | ||
81 | + CompletableFuture<Map.Entry<K, Versioned<V>>> firstEntry(); | ||
82 | + | ||
83 | + /** | ||
84 | + * Return the entry assocaited with the highest key in the map. | ||
85 | + * | ||
86 | + * @return the entry or null | ||
87 | + */ | ||
88 | + CompletableFuture<Map.Entry<K, Versioned<V>>> lastEntry(); | ||
89 | + | ||
90 | + /** | ||
91 | + * Return and remove the entry associated with the lowest key. | ||
92 | + * | ||
93 | + * @return the entry or null | ||
94 | + */ | ||
95 | + CompletableFuture<Map.Entry<K, Versioned<V>>> pollFirstEntry(); | ||
96 | + | ||
97 | + /** | ||
98 | + * Return and remove the entry associated with the highest key. | ||
99 | + * | ||
100 | + * @return the entry or null | ||
101 | + */ | ||
102 | + CompletableFuture<Map.Entry<K, Versioned<V>>> pollLastEntry(); | ||
103 | + | ||
104 | + /** | ||
105 | + * Return the entry associated with the greatest key less than key. | ||
106 | + * | ||
107 | + * @param key the key | ||
108 | + * @return the entry or null | ||
109 | + */ | ||
110 | + CompletableFuture<K> lowerKey(K key); | ||
111 | + | ||
112 | + /** | ||
113 | + * Return the entry associated with the highest key less than or equal to key. | ||
114 | + * | ||
115 | + * @param key the key | ||
116 | + * @return the entry or null | ||
117 | + */ | ||
118 | + CompletableFuture<K> floorKey(K key); | ||
119 | + | ||
120 | + /** | ||
121 | + * Return the lowest key greater than or equal to key. | ||
122 | + * | ||
123 | + * @param key the key | ||
124 | + * @return the key or null | ||
125 | + */ | ||
126 | + CompletableFuture<K> ceilingKey(K key); | ||
127 | + | ||
128 | + /** | ||
129 | + * Return the lowest key greater than key. | ||
130 | + * | ||
131 | + * @param key the key | ||
132 | + * @return the key or null | ||
133 | + */ | ||
134 | + CompletableFuture<K> higherKey(K key); | ||
135 | + | ||
136 | + /** | ||
137 | + * Returns a navigable set of the keys in this map. | ||
138 | + * | ||
139 | + * @return a navigable key set | ||
140 | + */ | ||
141 | + CompletableFuture<NavigableSet<K>> navigableKeySet(); | ||
142 | + | ||
143 | + default ConsistentTreeMap<K, V> asTreeMap() { | ||
144 | + return asTreeMap(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS); | ||
145 | + } | ||
146 | + | ||
147 | + default ConsistentTreeMap<K, V> asTreeMap(long timeoutMillis) { | ||
148 | + return new DefaultConsistentTreeMap<>(this, timeoutMillis); | ||
149 | + } | ||
150 | + | ||
151 | + | ||
152 | +} |
1 | +/* | ||
2 | + * Copyright 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 | + | ||
17 | +package org.onosproject.store.service; | ||
18 | + | ||
19 | +import java.util.Map; | ||
20 | +import java.util.NavigableSet; | ||
21 | + | ||
22 | +/** | ||
23 | + * Tree map interface counterpart to {@link AsyncConsistentTreeMap}. | ||
24 | + */ | ||
25 | + public interface ConsistentTreeMap<K, V> extends ConsistentMap<K, V> { | ||
26 | + | ||
27 | + /** | ||
28 | + * Returns the lowest key in the map. | ||
29 | + * | ||
30 | + * @return the key or null if none exist | ||
31 | + */ | ||
32 | + K firstKey(); | ||
33 | + | ||
34 | + /** | ||
35 | + * Returns the highest key in the map. | ||
36 | + * | ||
37 | + * @return the key or null if none exist | ||
38 | + */ | ||
39 | + K lastKey(); | ||
40 | + | ||
41 | + /** | ||
42 | + * Returns the entry associated with the least key greater than or equal to the key. | ||
43 | + * | ||
44 | + * @param key the key | ||
45 | + * @return the entry or null | ||
46 | + */ | ||
47 | + Map.Entry<K, Versioned<V>> ceilingEntry(K key); | ||
48 | + | ||
49 | + /** | ||
50 | + * Returns the entry associated with the greatest key less than or equal to key. | ||
51 | + * | ||
52 | + * @param key the key | ||
53 | + * @return the entry or null | ||
54 | + */ | ||
55 | + Map.Entry<K, Versioned<V>> floorEntry(K key); | ||
56 | + | ||
57 | + /** | ||
58 | + * Returns the entry associated with the lest key greater than key. | ||
59 | + * | ||
60 | + * @param key the key | ||
61 | + * @return the entry or null | ||
62 | + */ | ||
63 | + Map.Entry<K, Versioned<V>> higherEntry(K key); | ||
64 | + | ||
65 | + /** | ||
66 | + * Returns the entry associated with the largest key less than key. | ||
67 | + * | ||
68 | + * @param key the key | ||
69 | + * @return the entry or null | ||
70 | + */ | ||
71 | + Map.Entry<K, Versioned<V>> lowerEntry(K key); | ||
72 | + | ||
73 | + /** | ||
74 | + * Returns the entry associated with the lowest key in the map. | ||
75 | + * | ||
76 | + * @return the entry or null | ||
77 | + */ | ||
78 | + Map.Entry<K, Versioned<V>> firstEntry(); | ||
79 | + | ||
80 | + /** | ||
81 | + * Returns the entry associated with the highest key in the map. | ||
82 | + * | ||
83 | + * @return the entry or null | ||
84 | + */ | ||
85 | + Map.Entry<K, Versioned<V>> lastEntry(); | ||
86 | + | ||
87 | + /** | ||
88 | + * Returns and removes the entry associated with the lowest key. | ||
89 | + * | ||
90 | + * @return the entry or null | ||
91 | + */ | ||
92 | + Map.Entry<K, Versioned<V>> pollFirstEntry(); | ||
93 | + | ||
94 | + /** | ||
95 | + * Returns and removes the entry associated with the highest key. | ||
96 | + * | ||
97 | + * @return the entry or null | ||
98 | + */ | ||
99 | + Map.Entry<K, Versioned<V>> pollLastEntry(); | ||
100 | + | ||
101 | + /** | ||
102 | + * Returns the entry associated with the greatest key less than key. | ||
103 | + * | ||
104 | + * @param key the key | ||
105 | + * @return the entry or null | ||
106 | + */ | ||
107 | + K lowerKey(K key); | ||
108 | + | ||
109 | + /** | ||
110 | + * Returns the entry associated with the highest key less than or equal to key. | ||
111 | + * | ||
112 | + * @param key the key | ||
113 | + * @return the entry or null | ||
114 | + */ | ||
115 | + K floorKey(K key); | ||
116 | + | ||
117 | + /** | ||
118 | + * Returns the lowest key greater than or equal to key. | ||
119 | + * | ||
120 | + * @param key the key | ||
121 | + * @return the key or null | ||
122 | + */ | ||
123 | + K ceilingKey(K key); | ||
124 | + | ||
125 | + /** | ||
126 | + * Returns the lowest key greater than key. | ||
127 | + * | ||
128 | + * @param key the key | ||
129 | + * @return the key or null | ||
130 | + */ | ||
131 | + K higherKey(K key); | ||
132 | + | ||
133 | + /** | ||
134 | + * Returns a navigable set of the keys in this map. | ||
135 | + * | ||
136 | + * @return a navigable key set | ||
137 | + */ | ||
138 | + NavigableSet<K> navigableKeySet(); | ||
139 | + | ||
140 | +} |
-
Please register or login to post a comment