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