Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -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 | */ |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 17 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 18 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 19 | import org.onosproject.store.service.AtomicCounter; |
| 20 | import org.onosproject.store.service.StorageException; |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 21 | import org.onosproject.store.service.Synchronous; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 22 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 23 | import java.util.concurrent.CompletableFuture; |
| 24 | import java.util.concurrent.ExecutionException; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | import java.util.concurrent.TimeoutException; |
| 27 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 28 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 29 | * Default implementation for a {@code AtomicCounter} backed by a {@link AsyncAtomicCounter}. |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 30 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 31 | public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implements AtomicCounter { |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 32 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 33 | private final AsyncAtomicCounter asyncCounter; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 34 | private final long operationTimeoutMillis; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 35 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 36 | public DefaultAtomicCounter(AsyncAtomicCounter asyncCounter, long operationTimeoutMillis) { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 37 | super(asyncCounter); |
| 38 | this.asyncCounter = asyncCounter; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 39 | this.operationTimeoutMillis = operationTimeoutMillis; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public long incrementAndGet() { |
| 44 | return complete(asyncCounter.incrementAndGet()); |
| 45 | } |
| 46 | |
| 47 | @Override |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 48 | public long getAndIncrement() { |
| 49 | return complete(asyncCounter.getAndIncrement()); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public long getAndAdd(long delta) { |
| 54 | return complete(asyncCounter.getAndAdd(delta)); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public long addAndGet(long delta) { |
Madan Jampani | 6a4237d | 2016-06-26 20:17:26 -0700 | [diff] [blame] | 59 | return complete(asyncCounter.addAndGet(delta)); |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 63 | public void set(long value) { |
| 64 | complete(asyncCounter.set(value)); |
| 65 | } |
| 66 | |
| 67 | @Override |
Aaron Kruglikov | 82fd632 | 2015-10-06 12:02:46 -0700 | [diff] [blame] | 68 | public boolean compareAndSet(long expectedValue, long updateValue) { |
| 69 | return complete(asyncCounter.compareAndSet(expectedValue, updateValue)); |
| 70 | } |
| 71 | |
| 72 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 73 | public long get() { |
| 74 | return complete(asyncCounter.get()); |
| 75 | } |
| 76 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 77 | private <T> T complete(CompletableFuture<T> future) { |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 78 | try { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 79 | return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 80 | } catch (InterruptedException e) { |
| 81 | Thread.currentThread().interrupt(); |
| 82 | throw new StorageException.Interrupted(); |
| 83 | } catch (TimeoutException e) { |
| 84 | throw new StorageException.Timeout(); |
| 85 | } catch (ExecutionException e) { |
| 86 | throw new StorageException(e.getCause()); |
| 87 | } |
| 88 | } |
| 89 | } |