blob: a69c0cc63e433eaf33a64ac58768031b6b707aff [file] [log] [blame]
Madan Jampanib5d72d52015-04-03 16:53:50 -07001/*
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 */
16package org.onosproject.store.consistent.impl;
17
18import java.util.concurrent.CompletableFuture;
Madan Jampani04aeb452015-05-02 16:12:24 -070019
Madan Jampanib5d72d52015-04-03 16:53:50 -070020import org.onosproject.store.service.AsyncAtomicCounter;
Madan Jampani04aeb452015-05-02 16:12:24 -070021
Madan Jampanib5d72d52015-04-03 16:53:50 -070022import 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 */
30public 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 Jampani04aeb452015-05-02 16:12:24 -070042 return addAndGet(1L);
Madan Jampanib5d72d52015-04-03 16:53:50 -070043 }
44
45 @Override
46 public CompletableFuture<Long> get() {
Madan Jampani04aeb452015-05-02 16:12:24 -070047 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 Jampanib5d72d52015-04-03 16:53:50 -070063 }
64}