Skip to content

Commit

Permalink
remove private ruby in Memcached.java
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Aug 16, 2012
1 parent 3c5622a commit e804608
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions src/main/java/com/openfeint/memcached/Memcached.java
Expand Up @@ -31,8 +31,6 @@

@JRubyClass(name = "Memcached")
public class Memcached extends RubyObject {
private Ruby ruby;

private MemcachedClient client;

private Transcoder<IRubyObject> transcoder;
Expand All @@ -43,7 +41,6 @@ public class Memcached extends RubyObject {

public Memcached(final Ruby ruby, RubyClass rubyClass) {
super(ruby, rubyClass);
this.ruby = ruby;

ttl = 604800;
prefixKey = "";
Expand All @@ -55,7 +52,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
if (args.length > 1) {
options = args[1].convertToHash();
} else {
options = new RubyHash(ruby);
options = new RubyHash(getRuntime());
}
List<String> servers = new ArrayList<String>();
if (args.length > 0) {
Expand All @@ -79,9 +76,9 @@ public IRubyObject servers(ThreadContext context) {
if (addressStr.indexOf("/") == 0) {
addressStr = addressStr.replace("/", "");
}
addresses.add(ruby.newString(addressStr));
addresses.add(getRuntime().newString(addressStr));
}
return ruby.newArray(addresses);
return getRuntime().newArray(addresses);
}

@JRubyMethod(name = "add", required = 2, optional = 3)
Expand All @@ -92,7 +89,7 @@ public IRubyObject add(ThreadContext context, IRubyObject[] args) {
try {
boolean result = client.add(key, timeout, value, transcoder).get();
if (result == false) {
throw Error.newNotStored(ruby, "not stored");
throw Error.newNotStored(getRuntime(), "not stored");
}
return context.nil;
} catch (ExecutionException ee) {
Expand All @@ -110,7 +107,7 @@ public IRubyObject replace(ThreadContext context, IRubyObject [] args) {
try {
boolean result = client.replace(key, timeout, value, transcoder).get();
if (result == false) {
throw Error.newNotStored(ruby, "not stored");
throw Error.newNotStored(getRuntime(), "not stored");
}
return context.nil;
} catch (ExecutionException ee) {
Expand All @@ -128,7 +125,7 @@ public IRubyObject set(ThreadContext context, IRubyObject[] args) {
try {
boolean result = client.set(key, timeout, value, transcoder).get();
if (result == false) {
throw Error.newNotStored(ruby, "not stored");
throw Error.newNotStored(getRuntime(), "not stored");
}
return context.nil;
} catch (ExecutionException ee) {
Expand All @@ -142,18 +139,18 @@ public IRubyObject set(ThreadContext context, IRubyObject[] args) {
public IRubyObject get(ThreadContext context, IRubyObject key) {
IRubyObject value = client.get(getFullKey(key), transcoder);
if (value == null) {
throw Error.newNotFound(ruby, "not found");
throw Error.newNotFound(getRuntime(), "not found");
}
return value;
}

@JRubyMethod
public IRubyObject multiget(ThreadContext context, IRubyObject keys) {
RubyHash results = RubyHash.newHash(ruby);
RubyHash results = RubyHash.newHash(getRuntime());

Map<String, IRubyObject> bulkResults = client.getBulk(keys.convertToArray(), transcoder);
for (Map.Entry<String, IRubyObject> entry : bulkResults.entrySet()) {
results.op_aset(context, ruby.newString(entry.getKey()), entry.getValue());
results.op_aset(context, getRuntime().newString(entry.getKey()), entry.getValue());
}
return results;
}
Expand All @@ -164,7 +161,7 @@ public IRubyObject incr(ThreadContext context, IRubyObject[] args) {
int by = getIncrDecrBy(args);
int timeout = getTimeout(args);
long result = client.incr(key, by, 1, timeout);
return ruby.newFixnum(result);
return getRuntime().newFixnum(result);
}

@JRubyMethod(name = "decr", required = 1, optional = 2)
Expand All @@ -173,15 +170,15 @@ public IRubyObject decr(ThreadContext context, IRubyObject[] args) {
int by = getIncrDecrBy(args);
int timeout = getTimeout(args);
long result = client.decr(key, by, 0, timeout);
return ruby.newFixnum(result);
return getRuntime().newFixnum(result);
}

@JRubyMethod
public IRubyObject delete(ThreadContext context, IRubyObject key) {
try {
boolean result = client.delete(getFullKey(key)).get();
if (result == false) {
throw Error.newNotFound(ruby, "not found");
throw Error.newNotFound(getRuntime(), "not found");
}
return context.nil;
} catch (ExecutionException ee) {
Expand All @@ -205,13 +202,13 @@ public IRubyObject flush(ThreadContext context) {

@JRubyMethod
public IRubyObject stats(ThreadContext context) {
RubyHash results = RubyHash.newHash(ruby);
RubyHash results = RubyHash.newHash(getRuntime());
for(Map.Entry<SocketAddress, Map<String, String>> entry : client.getStats().entrySet()) {
RubyHash serverHash = RubyHash.newHash(ruby);
RubyHash serverHash = RubyHash.newHash(getRuntime());
for(Map.Entry<String, String> server : entry.getValue().entrySet()) {
serverHash.op_aset(context, ruby.newString(server.getKey()), ruby.newString(server.getValue()));
serverHash.op_aset(context, getRuntime().newString(server.getKey()), getRuntime().newString(server.getValue()));
}
results.op_aset(context, ruby.newString(entry.getKey().toString()), serverHash);
results.op_aset(context, getRuntime().newString(entry.getKey().toString()), serverHash);
}
return results;
}
Expand All @@ -234,26 +231,26 @@ protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash
String transcoderValue = null;
if (!options.isEmpty()) {
RubyHash opts = options.convertToHash();
if (opts.containsKey(ruby.newSymbol("distribution"))) {
distributionValue = opts.get(ruby.newSymbol("distribution")).toString();
if (opts.containsKey(getRuntime().newSymbol("distribution"))) {
distributionValue = opts.get(getRuntime().newSymbol("distribution")).toString();
}
if (opts.containsKey(ruby.newSymbol("hash"))) {
hashValue = opts.get(ruby.newSymbol("hash")).toString();
if (opts.containsKey(getRuntime().newSymbol("hash"))) {
hashValue = opts.get(getRuntime().newSymbol("hash")).toString();
}
if (opts.containsKey(ruby.newSymbol("binary_protocol"))) {
binaryValue = opts.get(ruby.newSymbol("binary_protocol")).toString();
if (opts.containsKey(getRuntime().newSymbol("binary_protocol"))) {
binaryValue = opts.get(getRuntime().newSymbol("binary_protocol")).toString();
}
if (opts.containsKey(ruby.newSymbol("default_ttl"))) {
ttl = Integer.parseInt(opts.get(ruby.newSymbol("default_ttl")).toString());
if (opts.containsKey(getRuntime().newSymbol("default_ttl"))) {
ttl = Integer.parseInt(opts.get(getRuntime().newSymbol("default_ttl")).toString());
}
if (opts.containsKey(ruby.newSymbol("namespace"))) {
prefixKey = opts.get(ruby.newSymbol("namespace")).toString();
if (opts.containsKey(getRuntime().newSymbol("namespace"))) {
prefixKey = opts.get(getRuntime().newSymbol("namespace")).toString();
}
if (opts.containsKey(ruby.newSymbol("prefix_key"))) {
prefixKey = opts.get(ruby.newSymbol("prefix_key")).toString();
if (opts.containsKey(getRuntime().newSymbol("prefix_key"))) {
prefixKey = opts.get(getRuntime().newSymbol("prefix_key")).toString();
}
if (opts.containsKey(ruby.newSymbol("transcoder"))) {
transcoderValue = opts.get(ruby.newSymbol("transcoder")).toString();
if (opts.containsKey(getRuntime().newSymbol("transcoder"))) {
transcoderValue = opts.get(getRuntime().newSymbol("transcoder")).toString();
}
}

Expand All @@ -262,7 +259,7 @@ protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash
} else if ("ketama".equals(distributionValue) || "consistent_ketama".equals(distributionValue)) {
builder.setLocatorType(Locator.CONSISTENT);
} else {
throw Error.newNotSupport(ruby, "distribution not support");
throw Error.newNotSupport(getRuntime(), "distribution not support");
}
if ("native".equals(hashValue)) {
builder.setHashAlg(DefaultHashAlgorithm.NATIVE_HASH);
Expand All @@ -279,7 +276,7 @@ protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash
} else if ("ketama".equals(hashValue)) {
builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
} else {
throw Error.newNotSupport(ruby, "hash not support");
throw Error.newNotSupport(getRuntime(), "hash not support");
}

if ("true".equals(binaryValue)) {
Expand All @@ -289,9 +286,9 @@ protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash
client = new MemcachedClient(builder.build(), addresses);

if ("marshal_zlib".equals(transcoderValue)) {
transcoder = new MarshalZlibTranscoder(ruby);
transcoder = new MarshalZlibTranscoder(getRuntime());
} else {
transcoder = new MarshalTranscoder(ruby);
transcoder = new MarshalTranscoder(getRuntime());
}
} catch (IOException ioe) {
throw context.runtime.newIOErrorFromException(ioe);
Expand Down
Binary file modified target/spymemcached-ext-0.0.1.jar
Binary file not shown.

0 comments on commit e804608

Please sign in to comment.