blob: c7c9f5d386de9a9802ffe4c0a4e05bbc775197c3 [file] [log] [blame]
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -07003 *
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
17package org.onosproject.store.primitives;
18
19import com.google.common.base.Throwables;
20import com.google.common.collect.Multiset;
21import org.onosproject.store.service.AsyncConsistentMultimap;
22import org.onosproject.store.service.ConsistentMapException;
23import org.onosproject.store.service.ConsistentMultimap;
Jonathan Hart46bf89b2017-02-27 15:56:42 -080024import org.onosproject.store.service.MultimapEventListener;
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070025import org.onosproject.store.service.Synchronous;
26import org.onosproject.store.service.Versioned;
27
28import java.util.Collection;
29import java.util.Map;
30import java.util.Set;
31import java.util.concurrent.CompletableFuture;
32import java.util.concurrent.ExecutionException;
Jonathan Hart46bf89b2017-02-27 15:56:42 -080033import java.util.concurrent.Executor;
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070034import java.util.concurrent.TimeUnit;
35import java.util.concurrent.TimeoutException;
36
37/**
38 * Implementation of {@link ConsistentMultimap} providing synchronous access to
39 * {@link AsyncConsistentMultimap}.
40 */
41public class DefaultConsistentMultimap<K, V>
42 extends Synchronous<AsyncConsistentMultimap<K, V>>
43 implements ConsistentMultimap<K, V> {
44
45 private final AsyncConsistentMultimap<K, V> asyncMultimap;
46 private final long operationTimeoutMillis;
47
48 public DefaultConsistentMultimap(
49 AsyncConsistentMultimap<K, V> asyncMultimap,
50 long operationTimeoutMillis) {
51 super(asyncMultimap);
52 this.asyncMultimap = asyncMultimap;
53 this.operationTimeoutMillis = operationTimeoutMillis;
54 }
55
56 @Override
57 public int size() {
58 return complete(asyncMultimap.size());
59 }
60
61 @Override
62 public boolean isEmpty() {
63 return complete(asyncMultimap.isEmpty());
64 }
65
66 @Override
67 public boolean containsKey(K key) {
68 return complete(asyncMultimap.containsKey(key));
69 }
70
71 @Override
72 public boolean containsValue(V value) {
73 return complete(asyncMultimap.containsValue(value));
74 }
75
76 @Override
77 public boolean containsEntry(K key, V value) {
78 return complete(asyncMultimap.containsEntry(key, value));
79 }
80
81 @Override
82 public boolean put(K key, V value) {
83 return complete(asyncMultimap.put(key, value));
84 }
85
86 @Override
87 public boolean remove(K key, V value) {
88 return complete(asyncMultimap.remove(key, value));
89 }
90
91 @Override
92 public boolean removeAll(K key, Collection<? extends V> values) {
93 return complete(asyncMultimap.removeAll(key, values));
94 }
95
96 @Override
97 public Versioned<Collection<? extends V>> removeAll(K key) {
98 return complete(asyncMultimap.removeAll(key));
99 }
100
101 @Override
102 public boolean putAll(K key, Collection<? extends V> values) {
103 return complete(asyncMultimap.putAll(key, values));
104 }
105
106 @Override
107 public Versioned<Collection<? extends V>> replaceValues(
108 K key, Collection<V> values) {
109 return complete(asyncMultimap.replaceValues(key, values));
110 }
111
112 @Override
113 public void clear() {
114 complete(asyncMultimap.clear());
115 }
116
117 @Override
118 public Versioned<Collection<? extends V>> get(K key) {
119 return complete(asyncMultimap.get(key));
120 }
121
122 @Override
123 public Set<K> keySet() {
124 return complete(asyncMultimap.keySet());
125 }
126
127 @Override
128 public Multiset<K> keys() {
129 return complete(asyncMultimap.keys());
130 }
131
132 @Override
133 public Multiset<V> values() {
134 return complete(asyncMultimap.values());
135 }
136
137 @Override
138 public Collection<Map.Entry<K, V>> entries() {
139 return complete(asyncMultimap.entries());
140 }
141
142 @Override
143 public Map<K, Collection<V>> asMap() {
144 throw new UnsupportedOperationException("This operation is not yet " +
145 "supported.");
146 //FIXME implement this when a new version of ConsistentMapBackedJavaMap is made for multimaps
147 }
148
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800149 @Override
150 public void addListener(MultimapEventListener<K, V> listener, Executor executor) {
151 complete(asyncMultimap.addListener(listener, executor));
152 }
153
154 @Override
155 public void removeListener(MultimapEventListener<K, V> listener) {
156 complete(asyncMultimap.removeListener(listener));
157 }
158
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700159 private <T> T complete(CompletableFuture<T> future) {
160 try {
161 return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
162 } catch (InterruptedException e) {
163 Thread.currentThread().interrupt();
164 throw new ConsistentMapException.Interrupted();
165 } catch (TimeoutException e) {
166 throw new ConsistentMapException.Timeout();
167 } catch (ExecutionException e) {
168 Throwables.propagateIfPossible(e.getCause());
169 throw new ConsistentMapException(e.getCause());
170 }
171 }
172}