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