DATAKV-36

+ changed BulkMapper from using low-level byte array to objects
This commit is contained in:
Costin Leau
2011-03-08 19:51:31 +02:00
parent 7b7777fba4
commit 32998b9d82
5 changed files with 26 additions and 78 deletions

View File

@@ -1,59 +0,0 @@
/*
* Copyright 2011 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.data.keyvalue.redis.core;
import java.util.Iterator;
import java.util.List;
/**
* Wrapper class allowing for stream-like access across a list of values.
*
* @author Costin Leau
*/
class BulkIterable<T> implements Iterable<T> {
private final List<T> list;
private volatile int index = 0;
public BulkIterable(List<T> list) {
this.list = list;
}
public boolean hasMore() {
throw new UnsupportedOperationException();
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
@Override
public boolean hasNext() {
return index < list.size();
}
@Override
public T next() {
return list.get(index++);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}

View File

@@ -21,11 +21,11 @@ import java.util.Iterator;
* Mapper translating Redis bulk value responses (typically returned by a sort query) to actual objects. Implementations of this interface do not have to worry
* about exception or connection handling.
* <p/>
* Typically used by {@link RedisTemplate} <tt>sortAndGet</tt> methods.
* Typically used by {@link RedisTemplate} <tt>sort</tt> methods.
*
* @author Costin Leau
*/
public interface BulkMapper<T> {
public interface BulkMapper<T, V> {
T mapBulk(Iterator<byte[]> valueStream);
T mapBulk(Iterator<V> valueStream);
}

View File

@@ -196,8 +196,9 @@ public interface RedisOperations<K, V> {
<T> List<T> sort(SortQuery<K> query, RedisSerializer<T> resultSerializer);
<T> List<T> sort(SortQuery<K> query, BulkMapper<T> bulkMapper);
<T> List<T> sort(SortQuery<K> query, BulkMapper<T, V> bulkMapper);
<T, S> List<T> sort(SortQuery<K> query, BulkMapper<T, S> bulkMapper, RedisSerializer<S> resultSerializer);
Long sort(SortQuery<K> query, K storeKey);
}

View File

@@ -1950,28 +1950,26 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return (List<T>) deserializeValues(vals, List.class, resultSerializer);
}
@SuppressWarnings("unchecked")
@Override
public <T> List<T> sort(SortQuery<K> query, BulkMapper<T> bulkMapper) {
final byte[] rawKey = rawKey(query.getKey());
final SortParameters params = convertQuery(query, stringSerializer);
public <T> List<T> sort(SortQuery<K> query, BulkMapper<T, V> bulkMapper) {
return sort(query, bulkMapper, valueSerializer);
}
List<byte[]> vals = execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
return connection.sort(rawKey, params);
}
}, true);
@Override
public <T, S> List<T> sort(SortQuery<K> query, BulkMapper<T, S> bulkMapper, RedisSerializer<S> resultSerializer) {
List<S> values = sort(query, resultSerializer);
int bulkSize = query.getGetPattern().size();
List<T> result = new ArrayList<T>(vals.size() / bulkSize + 1);
List<T> result = new ArrayList<T>(values.size() / bulkSize + 1);
final List<byte[]> bulk = new ArrayList<byte[]>(bulkSize);
final List<byte[]> listView = Collections.unmodifiableList(bulk);
final List<S> bulk = new ArrayList<S>(bulkSize);
final List<S> listView = Collections.unmodifiableList(bulk);
for (byte[] bs : vals) {
bulk.add(bs);
for (S s : values) {
bulk.add(s);
if (bulk.size() == bulkSize) {
bulkMapper.mapBulk(listView.iterator());
result.add(bulkMapper.mapBulk(listView.iterator()));
bulk.clear();
}
}

View File

@@ -72,4 +72,12 @@ class DefaultSortQuery<K> implements SortQuery<K> {
public List<String> getGetPattern() {
return gets;
}
@Override
public String toString() {
return "DefaultSortQuery [alpha=" + alpha + ", by=" + by + ", gets=" + gets + ", key=" + key + ", limit="
+ limit + ", order=" + order + "]";
}
}