Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/main/java/net/spy/memcached/v2/AsyncArcusCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
Expand Down Expand Up @@ -62,6 +64,11 @@
import net.spy.memcached.collection.ListDelete;
import net.spy.memcached.collection.ListGet;
import net.spy.memcached.collection.ListInsert;
import net.spy.memcached.collection.SetCreate;
import net.spy.memcached.collection.SetDelete;
import net.spy.memcached.collection.SetExist;
import net.spy.memcached.collection.SetGet;
import net.spy.memcached.collection.SetInsert;
import net.spy.memcached.internal.result.GetsResultImpl;
import net.spy.memcached.ops.APIType;
import net.spy.memcached.ops.BTreeGetBulkOperation;
Expand Down Expand Up @@ -1660,4 +1667,117 @@ public ArcusFuture<Boolean> lopDelete(String key, int from, int to, boolean drop
ListDelete delete = new ListDelete(from, to, dropIfEmpty, false);
return collectionDelete(key, delete);
}

public ArcusFuture<Boolean> sopCreate(String key, ElementValueType type,
CollectionAttributes attributes) {
if (attributes == null) {
throw new IllegalArgumentException("CollectionAttributes cannot be null");
}

SetCreate create = new SetCreate(
TranscoderUtils.examineFlags(type), attributes.getExpireTime(),
attributes.getMaxCount(), attributes.getReadable(), false);
return collectionCreate(key, create);
}

public ArcusFuture<Boolean> sopInsert(String key, T value) {
return sopInsert(key, value, null);
}

public ArcusFuture<Boolean> sopInsert(String key, T value, CollectionAttributes attributes) {
SetInsert<T> insert = new SetInsert<>(value, null, attributes);
return collectionInsert(key, "", insert);
}

public ArcusFuture<Boolean> sopExist(String key, T value) {
AbstractArcusResult<Boolean> result = new AbstractArcusResult<>(new AtomicReference<>());
ArcusFutureImpl<Boolean> future = new ArcusFutureImpl<>(result);
SetExist<T> exist = new SetExist<>(value, tcForCollection);
ArcusClient client = arcusClientSupplier.get();

OperationCallback cb = new OperationCallback() {
@Override
public void receivedStatus(OperationStatus status) {
switch (status.getStatusCode()) {
case EXIST:
result.set(true);
break;
case NOT_EXIST:
result.set(false);
break;
case ERR_NOT_FOUND:
result.set(null);
break;
case CANCELLED:
future.internalCancel();
break;
default:
/*
* TYPE_MISMATCH / UNREADABLE / NOT_SUPPORTED or unknown statement
*/
result.addError(key, status);
break;
}
}

@Override
public void complete() {
future.complete();
}
};
Operation op = client.getOpFact().collectionExist(key, "", exist, cb);
future.setOp(op);
client.addOp(key, op);

return future;
}

public ArcusFuture<Set<T>> sopGet(String key, int count, GetArgs args) {
AbstractArcusResult<Set<T>> result
= new AbstractArcusResult<>(new AtomicReference<>(new HashSet<>()));
ArcusFutureImpl<Set<T>> future = new ArcusFutureImpl<>(result);
SetGet get = new SetGet(count, args.isWithDelete(), args.isDropIfEmpty());
ArcusClient client = arcusClientSupplier.get();

CollectionGetOperation.Callback cb = new CollectionGetOperation.Callback() {
@Override
public void gotData(String subkey, int flags, byte[] data, byte[] eflag) {
CachedData cachedData = new CachedData(flags, data, tcForCollection.getMaxSize());
result.get().add(tcForCollection.decode(cachedData));
}

@Override
public void receivedStatus(OperationStatus status) {
switch (status.getStatusCode()) {
case SUCCESS:
case ERR_NOT_FOUND_ELEMENT:
break;
case ERR_NOT_FOUND:
result.set(null);
break;
case CANCELLED:
future.internalCancel();
break;
default:
/* TYPE_MISMATCH / UNREADABLE / NOT_SUPPORTED or unknown statement */
result.addError(key, status);
}
}

@Override
public void complete() {
future.complete();
}
};
Operation op = client.getOpFact().collectionGet(key, get, cb);
future.setOp(op);
client.addOp(key, op);

return future;
}

public ArcusFuture<Boolean> sopDelete(String key, T value, boolean dropIfEmpty) {
SetDelete<T> delete = new SetDelete<>(value, dropIfEmpty, false, tcForCollection);
return collectionDelete(key, delete);
}
}
66 changes: 66 additions & 0 deletions src/main/java/net/spy/memcached/v2/AsyncArcusCommandsIF.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.List;
import java.util.Map;
import java.util.Set;

import net.spy.memcached.CASValue;
import net.spy.memcached.collection.CollectionAttributes;
Expand Down Expand Up @@ -554,4 +555,69 @@ ArcusFuture<Boolean> lopCreate(String key, ElementValueType type,
* {@code false} if no elements are found in the range.
*/
ArcusFuture<Boolean> lopDelete(String key, int from, int to, boolean dropIfEmpty);

/**
* Create a set with the given attributes.
*
* @param key key of the set to create
* @param type element value type
* @param attributes initial attributes of the set
* @return {@code true} if created, {@code false} if the key already exists.
*/
ArcusFuture<Boolean> sopCreate(String key, ElementValueType type,
CollectionAttributes attributes);

/**
* Insert an element into a set.
*
* @param key key of the set
* @param value the value to insert
* @return {@code true} if the element was inserted, {@code false} if the element already exists,
* {@code null} if the key is not found.
*/
ArcusFuture<Boolean> sopInsert(String key, T value);

/**
* Insert an element into a set.
* If the set does not exist, it is created with the given attributes.
*
* @param key key of the set
* @param value the value to insert
* @param attributes attributes to use when creating the set, or {@code null} to not create
* @return {@code true} if the element was inserted, {@code false} if the element already exists,
* {@code null} if the key is not found.
*/
ArcusFuture<Boolean> sopInsert(String key, T value, CollectionAttributes attributes);

/**
* Check whether an element exists in a set.
*
* @param key key of the set
* @param value the value to check
* @return {@code true} if the element exists, {@code false} if the element is not found,
* {@code null} if the key is not found.
*/
ArcusFuture<Boolean> sopExist(String key, T value);

/**
* Get elements randomly from a set.
*
* @param key key of the set
* @param count number of elements to retrieve randomly (0 means all elements, max 1000)
* @param args arguments for get operation
* @return set of element values, an empty set if no elements are found,
* {@code null} if the key is not found.
*/
ArcusFuture<Set<T>> sopGet(String key, int count, GetArgs args);

/**
* Delete an element from a set.
*
* @param key key of the set
* @param value the value to delete
* @param dropIfEmpty whether to delete the set if it becomes empty after deletion
* @return {@code true} if the element was deleted, {@code false} if the element is not found,
* {@code null} if the key is not found.
*/
ArcusFuture<Boolean> sopDelete(String key, T value, boolean dropIfEmpty);
}
Loading
Loading