Created
October 3, 2012 07:12
-
-
Save couchtim/3825527 to your computer and use it in GitHub Desktop.
simple stand-alone append test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.spy.memcached.*; | |
import net.spy.memcached.ConnectionFactoryBuilder.Protocol; | |
import net.spy.memcached.auth.AuthDescriptor; | |
import net.spy.memcached.auth.PlainCallbackHandler; | |
import net.spy.memcached.internal.OperationFuture; | |
import net.spy.memcached.ops.OperationStatus; | |
public class SimpleAppend { | |
public static Boolean checkOp(String tag, OperationFuture result) { | |
System.err.print(tag + ": "); | |
OperationStatus status; | |
try { | |
status = result.getStatus(); | |
} | |
catch (Exception e) { | |
System.err.println("Exception during operation: " + e.getMessage()); | |
return false; | |
} | |
if (!status.isSuccess()) { | |
System.err.println("Error during operation: " + status); | |
} | |
else { | |
System.err.println(status.getMessage()); | |
} | |
return status.isSuccess(); | |
} | |
public static void main(String args[]) { | |
final String HOSTS = "10.4.2.12:11211 10.4.2.14:11211"; | |
final String BUCKET = "hasAPass"; | |
final String PASSWORD = "password"; | |
final String KEY = "myKey"; | |
final String INITIAL_VALUE = "myValue"; | |
final String APPEND_VALUE = INITIAL_VALUE + INITIAL_VALUE; | |
MemcachedClient c; | |
try { | |
AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, | |
new PlainCallbackHandler(BUCKET, PASSWORD)); | |
c = new MemcachedClient( | |
new ConnectionFactoryBuilder() | |
.setProtocol(Protocol.BINARY) | |
.setAuthDescriptor(ad) | |
.build(), | |
AddrUtil.getAddresses(HOSTS)); | |
} catch (Exception e) { | |
System.err.println("Error connecting to cluster: " + e.getMessage()); | |
System.exit(1); | |
return; //NOTREACHED | |
} | |
checkOp("set", c.set(KEY, 0, INITIAL_VALUE)); | |
System.err.println("Initial value for " + KEY + " is '" + c.get(KEY) + "'!"); | |
checkOp("append", c.append(0, KEY, INITIAL_VALUE)); | |
String after = (String)c.get(KEY); | |
System.err.println("After append, " + KEY + " is '" + after + "'!"); | |
if (!after.equals(APPEND_VALUE)) { | |
System.err.println("Error, expected '" + APPEND_VALUE + "', got '" + after + "'"); | |
System.exit(1); | |
return; //NOTREACHED | |
} | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment