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