+ add Jedis support for hash commands

This commit is contained in:
Costin Leau
2010-11-09 16:39:30 +02:00
parent 52061da2af
commit 2b903fe850
5 changed files with 233 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2010 the original author or authors.
*
* 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 org.springframework.datastore.redis.connection;
import org.springframework.datastore.redis.connection.RedisHashCommands.Entry;
/**
* Default {@link Entry} implementation.
*
* @author Costin Leau
*/
public class DefaultEntry implements Entry {
private final String field;
private final String value;
public DefaultEntry(String field, String value) {
this.field = field;
this.value = value;
}
@Override
public String getField() {
return null;
}
@Override
public String getValue() {
return null;
}
}

View File

@@ -24,7 +24,7 @@ import java.util.Collection;
* @author Costin Leau
*/
public interface RedisCommands extends RedisTxCommands, RedisStringCommands, RedisListCommands, RedisSetCommands,
RedisZSetCommands {
RedisZSetCommands, RedisHashCommands {
Boolean exists(String key);

View File

@@ -16,6 +16,9 @@
package org.springframework.datastore.redis.connection;
import java.util.List;
import java.util.Set;
/**
* Hash-specific commands supported by Redis.
*
@@ -23,5 +26,31 @@ package org.springframework.datastore.redis.connection;
*/
public interface RedisHashCommands {
public interface Entry {
public String getField();
public String getValue();
}
Integer hSet(String key, String field, String value);
String hGet(String key, String field);
List<String> hMGet(String key, String... fields);
void hMSet(String key, String[] fields, String[] values);
Integer hIncrBy(String key, String field, int delta);
Boolean hExists(String key, String field);
Boolean hDel(String key, String field);
Integer hLen(String key);
Set<String> hKeys(String key);
List<String> hVals(String key);
Set<Entry> hGetAll(String key);
}

View File

@@ -18,7 +18,9 @@ package org.springframework.datastore.redis.connection.jedis;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.dao.DataAccessException;
@@ -1052,4 +1054,138 @@ public class JedisConnection implements RedisConnection {
throw convertJedisAccessException(ex);
}
}
//
// Hash commands
//
@Override
public Boolean hDel(String key, String field) {
try {
if (isQueueing()) {
transaction.hdel(key, field);
return null;
}
return JedisUtils.convertCodeReply(jedis.hdel(key, field));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Boolean hExists(String key, String field) {
try {
if (isQueueing()) {
transaction.hexists(key, field);
return null;
}
return JedisUtils.convertCodeReply(jedis.hexists(key, field));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public String hGet(String key, String field) {
try {
if (isQueueing()) {
transaction.hget(key, field);
return null;
}
return jedis.hget(key, field);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Set<Entry> hGetAll(String key) {
try {
if (isQueueing()) {
transaction.hgetAll(key);
return null;
}
return JedisUtils.convert(jedis.hgetAll(key));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer hIncrBy(String key, String field, int delta) {
try {
if (isQueueing()) {
transaction.hincrBy(key, field, delta);
return null;
}
return jedis.hincrBy(key, field, delta);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Set<String> hKeys(String key) {
try {
if (isQueueing()) {
transaction.hkeys(key);
return null;
}
return new LinkedHashSet<String>(jedis.hkeys(key));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer hLen(String key) {
try {
if (isQueueing()) {
transaction.hlen(key);
return null;
}
return jedis.hlen(key);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public List<String> hMGet(String key, String... fields) {
try {
if (isQueueing()) {
transaction.hmget(key, fields);
return null;
}
return jedis.hmget(key, fields);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public void hMSet(String key, String[] fields, String[] values) {
Map<String, String> param = JedisUtils.convert(fields, values);
try {
if (isQueueing()) {
transaction.hmset(key, param);
}
jedis.hmset(key, param);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public List<String> hVals(String key) {
try {
if (isQueueing()) {
transaction.hvals(key);
return null;
}
return jedis.hvals(key);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
}

View File

@@ -18,7 +18,9 @@ package org.springframework.datastore.redis.connection.jedis;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
@@ -26,7 +28,9 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.datastore.redis.RedisConnectionFailureException;
import org.springframework.datastore.redis.UncategorizedRedisException;
import org.springframework.datastore.redis.connection.DefaultEntry;
import org.springframework.datastore.redis.connection.DefaultTuple;
import org.springframework.datastore.redis.connection.RedisHashCommands.Entry;
import org.springframework.datastore.redis.connection.RedisZSetCommands.Tuple;
import redis.clients.jedis.JedisException;
@@ -80,4 +84,22 @@ public abstract class JedisUtils {
return value;
}
static Set<Entry> convert(Map<String, String> hgetAll) {
Set<Entry> entries = new LinkedHashSet<Entry>(hgetAll.size());
for (Map.Entry<String, String> entry : hgetAll.entrySet()) {
entries.add(new DefaultEntry(entry.getKey(), entry.getValue()));
}
return entries;
}
static Map<String, String> convert(String[] fields, String[] values) {
Map<String, String> arg = new LinkedHashMap<String, String>(fields.length);
for (int i = 0; i < values.length; i++) {
arg.put(fields[i], values[i]);
}
return arg;
}
}