From 2b903fe8505766c8a69f116a4220ee31fffeb1bc Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 9 Nov 2010 16:39:30 +0200 Subject: [PATCH] + add Jedis support for hash commands --- .../redis/connection/DefaultEntry.java | 45 ++++++ .../redis/connection/RedisCommands.java | 2 +- .../redis/connection/RedisHashCommands.java | 29 ++++ .../connection/jedis/JedisConnection.java | 136 ++++++++++++++++++ .../redis/connection/jedis/JedisUtils.java | 22 +++ 5 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java new file mode 100644 index 000000000..db174291e --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java @@ -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; + } + +} diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java index a07f8faf0..ed2018aca 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java @@ -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); diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java index 059d7ed84..61abb2809 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java @@ -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 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 hKeys(String key); + + List hVals(String key); + + Set hGetAll(String key); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java index 72a2d9827..00ff0ea55 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java @@ -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 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 hKeys(String key) { + try { + if (isQueueing()) { + transaction.hkeys(key); + return null; + } + return new LinkedHashSet(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 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 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 hVals(String key) { + try { + if (isQueueing()) { + transaction.hvals(key); + return null; + } + return jedis.hvals(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } } \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java index 670a8d4a7..ab5dfdf2f 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java @@ -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 convert(Map hgetAll) { + Set entries = new LinkedHashSet(hgetAll.size()); + for (Map.Entry entry : hgetAll.entrySet()) { + entries.add(new DefaultEntry(entry.getKey(), entry.getValue())); + } + + return entries; + } + + static Map convert(String[] fields, String[] values) { + Map arg = new LinkedHashMap(fields.length); + + for (int i = 0; i < values.length; i++) { + arg.put(fields[i], values[i]); + } + return arg; + } } \ No newline at end of file