Jihwan Kim
Committed by Gerrit Code Review

[ONOS-3558] Define interfaces for supporting a AtomicLongMap

Change-Id: I7c03d3d4e9c501fbf0f9d8d28dcc43875e0987ab
1 +/*
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 + */
16 +package org.onosproject.store.service;
17 +
18 +import java.util.concurrent.CompletableFuture;
19 +
20 +/**
21 + * An async atomic counter map dispenses monotonically increasing values associated with key.
22 + */
23 +public interface AsyncAtomicCounterMap<K> {
24 +
25 + /**
26 + * Increments by one the value currently associated with key, and returns the new value.
27 + *
28 + * @param key key with which the specified value is to be associated
29 + */
30 + CompletableFuture<Long> incrementAndGet(K key);
31 +
32 + /**
33 + * Decrements by one the value currently associated with key, and returns the new value.
34 + *
35 + * @param key key with which the specified value is to be associated
36 + * @return updated value
37 + */
38 + CompletableFuture<Long> decrementAndGet(K key);
39 +
40 + /**
41 + * Increments by one the value currently associated with key, and returns the old value.
42 + *
43 + * @param key key with which the specified value is to be associated
44 + * @return previous value
45 + */
46 + CompletableFuture<Long> getAndIncrement(K key);
47 +
48 + /**
49 + * Decrements by one the value currently associated with key, and returns the old value.
50 + *
51 + * @param key key with which the specified value is to be associated
52 + * @return previous value
53 + */
54 + CompletableFuture<Long> getAndDecrement(K key);
55 +
56 + /**
57 + * Adds delta to the value currently associated with key, and returns the new value.
58 + *
59 + * @param key key with which the specified value is to be associated
60 + * @param delta the value to add
61 + * @return updated value
62 + */
63 + CompletableFuture<Long> addAndGet(K key, long delta);
64 +
65 + /**
66 + * Adds delta to the value currently associated with key, and returns the old value.
67 + *
68 + * @param key key with which the specified value is to be associated
69 + * @param delta the value to add
70 + * @return previous value
71 + */
72 + CompletableFuture<Long> getAndAdd(K key, long delta);
73 +
74 + /**
75 + * Returns the value associated with key, or zero if there is no value associated with key.
76 + *
77 + * @param key key with which the specified value is to be associated
78 + * @return current value
79 + */
80 + CompletableFuture<Long> get(K key);
81 +
82 + /**
83 + * Associates ewValue with key in this map, and returns the value previously
84 + * associated with key, or zero if there was no such value.
85 + *
86 + * @param key key with which the specified value is to be associated
87 + * @param newValue the value to put
88 + * @return previous value or zero
89 + */
90 + CompletableFuture<Long> put(K key, long newValue);
91 +
92 +
93 + /**
94 + * If key is not already associated with a value or if key is associated with
95 + * zero, associate it with newValue. Returns the previous value associated with
96 + * key, or zero if there was no mapping for key.
97 + *
98 + * @param key key with which the specified value is to be associated
99 + * @param newValue the value to put
100 + * @return previous value or zero
101 + */
102 + CompletableFuture<Long> putIfAbsent(K key, long newValue);
103 +
104 + /**
105 + * If (key, expectedOldValue) is currently in the map, this method replaces
106 + * expectedOldValue with newValue and returns true; otherwise, this method return false.
107 + *
108 + * If expectedOldValue is zero, this method will succeed if (key, zero)
109 + * is currently in the map, or if key is not in the map at all.
110 + *
111 + * @param key key with which the specified value is to be associated
112 + * @param expectedOldValue the expected value
113 + * @param newValue the value to replace
114 + * @return true if the value was replaced, false otherwise
115 + */
116 + CompletableFuture<Boolean> replace(K key, long expectedOldValue, long newValue);
117 +
118 + /**
119 + * Removes and returns the value associated with key. If key is not
120 + * in the map, this method has no effect and returns zero.
121 + *
122 + * @param key key with which the specified value is to be associated
123 + * @return the previous value associated with the specified key or null
124 + */
125 + CompletableFuture<Long> remove(K key);
126 +
127 + /**
128 + * If (key, value) is currently in the map, this method removes it and returns
129 + * true; otherwise, this method returns false.
130 + *
131 + * @param key key with which the specified value is to be associated
132 + * @param value the value to remove
133 + * @return true if the value was removed, false otherwise
134 + */
135 + CompletableFuture<Boolean> remove(K key, long value);
136 +}
1 +/*
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 + */
16 +package org.onosproject.store.service;
17 +
18 +/**
19 + * Distributed version of com.google.common.util.concurrent.AtomicLongMap.
20 + */
21 +public interface AtomicCounterMap<K> {
22 +
23 + /**
24 + * Increments by one the value currently associated with key, and returns the new value.
25 + *
26 + * @param key key with which the specified value is to be associated
27 + */
28 + long incrementAndGet(K key);
29 +
30 + /**
31 + * Decrements by one the value currently associated with key, and returns the new value.
32 + *
33 + * @param key key with which the specified value is to be associated
34 + * @return updated value
35 + */
36 + long decrementAndGet(K key);
37 +
38 + /**
39 + * Increments by one the value currently associated with key, and returns the old value.
40 + *
41 + * @param key key with which the specified value is to be associated
42 + * @return previous value
43 + */
44 + long getAndIncrement(K key);
45 +
46 + /**
47 + * Decrements by one the value currently associated with key, and returns the old value.
48 + *
49 + * @param key key with which the specified value is to be associated
50 + * @return previous value
51 + */
52 + long getAndDecrement(K key);
53 +
54 + /**
55 + * Adds delta to the value currently associated with key, and returns the new value.
56 + *
57 + * @param key key with which the specified value is to be associated
58 + * @param delta the value to add
59 + * @return updated value
60 + */
61 + long addAndGet(K key, long delta);
62 +
63 + /**
64 + * Adds delta to the value currently associated with key, and returns the old value.
65 + *
66 + * @param key key with which the specified value is to be associated
67 + * @param delta the value to add
68 + * @return previous value
69 + */
70 + long getAndAdd(K key, long delta);
71 +
72 + /**
73 + * Returns the value associated with key, or zero if there is no value associated with key.
74 + *
75 + * @param key key with which the specified value is to be associated
76 + * @return current value
77 + */
78 + long get(K key);
79 +
80 + /**
81 + * Associates ewValue with key in this map, and returns the value previously
82 + * associated with key, or zero if there was no such value.
83 + *
84 + * @param key key with which the specified value is to be associated
85 + * @param newValue the value to put
86 + * @return previous value or zero
87 + */
88 + long put(K key, long newValue);
89 +
90 +
91 + /**
92 + * If key is not already associated with a value or if key is associated with
93 + * zero, associate it with newValue. Returns the previous value associated with
94 + * key, or zero if there was no mapping for key.
95 + *
96 + * @param key key with which the specified value is to be associated
97 + * @param newValue the value to put
98 + * @return previous value or zero
99 + */
100 + long putIfAbsent(K key, long newValue);
101 +
102 + /**
103 + * If (key, expectedOldValue) is currently in the map, this method replaces
104 + * expectedOldValue with newValue and returns true; otherwise, this method return false.
105 + *
106 + * If expectedOldValue is zero, this method will succeed if (key, zero)
107 + * is currently in the map, or if key is not in the map at all.
108 + *
109 + * @param key key with which the specified value is to be associated
110 + * @param expectedOldValue the expected value
111 + * @param newValue the value to replace
112 + * @return true if the value was replaced, false otherwise
113 + */
114 + boolean replace(K key, long expectedOldValue, long newValue);
115 +
116 + /**
117 + * Removes and returns the value associated with key. If key is not
118 + * in the map, this method has no effect and returns zero.
119 + *
120 + * @param key key with which the specified value is to be associated
121 + * @return the previous value associated with the specified key or null
122 + */
123 + long remove(K key);
124 +
125 + /**
126 + * If (key, value) is currently in the map, this method removes it and returns
127 + * true; otherwise, this method returns false.
128 + *
129 + * @param key key with which the specified value is to be associated
130 + * @param value the value to remove
131 + * @return true if the value was removed, false otherwise
132 + */
133 + boolean remove(K key, long value);
134 +}
1 +/*
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 + */
16 +package org.onosproject.store.service;
17 +
18 +/**
19 + * Builder for AtomicCounterMap.
20 + */
21 +public interface AtomicCounterMapBuilder<K> {
22 +
23 + /**
24 + * Sets the name for the atomic counter map.
25 + * <p>
26 + * Each atomic counter map is identified by a unique name.
27 + * </p>
28 + * <p>
29 + * Note: This is a mandatory parameter.
30 + * </p>
31 + *
32 + * @param name name of the atomic counter
33 + * @return this AtomicCounterMapBuilder
34 + */
35 + AtomicCounterMapBuilder<K> withName(String name);
36 +
37 + /**
38 + * Creates this counter map on the partition that spans the entire cluster.
39 + * <p>
40 + * When partitioning is disabled, the counter state will be
41 + * ephemeral and does not survive a full cluster restart.
42 + * </p>
43 + * <p>
44 + * Note: By default partitions are enabled.
45 + * </p>
46 + * @return this AtomicCounterMapBuilder
47 + */
48 + AtomicCounterMapBuilder<K> withPartitionsDisabled();
49 +
50 + /**
51 + * Instantiates Metering service to gather usage and performance metrics.
52 + * By default, usage data will be stored.
53 + *
54 + * @return this AtomicCounterMapBuilder
55 + */
56 + AtomicCounterMapBuilder<K> withMeteringDisabled();
57 +
58 + /**
59 + * Builds a AtomicCounterMap based on the configuration options
60 + * supplied to this builder.
61 + *
62 + * @return new AtomicCounterMap
63 + * @throws java.lang.RuntimeException if a mandatory parameter is missing
64 + */
65 + AtomicCounterMap<K> build();
66 +
67 + /**
68 + * Builds a AsyncAtomicCounterMap based on the configuration options
69 + * supplied to this builder.
70 + *
71 + * @return new AsyncAtomicCounterMap
72 + * @throws java.lang.RuntimeException if a mandatory parameter is missing
73 + */
74 + AsyncAtomicCounterMap<K> buildAsyncAtomicCounterMap();
75 +}