+ add RedisIterator + implementations for Set and List

This commit is contained in:
Costin Leau
2010-11-09 10:50:43 +02:00
parent 16ae948d92
commit a8f8a83192
3 changed files with 129 additions and 64 deletions

View File

@@ -30,6 +30,18 @@ import org.springframework.datastore.redis.connection.RedisCommands;
*/
public class DefaultRedisList extends AbstractRedisCollection implements RedisList {
private class DefaultRedisListIterator extends RedisIterator {
public DefaultRedisListIterator(Iterator<String> delegate) {
super(delegate);
}
@Override
protected void removeFromRedisStorage(String item) {
DefaultRedisList.this.remove(item);
}
}
public DefaultRedisList(String key, RedisCommands commands) {
super(key, commands);
}
@@ -71,12 +83,17 @@ public class DefaultRedisList extends AbstractRedisCollection implements RedisLi
commands.lTrim(key, 0, -1);
}
@Override
public boolean remove(Object o) {
Integer result = commands.lRem(key, 0, o.toString());
return (result != null && result.intValue() > 0);
}
@Override
public boolean removeAll(Collection<?> c) {
boolean modified = false;
for (Object object : c) {
Integer result = commands.lRem(key, 0, object.toString());
modified |= (result != null && result.intValue() > 0);
modified |= remove(object);
}
return modified;

View File

@@ -28,6 +28,18 @@ import org.springframework.datastore.redis.connection.RedisCommands;
*/
public class DefaultRedisSet extends AbstractRedisCollection implements RedisSet {
private class DefaultRedisSetIterator extends RedisIterator {
public DefaultRedisSetIterator(Iterator<String> delegate) {
super(delegate);
}
@Override
protected void removeFromRedisStorage(String item) {
DefaultRedisSet.this.remove(item);
}
}
public DefaultRedisSet(String key, RedisCommands commands) {
super(key, commands);
}
@@ -45,116 +57,84 @@ public class DefaultRedisSet extends AbstractRedisCollection implements RedisSet
@Override
public Set<String> intersect(RedisSet... sets) {
return null;
return commands.sInter(extractKeys(sets));
}
@Override
public RedisSet intersectAndStore(String destKey, RedisSet... sets) {
return null;
commands.sInterStore(destKey, extractKeys(sets));
return new DefaultRedisSet(destKey, commands);
}
@Override
public Set<String> union(RedisSet... sets) {
return null;
return commands.sUnion(extractKeys(sets));
}
@Override
public RedisSet unionAndStore(String destKey, RedisSet... sets) {
return null;
}
@Override
public String getKey() {
return null;
commands.sUnionStore(destKey, extractKeys(sets));
return new DefaultRedisSet(destKey, commands);
}
@Override
public boolean add(String e) {
return false;
return commands.sAdd(key, e);
}
@Override
public boolean addAll(Collection<? extends String> c) {
return false;
boolean modified = false;
for (String string : c) {
modified |= add(string);
}
return modified;
}
@Override
public void clear() {
// intersect the set with a non existing one
// TODO: find a safer way to clean the set
commands.sInterStore(key, key, "NON-EXISTING");
}
@Override
public boolean contains(Object o) {
return false;
return commands.sIsMember(key, o.toString());
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean isEmpty() {
return false;
boolean contains = true;
for (Object object : c) {
contains &= contains(object);
}
return contains;
}
@Override
public Iterator<String> iterator() {
return null;
return new DefaultRedisSetIterator(commands.sMembers(key).iterator());
}
@Override
public boolean remove(Object o) {
return false;
return commands.sRem(key, o.toString());
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
boolean modified = false;
for (Object object : c) {
modified |= remove(object);
}
return modified;
}
@Override
public int size() {
return 0;
}
@Override
public Object[] toArray() {
return null;
}
@Override
public <T> 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;
return commands.sCard(key);
}
private String[] extractKeys(RedisSet... sets) {

View File

@@ -0,0 +1,68 @@
/*
* 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;
/**
* Iterator extension for Redis collection removal.
*
* @author Costin Leau
*/
abstract class RedisIterator implements Iterator<String> {
private final Iterator<String> delegate;
private String item;
/**
* Constructs a new <code>RedisIterator</code> instance.
*
* @param delegate
*/
RedisIterator(Iterator<String> delegate) {
this.delegate = delegate;
}
/**
* @return
* @see java.util.Iterator#hasNext()
*/
public boolean hasNext() {
return delegate.hasNext();
}
/**
* @return
* @see java.util.Iterator#next()
*/
public String next() {
item = delegate.next();
return item;
}
/**
*
* @see java.util.Iterator#remove()
*/
public void remove() {
delegate.remove();
removeFromRedisStorage(item);
item = null;
}
protected abstract void removeFromRedisStorage(String item);
}