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() { |
| 55 | return ConsistencyLevel.LINEARIZABLE; |
| 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() { |
| 81 | return ConsistencyLevel.BOUNDED_LINEARIZABLE; |
| 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 | /** |
| 248 | * Map transaction commit command. |
| 249 | */ |
| 250 | @SuppressWarnings("serial") |
| 251 | public static class TransactionCommit extends MapCommand<CommitResult> { |
| 252 | private TransactionId transactionId; |
| 253 | |
| 254 | public TransactionCommit() { |
| 255 | } |
| 256 | |
| 257 | public TransactionCommit(TransactionId transactionId) { |
| 258 | this.transactionId = transactionId; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the transaction identifier. |
| 263 | * @return transaction id |
| 264 | */ |
| 265 | public TransactionId transactionId() { |
| 266 | return transactionId; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 271 | super.writeObject(buffer, serializer); |
| 272 | serializer.writeObject(transactionId, buffer); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 277 | super.readObject(buffer, serializer); |
| 278 | transactionId = serializer.readObject(buffer); |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public String toString() { |
| 283 | return MoreObjects.toStringHelper(getClass()) |
| 284 | .add("transactionId", transactionId) |
| 285 | .toString(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Map transaction rollback command. |
| 291 | */ |
| 292 | @SuppressWarnings("serial") |
| 293 | public static class TransactionRollback extends MapCommand<RollbackResult> { |
| 294 | private TransactionId transactionId; |
| 295 | |
| 296 | public TransactionRollback() { |
| 297 | } |
| 298 | |
| 299 | public TransactionRollback(TransactionId transactionId) { |
| 300 | this.transactionId = transactionId; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Returns the transaction identifier. |
| 305 | * @return transaction id |
| 306 | */ |
| 307 | public TransactionId transactionId() { |
| 308 | return transactionId; |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 313 | super.writeObject(buffer, serializer); |
| 314 | serializer.writeObject(transactionId, buffer); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 319 | super.readObject(buffer, serializer); |
| 320 | transactionId = serializer.readObject(buffer); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public String toString() { |
| 325 | return MoreObjects.toStringHelper(getClass()) |
| 326 | .add("transactionId", transactionId) |
| 327 | .toString(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Map update command. |
| 333 | */ |
| 334 | @SuppressWarnings("serial") |
| 335 | public static class UpdateAndGet extends MapCommand<MapEntryUpdateResult<String, byte[]>> { |
| 336 | private String key; |
| 337 | private byte[] value; |
| 338 | private Match<byte[]> valueMatch; |
| 339 | private Match<Long> versionMatch; |
| 340 | |
| 341 | public UpdateAndGet() { |
| 342 | } |
| 343 | |
| 344 | public UpdateAndGet(String key, |
| 345 | byte[] value, |
| 346 | Match<byte[]> valueMatch, |
| 347 | Match<Long> versionMatch) { |
| 348 | this.key = key; |
| 349 | this.value = value; |
| 350 | this.valueMatch = valueMatch; |
| 351 | this.versionMatch = versionMatch; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Returns the key. |
| 356 | * @return key |
| 357 | */ |
| 358 | public String key() { |
| 359 | return this.key; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Returns the value. |
| 364 | * @return value |
| 365 | */ |
| 366 | public byte[] value() { |
| 367 | return this.value; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Returns the value match. |
| 372 | * @return value match |
| 373 | */ |
| 374 | public Match<byte[]> valueMatch() { |
| 375 | return this.valueMatch; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Returns the version match. |
| 380 | * @return version match |
| 381 | */ |
| 382 | public Match<Long> versionMatch() { |
| 383 | return this.versionMatch; |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public CompactionMode compaction() { |
| 388 | return value == null ? CompactionMode.FULL : CompactionMode.QUORUM; |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 393 | super.writeObject(buffer, serializer); |
| 394 | serializer.writeObject(key, buffer); |
| 395 | serializer.writeObject(value, buffer); |
| 396 | serializer.writeObject(valueMatch, buffer); |
| 397 | serializer.writeObject(versionMatch, buffer); |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 402 | super.readObject(buffer, serializer); |
| 403 | key = serializer.readObject(buffer); |
| 404 | value = serializer.readObject(buffer); |
| 405 | valueMatch = serializer.readObject(buffer); |
| 406 | versionMatch = serializer.readObject(buffer); |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public String toString() { |
| 411 | return MoreObjects.toStringHelper(getClass()) |
| 412 | .add("key", key) |
| 413 | .add("value", value) |
| 414 | .add("valueMatch", valueMatch) |
| 415 | .add("versionMatch", versionMatch) |
| 416 | .toString(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Get query. |
| 422 | */ |
| 423 | @SuppressWarnings("serial") |
| 424 | public static class Get extends KeyQuery<Versioned<byte[]>> { |
| 425 | public Get() { |
| 426 | } |
| 427 | |
| 428 | public Get(String key) { |
| 429 | super(key); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Is empty query. |
| 435 | */ |
| 436 | @SuppressWarnings("serial") |
| 437 | public static class IsEmpty extends MapQuery<Boolean> { |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * KeySet query. |
| 442 | */ |
| 443 | @SuppressWarnings("serial") |
| 444 | public static class KeySet extends MapQuery<Set<String>> { |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * KeySet query. |
| 449 | */ |
| 450 | @SuppressWarnings("serial") |
| 451 | public static class Values extends MapQuery<Collection<Versioned<byte[]>>> { |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * KeySet query. |
| 456 | */ |
| 457 | @SuppressWarnings("serial") |
| 458 | public static class EntrySet extends MapQuery<Set<Map.Entry<String, Versioned<byte[]>>>> { |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Size query. |
| 463 | */ |
| 464 | @SuppressWarnings("serial") |
| 465 | public static class Size extends MapQuery<Integer> { |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Clear command. |
| 470 | */ |
| 471 | @SuppressWarnings("serial") |
| 472 | public static class Clear extends MapCommand<MapEntryUpdateResult.Status> { |
| 473 | |
| 474 | @Override |
| 475 | public CompactionMode compaction() { |
| 476 | return CompactionMode.FULL; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Change listen. |
| 482 | */ |
| 483 | @SuppressWarnings("serial") |
| 484 | public static class Listen implements Command<Void>, CatalystSerializable { |
| 485 | @Override |
| 486 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public String toString() { |
| 495 | return MoreObjects.toStringHelper(getClass()) |
| 496 | .toString(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Change unlisten. |
| 502 | */ |
| 503 | @SuppressWarnings("serial") |
| 504 | public static class Unlisten implements Command<Void>, CatalystSerializable { |
| 505 | @Override |
| 506 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public String toString() { |
| 515 | return MoreObjects.toStringHelper(getClass()) |
| 516 | .toString(); |
| 517 | } |
| 518 | } |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 519 | |
| 520 | /** |
| 521 | * Map command type resolver. |
| 522 | */ |
| 523 | public static class TypeResolver implements SerializableTypeResolver { |
| 524 | @Override |
| 525 | public void resolve(SerializerRegistry registry) { |
| 526 | registry.register(ContainsKey.class, -761); |
| 527 | registry.register(ContainsValue.class, -762); |
| 528 | registry.register(Get.class, -763); |
| 529 | registry.register(EntrySet.class, -764); |
| 530 | registry.register(Values.class, -765); |
| 531 | registry.register(KeySet.class, -766); |
| 532 | registry.register(Clear.class, -767); |
| 533 | registry.register(IsEmpty.class, -768); |
| 534 | registry.register(Size.class, -769); |
| 535 | registry.register(Listen.class, -770); |
| 536 | registry.register(Unlisten.class, -771); |
| 537 | registry.register(TransactionPrepare.class, -772); |
| 538 | registry.register(TransactionCommit.class, -773); |
| 539 | registry.register(TransactionRollback.class, -774); |
| 540 | registry.register(UpdateAndGet.class, -775); |
| 541 | } |
| 542 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 543 | } |