andrea
Committed by Gerrit Code Review

minor name changes and javadoc

Change-Id: I43a05d75392efad9ac004867027a31adcc18c6f5
...@@ -58,4 +58,12 @@ public interface AsyncAtomicCounter { ...@@ -58,4 +58,12 @@ public interface AsyncAtomicCounter {
58 * @return current value 58 * @return current value
59 */ 59 */
60 CompletableFuture<Long> get(); 60 CompletableFuture<Long> get();
61 +
62 +
63 + /**
64 + * Atomically sets the given value to the current value.
65 + *
66 + * @return future void
67 + */
68 + CompletableFuture<Void> set(long value);
61 } 69 }
......
...@@ -51,6 +51,14 @@ public interface AtomicCounter { ...@@ -51,6 +51,14 @@ public interface AtomicCounter {
51 long addAndGet(long delta); 51 long addAndGet(long delta);
52 52
53 /** 53 /**
54 + * Atomically sets the given value to the current value.
55 + *
56 + * @param value the value to set
57 + */
58 + void set(long value);
59 +
60 +
61 + /**
54 * Returns the current value of the counter without modifying it. 62 * Returns the current value of the counter without modifying it.
55 * 63 *
56 * @return current value 64 * @return current value
......
...@@ -48,6 +48,11 @@ public final class TestAtomicCounter implements AtomicCounter { ...@@ -48,6 +48,11 @@ public final class TestAtomicCounter implements AtomicCounter {
48 } 48 }
49 49
50 @Override 50 @Override
51 + public void set(long value) {
52 + this.value.set(value);
53 + }
54 +
55 + @Override
51 public long get() { 56 public long get() {
52 return value.get(); 57 return value.get();
53 } 58 }
......
...@@ -45,6 +45,7 @@ public interface DatabaseProxy<K, V> { ...@@ -45,6 +45,7 @@ public interface DatabaseProxy<K, V> {
45 45
46 /** 46 /**
47 * Returns the number of entries in map. 47 * Returns the number of entries in map.
48 + *
48 * @param mapName map name 49 * @param mapName map name
49 * @return A completable future to be completed with the result once complete. 50 * @return A completable future to be completed with the result once complete.
50 */ 51 */
...@@ -148,6 +149,16 @@ public interface DatabaseProxy<K, V> { ...@@ -148,6 +149,16 @@ public interface DatabaseProxy<K, V> {
148 */ 149 */
149 CompletableFuture<Long> counterGetAndAdd(String counterName, long delta); 150 CompletableFuture<Long> counterGetAndAdd(String counterName, long delta);
150 151
152 +
153 + /**
154 + * Atomically sets the given value to current value of the specified counter.
155 + *
156 + * @param counterName counter name
157 + * @param value value to set
158 + * @return void future
159 + */
160 + CompletableFuture<Void> counterSet(String counterName, long value);
161 +
151 /** 162 /**
152 * Returns the current value of the specified atomic counter. 163 * Returns the current value of the specified atomic counter.
153 * 164 *
...@@ -158,6 +169,7 @@ public interface DatabaseProxy<K, V> { ...@@ -158,6 +169,7 @@ public interface DatabaseProxy<K, V> {
158 169
159 /** 170 /**
160 * Returns the size of queue. 171 * Returns the size of queue.
172 + *
161 * @param queueName queue name 173 * @param queueName queue name
162 * @return queue size 174 * @return queue size
163 */ 175 */
...@@ -165,6 +177,7 @@ public interface DatabaseProxy<K, V> { ...@@ -165,6 +177,7 @@ public interface DatabaseProxy<K, V> {
165 177
166 /** 178 /**
167 * Inserts an entry into the queue. 179 * Inserts an entry into the queue.
180 + *
168 * @param queueName queue name 181 * @param queueName queue name
169 * @param entry queue entry 182 * @param entry queue entry
170 * @return void future 183 * @return void future
...@@ -173,6 +186,7 @@ public interface DatabaseProxy<K, V> { ...@@ -173,6 +186,7 @@ public interface DatabaseProxy<K, V> {
173 186
174 /** 187 /**
175 * Removes an entry from the queue if the queue is non-empty. 188 * Removes an entry from the queue if the queue is non-empty.
189 + *
176 * @param queueName queue name 190 * @param queueName queue name
177 * @return entry future. Can be completed with null if queue is empty 191 * @return entry future. Can be completed with null if queue is empty
178 */ 192 */
...@@ -180,6 +194,7 @@ public interface DatabaseProxy<K, V> { ...@@ -180,6 +194,7 @@ public interface DatabaseProxy<K, V> {
180 194
181 /** 195 /**
182 * Returns but does not remove an entry from the queue. 196 * Returns but does not remove an entry from the queue.
197 + *
183 * @param queueName queue name 198 * @param queueName queue name
184 * @return entry. Can be null if queue is empty 199 * @return entry. Can be null if queue is empty
185 */ 200 */
......
...@@ -18,6 +18,7 @@ package org.onosproject.store.consistent.impl; ...@@ -18,6 +18,7 @@ package org.onosproject.store.consistent.impl;
18 import org.onosproject.store.service.AsyncAtomicCounter; 18 import org.onosproject.store.service.AsyncAtomicCounter;
19 19
20 import java.util.concurrent.CompletableFuture; 20 import java.util.concurrent.CompletableFuture;
21 +
21 import static com.google.common.base.Preconditions.checkNotNull; 22 import static com.google.common.base.Preconditions.checkNotNull;
22 23
23 /** 24 /**
...@@ -38,6 +39,7 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter { ...@@ -38,6 +39,7 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
38 private static final String GET_AND_ADD = "getAndAdd"; 39 private static final String GET_AND_ADD = "getAndAdd";
39 private static final String ADD_AND_GET = "addAndGet"; 40 private static final String ADD_AND_GET = "addAndGet";
40 private static final String GET = "get"; 41 private static final String GET = "get";
42 + private static final String SET = "set";
41 43
42 public DefaultAsyncAtomicCounter(String name, 44 public DefaultAsyncAtomicCounter(String name,
43 Database database, 45 Database database,
...@@ -81,4 +83,11 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter { ...@@ -81,4 +83,11 @@ public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
81 return database.counterAddAndGet(name, delta) 83 return database.counterAddAndGet(name, delta)
82 .whenComplete((r, e) -> timer.stop(e)); 84 .whenComplete((r, e) -> timer.stop(e));
83 } 85 }
86 +
87 + @Override
88 + public CompletableFuture<Void> set(long value) {
89 + final MeteringAgent.Context timer = monitor.startTimer(SET);
90 + return database.counterSet(name, value)
91 + .whenComplete((r, e) -> timer.stop(e));
92 + }
84 } 93 }
......
...@@ -63,6 +63,11 @@ public class DefaultAtomicCounter implements AtomicCounter { ...@@ -63,6 +63,11 @@ public class DefaultAtomicCounter implements AtomicCounter {
63 } 63 }
64 64
65 @Override 65 @Override
66 + public void set(long value) {
67 + complete(asyncCounter.set(value));
68 + }
69 +
70 + @Override
66 public long get() { 71 public long get() {
67 return complete(asyncCounter.get()); 72 return complete(asyncCounter.get());
68 } 73 }
......
...@@ -44,7 +44,7 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab ...@@ -44,7 +44,7 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab
44 private final Set<Consumer<StateMachineUpdate>> consumers = Sets.newCopyOnWriteArraySet(); 44 private final Set<Consumer<StateMachineUpdate>> consumers = Sets.newCopyOnWriteArraySet();
45 private final TriConsumer<String, Object, Object> watcher = new InternalStateMachineWatcher(); 45 private final TriConsumer<String, Object, Object> watcher = new InternalStateMachineWatcher();
46 46
47 - @SuppressWarnings({ "unchecked", "rawtypes" }) 47 + @SuppressWarnings({"unchecked", "rawtypes"})
48 public DefaultDatabase(ResourceManager context) { 48 public DefaultDatabase(ResourceManager context) {
49 super(context); 49 super(context);
50 this.stateMachine = new DefaultStateMachine(context, 50 this.stateMachine = new DefaultStateMachine(context,
...@@ -153,6 +153,11 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab ...@@ -153,6 +153,11 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab
153 } 153 }
154 154
155 @Override 155 @Override
156 + public CompletableFuture<Void> counterSet(String counterName, long value) {
157 + return checkOpen(() -> proxy.counterSet(counterName, value));
158 + }
159 +
160 + @Override
156 public CompletableFuture<Long> queueSize(String queueName) { 161 public CompletableFuture<Long> queueSize(String queueName) {
157 return checkOpen(() -> proxy.queueSize(queueName)); 162 return checkOpen(() -> proxy.queueSize(queueName));
158 } 163 }
......
...@@ -220,6 +220,11 @@ public class PartitionedDatabase implements Database { ...@@ -220,6 +220,11 @@ public class PartitionedDatabase implements Database {
220 return partitioner.getPartition(counterName, counterName).counterGetAndAdd(counterName, delta); 220 return partitioner.getPartition(counterName, counterName).counterGetAndAdd(counterName, delta);
221 } 221 }
222 222
223 + @Override
224 + public CompletableFuture<Void> counterSet(String counterName, long value) {
225 + checkState(isOpen.get(), DB_NOT_OPEN);
226 + return partitioner.getPartition(counterName, counterName).counterSet(counterName, value);
227 + }
223 228
224 @Override 229 @Override
225 public CompletableFuture<Long> queueSize(String queueName) { 230 public CompletableFuture<Long> queueSize(String queueName) {
...@@ -384,3 +389,4 @@ public class PartitionedDatabase implements Database { ...@@ -384,3 +389,4 @@ public class PartitionedDatabase implements Database {
384 partitions.forEach(p -> p.unregisterConsumer(consumer)); 389 partitions.forEach(p -> p.unregisterConsumer(consumer));
385 } 390 }
386 } 391 }
392 +
......