blob: 59cf1a5ab07fc15740b86686d76b14fb75afec01 [file] [log] [blame]
Aaron Kruglikovb56c2962016-04-04 17:03:24 -07001/*
2 * Copyright 2016-present 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 io.atomix.catalyst.buffer.BufferInput;
21import io.atomix.catalyst.buffer.BufferOutput;
22import io.atomix.catalyst.serializer.CatalystSerializable;
23import io.atomix.catalyst.serializer.SerializableTypeResolver;
24import io.atomix.catalyst.serializer.Serializer;
25import io.atomix.catalyst.serializer.SerializerRegistry;
26import io.atomix.catalyst.util.Assert;
27import io.atomix.copycat.Command;
28import io.atomix.copycat.Query;
29import org.onlab.util.Match;
30import org.onosproject.store.service.Versioned;
31
32import java.util.Collection;
33import java.util.Map;
34import java.util.NavigableMap;
35import java.util.NavigableSet;
36import java.util.Set;
37
38/**
39 * {@link org.onosproject.store.service.AsyncConsistentTreeMap} Resource
40 * state machine operations.
41 */
Aaron Kruglikova1801aa2016-07-12 15:17:30 -070042public final class AtomixConsistentTreeMapCommands {
Aaron Kruglikovb56c2962016-04-04 17:03:24 -070043
Aaron Kruglikova1801aa2016-07-12 15:17:30 -070044 private AtomixConsistentTreeMapCommands() {
Aaron Kruglikovb56c2962016-04-04 17:03:24 -070045 }
46
47 /**
48 * Abstract treeMap command.
49 */
50 @SuppressWarnings("serial")
51 public abstract static class TreeCommand<V>
52 implements Command<V>, CatalystSerializable {
53
54 @Override
55 public String toString() {
56 return MoreObjects.toStringHelper(getClass())
57 .toString();
58 }
59
60 @Override
61 public void writeObject(BufferOutput<?> buffer,
62 Serializer serializer) {
63 }
64
65 @Override
66 public void readObject(BufferInput<?> buffer, Serializer serializer) {
67 }
68 }
69
70 /**
71 * Abstract treeMap query.
72 */
73 @SuppressWarnings("serial")
74 public abstract static class TreeQuery<V>
75 implements Query<V>, CatalystSerializable {
76 @Override
77 public ConsistencyLevel consistency() {
78 return ConsistencyLevel.LINEARIZABLE_LEASE;
79 }
80
81 @Override
82 public String toString() {
83 return MoreObjects.toStringHelper(getClass())
84 .toString();
85 }
86
87 @Override
88 public void writeObject(BufferOutput<?> bufferOutput,
89 Serializer serializer) {
90
91 }
92
93 @Override
94 public void readObject(BufferInput<?> bufferInput,
95 Serializer serializer) {
96
97 }
98 }
99 /**
100 * Abstract key-based query.
101 */
102 @SuppressWarnings("serial")
103 public abstract static class KeyQuery<K> extends TreeQuery<K> {
104 protected String key;
105
106 public KeyQuery(String key) {
107 this.key = Assert.notNull(key, "key");
108 }
109
110 public KeyQuery() {
111 }
112
113 public String key() {
114 return key;
115 }
116
117 @Override
118 public String toString() {
119 return MoreObjects.toStringHelper(getClass())
120 .add("key", key)
121 .toString();
122 }
123
124 @Override
125 public void writeObject(BufferOutput<?> bufferOutput,
126 Serializer serializer) {
127 super.writeObject(bufferOutput, serializer);
128 serializer.writeObject(key, bufferOutput);
129 }
130
131 @Override
132 public void readObject(BufferInput<?> bufferInput,
133 Serializer serializer) {
134 super.readObject(bufferInput, serializer);
135 key = serializer.readObject(bufferInput);
136 }
137 }
138
139 /**
140 * Abstract value-based query.
141 */
142 @SuppressWarnings("serial")
143 public abstract static class ValueQuery<V> extends TreeQuery<V> {
144 protected byte[] value;
145
146 public ValueQuery() {}
147
148 public ValueQuery(byte[] value) {
149 this.value = Assert.notNull(value, "value");
150 }
151
152 public byte[] value() {
153 return value;
154 }
155
156 @Override
157 public void writeObject(BufferOutput<?> bufferOutput,
158 Serializer serializer) {
159 super.writeObject(bufferOutput, serializer);
160 serializer.writeObject(value, bufferOutput);
161 }
162
163 @Override
164 public void readObject(BufferInput<?> bufferInput,
165 Serializer serializer) {
166 super.readObject(bufferInput, serializer);
167 value = serializer.readObject(bufferInput);
168 }
169
170 @Override
171 public String toString() {
172 return MoreObjects.toStringHelper(getClass())
173 .add("value", value)
174 .toString();
175 }
176 }
177
178 /**
179 * Contains key command.
180 */
181 @SuppressWarnings("serial")
182 public static class ContainsKey extends KeyQuery<Boolean> {
183
184 public ContainsKey(String key) {
185 super(key);
186 }
187
188 public ContainsKey() {
189 }
190 }
191 /**
192 * Contains value command.
193 */
194 @SuppressWarnings("serial")
195 public static class ContainsValue extends ValueQuery<Boolean> {
196 public ContainsValue() {
197 }
198
199 public ContainsValue(byte[] value) {
200 super(value);
201 }
202
203 }
204
205 /**
206 * AsyncConsistentTreeMap update command.
207 */
208 @SuppressWarnings("serial")
209 public static class UpdateAndGet
210 extends TreeCommand<MapEntryUpdateResult<String, byte[]>> {
211 private String key;
212 private byte[] value;
213 private Match<byte[]> valueMatch;
214 private Match<Long> versionMatch;
215 public UpdateAndGet() {
216 }
217
218 public UpdateAndGet(String key,
219 byte[] value,
220 Match<byte[]> valueMatch,
221 Match<Long> versionMatch) {
222 this.key = key;
223 this.value = value;
224 this.valueMatch = valueMatch;
225 this.versionMatch = versionMatch;
226 }
227
228 public String key() {
229 return this.key;
230 }
231
232 public byte[] value() {
233 return this.value;
234 }
235
236 public Match<byte[]> valueMatch() {
237 return this.valueMatch;
238 }
239
240 public Match<Long> versionMatch() {
241 return this.versionMatch;
242 }
243
244 @Override
245 public CompactionMode compaction() {
Jordan Halterman0c83e842017-02-05 22:21:19 -0800246 return value == null ? CompactionMode.TOMBSTONE : CompactionMode.QUORUM;
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700247 }
248
249 @Override
250 public void writeObject(BufferOutput<?> buffer,
251 Serializer serializer) {
252 super.writeObject(buffer, serializer);
253 serializer.writeObject(key, buffer);
254 serializer.writeObject(value, buffer);
255 serializer.writeObject(valueMatch, buffer);
256 serializer.writeObject(versionMatch, buffer);
257 }
258
259 @Override
260 public void readObject(BufferInput<?> buffer, Serializer serializer) {
261 super.readObject(buffer, serializer);
262 key = serializer.readObject(buffer);
263 value = serializer.readObject(buffer);
264 valueMatch = serializer.readObject(buffer);
265 versionMatch = serializer.readObject(buffer);
266 }
267
268 @Override
269 public String toString() {
270 return MoreObjects.toStringHelper(getClass())
271 .add("key", key)
272 .add("value", value)
273 .add("valueMatch", valueMatch)
274 .add("versionMatch", versionMatch)
275 .toString();
276 }
277 }
278
279 /**
Jordan Haltermanf6272442017-04-20 02:18:08 -0700280 * Get query.
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700281 */
282 @SuppressWarnings("serial")
283 public static class Get extends KeyQuery<Versioned<byte[]>> {
284 public Get() {
285 }
286
287 public Get(String key) {
288 super(key);
289 }
290 }
291
292 /**
Jordan Haltermanf6272442017-04-20 02:18:08 -0700293 * Get or default query.
294 */
295 @SuppressWarnings("serial")
296 public static class GetOrDefault extends KeyQuery<Versioned<byte[]>> {
297 private byte[] defaultValue;
298
299 public GetOrDefault() {
300 }
301
302 public GetOrDefault(String key, byte[] defaultValue) {
303 super(key);
304 this.defaultValue = defaultValue;
305 }
306
307 /**
308 * Returns the default value.
309 *
310 * @return the default value
311 */
312 public byte[] defaultValue() {
313 return defaultValue;
314 }
315
316 @Override
317 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
318 super.writeObject(buffer, serializer);
319 serializer.writeObject(defaultValue, buffer);
320 }
321
322 @Override
323 public void readObject(BufferInput<?> buffer, Serializer serializer) {
324 super.readObject(buffer, serializer);
325 defaultValue = serializer.readObject(buffer);
326 }
327 }
328
329 /**
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700330 * Is empty query.
331 */
332 @SuppressWarnings("serial")
333 public static class IsEmpty extends TreeQuery<Boolean> {
334
335 }
336
337 /**
338 * Key set query.
339 */
340 @SuppressWarnings("serial")
341 public static class KeySet extends TreeQuery<Set<String>> {
342 }
343
344 /**
345 * Value set query.
346 */
347 @SuppressWarnings("serial")
348 public static class Values
349 extends TreeQuery<Collection<Versioned<byte[]>>> {
350 }
351
352 /**
353 * Entry set query.
354 */
355 @SuppressWarnings("serial")
356 public static class EntrySet
357 extends TreeQuery<Set<Map.Entry<String, Versioned<byte[]>>>> {
358 }
359
360 /**
361 * Size query.
362 */
363 @SuppressWarnings("serial")
364 public static class Size extends TreeQuery<Integer> {
365 }
366
367 /**
368 * Clear command.
369 */
370 @SuppressWarnings("serial")
371 public static class Clear
372 extends TreeCommand<MapEntryUpdateResult.Status> {
373 @Override
374 public CompactionMode compaction() {
Jordan Halterman0c83e842017-02-05 22:21:19 -0800375 return CompactionMode.TOMBSTONE;
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700376 }
377 }
378
379 /**
380 * Change listen.
381 */
382 @SuppressWarnings("serial")
383 public static class Listen implements Command<Void>, CatalystSerializable {
384 @Override
385 public void writeObject(BufferOutput<?> buffer,
386 Serializer serializer) {
387 }
388
389 @Override
390 public void readObject(BufferInput<?> buffer, Serializer serializer) {
391 }
392
393 @Override
Jordan Halterman0c83e842017-02-05 22:21:19 -0800394 public CompactionMode compaction() {
395 return CompactionMode.QUORUM;
396 }
397
398 @Override
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700399 public String toString() {
400 return MoreObjects.toStringHelper(getClass())
401 .toString();
402 }
403 }
404
405 /**
406 * Change unlisten.
407 */
408 @SuppressWarnings("serial")
409 public static class Unlisten implements Command<Void>,
410 CatalystSerializable {
411 @Override
412 public void writeObject(BufferOutput<?> buffer,
413 Serializer serializer) {
414 }
415
416 @Override
417 public void readObject(BufferInput<?> buffer, Serializer serializer) {
418 }
419
420 @Override
Jordan Halterman0c83e842017-02-05 22:21:19 -0800421 public CompactionMode compaction() {
422 return CompactionMode.TOMBSTONE;
423 }
424
425 @Override
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700426 public String toString() {
427 return MoreObjects.toStringHelper(getClass())
428 .toString();
429 }
430 }
431
432 /* Tree map specific commands below */
433
434 /**
435 * First key query.
436 */
437 @SuppressWarnings("serial")
438 public static class FirstKey<K> extends TreeQuery<K> {
439 }
440
441 /**
442 * Last key query.
443 */
444 @SuppressWarnings("serial")
445 public static class LastKey<K> extends TreeQuery<K> {
446 }
447
448 /**
449 * First entry query.
450 */
451 @SuppressWarnings("serial")
452 public static class FirstEntry<K, V> extends TreeQuery<Map.Entry<K, V>> {
453 }
454
455 /**
456 * Last entry query.
457 */
458 @SuppressWarnings("serial")
459 public static class LastEntry<K, V> extends TreeQuery<Map.Entry<K, V>> {
460 }
461
462 /**
463 * First entry query, if none exists returns null.
464 */
465 @SuppressWarnings("serial")
466 public static class PollFirstEntry<K, V>
467 extends TreeQuery<Map.Entry<K, V>> {
468 }
469
470 /**
471 * Last entry query, if none exists returns null.
472 */
473 @SuppressWarnings("serial")
474 public static class PollLastEntry<K, V>
475 extends TreeQuery<Map.Entry<K, V>> {
476 }
477
478 /**
479 * Query returns the entry associated with the largest key less than the
480 * passed in key.
481 */
482 @SuppressWarnings("serial")
483 public static class LowerEntry<K, V> extends KeyQuery<K> {
484 public LowerEntry() {
485 }
486
487 public LowerEntry(String key) {
488 super(key);
489 }
490 }
491
492 /**
493 * Query returns the largest key less than the specified key.
494 */
495 @SuppressWarnings("serial")
496 public static class LowerKey<K> extends KeyQuery<K> {
497 public LowerKey() {
498 }
499
500 public LowerKey(String key) {
501 super(key);
502 }
503 }
504
505 /**
506 * Query returns the entry associated with the largest key smaller than or
507 * equal to the specified key.
508 */
509 @SuppressWarnings("serial")
510 public static class FloorEntry<K, V> extends KeyQuery<Map.Entry<K, V>> {
511 public FloorEntry() {
512 }
513
514 public FloorEntry(String key) {
515 super(key);
516 }
517 }
518
519 /**
520 * Query returns the largest key smaller than or equal to the passed in
521 * key.
522 */
523 @SuppressWarnings("serial")
524 public static class FloorKey<K> extends KeyQuery<K> {
525 public FloorKey() {
526 }
527
528 public FloorKey(String key) {
529 super(key);
530 }
531 }
532
533 /**
534 * Returns the entry associated with the smallest key larger than or equal
535 * to the specified key.
536 */
537 @SuppressWarnings("serial")
538 public static class CeilingEntry<K, V> extends KeyQuery<Map.Entry<K, V>> {
539 public CeilingEntry() {
540 }
541
542 public CeilingEntry(String key) {
543 super(key);
544 }
545 }
546
547 /**
548 * Returns the smallest key larger than or equal to the specified key.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700549 *
550 * @param <K> key type
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700551 */
552 @SuppressWarnings("serial")
553 public static class CeilingKey<K> extends KeyQuery<K> {
554 public CeilingKey() {
555 }
556
557 public CeilingKey(String key) {
558 super(key);
559 }
560 }
561
562 /**
563 * Returns the entry associated with the smallest key larger than the
564 * specified key.
565 */
566 @SuppressWarnings("serial")
567 public static class HigherEntry<K, V> extends KeyQuery<Map.Entry<K, V>> {
568 public HigherEntry() {
569 }
570
571 public HigherEntry(String key) {
572 super(key);
573 }
574 }
575
576 /**
577 * Returns the smallest key larger than the specified key.
578 */
579 @SuppressWarnings("serial")
580 public static class HigherKey<K> extends KeyQuery<K> {
581 public HigherKey() {
582 }
583
584 public HigherKey(String key) {
585 super(key);
586 }
587 }
588
589 @SuppressWarnings("serial")
590 public static class NavigableKeySet<K, V>
591 extends TreeQuery<NavigableSet<K>> {
592 }
593
594 @SuppressWarnings("serial")
595 public static class SubMap<K, V> extends TreeQuery<NavigableMap<K, V>> {
596 private K fromKey;
597 private K toKey;
598 private boolean inclusiveFrom;
599 private boolean inclusiveTo;
600
601 public SubMap() {
602 }
603
604 public SubMap(K fromKey, K toKey, boolean inclusiveFrom,
605 boolean inclusiveTo) {
606 this.fromKey = fromKey;
607 this.toKey = toKey;
608 this.inclusiveFrom = inclusiveFrom;
609 this.inclusiveTo = inclusiveTo;
610 }
611
612 @Override
613 public String toString() {
614 return MoreObjects.toStringHelper(getClass())
615 .add("getFromKey", fromKey)
616 .add("getToKey", toKey)
617 .add("inclusiveFrotBound", inclusiveFrom)
618 .add("inclusiveToBound", inclusiveTo)
619 .toString();
620 }
621
622 @Override
623 public void writeObject(BufferOutput<?> bufferOutput,
624 Serializer serializer) {
625 super.writeObject(bufferOutput, serializer);
626 serializer.writeObject(fromKey, bufferOutput);
627 serializer.writeObject(toKey, bufferOutput);
628 serializer.writeObject(inclusiveFrom, bufferOutput);
629 serializer.writeObject(inclusiveTo, bufferOutput);
630 }
631
632 @Override
633 public void readObject(BufferInput<?> bufferInput,
634 Serializer serializer) {
635 super.readObject(bufferInput, serializer);
636 fromKey = serializer.readObject(bufferInput);
637 toKey = serializer.readObject(bufferInput);
638 inclusiveFrom = serializer.readObject(bufferInput);
639 inclusiveTo = serializer.readObject(bufferInput);
640 }
641
642 public K fromKey() {
643 return fromKey;
644 }
645
646 public K toKey() {
647 return toKey;
648 }
649
650 public boolean isInclusiveFrom() {
651 return inclusiveFrom;
652 }
653
654 public boolean isInclusiveTo() {
655 return inclusiveTo;
656 }
657 }
658
659 /**
660 * Tree map command type resolver.
661 */
662 public static class TypeResolver implements SerializableTypeResolver {
663 @Override
664 public void resolve(SerializerRegistry registry) {
665 //NOTE the registration values must be unique throughout the
666 // project.
667 registry.register(ContainsKey.class, -1161);
668 registry.register(ContainsValue.class, -1162);
669 registry.register(Get.class, -1163);
Jordan Haltermanf6272442017-04-20 02:18:08 -0700670 registry.register(GetOrDefault.class, -1192);
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700671 registry.register(EntrySet.class, -1164);
672 registry.register(Values.class, -1165);
673 registry.register(KeySet.class, -1166);
674 registry.register(Clear.class, -1167);
675 registry.register(IsEmpty.class, -1168);
676 registry.register(Size.class, -1169);
677 registry.register(Listen.class, -1170);
678 registry.register(Unlisten.class, -1171);
679 //Transaction related commands will be added here with numbers
680 // -1172 to -1174
681 registry.register(UpdateAndGet.class, -1175);
682 registry.register(FirstKey.class, -1176);
683 registry.register(LastKey.class, -1177);
684 registry.register(FirstEntry.class, -1178);
685 registry.register(LastEntry.class, -1179);
686 registry.register(PollFirstEntry.class, -1180);
687 registry.register(PollLastEntry.class, -1181);
688 registry.register(LowerEntry.class, -1182);
689 registry.register(LowerKey.class, -1183);
690 registry.register(FloorEntry.class, -1184);
691 registry.register(FloorKey.class, -1185);
692 registry.register(CeilingEntry.class, -1186);
693 registry.register(CeilingKey.class, -1187);
694 registry.register(HigherEntry.class, -1188);
695 registry.register(HigherKey.class, -1189);
696 registry.register(SubMap.class, -1190);
697 registry.register(NavigableKeySet.class, -1191);
698 }
699 }
700}