+ rewrite the collection support for Redis

+ add initial draft for List (still need to work the implementation details in terms of efficiency)
This commit is contained in:
Costin Leau
2010-11-08 20:04:32 +02:00
parent bfe5ab672e
commit d86ff3a851
4 changed files with 242 additions and 83 deletions

View File

@@ -1,83 +1,52 @@
/*
* 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.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import org.springframework.datastore.redis.core.RedisTemplate;
import org.springframework.datastore.redis.connection.RedisCommands;
public abstract class AbstractRedisCollection implements RedisCollection {
/**
* Base implementation for Redis collections.
*
* @author Costin Leau
*/
public abstract class AbstractRedisCollection extends AbstractCollection<String> implements RedisCollection {
protected RedisTemplate redisTemplate;
protected String redisKey;
protected final String key;
protected final RedisCommands commands;
public AbstractRedisCollection(RedisTemplate redisTemplate, String redisKey) {
this.redisTemplate = redisTemplate;
this.redisKey = redisKey;
}
public AbstractRedisCollection(String key, RedisCommands commands) {
this.key = key;
this.commands = commands;
}
@Override
public String getKey() {
return key;
}
/**
* They key used by the collection
*
* @return The redis key
*/
public String getRedisKey() {
return redisKey;
}
public abstract boolean add(String e);
public void clear() {
// redisTemplate.deleteKeys(redisKey);
}
public abstract void clear();
public boolean isEmpty() {
return size() == 0;
}
public abstract boolean removeAll(Collection<?> c);
public Object[] toArray() {
return new Object[0];
}
public boolean containsAll(Collection c) {
for (Object o : c) {
if(!contains(o)) return false;
}
return true;
}
public boolean addAll(Collection c) {
boolean changed = false;
for (Object e : c) {
boolean elChange = add(e);
if(elChange && !changed) changed = true;
}
return changed;
}
public boolean retainAll(Collection c) {
Iterator i = iterator();
boolean changed = false;
while (i.hasNext()) {
Object o = i.next();
if(!c.contains(o)) {
i.remove();
changed = true;
}
}
return changed;
}
public boolean removeAll(Collection c) {
boolean changed = false;
for (Object e : c) {
boolean elChange = remove(e);
if(elChange && !changed) changed = true;
}
return changed;
}
public Object[] toArray(Object[] array) {
return new Object[0];
}
}
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,147 @@
/*
* 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.List;
import java.util.ListIterator;
import org.springframework.datastore.redis.connection.RedisCommands;
/**
* Default implementation for {@link RedisList}.
*
* @author Costin Leau
*/
public class DefaultRedisList extends AbstractRedisCollection implements RedisList {
public DefaultRedisList(String key, RedisCommands commands) {
super(key, commands);
}
@Override
public List<String> range(int start, int end) {
return commands.lRange(key, start, end);
}
@Override
public RedisList trim(int start, int end) {
commands.lTrim(key, start, end);
return this;
}
private List<String> content() {
return commands.lRange(key, 0, -1);
}
@Override
public Iterator<String> iterator() {
return content().iterator();
}
@Override
public int size() {
return commands.lLen(key);
}
@Override
public boolean add(String value) {
commands.rPush(key, value);
return true;
}
@Override
public void clear() {
commands.lTrim(key, 0, -1);
}
@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);
}
return modified;
}
@Override
public void add(int index, String element) {
if (index == 0) {
commands.lPush(key, element);
}
else if (index == size()) {
commands.rPush(key, element);
}
throw new IllegalArgumentException("Redis supports insertion only at the beginning or the end of the list");
}
@Override
public boolean addAll(int index, Collection<? extends String> c) {
for (String string : c) {
add(index, string);
}
return true;
}
@Override
public String get(int index) {
return commands.lIndex(key, index);
}
@Override
public int indexOf(Object o) {
throw new UnsupportedOperationException();
}
@Override
public int lastIndexOf(Object o) {
throw new UnsupportedOperationException();
}
@Override
public ListIterator<String> listIterator() {
throw new UnsupportedOperationException();
}
@Override
public ListIterator<String> listIterator(int index) {
throw new UnsupportedOperationException();
}
@Override
public String remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public String set(int index, String element) {
String object = get(index);
commands.lSet(key, index, element);
return object;
}
@Override
public List<String> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
}
}

View File

@@ -1,21 +1,33 @@
/*
* Copyright 2006-2009 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.Set;
/**
* Basic interface for Redis collections.
*
* @author Graeme Rocher
*
* @author Costin Leau
*/
public interface RedisCollection extends Collection {
public interface RedisCollection extends Collection<String> {
/**
* They key used by the collection
*
* @return The redis key
*/
String getRedisKey();
Set<String> members();
/**
* Returns the key used by the backing Redis store for this collection.
*
* @return Redis key
*/
String getKey();
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2006-2009 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.List;
/**
* Redis extension for {@link List} contract. Supports List specific
* operations backed by Redis commands.
*
* @author Costin Leau
*/
public interface RedisList extends RedisCollection, List<String> {
List<String> range(int start, int end);
RedisList trim(int start, int end);
}