2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.consistent.impl;
18 import org.onosproject.store.service.AsyncAtomicCounter;
20 import java.util.concurrent.CompletableFuture;
21 import static com.google.common.base.Preconditions.checkNotNull;
24 * Default implementation for a distributed AsyncAtomicCounter backed by
25 * partitioned Raft DB.
27 * The initial value will be zero.
29 public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
31 private final String name;
32 private final Database database;
33 private final MeteringAgent monitor;
35 private static final String PRIMITIVE_NAME = "atomicCounter";
36 private static final String INCREMENT_AND_GET = "incrementAndGet";
37 private static final String GET_AND_INCREMENT = "getAndIncrement";
38 private static final String GET_AND_ADD = "getAndAdd";
39 private static final String ADD_AND_GET = "addAndGet";
40 private static final String GET = "get";
42 public DefaultAsyncAtomicCounter(String name,
44 boolean meteringEnabled) {
45 this.name = checkNotNull(name);
46 this.database = checkNotNull(database);
47 this.monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled);
51 public CompletableFuture<Long> incrementAndGet() {
52 final MeteringAgent.Context timer = monitor.startTimer(INCREMENT_AND_GET);
54 .whenComplete((r, e) -> timer.stop(e));
58 public CompletableFuture<Long> get() {
59 final MeteringAgent.Context timer = monitor.startTimer(GET);
60 return database.counterGet(name)
61 .whenComplete((r, e) -> timer.stop(e));
65 public CompletableFuture<Long> getAndIncrement() {
66 final MeteringAgent.Context timer = monitor.startTimer(GET_AND_INCREMENT);
68 .whenComplete((r, e) -> timer.stop(e));
72 public CompletableFuture<Long> getAndAdd(long delta) {
73 final MeteringAgent.Context timer = monitor.startTimer(GET_AND_ADD);
74 return database.counterGetAndAdd(name, delta)
75 .whenComplete((r, e) -> timer.stop(e));
79 public CompletableFuture<Long> addAndGet(long delta) {
80 final MeteringAgent.Context timer = monitor.startTimer(ADD_AND_GET);
81 return database.counterAddAndGet(name, delta)
82 .whenComplete((r, e) -> timer.stop(e));