Aaron Kruglikov

Adding commands for the AsyncConsistentTreeMap, commands relating to transaction…

…s are temporarily removed.

Change-Id: Iec5b972fb9de2dbfdfbe315b330778139ba92c90
1 +/*
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 +
17 +package org.onosproject.store.primitives.resources.impl;
18 +
19 +import com.google.common.base.MoreObjects;
20 +import io.atomix.catalyst.buffer.BufferInput;
21 +import io.atomix.catalyst.buffer.BufferOutput;
22 +import io.atomix.catalyst.serializer.CatalystSerializable;
23 +import io.atomix.catalyst.serializer.SerializableTypeResolver;
24 +import io.atomix.catalyst.serializer.Serializer;
25 +import io.atomix.catalyst.serializer.SerializerRegistry;
26 +import io.atomix.catalyst.util.Assert;
27 +import io.atomix.copycat.Command;
28 +import io.atomix.copycat.Query;
29 +import org.onlab.util.Match;
30 +import org.onosproject.store.service.Versioned;
31 +
32 +import java.util.Collection;
33 +import java.util.Map;
34 +import java.util.NavigableMap;
35 +import java.util.NavigableSet;
36 +import java.util.Set;
37 +
38 +/**
39 + * {@link org.onosproject.store.service.AsyncConsistentTreeMap} Resource
40 + * state machine operations.
41 + */
42 +public final class AsyncConsistentTreeMapCommands {
43 +
44 + private AsyncConsistentTreeMapCommands() {
45 + }
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.
502 + * @param <K>
503 + */
504 + @SuppressWarnings("serial")
505 + public static class CeilingKey<K> extends KeyQuery<K> {
506 + public CeilingKey() {
507 + }
508 +
509 + public CeilingKey(String key) {
510 + super(key);
511 + }
512 + }
513 +
514 + /**
515 + * Returns the entry associated with the smallest key larger than the
516 + * specified key.
517 + */
518 + @SuppressWarnings("serial")
519 + public static class HigherEntry<K, V> extends KeyQuery<Map.Entry<K, V>> {
520 + public HigherEntry() {
521 + }
522 +
523 + public HigherEntry(String key) {
524 + super(key);
525 + }
526 + }
527 +
528 + /**
529 + * Returns the smallest key larger than the specified key.
530 + */
531 + @SuppressWarnings("serial")
532 + public static class HigherKey<K> extends KeyQuery<K> {
533 + public HigherKey() {
534 + }
535 +
536 + public HigherKey(String key) {
537 + super(key);
538 + }
539 + }
540 +
541 + @SuppressWarnings("serial")
542 + public static class NavigableKeySet<K, V>
543 + extends TreeQuery<NavigableSet<K>> {
544 + }
545 +
546 + @SuppressWarnings("serial")
547 + public static class SubMap<K, V> extends TreeQuery<NavigableMap<K, V>> {
548 + private K fromKey;
549 + private K toKey;
550 + private boolean inclusiveFrom;
551 + private boolean inclusiveTo;
552 +
553 + public SubMap() {
554 + }
555 +
556 + public SubMap(K fromKey, K toKey, boolean inclusiveFrom,
557 + boolean inclusiveTo) {
558 + this.fromKey = fromKey;
559 + this.toKey = toKey;
560 + this.inclusiveFrom = inclusiveFrom;
561 + this.inclusiveTo = inclusiveTo;
562 + }
563 +
564 + @Override
565 + public String toString() {
566 + return MoreObjects.toStringHelper(getClass())
567 + .add("getFromKey", fromKey)
568 + .add("getToKey", toKey)
569 + .add("inclusiveFrotBound", inclusiveFrom)
570 + .add("inclusiveToBound", inclusiveTo)
571 + .toString();
572 + }
573 +
574 + @Override
575 + public void writeObject(BufferOutput<?> bufferOutput,
576 + Serializer serializer) {
577 + super.writeObject(bufferOutput, serializer);
578 + serializer.writeObject(fromKey, bufferOutput);
579 + serializer.writeObject(toKey, bufferOutput);
580 + serializer.writeObject(inclusiveFrom, bufferOutput);
581 + serializer.writeObject(inclusiveTo, bufferOutput);
582 + }
583 +
584 + @Override
585 + public void readObject(BufferInput<?> bufferInput,
586 + Serializer serializer) {
587 + super.readObject(bufferInput, serializer);
588 + fromKey = serializer.readObject(bufferInput);
589 + toKey = serializer.readObject(bufferInput);
590 + inclusiveFrom = serializer.readObject(bufferInput);
591 + inclusiveTo = serializer.readObject(bufferInput);
592 + }
593 +
594 + public K fromKey() {
595 + return fromKey;
596 + }
597 +
598 + public K toKey() {
599 + return toKey;
600 + }
601 +
602 + public boolean isInclusiveFrom() {
603 + return inclusiveFrom;
604 + }
605 +
606 + public boolean isInclusiveTo() {
607 + return inclusiveTo;
608 + }
609 + }
610 +
611 + /**
612 + * Tree map command type resolver.
613 + */
614 + public static class TypeResolver implements SerializableTypeResolver {
615 + @Override
616 + public void resolve(SerializerRegistry registry) {
617 + //NOTE the registration values must be unique throughout the
618 + // project.
619 + registry.register(ContainsKey.class, -1161);
620 + registry.register(ContainsValue.class, -1162);
621 + registry.register(Get.class, -1163);
622 + registry.register(EntrySet.class, -1164);
623 + registry.register(Values.class, -1165);
624 + registry.register(KeySet.class, -1166);
625 + registry.register(Clear.class, -1167);
626 + registry.register(IsEmpty.class, -1168);
627 + registry.register(Size.class, -1169);
628 + registry.register(Listen.class, -1170);
629 + registry.register(Unlisten.class, -1171);
630 + //Transaction related commands will be added here with numbers
631 + // -1172 to -1174
632 + registry.register(UpdateAndGet.class, -1175);
633 + registry.register(FirstKey.class, -1176);
634 + registry.register(LastKey.class, -1177);
635 + registry.register(FirstEntry.class, -1178);
636 + registry.register(LastEntry.class, -1179);
637 + registry.register(PollFirstEntry.class, -1180);
638 + registry.register(PollLastEntry.class, -1181);
639 + registry.register(LowerEntry.class, -1182);
640 + registry.register(LowerKey.class, -1183);
641 + registry.register(FloorEntry.class, -1184);
642 + registry.register(FloorKey.class, -1185);
643 + registry.register(CeilingEntry.class, -1186);
644 + registry.register(CeilingKey.class, -1187);
645 + registry.register(HigherEntry.class, -1188);
646 + registry.register(HigherKey.class, -1189);
647 + registry.register(SubMap.class, -1190);
648 + registry.register(NavigableKeySet.class, -1191);
649 + }
650 + }
651 +}
...@@ -156,6 +156,13 @@ public final class AtomixConsistentMapCommands { ...@@ -156,6 +156,13 @@ public final class AtomixConsistentMapCommands {
156 } 156 }
157 157
158 @Override 158 @Override
159 + public String toString() {
160 + return MoreObjects.toStringHelper(getClass())
161 + .add("value", value)
162 + .toString();
163 + }
164 +
165 + @Override
159 public void writeObject(BufferOutput<?> buffer, Serializer serializer) { 166 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
160 super.writeObject(buffer, serializer); 167 super.writeObject(buffer, serializer);
161 serializer.writeObject(value, buffer); 168 serializer.writeObject(value, buffer);
...@@ -192,13 +199,6 @@ public final class AtomixConsistentMapCommands { ...@@ -192,13 +199,6 @@ public final class AtomixConsistentMapCommands {
192 public ContainsValue(byte[] value) { 199 public ContainsValue(byte[] value) {
193 super(value); 200 super(value);
194 } 201 }
195 -
196 - @Override
197 - public String toString() {
198 - return MoreObjects.toStringHelper(getClass())
199 - .add("value", value)
200 - .toString();
201 - }
202 } 202 }
203 203
204 /** 204 /**
...@@ -453,14 +453,14 @@ public final class AtomixConsistentMapCommands { ...@@ -453,14 +453,14 @@ public final class AtomixConsistentMapCommands {
453 } 453 }
454 454
455 /** 455 /**
456 - * KeySet query. 456 + * ValueSet query.
457 */ 457 */
458 @SuppressWarnings("serial") 458 @SuppressWarnings("serial")
459 public static class Values extends MapQuery<Collection<Versioned<byte[]>>> { 459 public static class Values extends MapQuery<Collection<Versioned<byte[]>>> {
460 } 460 }
461 461
462 /** 462 /**
463 - * KeySet query. 463 + * EntrySet query.
464 */ 464 */
465 @SuppressWarnings("serial") 465 @SuppressWarnings("serial")
466 public static class EntrySet extends MapQuery<Set<Map.Entry<String, Versioned<byte[]>>>> { 466 public static class EntrySet extends MapQuery<Set<Map.Entry<String, Versioned<byte[]>>>> {
......