blob: c46332015e564bb05fd91d85975b5c11ae072ce5 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import io.atomix.catalyst.buffer.BufferInput;
19import io.atomix.catalyst.buffer.BufferOutput;
20import io.atomix.catalyst.serializer.CatalystSerializable;
21import io.atomix.catalyst.serializer.Serializer;
22import io.atomix.catalyst.util.Assert;
23import io.atomix.copycat.client.Command;
24import io.atomix.copycat.client.Query;
25
26import java.util.Collection;
27import java.util.Map;
28import java.util.Set;
29
30import org.onlab.util.Match;
31import org.onosproject.store.service.Versioned;
32
33import com.google.common.base.MoreObjects;
34
35/**
36 * {@link AtomixConsistentMap} resource state machine operations.
37 */
38public final class AtomixConsistentMapCommands {
39
40 private AtomixConsistentMapCommands() {
41 }
42
43 /**
44 * Abstract map command.
45 */
46 @SuppressWarnings("serial")
47 public abstract static class MapCommand<V> implements Command<V>, CatalystSerializable {
48
49 @Override
50 public ConsistencyLevel consistency() {
51 return ConsistencyLevel.LINEARIZABLE;
52 }
53
54 @Override
55 public String toString() {
56 return MoreObjects.toStringHelper(getClass())
57 .toString();
58 }
59
60 @Override
61 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
62 }
63
64 @Override
65 public void readObject(BufferInput<?> buffer, Serializer serializer) {
66 }
67 }
68
69 /**
70 * Abstract map query.
71 */
72 @SuppressWarnings("serial")
73 public abstract static class MapQuery<V> implements Query<V>, CatalystSerializable {
74
75 @Override
76 public ConsistencyLevel consistency() {
77 return ConsistencyLevel.BOUNDED_LINEARIZABLE;
78 }
79
80 @Override
81 public String toString() {
82 return MoreObjects.toStringHelper(getClass())
83 .toString();
84 }
85
86 @Override
87 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
88 }
89
90 @Override
91 public void readObject(BufferInput<?> buffer, Serializer serializer) {
92 }
93 }
94
95 /**
96 * Abstract key-based query.
97 */
98 @SuppressWarnings("serial")
99 public abstract static class KeyQuery<V> extends MapQuery<V> {
100 protected String key;
101
102 public KeyQuery() {
103 }
104
105 public KeyQuery(String key) {
106 this.key = Assert.notNull(key, "key");
107 }
108
109 /**
110 * Returns the key.
111 * @return key
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<?> buffer, Serializer serializer) {
126 super.writeObject(buffer, serializer);
127 serializer.writeObject(key, buffer);
128 }
129
130 @Override
131 public void readObject(BufferInput<?> buffer, Serializer serializer) {
132 super.readObject(buffer, serializer);
133 key = serializer.readObject(buffer);
134 }
135 }
136
137 /**
138 * Abstract key-based query.
139 */
140 @SuppressWarnings("serial")
141 public abstract static class ValueQuery<V> extends MapQuery<V> {
142 protected byte[] value;
143
144 public ValueQuery() {
145 }
146
147 public ValueQuery(byte[] value) {
148 this.value = Assert.notNull(value, "value");
149 }
150
151 /**
152 * Returns the key.
153 * @return key
154 */
155 public byte[] value() {
156 return value;
157 }
158
159 @Override
160 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
161 super.writeObject(buffer, serializer);
162 serializer.writeObject(value, buffer);
163 }
164
165 @Override
166 public void readObject(BufferInput<?> buffer, Serializer serializer) {
167 super.readObject(buffer, serializer);
168 value = serializer.readObject(buffer);
169 }
170 }
171
172 /**
173 * Contains key command.
174 */
175 @SuppressWarnings("serial")
176 public static class ContainsKey extends KeyQuery<Boolean> {
177 public ContainsKey() {
178 }
179
180 public ContainsKey(String key) {
181 super(key);
182 }
183 }
184
185 /**
186 * Contains key command.
187 */
188 @SuppressWarnings("serial")
189 public static class ContainsValue extends ValueQuery<Boolean> {
190 public ContainsValue() {
191 }
192
193 public ContainsValue(byte[] value) {
194 super(value);
195 }
196
197 @Override
198 public String toString() {
199 return MoreObjects.toStringHelper(getClass())
200 .add("value", value)
201 .toString();
202 }
203 }
204
205 /**
206 * Map prepare command.
207 */
208 @SuppressWarnings("serial")
209 public static class TransactionPrepare extends MapCommand<PrepareResult> {
210 private TransactionalMapUpdate<String, byte[]> update;
211
212 public TransactionPrepare() {
213 }
214
215 public TransactionPrepare(TransactionalMapUpdate<String, byte[]> update) {
216 this.update = update;
217 }
218
219 public TransactionalMapUpdate<String, byte[]> transactionUpdate() {
220 return update;
221 }
222
223 @Override
224 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
225 super.writeObject(buffer, serializer);
226 serializer.writeObject(update, buffer);
227 }
228
229 @Override
230 public void readObject(BufferInput<?> buffer, Serializer serializer) {
231 super.readObject(buffer, serializer);
232 update = serializer.readObject(buffer);
233 }
234
235 @Override
236 public String toString() {
237 return MoreObjects.toStringHelper(getClass())
238 .add("update", update)
239 .toString();
240 }
241 }
242
243 /**
244 * Map transaction commit command.
245 */
246 @SuppressWarnings("serial")
247 public static class TransactionCommit extends MapCommand<CommitResult> {
248 private TransactionId transactionId;
249
250 public TransactionCommit() {
251 }
252
253 public TransactionCommit(TransactionId transactionId) {
254 this.transactionId = transactionId;
255 }
256
257 /**
258 * Returns the transaction identifier.
259 * @return transaction id
260 */
261 public TransactionId transactionId() {
262 return transactionId;
263 }
264
265 @Override
266 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
267 super.writeObject(buffer, serializer);
268 serializer.writeObject(transactionId, buffer);
269 }
270
271 @Override
272 public void readObject(BufferInput<?> buffer, Serializer serializer) {
273 super.readObject(buffer, serializer);
274 transactionId = serializer.readObject(buffer);
275 }
276
277 @Override
278 public String toString() {
279 return MoreObjects.toStringHelper(getClass())
280 .add("transactionId", transactionId)
281 .toString();
282 }
283 }
284
285 /**
286 * Map transaction rollback command.
287 */
288 @SuppressWarnings("serial")
289 public static class TransactionRollback extends MapCommand<RollbackResult> {
290 private TransactionId transactionId;
291
292 public TransactionRollback() {
293 }
294
295 public TransactionRollback(TransactionId transactionId) {
296 this.transactionId = transactionId;
297 }
298
299 /**
300 * Returns the transaction identifier.
301 * @return transaction id
302 */
303 public TransactionId transactionId() {
304 return transactionId;
305 }
306
307 @Override
308 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
309 super.writeObject(buffer, serializer);
310 serializer.writeObject(transactionId, buffer);
311 }
312
313 @Override
314 public void readObject(BufferInput<?> buffer, Serializer serializer) {
315 super.readObject(buffer, serializer);
316 transactionId = serializer.readObject(buffer);
317 }
318
319 @Override
320 public String toString() {
321 return MoreObjects.toStringHelper(getClass())
322 .add("transactionId", transactionId)
323 .toString();
324 }
325 }
326
327 /**
328 * Map update command.
329 */
330 @SuppressWarnings("serial")
331 public static class UpdateAndGet extends MapCommand<MapEntryUpdateResult<String, byte[]>> {
332 private String key;
333 private byte[] value;
334 private Match<byte[]> valueMatch;
335 private Match<Long> versionMatch;
336
337 public UpdateAndGet() {
338 }
339
340 public UpdateAndGet(String key,
341 byte[] value,
342 Match<byte[]> valueMatch,
343 Match<Long> versionMatch) {
344 this.key = key;
345 this.value = value;
346 this.valueMatch = valueMatch;
347 this.versionMatch = versionMatch;
348 }
349
350 /**
351 * Returns the key.
352 * @return key
353 */
354 public String key() {
355 return this.key;
356 }
357
358 /**
359 * Returns the value.
360 * @return value
361 */
362 public byte[] value() {
363 return this.value;
364 }
365
366 /**
367 * Returns the value match.
368 * @return value match
369 */
370 public Match<byte[]> valueMatch() {
371 return this.valueMatch;
372 }
373
374 /**
375 * Returns the version match.
376 * @return version match
377 */
378 public Match<Long> versionMatch() {
379 return this.versionMatch;
380 }
381
382 @Override
383 public CompactionMode compaction() {
384 return value == null ? CompactionMode.FULL : CompactionMode.QUORUM;
385 }
386
387 @Override
388 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
389 super.writeObject(buffer, serializer);
390 serializer.writeObject(key, buffer);
391 serializer.writeObject(value, buffer);
392 serializer.writeObject(valueMatch, buffer);
393 serializer.writeObject(versionMatch, buffer);
394 }
395
396 @Override
397 public void readObject(BufferInput<?> buffer, Serializer serializer) {
398 super.readObject(buffer, serializer);
399 key = serializer.readObject(buffer);
400 value = serializer.readObject(buffer);
401 valueMatch = serializer.readObject(buffer);
402 versionMatch = serializer.readObject(buffer);
403 }
404
405 @Override
406 public String toString() {
407 return MoreObjects.toStringHelper(getClass())
408 .add("key", key)
409 .add("value", value)
410 .add("valueMatch", valueMatch)
411 .add("versionMatch", versionMatch)
412 .toString();
413 }
414 }
415
416 /**
417 * Get query.
418 */
419 @SuppressWarnings("serial")
420 public static class Get extends KeyQuery<Versioned<byte[]>> {
421 public Get() {
422 }
423
424 public Get(String key) {
425 super(key);
426 }
427 }
428
429 /**
430 * Is empty query.
431 */
432 @SuppressWarnings("serial")
433 public static class IsEmpty extends MapQuery<Boolean> {
434 }
435
436 /**
437 * KeySet query.
438 */
439 @SuppressWarnings("serial")
440 public static class KeySet extends MapQuery<Set<String>> {
441 }
442
443 /**
444 * KeySet query.
445 */
446 @SuppressWarnings("serial")
447 public static class Values extends MapQuery<Collection<Versioned<byte[]>>> {
448 }
449
450 /**
451 * KeySet query.
452 */
453 @SuppressWarnings("serial")
454 public static class EntrySet extends MapQuery<Set<Map.Entry<String, Versioned<byte[]>>>> {
455 }
456
457 /**
458 * Size query.
459 */
460 @SuppressWarnings("serial")
461 public static class Size extends MapQuery<Integer> {
462 }
463
464 /**
465 * Clear command.
466 */
467 @SuppressWarnings("serial")
468 public static class Clear extends MapCommand<MapEntryUpdateResult.Status> {
469
470 @Override
471 public CompactionMode compaction() {
472 return CompactionMode.FULL;
473 }
474 }
475
476 /**
477 * Change listen.
478 */
479 @SuppressWarnings("serial")
480 public static class Listen implements Command<Void>, CatalystSerializable {
481 @Override
482 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
483 }
484
485 @Override
486 public void readObject(BufferInput<?> buffer, Serializer serializer) {
487 }
488
489 @Override
490 public String toString() {
491 return MoreObjects.toStringHelper(getClass())
492 .toString();
493 }
494 }
495
496 /**
497 * Change unlisten.
498 */
499 @SuppressWarnings("serial")
500 public static class Unlisten implements Command<Void>, CatalystSerializable {
501 @Override
502 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
503 }
504
505 @Override
506 public void readObject(BufferInput<?> buffer, Serializer serializer) {
507 }
508
509 @Override
510 public String toString() {
511 return MoreObjects.toStringHelper(getClass())
512 .toString();
513 }
514 }
515}