blob: 6838ab30ef3db22c02142411a275fc03a683b9d7 [file] [log] [blame]
Aaron Kruglikova26f6542016-04-19 13:37:42 -07001/*
2 * Copyright 2016 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
17package org.onosproject.store.primitives.resources.impl;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.Multiset;
21import io.atomix.catalyst.buffer.BufferInput;
22import io.atomix.catalyst.buffer.BufferOutput;
23import io.atomix.catalyst.serializer.CatalystSerializable;
24import io.atomix.catalyst.serializer.SerializableTypeResolver;
25import io.atomix.catalyst.serializer.Serializer;
26import io.atomix.catalyst.serializer.SerializerRegistry;
27import io.atomix.catalyst.util.Assert;
28import io.atomix.copycat.Command;
29import io.atomix.copycat.Query;
30import org.onlab.util.Match;
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -070031import org.onosproject.store.service.Versioned;
Aaron Kruglikova26f6542016-04-19 13:37:42 -070032
33import java.util.Collection;
Aaron Kruglikova26f6542016-04-19 13:37:42 -070034import java.util.Map;
35import java.util.Set;
36
37/**
38 * AsyncConsistentMultimap state machine commands.
39 */
40public final class AsyncConsistentMultimapCommands {
41
42 private AsyncConsistentMultimapCommands() {
43 }
44
45 /**
46 * Abstract multimap command.
47 */
48 @SuppressWarnings("serial")
49 public abstract static class MultimapCommand<V> implements Command<V>,
50 CatalystSerializable {
51 @Override
52 public ConsistencyLevel consistency() {
53 return ConsistencyLevel.SEQUENTIAL;
54 }
55
56 @Override
57 public String toString() {
58 return MoreObjects.toStringHelper(getClass())
59 .toString();
60 }
61
62 @Override
63 public void writeObject(BufferOutput<?> buffer,
64 Serializer serializer) {
65 }
66
67 @Override
68 public void readObject(BufferInput<?> buffer, Serializer serializer) {
69 }
70 }
71
72 /**
73 * Abstract multimap query.
74 */
75 @SuppressWarnings("serial")
76 public abstract static class MultimapQuery<V> implements Query<V>,
77 CatalystSerializable {
78 @Override
79 public ConsistencyLevel consistency() {
80 return ConsistencyLevel.SEQUENTIAL;
81 }
82
83 @Override
84 public String toString() {
85 return MoreObjects.toStringHelper(getClass())
86 .toString();
87 }
88
89 @Override
90 public void writeObject(BufferOutput<?> buffer,
91 Serializer serializer) {
92 }
93
94 @Override
95 public void readObject(BufferInput<?> buffer,
96 Serializer serializer) {
97 }
98 }
99
100 /**
101 * Abstract key-based multimap query.
102 */
103 @SuppressWarnings("serial")
104 public abstract static class KeyQuery<V> extends MultimapQuery<V> {
105 protected String key;
106
107 public KeyQuery() {
108 }
109
110 public KeyQuery(String key) {
111 this.key = Assert.notNull(key, "key");
112 }
113
114 public String key() {
115 return key;
116 }
117
118 @Override
119 public String toString() {
120 return MoreObjects.toStringHelper(getClass())
121 .add("key", key)
122 .toString();
123 }
124
125 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700126 public void writeObject(BufferOutput<?> buffer,
127 Serializer serializer) {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700128 super.writeObject(buffer, serializer);
129 serializer.writeObject(key, buffer);
130 }
131
132 @Override
133 public void readObject(BufferInput<?> buffer, Serializer serializer) {
134 super.readObject(buffer, serializer);
135 key = serializer.readObject(buffer);
136 }
137 }
138
139 /**
140 * Abstract value-based query.
141 */
142 @SuppressWarnings("serial")
143 public abstract static class ValueQuery<V> extends MultimapQuery<V> {
144 protected byte[] value;
145
146 public ValueQuery() {
147 }
148
149 public ValueQuery(byte[] value) {
150 this.value = Assert.notNull(value, "value");
151 }
152
153 /**
154 * Returns the value.
155 *
156 * @return value.
157 */
158 public byte[] value() {
159 return value;
160 }
161
162 @Override
163 public String toString() {
164 return MoreObjects.toStringHelper(getClass())
165 .add("value", value)
166 .toString();
167 }
168
169 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700170 public void writeObject(BufferOutput<?> buffer,
171 Serializer serializer) {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700172 super.writeObject(buffer, serializer);
173 }
174
175 @Override
176 public void readObject(BufferInput<?> buffer, Serializer serializer) {
177 super.readObject(buffer, serializer);
178 }
179 }
180
181 /**
182 * Size query.
183 */
184 public static class Size extends MultimapQuery<Integer> {
185 }
186
187 /**
188 * Is empty query.
189 */
190 public static class IsEmpty extends MultimapQuery<Boolean> {
191 }
192
193 /**
194 * Contains key query.
195 */
196 @SuppressWarnings("serial")
197 public static class ContainsKey extends KeyQuery<Boolean> {
198 public ContainsKey() {
199 }
200
201 public ContainsKey(String key) {
202 super(key);
203 }
204
205 }
206
207 /**
208 * Contains value query.
209 */
210 @SuppressWarnings("serial")
211 public static class ContainsValue extends ValueQuery<Boolean> {
212 public ContainsValue() {
213 }
214
215 public ContainsValue(byte[] value) {
216 super(value);
217 }
218 }
219
220 /**
221 * Contains entry query.
222 */
223 @SuppressWarnings("serial")
224 public static class ContainsEntry extends MultimapQuery<Boolean> {
225 protected String key;
226 protected byte[] value;
227
228 public ContainsEntry() {
229 }
230
231 public ContainsEntry(String key, byte[] value) {
232 this.key = Assert.notNull(key, "key");
233 this.value = Assert.notNull(value, "value");
234 }
235
236 public String key() {
237 return key;
238 }
239
240 public byte[] value() {
241 return value;
242 }
243
244 @Override
245 public String toString() {
246 return MoreObjects.toStringHelper(getClass())
247 .add("key", key)
248 .add("value", value)
249 .toString();
250 }
251
252 @Override
253 public void writeObject(BufferOutput<?> buffer,
254 Serializer serializer) {
255 super.writeObject(buffer, serializer);
256 serializer.writeObject(key, buffer);
257 serializer.writeObject(value, buffer);
258 }
259
260 @Override
261 public void readObject(BufferInput<?> buffer, Serializer serializer) {
262 super.readObject(buffer, serializer);
263 key = serializer.readObject(buffer);
264 value = serializer.readObject(buffer);
265
266 }
267 }
268
269 /**
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700270 * Remove command, backs remove and removeAll's that return booleans.
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700271 */
272 @SuppressWarnings("serial")
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700273 public static class RemoveAll extends
274 MultimapCommand<Versioned<Collection<? extends byte[]>>> {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700275 private String key;
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700276 private Match<Long> versionMatch;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700277
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700278 public RemoveAll() {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700279 }
280
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700281 public RemoveAll(String key, Match<Long> versionMatch) {
282 this.key = Assert.notNull(key, "key");
283 this.versionMatch = versionMatch;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700284 }
285
286 public String key() {
287 return this.key;
288 }
289
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700290 public Match<Long> versionMatch() {
291 return versionMatch;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700292 }
293
294 @Override
295 public CompactionMode compaction() {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700296 return CompactionMode.FULL;
297 }
298
299 @Override
300 public void writeObject(BufferOutput<?> buffer,
301 Serializer serializer) {
302 super.writeObject(buffer, serializer);
303 serializer.writeObject(key, buffer);
304 serializer.writeObject(versionMatch, buffer);
305 }
306
307 @Override
308 public void readObject(BufferInput<?> buffer, Serializer serializer) {
309 super.readObject(buffer, serializer);
310 key = serializer.readObject(buffer);
311 versionMatch = serializer.readObject(buffer);
312 }
313
314 @Override
315 public String toString() {
316 return MoreObjects.toStringHelper(getClass())
317 .add("key", key)
318 .add("versionMatch", versionMatch)
319 .toString();
320 }
321 }
322
323 /**
324 * Remove command, backs remove and removeAll's that return booleans.
325 */
326 @SuppressWarnings("serial")
327 public static class MultiRemove extends
328 MultimapCommand<Boolean> {
329 private String key;
330 private Collection<byte[]> values;
331 private Match<Long> versionMatch;
332
333 public MultiRemove() {
334 }
335
336 public MultiRemove(String key, Collection<byte[]> valueMatches,
337 Match<Long> versionMatch) {
338 this.key = Assert.notNull(key, "key");
339 this.values = valueMatches;
340 this.versionMatch = versionMatch;
341 }
342
343 public String key() {
344 return this.key;
345 }
346
347 public Collection<byte[]> values() {
348 return values;
349 }
350
351 public Match<Long> versionMatch() {
352 return versionMatch;
353 }
354
355 @Override
356 public CompactionMode compaction() {
357 return CompactionMode.FULL;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700358 }
359
360 @Override
361 public void writeObject(BufferOutput<?> buffer,
362 Serializer serializer) {
363 super.writeObject(buffer, serializer);
364 serializer.writeObject(key, buffer);
365 serializer.writeObject(values, buffer);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700366 serializer.writeObject(versionMatch, buffer);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700367 }
368
369 @Override
370 public void readObject(BufferInput<?> buffer, Serializer serializer) {
371 super.readObject(buffer, serializer);
372 key = serializer.readObject(buffer);
373 values = serializer.readObject(buffer);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700374 versionMatch = serializer.readObject(buffer);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700375 }
376
377 @Override
378 public String toString() {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700379 return MoreObjects.toStringHelper(getClass())
380 .add("key", key)
381 .add("values", values)
382 .add("versionMatch", versionMatch)
383 .toString();
384 }
385 }
386
387 /**
388 * Command to back the put and putAll methods.
389 */
390 @SuppressWarnings("serial")
391 public static class Put extends MultimapCommand<Boolean> {
392 private String key;
393 private Collection<? extends byte[]> values;
394 private Match<Long> versionMatch;
395
396 public Put() {
397 }
398
399 public Put(String key, Collection<? extends byte[]> values,
400 Match<Long> versionMatch) {
401 this.key = Assert.notNull(key, "key");
402 this.values = values;
403 this.versionMatch = versionMatch;
404 }
405
406 public String key() {
407 return key;
408 }
409
410 public Collection<? extends byte[]> values() {
411 return values;
412 }
413
414 public Match<Long> versionMatch() {
415 return versionMatch;
416 }
417
418 @Override
419 public CompactionMode compaction() {
420 return CompactionMode.QUORUM;
421 }
422
423 @Override
424 public void writeObject(BufferOutput<?> buffer,
425 Serializer serializer) {
426 super.writeObject(buffer, serializer);
427 serializer.writeObject(key, buffer);
428 serializer.writeObject(values, buffer);
429 serializer.writeObject(versionMatch, buffer);
430 }
431
432 @Override
433 public void readObject(BufferInput<?> buffer, Serializer serializer) {
434 super.readObject(buffer, serializer);
435 key = serializer.readObject(buffer);
436 values = serializer.readObject(buffer);
437 versionMatch = serializer.readObject(buffer);
438 }
439
440 @Override
441 public String toString() {
442 return MoreObjects.toStringHelper(getClass())
443 .add("key", key)
444 .add("values", values)
445 .add("versionMatch", versionMatch)
446 .toString();
447 }
448 }
449
450 /**
451 * Replace command, returns the collection that was replaced.
452 */
453 @SuppressWarnings("serial")
454 public static class Replace extends
455 MultimapCommand<Versioned<Collection<? extends byte[]>>> {
456 private String key;
457 private Collection<byte[]> values;
458 private Match<Long> versionMatch;
459
460 public Replace() {
461 }
462
463 public Replace(String key, Collection<byte[]> values,
464 Match<Long> versionMatch) {
465 this.key = Assert.notNull(key, "key");
466 this.values = values;
467 this.versionMatch = versionMatch;
468 }
469
470 public String key() {
471 return this.key;
472 }
473
474 public Match<Long> versionMatch() {
475 return versionMatch;
476 }
477
478 public Collection<byte[]> values() {
479 return values;
480 }
481
482 @Override
483 public CompactionMode compaction() {
484 return CompactionMode.FULL;
485 }
486
487 @Override
488 public void writeObject(BufferOutput<?> buffer,
489 Serializer serializer) {
490 super.writeObject(buffer, serializer);
491 serializer.writeObject(key, buffer);
492 serializer.writeObject(values, buffer);
493 serializer.writeObject(versionMatch, buffer);
494 }
495
496 @Override
497 public void readObject(BufferInput<?> buffer, Serializer serializer) {
498 super.readObject(buffer, serializer);
499 key = serializer.readObject(buffer);
500 values = serializer.readObject(buffer);
501 versionMatch = serializer.readObject(buffer);
502 }
503
504 @Override
505 public String toString() {
506 return MoreObjects.toStringHelper(getClass())
507 .add("key", key)
508 .add("values", values)
509 .add("versionMatch", versionMatch)
510 .toString();
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700511 }
512 }
513
514 /**
515 * Clear multimap command.
516 */
517 @SuppressWarnings("serial")
518 public static class Clear extends MultimapCommand<Void> {
519 }
520
521 /**
522 * Key set query.
523 */
524 @SuppressWarnings("serial")
525 public static class KeySet extends MultimapQuery<Set<String>> {
526 }
527
528 /**
529 * Key multiset query.
530 */
531 @SuppressWarnings("serial")
532 public static class Keys extends MultimapQuery<Multiset<String>> {
533 }
534
535 /**
536 * Value collection query.
537 */
538 @SuppressWarnings("serial")
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700539 public static class Values extends MultimapQuery<Multiset<byte[]>> {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700540 }
541
542 /**
543 * Entry set query.
544 */
545 @SuppressWarnings("serial")
546 public static class Entries extends
547 MultimapQuery<Collection<Map.Entry<String, byte[]>>> {
548 }
549
550 /**
551 * Get value query.
552 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700553 public static class Get extends
554 KeyQuery<Versioned<Collection<? extends byte[]>>> {
555 public Get(String key) {
556 super(key);
557 }
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700558 }
559
560 /**
561 * Multimap command type resolver.
562 */
563 @SuppressWarnings("serial")
564 public static class TypeResolver implements SerializableTypeResolver {
565 @Override
566 public void resolve(SerializerRegistry registry) {
567 registry.register(ContainsKey.class, -1000);
568 registry.register(ContainsValue.class, -1001);
569 registry.register(ContainsEntry.class, -1002);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700570 registry.register(Replace.class, -1003);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700571 registry.register(Clear.class, -1004);
572 registry.register(KeySet.class, -1005);
573 registry.register(Keys.class, -1006);
574 registry.register(Values.class, -1007);
575 registry.register(Entries.class, -1008);
576 registry.register(Size.class, -1009);
577 registry.register(IsEmpty.class, -1010);
578 registry.register(Get.class, -1011);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700579 registry.register(Put.class, -1012);
580 registry.register(RemoveAll.class, -1013);
581 registry.register(MultiRemove.class, -1014);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700582 }
583 }
584}