blob: 2d560c41e88ca60a74ab403ebf38a142ec248862 [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() {
246 return value == null ? CompactionMode.FULL : CompactionMode.QUORUM;
247 }
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 /**
280 * Get query command.
281 */
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 /**
293 * Is empty query.
294 */
295 @SuppressWarnings("serial")
296 public static class IsEmpty extends TreeQuery<Boolean> {
297
298 }
299
300 /**
301 * Key set query.
302 */
303 @SuppressWarnings("serial")
304 public static class KeySet extends TreeQuery<Set<String>> {
305 }
306
307 /**
308 * Value set query.
309 */
310 @SuppressWarnings("serial")
311 public static class Values
312 extends TreeQuery<Collection<Versioned<byte[]>>> {
313 }
314
315 /**
316 * Entry set query.
317 */
318 @SuppressWarnings("serial")
319 public static class EntrySet
320 extends TreeQuery<Set<Map.Entry<String, Versioned<byte[]>>>> {
321 }
322
323 /**
324 * Size query.
325 */
326 @SuppressWarnings("serial")
327 public static class Size extends TreeQuery<Integer> {
328 }
329
330 /**
331 * Clear command.
332 */
333 @SuppressWarnings("serial")
334 public static class Clear
335 extends TreeCommand<MapEntryUpdateResult.Status> {
336 @Override
337 public CompactionMode compaction() {
338 return CompactionMode.FULL;
339 }
340 }
341
342 /**
343 * Change listen.
344 */
345 @SuppressWarnings("serial")
346 public static class Listen implements Command<Void>, CatalystSerializable {
347 @Override
348 public void writeObject(BufferOutput<?> buffer,
349 Serializer serializer) {
350 }
351
352 @Override
353 public void readObject(BufferInput<?> buffer, Serializer serializer) {
354 }
355
356 @Override
357 public String toString() {
358 return MoreObjects.toStringHelper(getClass())
359 .toString();
360 }
361 }
362
363 /**
364 * Change unlisten.
365 */
366 @SuppressWarnings("serial")
367 public static class Unlisten implements Command<Void>,
368 CatalystSerializable {
369 @Override
370 public void writeObject(BufferOutput<?> buffer,
371 Serializer serializer) {
372 }
373
374 @Override
375 public void readObject(BufferInput<?> buffer, Serializer serializer) {
376 }
377
378 @Override
379 public String toString() {
380 return MoreObjects.toStringHelper(getClass())
381 .toString();
382 }
383 }
384
385 /* Tree map specific commands below */
386
387 /**
388 * First key query.
389 */
390 @SuppressWarnings("serial")
391 public static class FirstKey<K> extends TreeQuery<K> {
392 }
393
394 /**
395 * Last key query.
396 */
397 @SuppressWarnings("serial")
398 public static class LastKey<K> extends TreeQuery<K> {
399 }
400
401 /**
402 * First entry query.
403 */
404 @SuppressWarnings("serial")
405 public static class FirstEntry<K, V> extends TreeQuery<Map.Entry<K, V>> {
406 }
407
408 /**
409 * Last entry query.
410 */
411 @SuppressWarnings("serial")
412 public static class LastEntry<K, V> extends TreeQuery<Map.Entry<K, V>> {
413 }
414
415 /**
416 * First entry query, if none exists returns null.
417 */
418 @SuppressWarnings("serial")
419 public static class PollFirstEntry<K, V>
420 extends TreeQuery<Map.Entry<K, V>> {
421 }
422
423 /**
424 * Last entry query, if none exists returns null.
425 */
426 @SuppressWarnings("serial")
427 public static class PollLastEntry<K, V>
428 extends TreeQuery<Map.Entry<K, V>> {
429 }
430
431 /**
432 * Query returns the entry associated with the largest key less than the
433 * passed in key.
434 */
435 @SuppressWarnings("serial")
436 public static class LowerEntry<K, V> extends KeyQuery<K> {
437 public LowerEntry() {
438 }
439
440 public LowerEntry(String key) {
441 super(key);
442 }
443 }
444
445 /**
446 * Query returns the largest key less than the specified key.
447 */
448 @SuppressWarnings("serial")
449 public static class LowerKey<K> extends KeyQuery<K> {
450 public LowerKey() {
451 }
452
453 public LowerKey(String key) {
454 super(key);
455 }
456 }
457
458 /**
459 * Query returns the entry associated with the largest key smaller than or
460 * equal to the specified key.
461 */
462 @SuppressWarnings("serial")
463 public static class FloorEntry<K, V> extends KeyQuery<Map.Entry<K, V>> {
464 public FloorEntry() {
465 }
466
467 public FloorEntry(String key) {
468 super(key);
469 }
470 }
471
472 /**
473 * Query returns the largest key smaller than or equal to the passed in
474 * key.
475 */
476 @SuppressWarnings("serial")
477 public static class FloorKey<K> extends KeyQuery<K> {
478 public FloorKey() {
479 }
480
481 public FloorKey(String key) {
482 super(key);
483 }
484 }
485
486 /**
487 * Returns the entry associated with the smallest key larger than or equal
488 * to the specified key.
489 */
490 @SuppressWarnings("serial")
491 public static class CeilingEntry<K, V> extends KeyQuery<Map.Entry<K, V>> {
492 public CeilingEntry() {
493 }
494
495 public CeilingEntry(String key) {
496 super(key);
497 }
498 }
499
500 /**
501 * Returns the smallest key larger than or equal to the specified key.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700502 *
503 * @param <K> key type
Aaron Kruglikovb56c2962016-04-04 17:03:24 -0700504 */
505 @SuppressWarnings("serial")
506 public static class CeilingKey<K> extends KeyQuery<K> {
507 public CeilingKey() {
508 }
509
510 public CeilingKey(String key) {
511 super(key);
512 }
513 }
514
515 /**
516 * Returns the entry associated with the smallest key larger than the
517 * specified key.
518 */
519 @SuppressWarnings("serial")
520 public static class HigherEntry<K, V> extends KeyQuery<Map.Entry<K, V>> {
521 public HigherEntry() {
522 }
523
524 public HigherEntry(String key) {
525 super(key);
526 }
527 }
528
529 /**
530 * Returns the smallest key larger than the specified key.
531 */
532 @SuppressWarnings("serial")
533 public static class HigherKey<K> extends KeyQuery<K> {
534 public HigherKey() {
535 }
536
537 public HigherKey(String key) {
538 super(key);
539 }
540 }
541
542 @SuppressWarnings("serial")
543 public static class NavigableKeySet<K, V>
544 extends TreeQuery<NavigableSet<K>> {
545 }
546
547 @SuppressWarnings("serial")
548 public static class SubMap<K, V> extends TreeQuery<NavigableMap<K, V>> {
549 private K fromKey;
550 private K toKey;
551 private boolean inclusiveFrom;
552 private boolean inclusiveTo;
553
554 public SubMap() {
555 }
556
557 public SubMap(K fromKey, K toKey, boolean inclusiveFrom,
558 boolean inclusiveTo) {
559 this.fromKey = fromKey;
560 this.toKey = toKey;
561 this.inclusiveFrom = inclusiveFrom;
562 this.inclusiveTo = inclusiveTo;
563 }
564
565 @Override
566 public String toString() {
567 return MoreObjects.toStringHelper(getClass())
568 .add("getFromKey", fromKey)
569 .add("getToKey", toKey)
570 .add("inclusiveFrotBound", inclusiveFrom)
571 .add("inclusiveToBound", inclusiveTo)
572 .toString();
573 }
574
575 @Override
576 public void writeObject(BufferOutput<?> bufferOutput,
577 Serializer serializer) {
578 super.writeObject(bufferOutput, serializer);
579 serializer.writeObject(fromKey, bufferOutput);
580 serializer.writeObject(toKey, bufferOutput);
581 serializer.writeObject(inclusiveFrom, bufferOutput);
582 serializer.writeObject(inclusiveTo, bufferOutput);
583 }
584
585 @Override
586 public void readObject(BufferInput<?> bufferInput,
587 Serializer serializer) {
588 super.readObject(bufferInput, serializer);
589 fromKey = serializer.readObject(bufferInput);
590 toKey = serializer.readObject(bufferInput);
591 inclusiveFrom = serializer.readObject(bufferInput);
592 inclusiveTo = serializer.readObject(bufferInput);
593 }
594
595 public K fromKey() {
596 return fromKey;
597 }
598
599 public K toKey() {
600 return toKey;
601 }
602
603 public boolean isInclusiveFrom() {
604 return inclusiveFrom;
605 }
606
607 public boolean isInclusiveTo() {
608 return inclusiveTo;
609 }
610 }
611
612 /**
613 * Tree map command type resolver.
614 */
615 public static class TypeResolver implements SerializableTypeResolver {
616 @Override
617 public void resolve(SerializerRegistry registry) {
618 //NOTE the registration values must be unique throughout the
619 // project.
620 registry.register(ContainsKey.class, -1161);
621 registry.register(ContainsValue.class, -1162);
622 registry.register(Get.class, -1163);
623 registry.register(EntrySet.class, -1164);
624 registry.register(Values.class, -1165);
625 registry.register(KeySet.class, -1166);
626 registry.register(Clear.class, -1167);
627 registry.register(IsEmpty.class, -1168);
628 registry.register(Size.class, -1169);
629 registry.register(Listen.class, -1170);
630 registry.register(Unlisten.class, -1171);
631 //Transaction related commands will be added here with numbers
632 // -1172 to -1174
633 registry.register(UpdateAndGet.class, -1175);
634 registry.register(FirstKey.class, -1176);
635 registry.register(LastKey.class, -1177);
636 registry.register(FirstEntry.class, -1178);
637 registry.register(LastEntry.class, -1179);
638 registry.register(PollFirstEntry.class, -1180);
639 registry.register(PollLastEntry.class, -1181);
640 registry.register(LowerEntry.class, -1182);
641 registry.register(LowerKey.class, -1183);
642 registry.register(FloorEntry.class, -1184);
643 registry.register(FloorKey.class, -1185);
644 registry.register(CeilingEntry.class, -1186);
645 registry.register(CeilingKey.class, -1187);
646 registry.register(HigherEntry.class, -1188);
647 registry.register(HigherKey.class, -1189);
648 registry.register(SubMap.class, -1190);
649 registry.register(NavigableKeySet.class, -1191);
650 }
651 }
652}