blob: b79effc1c3677ebf8efd5a98dff066dcac9d600e [file] [log] [blame]
Jordan Haltermanc955df72017-02-04 20:43:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Haltermanc955df72017-02-04 20:43:28 -08003 *
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 */
16package org.onosproject.store.primitives;
17
18import com.google.common.base.Throwables;
19import org.onosproject.store.service.AsyncAtomicCounterMap;
20import org.onosproject.store.service.AtomicCounterMap;
21import org.onosproject.store.service.ConsistentMapException;
22import org.onosproject.store.service.Synchronous;
23
24import java.util.concurrent.CompletableFuture;
25import java.util.concurrent.ExecutionException;
26import java.util.concurrent.TimeUnit;
27import java.util.concurrent.TimeoutException;
28
29/**
30 * Default implementation of {@code AtomicCounterMap}.
31 *
32 * @param <K> map key type
33 */
34public 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 Milkey6a51cb92018-03-06 09:03:03 -0800129 Throwables.throwIfUnchecked(e.getCause());
Jordan Haltermanc955df72017-02-04 20:43:28 -0800130 throw new ConsistentMapException(e.getCause());
131 }
132 }
133}