From 2396c84471b325e728ccc442a8f226f44ba86669 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 9 Nov 2010 18:19:04 +0200 Subject: [PATCH] + add RedisMap and default impl --- .../datastore/redis/util/DefaultRedisMap.java | 172 ++++++++++++++++++ .../datastore/redis/util/RedisMap.java | 30 +++ 2 files changed, 202 insertions(+) create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisMap.java create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisMap.java diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisMap.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisMap.java new file mode 100644 index 000000000..da3e23ef3 --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisMap.java @@ -0,0 +1,172 @@ +/* + * 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.util; + +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +import org.springframework.datastore.redis.connection.RedisCommands; + +/** + * Default {@link RedisMap} implementation. + * + * @author Costin Leau + */ +public class DefaultRedisMap implements RedisMap { + + private class DefaultRedisMapEntry implements Map.Entry { + + private String key, value; + + /** + * Constructs a new DefaultRedisMapEntry instance. + * + * @param entry + */ + public DefaultRedisMapEntry(org.springframework.datastore.redis.connection.RedisHashCommands.Entry entry) { + this.key = entry.getField(); + this.value = entry.getValue(); + } + + @Override + public String getKey() { + return key; + } + + @Override + public String getValue() { + return value; + } + + @Override + public String setValue(String value) { + throw new UnsupportedOperationException(); + } + } + + protected final String redisKey; + protected final RedisCommands commands; + + /** + * Constructs a new DefaultRedisMap instance. + * + * @param key + * @param commands + */ + public DefaultRedisMap(String key, RedisCommands commands) { + this.redisKey = key; + this.commands = commands; + } + + @Override + public Integer increment(String key, int delta) { + return commands.hIncrBy(redisKey, key, delta); + } + + @Override + public boolean putIfAbsent(String key, String value) { + return commands.hSetNX(redisKey, key, value); + } + + @Override + public String getKey() { + return redisKey; + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean containsKey(Object key) { + return commands.hExists(redisKey, key.toString()); + } + + @Override + public boolean containsValue(Object value) { + throw new UnsupportedOperationException(); + } + + @Override + public Set> entrySet() { + return createEntrySet(commands.hGetAll(redisKey)); + } + + private Set> createEntrySet(Set entries) { + Set> result = new LinkedHashSet>( + entries.size()); + + for (org.springframework.datastore.redis.connection.RedisHashCommands.Entry entry : entries) { + result.add(new DefaultRedisMapEntry(entry)); + } + return result; + } + + @Override + public String get(Object key) { + return commands.hGet(redisKey, key.toString()); + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + @Override + public Set keySet() { + return commands.hKeys(redisKey); + + } + + @Override + public String put(String key, String value) { + String previous = commands.hGet(redisKey, key); + if (commands.hSet(redisKey, key, value)) { + return null; + } + return previous; + } + + @Override + public void putAll(Map m) { + String[] keys = m.keySet().toArray(new String[m.size()]); + String[] values = m.values().toArray(new String[m.size()]); + + commands.hMSet(redisKey, keys, values); + } + + @Override + public String remove(Object key) { + String previous = commands.hGet(redisKey, key.toString()); + if (commands.hDel(redisKey, key.toString())) { + return previous; + } + return null; + } + + @Override + public int size() { + return commands.hLen(redisKey); + } + + @Override + public Collection values() { + return commands.hVals(redisKey); + } +} \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisMap.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisMap.java new file mode 100644 index 000000000..8ad2956aa --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisMap.java @@ -0,0 +1,30 @@ +/* + * 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.util; + +import java.util.Map; + +/** + * Map view of a Redis hash. + * + * @author Costin Leau + */ +public interface RedisMap extends RedisStore, Map { + + boolean putIfAbsent(String key, String value); + + Integer increment(String key, int delta); +}