Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.consistent.impl; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 19 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 20 | import org.onosproject.store.service.AsyncAtomicCounter; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 21 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.*; |
| 23 | |
| 24 | /** |
| 25 | * Default implementation for a distributed AsyncAtomicCounter backed by |
| 26 | * partitioned Raft DB. |
| 27 | * <p> |
| 28 | * The initial value will be zero. |
| 29 | */ |
| 30 | public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter { |
| 31 | |
| 32 | private final String name; |
| 33 | private final Database database; |
| 34 | |
| 35 | public DefaultAsyncAtomicCounter(String name, Database database) { |
| 36 | this.name = checkNotNull(name); |
| 37 | this.database = checkNotNull(database); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public CompletableFuture<Long> incrementAndGet() { |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 42 | return addAndGet(1L); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public CompletableFuture<Long> get() { |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 47 | return database.counterGet(name); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public CompletableFuture<Long> getAndIncrement() { |
| 52 | return getAndAdd(1L); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public CompletableFuture<Long> getAndAdd(long delta) { |
| 57 | return database.counterGetAndAdd(name, delta); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Long> addAndGet(long delta) { |
| 62 | return database.counterAddAndGet(name, delta); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 63 | } |
| 64 | } |