Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [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 | package org.onosproject.store.primitives; |
| 17 | |
| 18 | import com.google.common.base.Throwables; |
| 19 | import org.onosproject.store.service.AsyncAtomicCounterMap; |
| 20 | import org.onosproject.store.service.AtomicCounterMap; |
| 21 | import org.onosproject.store.service.ConsistentMapException; |
| 22 | import org.onosproject.store.service.Synchronous; |
| 23 | |
| 24 | import java.util.concurrent.CompletableFuture; |
| 25 | import java.util.concurrent.ExecutionException; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | import java.util.concurrent.TimeoutException; |
| 28 | |
| 29 | /** |
| 30 | * Default implementation of {@code AtomicCounterMap}. |
| 31 | * |
| 32 | * @param <K> map key type |
| 33 | */ |
| 34 | public class DefaultAtomicCounterMap<K> extends Synchronous<AsyncAtomicCounterMap<K>> implements AtomicCounterMap<K> { |
| 35 | |
| 36 | private final AsyncAtomicCounterMap<K> asyncCounterMap; |
| 37 | private final long operationTimeoutMillis; |
| 38 | |
| 39 | public DefaultAtomicCounterMap(AsyncAtomicCounterMap<K> asyncCounterMap, long operationTimeoutMillis) { |
| 40 | super(asyncCounterMap); |
| 41 | this.asyncCounterMap = asyncCounterMap; |
| 42 | this.operationTimeoutMillis = operationTimeoutMillis; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public long incrementAndGet(K key) { |
| 47 | return complete(asyncCounterMap.incrementAndGet(key)); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public long decrementAndGet(K key) { |
| 52 | return complete(asyncCounterMap.decrementAndGet(key)); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public long getAndIncrement(K key) { |
| 57 | return complete(asyncCounterMap.getAndIncrement(key)); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public long getAndDecrement(K key) { |
| 62 | return complete(asyncCounterMap.getAndDecrement(key)); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public long addAndGet(K key, long delta) { |
| 67 | return complete(asyncCounterMap.addAndGet(key, delta)); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public long getAndAdd(K key, long delta) { |
| 72 | return complete(asyncCounterMap.getAndAdd(key, delta)); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public long get(K key) { |
| 77 | return complete(asyncCounterMap.get(key)); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public long put(K key, long newValue) { |
| 82 | return complete(asyncCounterMap.put(key, newValue)); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public long putIfAbsent(K key, long newValue) { |
| 87 | return complete(asyncCounterMap.putIfAbsent(key, newValue)); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean replace(K key, long expectedOldValue, long newValue) { |
| 92 | return complete(asyncCounterMap.replace(key, expectedOldValue, newValue)); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public long remove(K key) { |
| 97 | return complete(asyncCounterMap.remove(key)); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean remove(K key, long value) { |
| 102 | return complete(asyncCounterMap.remove(key, value)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int size() { |
| 107 | return complete(asyncCounterMap.size()); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean isEmpty() { |
| 112 | return complete(asyncCounterMap.isEmpty()); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void clear() { |
| 117 | complete(asyncCounterMap.clear()); |
| 118 | } |
| 119 | |
| 120 | private <T> T complete(CompletableFuture<T> future) { |
| 121 | try { |
| 122 | return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
| 123 | } catch (InterruptedException e) { |
| 124 | Thread.currentThread().interrupt(); |
| 125 | throw new ConsistentMapException.Interrupted(); |
| 126 | } catch (TimeoutException e) { |
| 127 | throw new ConsistentMapException.Timeout(name()); |
| 128 | } catch (ExecutionException e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 129 | Throwables.throwIfUnchecked(e.getCause()); |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 130 | throw new ConsistentMapException(e.getCause()); |
| 131 | } |
| 132 | } |
| 133 | } |