Skip to content

Commit

Permalink
convert argument to Map<String, String> instead of RubyHash
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Aug 29, 2012
1 parent 9e22a06 commit 1ecd549
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
71 changes: 36 additions & 35 deletions src/main/java/com/openfeint/memcached/Memcached.java
Expand Up @@ -29,6 +29,7 @@
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -58,11 +59,12 @@ public Memcached(final Ruby ruby, RubyClass rubyClass) {
@JRubyMethod(name = "initialize", optional = 2)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
Ruby ruby = context.getRuntime();
RubyHash options;
Map<String, String> options = new HashMap<String, String>();
if (args.length > 1) {
options = args[1].convertToHash();
} else {
options = new RubyHash(ruby);
RubyHash arguments = args[1].convertToHash();
for (Object key : arguments.keySet()) {
options.put(key.toString(), arguments.get(key).toString());
}
}
List<String> servers = new ArrayList<String>();
if (args.length > 0) {
Expand Down Expand Up @@ -376,48 +378,47 @@ protected int getDefaultTTL() {
return ttl;
}

protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash options) {
protected IRubyObject init(ThreadContext context, List<String> servers, Map<String, String> opts) {
Ruby ruby = context.getRuntime();
List<InetSocketAddress> addresses = AddrUtil.getAddresses(servers);
try {
ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

String distributionValue = "ketama";
String hashValue = "fnv1_32";
RubyBoolean binaryValue = ruby.getFalse();
RubyBoolean shouldOptimize = ruby.getFalse();
boolean binaryValue = false;
boolean shouldOptimize = false;
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.isEmpty()) {
if (opts.containsKey("distribution")) {
distributionValue = opts.get("distribution");
}
if (opts.containsKey(ruby.newSymbol("hash"))) {
hashValue = opts.get(ruby.newSymbol("hash")).toString();
if (opts.containsKey("hash")) {
hashValue = opts.get("hash");
}
if (opts.containsKey(ruby.newSymbol("binary_protocol"))) {
binaryValue = (RubyBoolean) opts.get(ruby.newSymbol("binary_protocol"));
if (opts.containsKey("binary_protocol")) {
binaryValue = Boolean.parseBoolean(opts.get("binary_protocol"));
}
if (opts.containsKey(ruby.newSymbol("should_optimize"))) {
shouldOptimize = (RubyBoolean) opts.get(ruby.newSymbol("should_optimize"));
if (opts.containsKey("should_optimize")) {
shouldOptimize = Boolean.parseBoolean(opts.get("should_optimize"));
}
if (opts.containsKey(ruby.newSymbol("default_ttl"))) {
ttl = Integer.parseInt(opts.get(ruby.newSymbol("default_ttl")).toString());
if (opts.containsKey("default_ttl")) {
ttl = Integer.parseInt(opts.get("default_ttl"));
}
if (opts.containsKey(ruby.newSymbol("timeout"))) {
timeout = Integer.parseInt(opts.get(ruby.newSymbol("timeout")).toString());
if (opts.containsKey("timeout")) {
timeout = Integer.parseInt(opts.get("timeout"));
}
if (opts.containsKey(ruby.newSymbol("exception_retry_limit"))) {
exceptionRetryLimit = Integer.parseInt(opts.get(ruby.newSymbol("exception_retry_limit")).toString());
if (opts.containsKey("exception_retry_limit")) {
exceptionRetryLimit = Integer.parseInt(opts.get("exception_retry_limit"));
}
if (opts.containsKey(ruby.newSymbol("namespace"))) {
prefixKey = opts.get(ruby.newSymbol("namespace")).toString();
if (opts.containsKey("namespace")) {
prefixKey = opts.get("namespace");
}
if (opts.containsKey(ruby.newSymbol("prefix_key"))) {
prefixKey = opts.get(ruby.newSymbol("prefix_key")).toString();
if (opts.containsKey("prefix_key")) {
prefixKey = opts.get("prefix_key");
}
if (opts.containsKey(ruby.newSymbol("transcoder"))) {
transcoderValue = opts.get(ruby.newSymbol("transcoder")).toString();
if (opts.containsKey("transcoder")) {
transcoderValue = opts.get("transcoder");
}
}

Expand Down Expand Up @@ -446,10 +447,10 @@ protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash
throw Error.newNotSupport(ruby, "hash not support");
}

if (ruby.getTrue() == binaryValue) {
if (binaryValue) {
builder.setProtocol(Protocol.BINARY);
}
if (ruby.getTrue() == shouldOptimize) {
if (shouldOptimize) {
builder.setShouldOptimize(true);
}

Expand All @@ -465,11 +466,11 @@ protected IRubyObject init(ThreadContext context, List<String> servers, RubyHash
builder.setTranscoder(transcoder);

client = new MemcachedClient(builder.build(), addresses);
} catch (IOException ioe) {
throw ruby.newIOErrorFromException(ioe);
}

return context.nil;
return context.nil;
} catch (IOException e) {
throw ruby.newIOErrorFromException(e);
}
}

private int getExpiry(IRubyObject[] args) {
Expand Down
37 changes: 22 additions & 15 deletions src/main/java/com/openfeint/memcached/Rails.java
Expand Up @@ -16,6 +16,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

@JRubyClass(name = "Memcached::Rails", parent = "Memcached")
public class Rails extends Memcached {
Expand All @@ -30,12 +32,7 @@ public Rails(final Ruby ruby, RubyClass rubyClass) {
@JRubyMethod(name = "initialize", rest = true)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
Ruby ruby = context.getRuntime();
RubyHash opts;
if (args[args.length - 1] instanceof RubyHash) {
opts = (RubyHash) args[args.length - 1];
} else {
opts = new RubyHash(ruby);
}

List<String> servers = new ArrayList<String>();
for (IRubyObject arg : args) {
if (arg instanceof RubyString) {
Expand All @@ -44,20 +41,30 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
servers.addAll((List<String>) arg.convertToArray());
}
}
if (servers.isEmpty()) {
IRubyObject serverNames = (IRubyObject) opts.get(ruby.newSymbol("servers"));
servers.addAll((List<String>) serverNames.convertToArray());

Map<String, String> options = new HashMap<String, String>();
if (args[args.length - 1] instanceof RubyHash) {
RubyHash arguments = args[args.length - 1].convertToHash();
for (Object key : arguments.keySet()) {
if (!"servers".equals(key.toString())) {
options.put(key.toString(), arguments.get(key).toString());
}
}
if (servers.isEmpty()) {
IRubyObject serverNames = (IRubyObject) arguments.get(ruby.newSymbol("servers"));
servers.addAll((List<String>) serverNames.convertToArray());
}
}
if (opts.containsKey(ruby.newSymbol("namespace"))) {
opts.put(ruby.newSymbol("prefix_key"), opts.get(ruby.newSymbol("namespace")));
if (options.containsKey("namespace")) {
options.put("prefix_key", options.get("namespace"));
}
if (opts.containsKey(ruby.newSymbol("namespace_separator"))) {
opts.put(ruby.newSymbol("prefix_delimiter"), opts.get(ruby.newSymbol("namespace_separator")));
if (options.containsKey("namespace_separator")) {
options.put("prefix_delimiter", options.get("namespace_separator"));
}
if (opts.containsKey(ruby.newSymbol("string_return_types"))) {
if (options.containsKey("string_return_types")) {
stringReturnTypes = true;
}
return super.init(context, servers, opts);
return super.init(context, servers, options);
}

@JRubyMethod(name = "active?")
Expand Down
Binary file modified target/spymemcached-ext-0.0.1.jar
Binary file not shown.

4 comments on commit 1ecd549

@LeifWarner
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with the spymemcached-ext.jar?

@flyerhzm
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spymemcached-ext.jar is an extension to spymemcached, which provides the exactly same consistent algorithm of memcached.gem.

@LeifWarner
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a build of spymemcached + jruby wrapper + the KetamaNodeLocator in the net/spy/memcached dir of this repo?

@flyerhzm
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is.

Please sign in to comment.