Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright 2011 Netflix
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.netflix.dyno.jedis;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.dyno.connectionpool.*;
import com.netflix.dyno.connectionpool.exception.DynoConnectException;
import com.netflix.dyno.connectionpool.exception.DynoException;
import com.netflix.dyno.connectionpool.exception.NoAvailableHostsException;
import com.netflix.dyno.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.dyno.connectionpool.impl.ConnectionPoolImpl;
import com.netflix.dyno.connectionpool.impl.lb.HttpEndpointBasedTokenMapSupplier;
import com.netflix.dyno.connectionpool.impl.utils.CollectionUtils;
import com.netflix.dyno.connectionpool.impl.utils.ZipUtils;
import com.netflix.dyno.contrib.*;
import org.slf4j.Logger;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import static com.netflix.dyno.connectionpool.ConnectionPoolConfiguration.CompressionStrategy;
public class DynoJedisClient implements JedisCommands, BinaryJedisCommands, MultiKeyCommands {
private static final Logger Logger = org.slf4j.LoggerFactory.getLogger(DynoJedisClient.class);
private final String appName;
private final ConnectionPool connPool;
private final AtomicReference pipelineMonitor = new AtomicReference();
private final EnumSet compressionOperations = EnumSet.of(OpName.APPEND);
public DynoJedisClient(String name, ConnectionPool pool, DynoOPMonitor operationMonitor) {
this.appName = name;
this.connPool = pool;
}
public ConnectionPoolImpl getConnPool() {
return (ConnectionPoolImpl) connPool;
}
private abstract class BaseKeyOperation implements Operation {
private final String key;
private final byte[] binaryKey;
private final OpName op;
private BaseKeyOperation(final String k, final OpName o) {
this.key = k;
this.binaryKey = null;
this.op = o;
}
private BaseKeyOperation(final byte[] k, final OpName o) {
this.key = null;
this.binaryKey = null;
this.op = o;
}
@Override
public String getName() {
return op.name();
}
@Override
public String getKey() {
return this.key;
}
public byte[] getBinaryKey() {
return this.binaryKey;
}
}
/**
* The following commands are supported
*
*
* String Operations
*
{@link #get(String) GET}
*
{@link #getSet(String, String) GETSET}
*
{@link #set(String, String) SET}
*
{@link #setex(String, int, String) SETEX}
*
*
* Hash Operations
*
{@link #hget(String, String) HGET}
*
{@link #hgetAll(String) HGETALL}
*
{@link #hmget(String, String...) HMGET}
*
{@link #hmset(String, Map) HMSET}
*
{@link #hscan(String, String) HSCAN}
*
{@link #hset(String, String, String) HSET}
*
{@link #hsetnx(String, String, String) HSETNX}
*
{@link #hvals(String) HVALS}
*
*
*
*
{@link #get(byte[]) GET}
*
{@link #set(byte[], byte[]) SET}
*
{@link #setex(byte[], int, byte[]) SETEX}
*
*
* @param the parameterized type
*/
private abstract class CompressionValueOperation extends BaseKeyOperation implements CompressionOperation {
private CompressionValueOperation(String k, OpName o) {
super(k, o);
}
/**
* Compresses the value based on the threshold defined by
* {@link ConnectionPoolConfiguration#getValueCompressionThreshold()}
*
* @param value
* @return
*/
@Override
public String compressValue(String value, ConnectionContext ctx) {
String result = value;
int thresholdBytes = connPool.getConfiguration().getValueCompressionThreshold();
try {
// prefer speed over accuracy here so rather than using getBytes() to get the actual size
// just estimate using 2 bytes per character
if ((2 * value.length()) > thresholdBytes) {
result = ZipUtils.compressStringToBase64String(value);
ctx.setMetadata("compression", true);
}
} catch (IOException e) {
Logger.warn("UNABLE to compress [" + value + "] for key [" + getKey() + "]; sending value uncompressed");
}
return result;
}
@Override
public String decompressValue(String value, ConnectionContext ctx) {
try {
if (ZipUtils.isCompressed(value)) {
ctx.setMetadata("decompression", true);
return ZipUtils.decompressFromBase64String(value);
}
} catch (IOException e) {
Logger.warn("Unable to decompress value [" + value + "]");
}
return value;
}
}
public TopologyView getTopologyView() {
return (TopologyView) this.getConnPool();
}
@Override
public Long append(final String key, final String value) {
return d_append(key, value).getResult();
}
public OperationResult d_append(final String key, final String value) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.APPEND) {
@Override
public Long execute(Jedis client, ConnectionContext state) {
return client.append(key, value);
}
});
}
@Override
public Long decr(final String key) {
return d_decr(key).getResult();
}
public OperationResult d_decr(final String key) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.DECR) {
@Override
public Long execute(Jedis client, ConnectionContext state) {
return client.decr(key);
}
});
}
@Override
public Long decrBy(final String key, final long delta) {
return d_decrBy(key, delta).getResult();
}
public OperationResult d_decrBy(final String key, final Long delta) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.DECRBY) {
@Override
public Long execute(Jedis client, ConnectionContext state) {
return client.decrBy(key, delta);
}
});
}
@Override
public Long del(final String key) {
return d_del(key).getResult();
}
public OperationResult d_del(final String key) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.DEL) {
@Override
public Long execute(Jedis client, ConnectionContext state) {
return client.del(key);
}
});
}
public byte[] dump(final String key) {
return d_dump(key).getResult();
}
public OperationResult d_dump(final String key) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.DUMP) {
@Override
public byte[] execute(Jedis client, ConnectionContext state) {
return client.dump(key);
}
});
}
@Override
public Boolean exists(final String key) {
return d_exists(key).getResult();
}
public OperationResult d_exists(final String key) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.EXISTS) {
@Override
public Boolean execute(Jedis client, ConnectionContext state) {
return client.exists(key);
}
});
}
@Override
public Long expire(final String key, final int seconds) {
return d_expire(key, seconds).getResult();
}
@Override
public Long pexpire(String key, long milliseconds) {
throw new UnsupportedOperationException("not yet implemented");
}
public OperationResult d_expire(final String key, final Integer seconds) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.EXPIRE) {
@Override
public Long execute(Jedis client, ConnectionContext state) {
return client.expire(key, seconds);
}
});
}
@Override
public Long expireAt(final String key, final long unixTime) {
return d_expireAt(key, unixTime).getResult();
}
public OperationResult d_expireAt(final String key, final Long unixTime) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.EXPIREAT) {
@Override
public Long execute(Jedis client, ConnectionContext state) {
return client.expireAt(key, unixTime);
}
});
}
@Override
public String get(final String key) {
return d_get(key).getResult();
}
public OperationResult d_get(final String key) {
if (CompressionStrategy.NONE == connPool.getConfiguration().getCompressionStrategy()) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.GET) {
@Override
public String execute(Jedis client, ConnectionContext state) throws DynoException {
return client.get(key);
}
});
} else {
return connPool.executeWithFailover(new CompressionValueOperation(key, OpName.GET) {
@Override
public String execute(final Jedis client, final ConnectionContext state) throws DynoException {
return decompressValue(client.get(key), state);
}
});
}
}
@Override
public Boolean getbit(final String key, final long offset) {
return d_getbit(key, offset).getResult();
}
public OperationResult d_getbit(final String key, final Long offset) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.GETBIT) {
@Override
public Boolean execute(Jedis client, ConnectionContext state) {
return client.getbit(key, offset);
}
});
}
@Override
public String getrange(final String key, final long startOffset, final long endOffset) {
return d_getrange(key, startOffset, endOffset).getResult();
}
public OperationResult d_getrange(final String key, final Long startOffset, final Long endOffset) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.GETRANGE) {
@Override
public String execute(Jedis client, ConnectionContext state) {
return client.getrange(key, startOffset, endOffset);
}
});
}
@Override
public String getSet(final String key, final String value) {
return d_getSet(key, value).getResult();
}
public OperationResult d_getSet(final String key, final String value) {
if (CompressionStrategy.NONE == connPool.getConfiguration().getCompressionStrategy()) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.GETSET) {
@Override
public String execute(Jedis client, ConnectionContext state) throws DynoException {
return client.getSet(key, value);
}
});
} else {
return connPool.executeWithFailover(new CompressionValueOperation(key, OpName.GETSET) {
@Override
public String execute(Jedis client, ConnectionContext state) throws DynoException {
return decompressValue(client.getSet(key, compressValue(value, state)), state);
}
});
}
}
@Override
public Long hdel(final String key, final String... fields) {
return d_hdel(key, fields).getResult();
}
public OperationResult d_hdel(final String key, final String... fields) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.HDEL) {
@Override
public Long execute(Jedis client, ConnectionContext state) {
return client.hdel(key, fields);
}
});
}
@Override
public Boolean hexists(final String key, final String field) {
return d_hexists(key, field).getResult();
}
public OperationResult d_hexists(final String key, final String field) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.HEXISTS) {
@Override
public Boolean execute(Jedis client, ConnectionContext state) {
return client.hexists(key, field);
}
});
}
@Override
public String hget(final String key, final String field) {
return d_hget(key, field).getResult();
}
public OperationResult d_hget(final String key, final String field) {
if (CompressionStrategy.NONE == connPool.getConfiguration().getCompressionStrategy()) {
return connPool.executeWithFailover(new BaseKeyOperation(key, OpName.HGET) {
@Override
public String execute(Jedis client, ConnectionContext state) throws DynoException {
return client.hget(key, field);
}
});
} else {
return connPool.executeWithFailover(new CompressionValueOperation(key, OpName.HGET) {
@Override
public String execute(final Jedis client, final ConnectionContext state) throws DynoException {
return decompressValue(client.hget(key, field), state);
}
});
}
}
@Override
public Map hgetAll(final String key) {
return d_hgetAll(key).getResult();
}
public OperationResult