Skip to content

Commit 3b587b3

Browse files
committed
FEATURE: Add CompletableFuture Set APIs
1 parent a930396 commit 3b587b3

3 files changed

Lines changed: 563 additions & 0 deletions

File tree

src/main/java/net/spy/memcached/v2/AsyncArcusCommands.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import java.util.ArrayList;
2222
import java.util.Collection;
2323
import java.util.HashMap;
24+
import java.util.HashSet;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Set;
2628
import java.util.concurrent.CompletableFuture;
2729
import java.util.concurrent.atomic.AtomicReference;
2830
import java.util.function.Supplier;
@@ -53,6 +55,8 @@
5355
import net.spy.memcached.collection.CollectionCount;
5456
import net.spy.memcached.collection.CollectionCreate;
5557
import net.spy.memcached.collection.CollectionDelete;
58+
import net.spy.memcached.collection.CollectionExist;
59+
import net.spy.memcached.collection.CollectionGet;
5660
import net.spy.memcached.collection.CollectionInsert;
5761
import net.spy.memcached.collection.CollectionMutate;
5862
import net.spy.memcached.collection.CollectionUpdate;
@@ -62,6 +66,11 @@
6266
import net.spy.memcached.collection.ListDelete;
6367
import net.spy.memcached.collection.ListGet;
6468
import net.spy.memcached.collection.ListInsert;
69+
import net.spy.memcached.collection.SetCreate;
70+
import net.spy.memcached.collection.SetDelete;
71+
import net.spy.memcached.collection.SetExist;
72+
import net.spy.memcached.collection.SetGet;
73+
import net.spy.memcached.collection.SetInsert;
6574
import net.spy.memcached.internal.result.GetsResultImpl;
6675
import net.spy.memcached.ops.APIType;
6776
import net.spy.memcached.ops.BTreeGetBulkOperation;
@@ -1660,4 +1669,117 @@ public ArcusFuture<Boolean> lopDelete(String key, int from, int to, boolean drop
16601669
ListDelete delete = new ListDelete(from, to, dropIfEmpty, false);
16611670
return collectionDelete(key, delete);
16621671
}
1672+
1673+
public ArcusFuture<Boolean> sopCreate(String key, ElementValueType type,
1674+
CollectionAttributes attributes) {
1675+
if (attributes == null) {
1676+
throw new IllegalArgumentException("CollectionAttributes cannot be null");
1677+
}
1678+
1679+
SetCreate create = new SetCreate(
1680+
TranscoderUtils.examineFlags(type), attributes.getExpireTime(),
1681+
attributes.getMaxCount(), attributes.getReadable(), false);
1682+
return collectionCreate(key, create);
1683+
}
1684+
1685+
public ArcusFuture<Boolean> sopInsert(String key, T value) {
1686+
return sopInsert(key, value, null);
1687+
}
1688+
1689+
public ArcusFuture<Boolean> sopInsert(String key, T value, CollectionAttributes attributes) {
1690+
CollectionInsert<T> collectionInsert = new SetInsert<>(value, null, attributes);
1691+
return collectionInsert(key, "", collectionInsert);
1692+
}
1693+
1694+
public ArcusFuture<Boolean> sopExist(String key, T value) {
1695+
AbstractArcusResult<Boolean> result = new AbstractArcusResult<>(new AtomicReference<>());
1696+
ArcusFutureImpl<Boolean> future = new ArcusFutureImpl<>(result);
1697+
ArcusClient client = arcusClientSupplier.get();
1698+
CollectionExist collectionExist = new SetExist<>(value, tcForCollection);
1699+
1700+
OperationCallback cb = new OperationCallback() {
1701+
@Override
1702+
public void receivedStatus(OperationStatus status) {
1703+
switch (status.getStatusCode()) {
1704+
case EXIST:
1705+
result.set(true);
1706+
break;
1707+
case NOT_EXIST:
1708+
result.set(false);
1709+
break;
1710+
case ERR_NOT_FOUND:
1711+
result.set(null);
1712+
break;
1713+
case CANCELLED:
1714+
future.internalCancel();
1715+
break;
1716+
default:
1717+
/*
1718+
* TYPE_MISMATCH / UNREADABLE / NOT_SUPPORTED or unknown statement
1719+
*/
1720+
result.addError(key, status);
1721+
break;
1722+
}
1723+
}
1724+
1725+
@Override
1726+
public void complete() {
1727+
future.complete();
1728+
}
1729+
};
1730+
Operation op = client.getOpFact().collectionExist(key, "", collectionExist, cb);
1731+
future.setOp(op);
1732+
client.addOp(key, op);
1733+
1734+
return future;
1735+
}
1736+
1737+
public ArcusFuture<Set<T>> sopGet(String key, int count, GetArgs args) {
1738+
AbstractArcusResult<Set<T>> result
1739+
= new AbstractArcusResult<>(new AtomicReference<>(new HashSet<>()));
1740+
ArcusFutureImpl<Set<T>> future = new ArcusFutureImpl<>(result);
1741+
ArcusClient client = arcusClientSupplier.get();
1742+
CollectionGet collectionGet = new SetGet(count, args.isWithDelete(), args.isDropIfEmpty());
1743+
1744+
CollectionGetOperation.Callback cb = new CollectionGetOperation.Callback() {
1745+
@Override
1746+
public void gotData(String subkey, int flags, byte[] data, byte[] eflag) {
1747+
CachedData cachedData = new CachedData(flags, data, tcForCollection.getMaxSize());
1748+
result.get().add(tcForCollection.decode(cachedData));
1749+
}
1750+
1751+
@Override
1752+
public void receivedStatus(OperationStatus status) {
1753+
switch (status.getStatusCode()) {
1754+
case SUCCESS:
1755+
case ERR_NOT_FOUND_ELEMENT:
1756+
break;
1757+
case ERR_NOT_FOUND:
1758+
result.set(null);
1759+
break;
1760+
case CANCELLED:
1761+
future.internalCancel();
1762+
break;
1763+
default:
1764+
/* TYPE_MISMATCH / UNREADABLE / NOT_SUPPORTED or unknown statement */
1765+
result.addError(key, status);
1766+
}
1767+
}
1768+
1769+
@Override
1770+
public void complete() {
1771+
future.complete();
1772+
}
1773+
};
1774+
Operation op = client.getOpFact().collectionGet(key, collectionGet, cb);
1775+
future.setOp(op);
1776+
client.addOp(key, op);
1777+
1778+
return future;
1779+
}
1780+
1781+
public ArcusFuture<Boolean> sopDelete(String key, T value, boolean dropIfEmpty) {
1782+
CollectionDelete collectionDelete = new SetDelete<>(value, dropIfEmpty, false, tcForCollection);
1783+
return collectionDelete(key, collectionDelete);
1784+
}
16631785
}

