From 962fdcfa08fecec94e5d85e8040466636d1797b8 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 9 Nov 2010 10:09:17 +0200 Subject: [PATCH] + overhauled RedisSet --- .../datastore/redis/util/DefaultRedisSet.java | 168 ++++++++++++++++++ .../datastore/redis/util/RedisSet.java | 120 +++---------- .../datastore/redis/util/Sets.java | 20 --- 3 files changed, 194 insertions(+), 114 deletions(-) create mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisSet.java delete mode 100644 spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/Sets.java diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisSet.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisSet.java new file mode 100644 index 000000000..8d15ed6f4 --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisSet.java @@ -0,0 +1,168 @@ +/* + * 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.Iterator; +import java.util.Set; + +import org.springframework.datastore.redis.connection.RedisCommands; + +/** + * Default implementation for {@link RedisSet}. + * + * @author Costin Leau + */ +public class DefaultRedisSet extends AbstractRedisCollection implements RedisSet { + + public DefaultRedisSet(String key, RedisCommands commands) { + super(key, commands); + } + + @Override + public Set diff(RedisSet... sets) { + return commands.sDiff(extractKeys(sets)); + } + + @Override + public RedisSet diffAndStore(String destKey, RedisSet... sets) { + commands.sDiffStore(destKey, extractKeys(sets)); + return new DefaultRedisSet(destKey, commands); + } + + @Override + public Set intersect(RedisSet... sets) { + return null; + } + + @Override + public RedisSet intersectAndStore(String destKey, RedisSet... sets) { + return null; + } + + @Override + public Set union(RedisSet... sets) { + return null; + } + + @Override + public RedisSet unionAndStore(String destKey, RedisSet... sets) { + return null; + } + + @Override + public String getKey() { + return null; + } + + @Override + public boolean add(String e) { + return false; + } + + @Override + public boolean addAll(Collection c) { + return false; + } + + @Override + public void clear() { + } + + @Override + public boolean contains(Object o) { + return false; + } + + @Override + public boolean containsAll(Collection c) { + return false; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public boolean remove(Object o) { + return false; + } + + @Override + public boolean removeAll(Collection c) { + return false; + } + + @Override + public boolean retainAll(Collection c) { + return false; + } + + @Override + public int size() { + return 0; + } + + @Override + public Object[] toArray() { + return null; + } + + @Override + public T[] toArray(T[] a) { + return null; + } + + @Override + public String element() { + return null; + } + + @Override + public boolean offer(String e) { + return false; + } + + @Override + public String peek() { + return null; + } + + @Override + public String poll() { + return null; + } + + @Override + public String remove() { + return null; + } + + private String[] extractKeys(RedisSet... sets) { + String[] keys = new String[sets.length]; + for (int i = 0; i < keys.length; i++) { + keys[i] = sets[i].getKey(); + } + + return keys; + } +} \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisSet.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisSet.java index d84aa079a..7696697ac 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisSet.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisSet.java @@ -1,108 +1,40 @@ +/* + * 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.Iterator; +import java.util.Queue; import java.util.Set; -import org.springframework.datastore.redis.core.RedisTemplate; - /** + * Redis extension for the {@link Set} contract. Supports {@link Set} specific + * operations backed by Redis commands. * - * @author Graeme Rocher - * + * @author Costin Leau */ -public class RedisSet extends AbstractRedisCollection implements Set { +public interface RedisSet extends RedisCollection, Set, Queue { - public RedisSet(RedisTemplate redisTemplate, String redisKey) { - super(redisTemplate, redisKey); - } + Set intersect(RedisSet... sets); - public int size() { - // return redisTemplate.getSetOperations().size(redisKey); - throw new UnsupportedOperationException(); - } + Set union(RedisSet... sets); - public boolean contains(Object o) { - //TODO investigate cast - // return redisTemplate.getSetOperations().contains(redisKey, (String)o); - throw new UnsupportedOperationException(); - } + Set diff(RedisSet... sets); - public Iterator iterator() { - // return redisTemplate.getSetOperations().getAll(redisKey).iterator(); - throw new UnsupportedOperationException(); - } + RedisSet intersectAndStore(String destKey, RedisSet... sets); - public boolean add(Object o) { - //TODO investigate cast - // return redisTemplate.getSetOperations().add(redisKey, (String)o); - throw new UnsupportedOperationException(); - } + RedisSet unionAndStore(String destKey, RedisSet... sets); - public boolean remove(Object o) { - //TODO investigate cast - // return redisTemplate.getSetOperations().remove(redisKey, (String)o); - throw new UnsupportedOperationException(); - } - - - public Set members() { - // return redisTemplate.getSetOperations().getAll(redisKey); - throw new UnsupportedOperationException(); - } - - /* - public List members(final int offset, final int max) { - return redisTemplate.sort(redisKey, redisTemplate.sortParams().limit(offset, max)); - - }*/ - - public String getRandom() { - // return redisTemplate.getSetOperations().getRandom(redisKey); - throw new UnsupportedOperationException(); - } - - public boolean removeRandom() { - // return redisTemplate.getSetOperations().removeRandom(redisKey); - throw new UnsupportedOperationException(); - } - - /* - public intersection(RedisSet... redisSets) { - //storeIntersectionOfSets.. - return null; - } - */ - - public RedisSet intersection(String newKey, RedisSet... redisSets) { - throw new UnsupportedOperationException(); - /* - String[] keys = new String[redisSets.length]; - int i = 0; - for (RedisSet redisSet : redisSets) { - keys[i] = redisSet.getRedisKey(); - i++; - } - redisTemplate.getSetOperations().storeIntersectionOfSets(newKey, keys); - - RedisSet resultSet = new RedisSet(redisTemplate, newKey); - Set results = redisTemplate.getSetOperations().getAll(newKey); - resultSet.addAll(results); - return resultSet; - */ - } - - - void union(RedisSet... redisSets) { - //storeUnionOfSets - } - - void difference(RedisSet... redisSets) { - - } - - //consider methods in google collections such as - // cartesianProduct, filter, powerSet, symmetricDifference, newRedisSet - //TODO move to another set - - // + RedisSet diffAndStore(String destKey, RedisSet... sets); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/Sets.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/Sets.java deleted file mode 100644 index 44f209b29..000000000 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/Sets.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.springframework.datastore.redis.util; - -import java.util.Set; - -import org.springframework.datastore.redis.core.RedisTemplate; - -public class Sets { - - protected RedisTemplate redisTemplate; - - public Sets(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - - //TODO what key to assing? - - - - -}