|
59 | 59 | import net.spy.memcached.ops.CollectionInsertOperation; |
60 | 60 | import net.spy.memcached.ops.ConcatenationType; |
61 | 61 | import net.spy.memcached.ops.GetOperation; |
| 62 | +import net.spy.memcached.ops.Mutator; |
62 | 63 | import net.spy.memcached.ops.GetsOperation; |
63 | 64 | import net.spy.memcached.ops.Operation; |
64 | 65 | import net.spy.memcached.ops.OperationCallback; |
@@ -440,6 +441,70 @@ public void complete() { |
440 | 441 | return future; |
441 | 442 | } |
442 | 443 |
|
| 444 | + |
| 445 | + public ArcusFuture<Long> incr(String key, int delta) { |
| 446 | + return mutate(Mutator.incr, key, delta, -1L, 0); |
| 447 | + } |
| 448 | + |
| 449 | + public ArcusFuture<Long> incr(String key, int delta, long initial, int exp) { |
| 450 | + if (initial < 0) { |
| 451 | + throw new IllegalArgumentException("Initial value must be 0 or greater."); |
| 452 | + } |
| 453 | + return mutate(Mutator.incr, key, delta, initial, exp); |
| 454 | + } |
| 455 | + |
| 456 | + public ArcusFuture<Long> decr(String key, int delta) { |
| 457 | + return mutate(Mutator.decr, key, delta, -1L, 0); |
| 458 | + } |
| 459 | + |
| 460 | + public ArcusFuture<Long> decr(String key, int delta, long initial, int exp) { |
| 461 | + if (initial < 0) { |
| 462 | + throw new IllegalArgumentException("Initial value must be 0 or greater."); |
| 463 | + } |
| 464 | + return mutate(Mutator.decr, key, delta, initial, exp); |
| 465 | + } |
| 466 | + |
| 467 | + private ArcusFuture<Long> mutate(Mutator mutator, String key, int delta, long initial, int exp) { |
| 468 | + if (delta <= 0) { |
| 469 | + throw new IllegalArgumentException("Delta must be greater than 0."); |
| 470 | + } |
| 471 | + |
| 472 | + AbstractArcusResult<Long> result = new AbstractArcusResult<>(new AtomicReference<>()); |
| 473 | + ArcusFutureImpl<Long> future = new ArcusFutureImpl<>(result); |
| 474 | + ArcusClient client = arcusClientSupplier.get(); |
| 475 | + |
| 476 | + OperationCallback cb = new OperationCallback() { |
| 477 | + @Override |
| 478 | + public void receivedStatus(OperationStatus status) { |
| 479 | + switch (status.getStatusCode()) { |
| 480 | + case SUCCESS: |
| 481 | + result.set(Long.parseLong(status.getMessage())); |
| 482 | + break; |
| 483 | + case ERR_NOT_FOUND: |
| 484 | + result.set(-1L); |
| 485 | + break; |
| 486 | + case CANCELLED: |
| 487 | + future.internalCancel(); |
| 488 | + break; |
| 489 | + default: |
| 490 | + // TYPE_MISMATCH or unknown statement |
| 491 | + result.addError(key, status); |
| 492 | + break; |
| 493 | + } |
| 494 | + } |
| 495 | + |
| 496 | + @Override |
| 497 | + public void complete() { |
| 498 | + future.complete(); |
| 499 | + } |
| 500 | + }; |
| 501 | + Operation op = client.getOpFact().mutate(mutator, key, delta, initial, exp, cb); |
| 502 | + future.setOp(op); |
| 503 | + client.addOp(key, op); |
| 504 | + |
| 505 | + return future; |
| 506 | + } |
| 507 | + |
443 | 508 | public ArcusFuture<Map<String, CASValue<T>>> multiGets(List<String> keys) { |
444 | 509 | ArcusClient client = arcusClientSupplier.get(); |
445 | 510 | Collection<Map.Entry<MemcachedNode, List<String>>> arrangedKeys |
|
0 commit comments