blob: 251a7caeaf7e875541a7ee05773ff1ccdb4ead7b [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 {
Aaron Kruglikova26f6542016-04-19 13:37:42 -070051
52 @Override
53 public String toString() {
54 return MoreObjects.toStringHelper(getClass())
55 .toString();
56 }
57
58 @Override
59 public void writeObject(BufferOutput<?> buffer,
60 Serializer serializer) {
61 }
62
63 @Override
64 public void readObject(BufferInput<?> buffer, Serializer serializer) {
65 }
66 }
67
68 /**
69 * Abstract multimap query.
70 */
71 @SuppressWarnings("serial")
72 public abstract static class MultimapQuery<V> implements Query<V>,
73 CatalystSerializable {
74 @Override
75 public ConsistencyLevel consistency() {
76 return ConsistencyLevel.SEQUENTIAL;
77 }
78
79 @Override
80 public String toString() {
81 return MoreObjects.toStringHelper(getClass())
82 .toString();
83 }
84
85 @Override
86 public void writeObject(BufferOutput<?> buffer,
87 Serializer serializer) {
88 }
89
90 @Override
91 public void readObject(BufferInput<?> buffer,
92 Serializer serializer) {
93 }
94 }
95
96 /**
97 * Abstract key-based multimap query.
98 */
99 @SuppressWarnings("serial")
100 public abstract static class KeyQuery<V> extends MultimapQuery<V> {
101 protected String key;
102
103 public KeyQuery() {
104 }
105
106 public KeyQuery(String key) {
107 this.key = Assert.notNull(key, "key");
108 }
109
110 public String key() {
111 return key;
112 }
113
114 @Override
115 public String toString() {
116 return MoreObjects.toStringHelper(getClass())
117 .add("key", key)
118 .toString();
119 }
120
121 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700122 public void writeObject(BufferOutput<?> buffer,
123 Serializer serializer) {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700124 super.writeObject(buffer, serializer);
125 serializer.writeObject(key, buffer);
126 }
127
128 @Override
129 public void readObject(BufferInput<?> buffer, Serializer serializer) {
130 super.readObject(buffer, serializer);
131 key = serializer.readObject(buffer);
132 }
133 }
134
135 /**
136 * Abstract value-based query.
137 */
138 @SuppressWarnings("serial")
139 public abstract static class ValueQuery<V> extends MultimapQuery<V> {
140 protected byte[] value;
141
142 public ValueQuery() {
143 }
144
145 public ValueQuery(byte[] value) {
146 this.value = Assert.notNull(value, "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 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700166 public void writeObject(BufferOutput<?> buffer,
167 Serializer serializer) {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700168 super.writeObject(buffer, serializer);
169 }
170
171 @Override
172 public void readObject(BufferInput<?> buffer, Serializer serializer) {
173 super.readObject(buffer, serializer);
174 }
175 }
176
177 /**
178 * Size query.
179 */
180 public static class Size extends MultimapQuery<Integer> {
181 }
182
183 /**
184 * Is empty query.
185 */
186 public static class IsEmpty extends MultimapQuery<Boolean> {
187 }
188
189 /**
190 * Contains key query.
191 */
192 @SuppressWarnings("serial")
193 public static class ContainsKey extends KeyQuery<Boolean> {
194 public ContainsKey() {
195 }
196
197 public ContainsKey(String key) {
198 super(key);
199 }
200
201 }
202
203 /**
204 * Contains value query.
205 */
206 @SuppressWarnings("serial")
207 public static class ContainsValue extends ValueQuery<Boolean> {
208 public ContainsValue() {
209 }
210
211 public ContainsValue(byte[] value) {
212 super(value);
213 }
214 }
215
216 /**
217 * Contains entry query.
218 */
219 @SuppressWarnings("serial")
220 public static class ContainsEntry extends MultimapQuery<Boolean> {
221 protected String key;
222 protected byte[] value;
223
224 public ContainsEntry() {
225 }
226
227 public ContainsEntry(String key, byte[] value) {
228 this.key = Assert.notNull(key, "key");
229 this.value = Assert.notNull(value, "value");
230 }
231
232 public String key() {
233 return key;
234 }
235
236 public byte[] value() {
237 return value;
238 }
239
240 @Override
241 public String toString() {
242 return MoreObjects.toStringHelper(getClass())
243 .add("key", key)
244 .add("value", value)
245 .toString();
246 }
247
248 @Override
249 public void writeObject(BufferOutput<?> buffer,
250 Serializer serializer) {
251 super.writeObject(buffer, serializer);
252 serializer.writeObject(key, buffer);
253 serializer.writeObject(value, buffer);
254 }
255
256 @Override
257 public void readObject(BufferInput<?> buffer, Serializer serializer) {
258 super.readObject(buffer, serializer);
259 key = serializer.readObject(buffer);
260 value = serializer.readObject(buffer);
261
262 }
263 }
264
265 /**
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700266 * Remove command, backs remove and removeAll's that return booleans.
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700267 */
268 @SuppressWarnings("serial")
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700269 public static class RemoveAll extends
270 MultimapCommand<Versioned<Collection<? extends byte[]>>> {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700271 private String key;
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700272 private Match<Long> versionMatch;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700273
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700274 public RemoveAll() {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700275 }
276
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700277 public RemoveAll(String key, Match<Long> versionMatch) {
278 this.key = Assert.notNull(key, "key");
279 this.versionMatch = versionMatch;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700280 }
281
282 public String key() {
283 return this.key;
284 }
285
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700286 public Match<Long> versionMatch() {
287 return versionMatch;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700288 }
289
290 @Override
291 public CompactionMode compaction() {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700292 return CompactionMode.FULL;
293 }
294
295 @Override
296 public void writeObject(BufferOutput<?> buffer,
297 Serializer serializer) {
298 super.writeObject(buffer, serializer);
299 serializer.writeObject(key, buffer);
300 serializer.writeObject(versionMatch, buffer);
301 }
302
303 @Override
304 public void readObject(BufferInput<?> buffer, Serializer serializer) {
305 super.readObject(buffer, serializer);
306 key = serializer.readObject(buffer);
307 versionMatch = serializer.readObject(buffer);
308 }
309
310 @Override
311 public String toString() {
312 return MoreObjects.toStringHelper(getClass())
313 .add("key", key)
314 .add("versionMatch", versionMatch)
315 .toString();
316 }
317 }
318
319 /**
320 * Remove command, backs remove and removeAll's that return booleans.
321 */
322 @SuppressWarnings("serial")
323 public static class MultiRemove extends
324 MultimapCommand<Boolean> {
325 private String key;
326 private Collection<byte[]> values;
327 private Match<Long> versionMatch;
328
329 public MultiRemove() {
330 }
331
332 public MultiRemove(String key, Collection<byte[]> valueMatches,
333 Match<Long> versionMatch) {
334 this.key = Assert.notNull(key, "key");
335 this.values = valueMatches;
336 this.versionMatch = versionMatch;
337 }
338
339 public String key() {
340 return this.key;
341 }
342
343 public Collection<byte[]> values() {
344 return values;
345 }
346
347 public Match<Long> versionMatch() {
348 return versionMatch;
349 }
350
351 @Override
352 public CompactionMode compaction() {
353 return CompactionMode.FULL;
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700354 }
355
356 @Override
357 public void writeObject(BufferOutput<?> buffer,
358 Serializer serializer) {
359 super.writeObject(buffer, serializer);
360 serializer.writeObject(key, buffer);
361 serializer.writeObject(values, buffer);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700362 serializer.writeObject(versionMatch, buffer);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700363 }
364
365 @Override
366 public void readObject(BufferInput<?> buffer, Serializer serializer) {
367 super.readObject(buffer, serializer);
368 key = serializer.readObject(buffer);
369 values = serializer.readObject(buffer);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700370 versionMatch = serializer.readObject(buffer);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700371 }
372
373 @Override
374 public String toString() {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700375 return MoreObjects.toStringHelper(getClass())
376 .add("key", key)
377 .add("values", values)
378 .add("versionMatch", versionMatch)
379 .toString();
380 }
381 }
382
383 /**
384 * Command to back the put and putAll methods.
385 */
386 @SuppressWarnings("serial")
387 public static class Put extends MultimapCommand<Boolean> {
388 private String key;
389 private Collection<? extends byte[]> values;
390 private Match<Long> versionMatch;
391
392 public Put() {
393 }
394
395 public Put(String key, Collection<? extends byte[]> values,
396 Match<Long> versionMatch) {
397 this.key = Assert.notNull(key, "key");
398 this.values = values;
399 this.versionMatch = versionMatch;
400 }
401
402 public String key() {
403 return key;
404 }
405
406 public Collection<? extends byte[]> values() {
407 return values;
408 }
409
410 public Match<Long> versionMatch() {
411 return versionMatch;
412 }
413
414 @Override
415 public CompactionMode compaction() {
416 return CompactionMode.QUORUM;
417 }
418
419 @Override
420 public void writeObject(BufferOutput<?> buffer,
421 Serializer serializer) {
422 super.writeObject(buffer, serializer);
423 serializer.writeObject(key, buffer);
424 serializer.writeObject(values, buffer);
425 serializer.writeObject(versionMatch, buffer);
426 }
427
428 @Override
429 public void readObject(BufferInput<?> buffer, Serializer serializer) {
430 super.readObject(buffer, serializer);
431 key = serializer.readObject(buffer);
432 values = serializer.readObject(buffer);
433 versionMatch = serializer.readObject(buffer);
434 }
435
436 @Override
437 public String toString() {
438 return MoreObjects.toStringHelper(getClass())
439 .add("key", key)
440 .add("values", values)
441 .add("versionMatch", versionMatch)
442 .toString();
443 }
444 }
445
446 /**
447 * Replace command, returns the collection that was replaced.
448 */
449 @SuppressWarnings("serial")
450 public static class Replace extends
451 MultimapCommand<Versioned<Collection<? extends byte[]>>> {
452 private String key;
453 private Collection<byte[]> values;
454 private Match<Long> versionMatch;
455
456 public Replace() {
457 }
458
459 public Replace(String key, Collection<byte[]> values,
460 Match<Long> versionMatch) {
461 this.key = Assert.notNull(key, "key");
462 this.values = values;
463 this.versionMatch = versionMatch;
464 }
465
466 public String key() {
467 return this.key;
468 }
469
470 public Match<Long> versionMatch() {
471 return versionMatch;
472 }
473
474 public Collection<byte[]> values() {
475 return values;
476 }
477
478 @Override
479 public CompactionMode compaction() {
480 return CompactionMode.FULL;
481 }
482
483 @Override
484 public void writeObject(BufferOutput<?> buffer,
485 Serializer serializer) {
486 super.writeObject(buffer, serializer);
487 serializer.writeObject(key, buffer);
488 serializer.writeObject(values, buffer);
489 serializer.writeObject(versionMatch, buffer);
490 }
491
492 @Override
493 public void readObject(BufferInput<?> buffer, Serializer serializer) {
494 super.readObject(buffer, serializer);
495 key = serializer.readObject(buffer);
496 values = serializer.readObject(buffer);
497 versionMatch = serializer.readObject(buffer);
498 }
499
500 @Override
501 public String toString() {
502 return MoreObjects.toStringHelper(getClass())
503 .add("key", key)
504 .add("values", values)
505 .add("versionMatch", versionMatch)
506 .toString();
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700507 }
508 }
509
510 /**
511 * Clear multimap command.
512 */
513 @SuppressWarnings("serial")
514 public static class Clear extends MultimapCommand<Void> {
515 }
516
517 /**
518 * Key set query.
519 */
520 @SuppressWarnings("serial")
521 public static class KeySet extends MultimapQuery<Set<String>> {
522 }
523
524 /**
525 * Key multiset query.
526 */
527 @SuppressWarnings("serial")
528 public static class Keys extends MultimapQuery<Multiset<String>> {
529 }
530
531 /**
532 * Value collection query.
533 */
534 @SuppressWarnings("serial")
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700535 public static class Values extends MultimapQuery<Multiset<byte[]>> {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700536 }
537
538 /**
539 * Entry set query.
540 */
541 @SuppressWarnings("serial")
542 public static class Entries extends
543 MultimapQuery<Collection<Map.Entry<String, byte[]>>> {
544 }
545
546 /**
547 * Get value query.
548 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700549 public static class Get extends
550 KeyQuery<Versioned<Collection<? extends byte[]>>> {
551 public Get(String key) {
552 super(key);
553 }
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700554 }
555
556 /**
557 * Multimap command type resolver.
558 */
559 @SuppressWarnings("serial")
560 public static class TypeResolver implements SerializableTypeResolver {
561 @Override
562 public void resolve(SerializerRegistry registry) {
563 registry.register(ContainsKey.class, -1000);
564 registry.register(ContainsValue.class, -1001);
565 registry.register(ContainsEntry.class, -1002);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700566 registry.register(Replace.class, -1003);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700567 registry.register(Clear.class, -1004);
568 registry.register(KeySet.class, -1005);
569 registry.register(Keys.class, -1006);
570 registry.register(Values.class, -1007);
571 registry.register(Entries.class, -1008);
572 registry.register(Size.class, -1009);
573 registry.register(IsEmpty.class, -1010);
574 registry.register(Get.class, -1011);
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700575 registry.register(Put.class, -1012);
576 registry.register(RemoveAll.class, -1013);
577 registry.register(MultiRemove.class, -1014);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700578 }
579 }
580}