blob: 4d38d717345da2d0410771a0a58ffb202701e634 [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jordan Halterman2bf177c2017-06-29 01:49:08 -07003 *
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
17package org.onosproject.store.primitives.resources.impl;
18
19import java.util.ArrayList;
20import java.util.Collection;
Jordan Halterman15f33712018-06-21 00:00:15 -070021import java.util.Iterator;
22import java.util.Map;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070023
24import com.google.common.base.MoreObjects;
25import com.google.common.collect.Maps;
26import io.atomix.protocols.raft.operation.OperationId;
27import io.atomix.protocols.raft.operation.OperationType;
28import org.onlab.util.KryoNamespace;
29import org.onlab.util.Match;
30import org.onosproject.store.serializers.KryoNamespaces;
31import org.onosproject.store.service.Versioned;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * AsyncConsistentMultimap state machine commands.
37 */
38public enum AtomixConsistentSetMultimapOperations implements OperationId {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080039 GET(OperationType.QUERY),
40 SIZE(OperationType.QUERY),
41 IS_EMPTY(OperationType.QUERY),
42 CONTAINS_KEY(OperationType.QUERY),
43 CONTAINS_VALUE(OperationType.QUERY),
44 CONTAINS_ENTRY(OperationType.QUERY),
45 KEY_SET(OperationType.QUERY),
46 KEYS(OperationType.QUERY),
47 VALUES(OperationType.QUERY),
48 ENTRIES(OperationType.QUERY),
49 PUT(OperationType.COMMAND),
Jordan Halterman8c57a092018-06-04 14:53:06 -070050 PUT_AND_GET(OperationType.COMMAND),
Jordan Halterman2b7501c2017-11-29 14:05:33 -080051 REMOVE(OperationType.COMMAND),
Jordan Halterman8c57a092018-06-04 14:53:06 -070052 REMOVE_AND_GET(OperationType.COMMAND),
Jordan Halterman2b7501c2017-11-29 14:05:33 -080053 REMOVE_ALL(OperationType.COMMAND),
54 REPLACE(OperationType.COMMAND),
55 CLEAR(OperationType.COMMAND),
56 ADD_LISTENER(OperationType.COMMAND),
Jordan Halterman5e884352018-05-21 22:11:07 -070057 REMOVE_LISTENER(OperationType.COMMAND),
Jordan Halterman15f33712018-06-21 00:00:15 -070058 OPEN_ITERATOR(OperationType.COMMAND),
59 NEXT(OperationType.QUERY),
60 CLOSE_ITERATOR(OperationType.COMMAND);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070061
Jordan Halterman2bf177c2017-06-29 01:49:08 -070062 private final OperationType type;
63
Jordan Halterman2b7501c2017-11-29 14:05:33 -080064 AtomixConsistentSetMultimapOperations(OperationType type) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070065 this.type = type;
66 }
67
68 @Override
69 public String id() {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080070 return name();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070071 }
72
73 @Override
74 public OperationType type() {
75 return type;
76 }
77
78 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
79 .register(KryoNamespaces.BASIC)
80 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
81 .register(ContainsEntry.class)
82 .register(ContainsKey.class)
83 .register(ContainsValue.class)
84 .register(Get.class)
85 .register(MultiRemove.class)
86 .register(Put.class)
87 .register(RemoveAll.class)
88 .register(Replace.class)
89 .register(Match.class)
90 .register(Versioned.class)
91 .register(ArrayList.class)
92 .register(Maps.immutableEntry("", "").getClass())
Jordan Halterman15f33712018-06-21 00:00:15 -070093 .register(IteratorBatch.class)
94 .register(IteratorPosition.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070095 .build("AtomixConsistentSetMultimapOperations");
96
97 /**
98 * Abstract multimap command.
99 */
100 @SuppressWarnings("serial")
101 public abstract static class MultimapOperation {
102 @Override
103 public String toString() {
104 return MoreObjects.toStringHelper(getClass())
105 .toString();
106 }
107 }
108
109 /**
110 * Abstract key-based multimap query.
111 */
112 @SuppressWarnings("serial")
113 public abstract static class KeyOperation extends MultimapOperation {
114 protected String key;
115
116 public KeyOperation() {
117 }
118
119 public KeyOperation(String key) {
120 this.key = checkNotNull(key);
121 }
122
123 public String key() {
124 return key;
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(getClass())
130 .add("key", key)
131 .toString();
132 }
133 }
134
135 /**
136 * Abstract value-based query.
137 */
138 @SuppressWarnings("serial")
139 public abstract static class ValueOperation extends MultimapOperation {
140 protected byte[] value;
141
142 public ValueOperation() {
143 }
144
145 public ValueOperation(byte[] value) {
146 this.value = checkNotNull(value);
147 }
148
149 /**
150 * Returns the value.
151 *
152 * @return value.
153 */
154 public byte[] value() {
155 return value;
156 }
157
158 @Override
159 public String toString() {
160 return MoreObjects.toStringHelper(getClass())
161 .add("value", value)
162 .toString();
163 }
164 }
165
166 /**
167 * Contains key query.
168 */
169 @SuppressWarnings("serial")
170 public static class ContainsKey extends KeyOperation {
171 public ContainsKey() {
172 }
173
174 public ContainsKey(String key) {
175 super(key);
176 }
177 }
178
179 /**
180 * Contains value query.
181 */
182 @SuppressWarnings("serial")
183 public static class ContainsValue extends ValueOperation {
184 public ContainsValue() {
185 }
186
187 public ContainsValue(byte[] value) {
188 super(value);
189 }
190 }
191
192 /**
193 * Contains entry query.
194 */
195 @SuppressWarnings("serial")
196 public static class ContainsEntry extends MultimapOperation {
197 protected String key;
198 protected byte[] value;
199
200 public ContainsEntry() {
201 }
202
203 public ContainsEntry(String key, byte[] value) {
204 this.key = checkNotNull(key);
205 this.value = checkNotNull(value);
206 }
207
208 public String key() {
209 return key;
210 }
211
212 public byte[] value() {
213 return value;
214 }
215
216 @Override
217 public String toString() {
218 return MoreObjects.toStringHelper(getClass())
219 .add("key", key)
220 .add("value", value)
221 .toString();
222 }
223 }
224
225 /**
226 * Remove command, backs remove and removeAll's that return booleans.
227 */
228 @SuppressWarnings("serial")
229 public static class RemoveAll extends MultimapOperation {
230 private String key;
231 private Match<Long> versionMatch;
232
233 public RemoveAll() {
234 }
235
236 public RemoveAll(String key, Match<Long> versionMatch) {
237 this.key = checkNotNull(key);
238 this.versionMatch = versionMatch;
239 }
240
241 public String key() {
242 return this.key;
243 }
244
245 public Match<Long> versionMatch() {
246 return versionMatch;
247 }
248
249 @Override
250 public String toString() {
251 return MoreObjects.toStringHelper(getClass())
252 .add("key", key)
253 .add("versionMatch", versionMatch)
254 .toString();
255 }
256 }
257
258 /**
259 * Remove command, backs remove and removeAll's that return booleans.
260 */
261 @SuppressWarnings("serial")
262 public static class MultiRemove extends MultimapOperation {
263 private String key;
264 private Collection<byte[]> values;
265 private Match<Long> versionMatch;
266
267 public MultiRemove() {
268 }
269
270 public MultiRemove(String key, Collection<byte[]> valueMatches,
271 Match<Long> versionMatch) {
272 this.key = checkNotNull(key);
273 this.values = valueMatches;
274 this.versionMatch = versionMatch;
275 }
276
277 public String key() {
278 return this.key;
279 }
280
281 public Collection<byte[]> values() {
282 return values;
283 }
284
285 public Match<Long> versionMatch() {
286 return versionMatch;
287 }
288
289 @Override
290 public String toString() {
291 return MoreObjects.toStringHelper(getClass())
292 .add("key", key)
293 .add("values", values)
294 .add("versionMatch", versionMatch)
295 .toString();
296 }
297 }
298
299 /**
300 * Command to back the put and putAll methods.
301 */
302 @SuppressWarnings("serial")
303 public static class Put extends MultimapOperation {
304 private String key;
305 private Collection<? extends byte[]> values;
306 private Match<Long> versionMatch;
307
308 public Put() {
309 }
310
311 public Put(String key, Collection<? extends byte[]> values, Match<Long> versionMatch) {
312 this.key = checkNotNull(key);
313 this.values = values;
314 this.versionMatch = versionMatch;
315 }
316
317 public String key() {
318 return key;
319 }
320
321 public Collection<? extends byte[]> values() {
322 return values;
323 }
324
325 public Match<Long> versionMatch() {
326 return versionMatch;
327 }
328
329 @Override
330 public String toString() {
331 return MoreObjects.toStringHelper(getClass())
332 .add("key", key)
333 .add("values", values)
334 .add("versionMatch", versionMatch)
335 .toString();
336 }
337 }
338
339 /**
340 * Replace command, returns the collection that was replaced.
341 */
342 @SuppressWarnings("serial")
343 public static class Replace extends MultimapOperation {
344 private String key;
345 private Collection<byte[]> values;
346 private Match<Long> versionMatch;
347
348 public Replace() {
349 }
350
351 public Replace(String key, Collection<byte[]> values,
352 Match<Long> versionMatch) {
353 this.key = checkNotNull(key);
354 this.values = values;
355 this.versionMatch = versionMatch;
356 }
357
358 public String key() {
359 return this.key;
360 }
361
362 public Match<Long> versionMatch() {
363 return versionMatch;
364 }
365
366 public Collection<byte[]> values() {
367 return values;
368 }
369
370 @Override
371 public String toString() {
372 return MoreObjects.toStringHelper(getClass())
373 .add("key", key)
374 .add("values", values)
375 .add("versionMatch", versionMatch)
376 .toString();
377 }
378 }
379
380 /**
381 * Get value query.
382 */
383 public static class Get extends KeyOperation {
384 public Get() {
385 }
386
387 public Get(String key) {
388 super(key);
389 }
390 }
Jordan Halterman15f33712018-06-21 00:00:15 -0700391
392 /**
393 * Iterator position.
394 */
395 public static class IteratorPosition {
396 private long iteratorId;
397 private int position;
398
399 private IteratorPosition() {
400 }
401
402 public IteratorPosition(long iteratorId, int position) {
403 this.iteratorId = iteratorId;
404 this.position = position;
405 }
406
407 public long iteratorId() {
408 return iteratorId;
409 }
410
411 public int position() {
412 return position;
413 }
414 }
415
416 /**
417 * Iterator batch.
418 */
419 public static class IteratorBatch implements Iterator<Map.Entry<String, byte[]>> {
420 private int position;
421 private Collection<Map.Entry<String, byte[]>> entries;
422 private transient volatile Iterator<Map.Entry<String, byte[]>> iterator;
423
424 private IteratorBatch() {
425 }
426
427 public IteratorBatch(int position, Collection<Map.Entry<String, byte[]>> entries) {
428 this.position = position;
429 this.entries = entries;
430 }
431
432 public int position() {
433 return position;
434 }
435
436 public Collection<Map.Entry<String, byte[]>> entries() {
437 return entries;
438 }
439
440 private Iterator<Map.Entry<String, byte[]>> iterator() {
441 Iterator<Map.Entry<String, byte[]>> iterator = this.iterator;
442 if (iterator == null) {
443 synchronized (entries) {
444 iterator = this.iterator;
445 if (iterator == null) {
446 iterator = entries.iterator();
447 this.iterator = iterator;
448 }
449 }
450 }
451 return iterator;
452 }
453
454 @Override
455 public boolean hasNext() {
456 return iterator().hasNext();
457 }
458
459 @Override
460 public Map.Entry<String, byte[]> next() {
461 return iterator().next();
462 }
463 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700464}