Permalink
Browse files

increment and decrement work with MarshalTranscoder now

  • Loading branch information...
1 parent 2ede784 commit 5ab76f148f992a9761c3bda6c70f6c4261271532 @flyerhzm flyerhzm committed Aug 22, 2012
View
53 spec/memcached_spec.rb
@@ -126,6 +126,51 @@
end
end
+ context "increment" do
+ it "should increment to default value" do
+ @memcached.delete "intkey" rescue nil
+ @memcached.increment "intkey"
+ @memcached.get("intkey").should == 1
+ end
+
+ it "should increment by 1" do
+ @memcached.delete "intkey" rescue nil
+ @memcached.increment "intkey"
+ @memcached.increment "intkey"
+ @memcached.get("intkey").should == 2
+ end
+
+ it "should increment by 10" do
+ @memcached.delete "intkey" rescue nil
+ @memcached.increment "intkey"
+ @memcached.increment "intkey", 10
+ @memcached.get("intkey").should == 11
+ end
+ end
+
+ context "decrement" do
+ it "should decrement to default value" do
+ @memcached.delete "intkey" rescue nil
+ @memcached.decrement "intkey"
+ @memcached.get("intkey").should == 0
+ end
+
+ it "should decrement by 1" do
+ @memcached.delete "intkey" rescue nil
+ 2.times { @memcached.increment "intkey" }
+ @memcached.decrement "intkey"
+ @memcached.get("intkey").should == 1
+ end
+
+ it "should decrement by 10" do
+ @memcached.delete "intkey" rescue nil
+ @memcached.increment "intkey"
+ @memcached.increment "intkey", 20
+ @memcached.decrement "intkey", 10
+ @memcached.get("intkey").should == 11
+ end
+ end
+
context "flush" do
it "should flush all keys" do
@memcached.set "key1", "value2"
@@ -172,6 +217,14 @@
@prefix_memcached.set "key2", "value2"
@prefix_memcached.get(["key1", "key2"]).should == {"key1" => "value1", "key2" => "value2"}
end
+
+ it "should increment/decrement with prefix_key" do
+ @prefix_memcached.delete "intkey" rescue nil
+ @prefix_memcached.increment "intkey"
+ @prefix_memcached.increment "intkey", 10
+ @prefix_memcached.decrement "intkey", 5
+ @memcached.get("jrubyintkey").should == 6
+ end
end
end
View
29 src/main/java/com/openfeint/memcached/Memcached.java
@@ -37,7 +37,7 @@
public class Memcached extends RubyObject {
private MemcachedClient client;
- private Transcoder<IRubyObject> transcoder;
+ private Transcoder transcoder;
private int ttl;
@@ -102,7 +102,7 @@ public IRubyObject add(ThreadContext context, IRubyObject[] args) {
int retry = 0;
while (true) {
try {
- boolean result = client.add(key, expiry, value, transcoder).get();
+ boolean result = (Boolean) client.add(key, expiry, value, transcoder).get();
if (result == false) {
throw Error.newNotStored(ruby, "not stored");
}
@@ -145,7 +145,7 @@ public IRubyObject replace(ThreadContext context, IRubyObject [] args) {
int retry = 0;
while (true) {
try {
- boolean result = client.replace(key, expiry, value, transcoder).get();
+ boolean result = (Boolean) client.replace(key, expiry, value, transcoder).get();
if (result == false) {
throw Error.newNotStored(ruby, "not stored");
}
@@ -188,7 +188,7 @@ public IRubyObject set(ThreadContext context, IRubyObject[] args) {
int retry = 0;
while (true) {
try {
- boolean result = client.set(key, expiry, value, transcoder).get();
+ boolean result = (Boolean) client.set(key, expiry, value, transcoder).get();
if (result == false) {
throw Error.newNotStored(ruby, "not stored");
}
@@ -230,18 +230,24 @@ public IRubyObject get(ThreadContext context, IRubyObject[] args) {
while (true) {
try {
if (keys instanceof RubyString) {
- IRubyObject value = client.get(getFullKey(keys.toString()), transcoder);
- if (value == null) {
+ Object ret = client.get(getFullKey(keys.toString()), transcoder);
+ if (ret == null) {
throw Error.newNotFound(ruby, "not found");
}
+ IRubyObject value;
+ if (ret instanceof IRubyObject) {
+ value = (IRubyObject) ret;
+ } else {
+ value = ruby.newFixnum((Long) ret);
+ }
return value;
} else if (keys instanceof RubyArray) {
RubyHash results = RubyHash.newHash(ruby);
Map<String, IRubyObject> bulkResults = client.getBulk(getFullKeys(keys.convertToArray()), transcoder);
for (String key : (List<String>) keys.convertToArray()) {
if (bulkResults.containsKey(getFullKey(key))) {
- results.put(key, bulkResults.get(getFullKey(key)));
+ results.put(key, (IRubyObject) bulkResults.get(getFullKey(key)));
}
}
return results;
@@ -257,7 +263,7 @@ public IRubyObject get(ThreadContext context, IRubyObject[] args) {
}
}
- @JRubyMethod(name = "incr", required = 1, optional = 2)
+ @JRubyMethod(name = { "increment", "incr" }, required = 1, optional = 2)
public IRubyObject incr(ThreadContext context, IRubyObject[] args) {
Ruby ruby = context.getRuntime();
String key = getFullKey(args[0].toString());
@@ -267,7 +273,7 @@ public IRubyObject incr(ThreadContext context, IRubyObject[] args) {
return ruby.newFixnum(result);
}
- @JRubyMethod(name = "decr", required = 1, optional = 2)
+ @JRubyMethod(name = { "decrement", "decr" }, required = 1, optional = 2)
public IRubyObject decr(ThreadContext context, IRubyObject[] args) {
Ruby ruby = context.getRuntime();
String key = getFullKey(args[0].toString());
@@ -438,13 +444,14 @@ protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash
builder.setOpTimeout(timeout);
}
builder.setDaemon(true);
- client = new MemcachedClient(builder.build(), addresses);
-
if ("marshal_zlib".equals(transcoderValue)) {
transcoder = new MarshalZlibTranscoder(ruby);
} else {
transcoder = new MarshalTranscoder(ruby);
}
+ builder.setTranscoder(transcoder);
+
+ client = new MemcachedClient(builder.build(), addresses);
} catch (IOException ioe) {
throw ruby.newIOErrorFromException(ioe);
}
View
40 src/main/java/com/openfeint/memcached/transcoder/MarshalTranscoder.java
@@ -16,9 +16,10 @@
* MarshalTranscoder does marshaling and unmarshaling.
*
*/
-public class MarshalTranscoder implements Transcoder<IRubyObject> {
+public class MarshalTranscoder implements Transcoder {
private Ruby ruby;
private int flags;
+ static final int SPECIAL_LONG = (3 << 8);
public MarshalTranscoder(Ruby ruby) {
this(ruby, 0);
@@ -33,23 +34,32 @@ public boolean asyncDecode(CachedData d) {
return false;
}
- public CachedData encode(IRubyObject o) {
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- MarshalStream marshal = new MarshalStream(ruby, baos, Integer.MAX_VALUE);
- marshal.dumpObject(o);
- byte[] bytes = baos.toByteArray();
- return new CachedData(getFlags(), bytes, bytes.length);
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
+ public CachedData encode(Object o) {
+ if (o instanceof IRubyObject) {
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ MarshalStream marshal = new MarshalStream(ruby, baos, Integer.MAX_VALUE);
+ marshal.dumpObject((IRubyObject) o);
+ byte[] bytes = baos.toByteArray();
+ return new CachedData(getFlags(), bytes, bytes.length);
+ } catch (IOException ioe) {
+ throw ruby.newIOErrorFromException(ioe);
+ }
+ } else {
+ byte[] bytes = o.toString().getBytes();
+ return new CachedData(SPECIAL_LONG, bytes, bytes.length);
}
}
- public IRubyObject decode(CachedData d) {
- try {
- return new UnmarshalStream(ruby, new ByteArrayInputStream(d.getData()), null, false, false).unmarshalObject();
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
+ public Object decode(CachedData d) {
+ if (d.getFlags() == SPECIAL_LONG) {
+ return Long.parseLong(new String(d.getData()).trim());
+ } else {
+ try {
+ return new UnmarshalStream(ruby, new ByteArrayInputStream(d.getData()), null, false, false).unmarshalObject();
+ } catch (IOException ioe) {
+ throw ruby.newIOErrorFromException(ioe);
+ }
}
}
View
89 src/main/java/com/openfeint/memcached/transcoder/MarshalZlibTranscoder.java
@@ -22,7 +22,7 @@
* MarshalZlibTranscoder do marshaling/unmarshaling and compressing/decompressing with zlib.
*
*/
-public class MarshalZlibTranscoder implements Transcoder<IRubyObject> {
+public class MarshalZlibTranscoder implements Transcoder {
private Ruby ruby;
private int flags;
@@ -39,52 +39,61 @@ public boolean asyncDecode(CachedData d) {
return false;
}
- public CachedData encode(IRubyObject o) {
- try {
- ByteArrayOutputStream out1 = new ByteArrayOutputStream();
- MarshalStream marshal = new MarshalStream(ruby, out1, Integer.MAX_VALUE);
- marshal.dumpObject(o);
+ public CachedData encode(Object o) {
+ if (o instanceof IRubyObject) {
+ try {
+ ByteArrayOutputStream out1 = new ByteArrayOutputStream();
+ MarshalStream marshal = new MarshalStream(ruby, out1, Integer.MAX_VALUE);
+ marshal.dumpObject((IRubyObject) o);
- byte[] bytes;
- if (flags == 1) {
- ByteArrayOutputStream out2 = new ByteArrayOutputStream();
- ZOutputStream zout = new ZOutputStream(out2, JZlib.Z_DEFAULT_COMPRESSION);
- zout.write(out1.toByteArray());
- zout.close();
- bytes = out2.toByteArray();
- } else {
- bytes = out1.toByteArray();
- }
+ byte[] bytes;
+ if (flags == 1) {
+ ByteArrayOutputStream out2 = new ByteArrayOutputStream();
+ ZOutputStream zout = new ZOutputStream(out2, JZlib.Z_DEFAULT_COMPRESSION);
+ zout.write(out1.toByteArray());
+ zout.close();
+ bytes = out2.toByteArray();
+ } else {
+ bytes = out1.toByteArray();
+ }
- return new CachedData(getFlags(), bytes, bytes.length);
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
+ return new CachedData(getFlags(), bytes, bytes.length);
+ } catch (IOException ioe) {
+ throw ruby.newIOErrorFromException(ioe);
+ }
+ } else {
+ byte[] bytes = o.toString().getBytes();
+ return new CachedData(MarshalTranscoder.SPECIAL_LONG, bytes, bytes.length);
}
}
- public IRubyObject decode(CachedData d) {
- try {
- byte[] bytes;
- if (d.getFlags() == 1) {
- ByteArrayInputStream in = new ByteArrayInputStream(d.getData());
- ZInputStream zin = new ZInputStream(in);
+ public Object decode(CachedData d) {
+ if (d.getFlags() == MarshalTranscoder.SPECIAL_LONG) {
+ return Long.parseLong(new String(d.getData()));
+ } else {
+ try {
+ byte[] bytes;
+ if (d.getFlags() == 1) {
+ ByteArrayInputStream in = new ByteArrayInputStream(d.getData());
+ ZInputStream zin = new ZInputStream(in);
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- int nRead;
- byte[] data = new byte[1024];
- while ((nRead = zin.read(data, 0, data.length)) != -1) {
- buffer.write(data, 0, nRead);
- }
- buffer.flush();
- bytes = buffer.toByteArray();
- zin.close();
- } else {
- bytes = d.getData();
- }
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ int nRead;
+ byte[] data = new byte[1024];
+ while ((nRead = zin.read(data, 0, data.length)) != -1) {
+ buffer.write(data, 0, nRead);
+ }
+ buffer.flush();
+ bytes = buffer.toByteArray();
+ zin.close();
+ } else {
+ bytes = d.getData();
+ }
- return new UnmarshalStream(ruby, new ByteArrayInputStream(bytes), null, false, false).unmarshalObject();
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
+ return new UnmarshalStream(ruby, new ByteArrayInputStream(bytes), null, false, false).unmarshalObject();
+ } catch (IOException ioe) {
+ throw ruby.newIOErrorFromException(ioe);
+ }
}
}
View
BIN target/spymemcached-ext-0.0.1.jar
Binary file not shown.

0 comments on commit 5ab76f1

Please sign in to comment.