blob: 3cad25f533e719e14f37d82a80c28459004dfa64 [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;
19import org.onosproject.store.service.AsyncAtomicCounter;
20import static com.google.common.base.Preconditions.*;
21
22/**
23 * Default implementation for a distributed AsyncAtomicCounter backed by
24 * partitioned Raft DB.
25 * <p>
26 * The initial value will be zero.
27 */
28public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
29
30 private final String name;
31 private final Database database;
32
33 public DefaultAsyncAtomicCounter(String name, Database database) {
34 this.name = checkNotNull(name);
35 this.database = checkNotNull(database);
36 }
37
38 @Override
39 public CompletableFuture<Long> incrementAndGet() {
40 return database.nextValue(name);
41 }
42
43 @Override
44 public CompletableFuture<Long> get() {
45 return database.currentValue(name);
46 }
47}