Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support For Alternative Transcoder Services #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.spy.memcached.metrics.MetricCollector;
import net.spy.memcached.metrics.MetricType;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.transcoders.TranscoderService;
import net.spy.memcached.transcoders.Transcoder;

/**
Expand Down Expand Up @@ -152,6 +153,11 @@ MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
*/
Transcoder<Object> getDefaultTranscoder();

/**
* Get the default transcoder service.
*/
TranscoderService getDefaultTranscoderService(boolean daemon);

/**
* If true, low-level optimization is in effect.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.spy.memcached.ops.OperationQueueFactory;
import net.spy.memcached.protocol.ascii.AsciiOperationFactory;
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.transcoders.TranscoderService;
import net.spy.memcached.transcoders.Transcoder;

/**
Expand All @@ -48,6 +49,7 @@ public class ConnectionFactoryBuilder {
protected OperationQueueFactory writeQueueFactory;

protected Transcoder<Object> transcoder;
protected TranscoderService transcoderService;

protected FailureMode failureMode;

Expand Down Expand Up @@ -145,6 +147,14 @@ public ConnectionFactoryBuilder setTranscoder(Transcoder<Object> t) {
return this;
}

/**
* Set the default transcoder service.
*/
public ConnectionFactoryBuilder setTranscoderService(TranscoderService t) {
transcoderService = t;
return this;
}

/**
* Set the failure mode.
*/
Expand Down Expand Up @@ -367,6 +377,11 @@ public Transcoder<Object> getDefaultTranscoder() {
return transcoder == null ? super.getDefaultTranscoder() : transcoder;
}

@Override
public TranscoderService getDefaultTranscoderService(boolean daemon) {
return transcoderService == null ? super.getDefaultTranscoderService(daemon) : transcoderService;
}

@Override
public FailureMode getFailureMode() {
return failureMode == null ? super.getFailureMode() : failureMode;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/spy/memcached/DefaultConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -51,7 +49,9 @@
import net.spy.memcached.protocol.ascii.AsciiOperationFactory;
import net.spy.memcached.protocol.binary.BinaryMemcachedNodeImpl;
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.transcoders.TranscoderService;
import net.spy.memcached.transcoders.SerializingTranscoder;
import net.spy.memcached.transcoders.TranscodeService;
import net.spy.memcached.transcoders.Transcoder;

/**
Expand Down Expand Up @@ -379,6 +379,15 @@ public Transcoder<Object> getDefaultTranscoder() {
return new SerializingTranscoder();
}

/*
* (non-Javadoc)
*
* @see net.spy.memcached.ConnectionFactory#getDefaultTranscoderService()
*/
public TranscoderService getDefaultTranscoderService(boolean daemon) {
return new TranscodeService(daemon);
}

/*
* (non-Javadoc)
*
Expand Down
70 changes: 41 additions & 29 deletions src/main/java/net/spy/memcached/MemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@

package net.spy.memcached;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import net.spy.memcached.auth.AuthDescriptor;
import net.spy.memcached.auth.AuthThreadMonitor;
import net.spy.memcached.compat.SpyObject;
Expand Down Expand Up @@ -51,34 +77,9 @@
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.transcoders.TranscodeService;
import net.spy.memcached.transcoders.Transcoder;
import net.spy.memcached.transcoders.TranscoderService;
import net.spy.memcached.util.StringUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

/**
* Client to a memcached server.
*
Expand Down Expand Up @@ -149,7 +150,7 @@ public class MemcachedClient extends SpyObject implements MemcachedClientIF,

protected final Transcoder<Object> transcoder;

protected final TranscodeService tcService;
protected final TranscoderService tcService;

protected final AuthDescriptor authDescriptor;

Expand Down Expand Up @@ -202,7 +203,7 @@ public MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs)
throw new IllegalArgumentException("Operation timeout must be positive.");
}
connFactory = cf;
tcService = new TranscodeService(cf.isDaemon());
tcService = cf.getDefaultTranscoderService(cf.isDaemon());
transcoder = cf.getDefaultTranscoder();
opFact = cf.getOperationFactory();
assert opFact != null : "Connection factory failed to make op factory";
Expand Down Expand Up @@ -2635,8 +2636,19 @@ public MemcachedConnection getConnection() {
return mconn;
}

/**
* @deprecated use {@link #getTranscodeService()}
*/
public TranscodeService getTranscoderService() {
return tcService;
if (tcService instanceof TranscodeService) {
return (TranscodeService)tcService;
}
throw new RuntimeException("Configured TranscoderService is not an "
+ "instance of TranscodeService!");
}

public TranscoderService getTranscodeService() {
return tcService;
}

public ExecutorService getExecutorService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@
import java.util.concurrent.atomic.AtomicBoolean;

import net.spy.memcached.CachedData;
import net.spy.memcached.compat.SpyObject;
import net.spy.memcached.internal.BasicThreadFactory;

/**
* Asynchronous transcoder.
*/
public class TranscodeService extends SpyObject {
public class TranscodeService extends TranscoderService {

private final ThreadPoolExecutor pool;

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/spy/memcached/transcoders/TranscoderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.spy.memcached.transcoders;

import java.util.concurrent.Future;

import net.spy.memcached.CachedData;
import net.spy.memcached.compat.SpyObject;

public abstract class TranscoderService extends SpyObject {

public abstract <T> Future<T> decode(final Transcoder<T> tc, final CachedData cachedData);
public abstract void shutdown();
public abstract boolean isShutdown();

}