andrea
Committed by Gerrit Code Review

minor name changes and javadoc

Change-Id: I43a05d75392efad9ac004867027a31adcc18c6f5
......@@ -58,4 +58,12 @@ public interface AsyncAtomicCounter {
* @return current value
*/
CompletableFuture<Long> get();
/**
* Atomically sets the given value to the current value.
*
* @return future void
*/
CompletableFuture<Void> set(long value);
}
......
......@@ -51,6 +51,14 @@ public interface AtomicCounter {
long addAndGet(long delta);
/**
* Atomically sets the given value to the current value.
*
* @param value the value to set
*/
void set(long value);
/**
* Returns the current value of the counter without modifying it.
*
* @return current value
......
......@@ -48,6 +48,11 @@ public final class TestAtomicCounter implements AtomicCounter {
}
@Override
public void set(long value) {
this.value.set(value);
}
@Override
public long get() {
return value.get();
}
......
......@@ -45,6 +45,7 @@ public interface DatabaseProxy<K, V> {
/**
* Returns the number of entries in map.
*
* @param mapName map name
* @return A completable future to be completed with the result once complete.
*/
......@@ -148,6 +149,16 @@ public interface DatabaseProxy<K, V> {
*/
CompletableFuture<Long> counterGetAndAdd(String counterName, long delta);
/**
* Atomically sets the given value to current value of the specified counter.
*
* @param counterName counter name
* @param value value to set
* @return void future
*/
CompletableFuture<Void> counterSet(String counterName, long value);
/**
* Returns the current value of the specified atomic counter.
*
......@@ -158,6 +169,7 @@ public interface DatabaseProxy<K, V> {
/**
* Returns the size of queue.
*
* @param queueName queue name
* @return queue size
*/
......@@ -165,6 +177,7 @@ public interface DatabaseProxy<K, V> {
/**
* Inserts an entry into the queue.
*
* @param queueName queue name
* @param entry queue entry
* @return void future
......@@ -173,6 +186,7 @@ public interface DatabaseProxy<K, V> {
/**
* Removes an entry from the queue if the queue is non-empty.
*
* @param queueName queue name
* @return entry future. Can be completed with null if queue is empty
*/
......@@ -180,6 +194,7 @@ public interface DatabaseProxy<K, V> {
/**
* Returns but does not remove an entry from the queue.
*
* @param queueName queue name
* @return entry. Can be null if queue is empty
*/
......
......@@ -18,6 +18,7 @@ package org.onosproject.store.consistent.impl;
import org.onosproject.store.service.AsyncAtomicCounter;
import java.util.concurrent.CompletableFuture;
import static com.google.common.base.Preconditions.checkNotNull;
/**
......@@ -38,6 +39,7 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
private static final String GET_AND_ADD = "getAndAdd";
private static final String ADD_AND_GET = "addAndGet";
private static final String GET = "get";
private static final String SET = "set";
public DefaultAsyncAtomicCounter(String name,
Database database,
......@@ -81,4 +83,11 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
return database.counterAddAndGet(name, delta)
.whenComplete((r, e) -> timer.stop(e));
}
@Override
public CompletableFuture<Void> set(long value) {
final MeteringAgent.Context timer = monitor.startTimer(SET);
return database.counterSet(name, value)
.whenComplete((r, e) -> timer.stop(e));
}
}
......
......@@ -63,6 +63,11 @@ public class DefaultAtomicCounter implements AtomicCounter {
}
@Override
public void set(long value) {
complete(asyncCounter.set(value));
}
@Override
public long get() {
return complete(asyncCounter.get());
}
......
......@@ -44,7 +44,7 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab
private final Set<Consumer<StateMachineUpdate>> consumers = Sets.newCopyOnWriteArraySet();
private final TriConsumer<String, Object, Object> watcher = new InternalStateMachineWatcher();
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
public DefaultDatabase(ResourceManager context) {
super(context);
this.stateMachine = new DefaultStateMachine(context,
......@@ -153,6 +153,11 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab
}
@Override
public CompletableFuture<Void> counterSet(String counterName, long value) {
return checkOpen(() -> proxy.counterSet(counterName, value));
}
@Override
public CompletableFuture<Long> queueSize(String queueName) {
return checkOpen(() -> proxy.queueSize(queueName));
}
......
......@@ -220,6 +220,11 @@ public class PartitionedDatabase implements Database {
return partitioner.getPartition(counterName, counterName).counterGetAndAdd(counterName, delta);
}
@Override
public CompletableFuture<Void> counterSet(String counterName, long value) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(counterName, counterName).counterSet(counterName, value);
}
@Override
public CompletableFuture<Long> queueSize(String queueName) {
......@@ -384,3 +389,4 @@ public class PartitionedDatabase implements Database {
partitions.forEach(p -> p.unregisterConsumer(consumer));
}
}
......