src/main/java/net/spy/memcached/v2/AsyncArcusCommandsIF.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Set;
2223

2324
import net.spy.memcached.CASValue;
2425
import net.spy.memcached.collection.CollectionAttributes;
@@ -554,4 +555,69 @@ ArcusFuture<Boolean> lopCreate(String key, ElementValueType type,
554555
* {@code false} if no elements are found in the range.
555556
*/
556557
ArcusFuture<Boolean> lopDelete(String key, int from, int to, boolean dropIfEmpty);
558+
559+
/**
560+
* Create a set with the given attributes.
561+
*
562+
* @param key key of the set to create
563+
* @param type element value type
564+
* @param attributes initial attributes of the set
565+
* @return {@code true} if created, {@code false} if the key already exists.
566+
*/
567+
ArcusFuture<Boolean> sopCreate(String key, ElementValueType type,
568+
CollectionAttributes attributes);
569+
570+
/**
571+
* Insert an element into a set.
572+
*
573+
* @param key key of the set
574+
* @param value the value to insert
575+
* @return {@code true} if the element was inserted, {@code false} if the element already exists,
576+
* {@code null} if the key is not found.
577+
*/
578+
ArcusFuture<Boolean> sopInsert(String key, T value);
579+
580+
/**
581+
* Insert an element into a set.
582+
* If the set does not exist, it is created with the given attributes.
583+
*
584+
* @param key key of the set
585+
* @param value the value to insert
586+
* @param attributes attributes to use when creating the set, or {@code null} to not create
587+
* @return {@code true} if the element was inserted, {@code false} if the element already exists,
588+
* {@code null} if the key is not found.
589+
*/
590+
ArcusFuture<Boolean> sopInsert(String key, T value, CollectionAttributes attributes);
591+
592+
/**
593+
* Check whether an element exists in a set.
594+
*
595+
* @param key key of the set
596+
* @param value the value to check
597+
* @return {@code true} if the element exists, {@code false} if the element is not found,
598+
* {@code null} if the key is not found.
599+
*/
600+
ArcusFuture<Boolean> sopExist(String key, T value);
601+
602+
/**
603+
* Get elements from a set.
604+
*
605+
* @param key key of the set
606+
* @param count maximum number of elements to retrieve
607+
* @param args arguments for get operation
608+
* @return set of element values, an empty set if no elements are found,
609+
* {@code null} if the key is not found.
610+
*/
611+
ArcusFuture<Set<T>> sopGet(String key, int count, GetArgs args);
612+
613+
/**
614+
* Delete an element from a set.
615+
*
616+
* @param key key of the set
617+
* @param value the value to delete
618+
* @param dropIfEmpty whether to delete the set if it becomes empty after deletion
619+
* @return {@code true} if the element was deleted, {@code false} if the element is not found,
620+
* {@code null} if the key is not found.
621+
*/
622+
ArcusFuture<Boolean> sopDelete(String key, T value, boolean dropIfEmpty);
557623
}

0 commit comments

Comments
 (0)