直接上代码,围绕着代码来讨论
java
redisTemplate.execute((RedisCallback<Object>) (connection) -> {
Cursor<byte[]> scan = connection
.scan(ScanOptions.scanOptions().count(2).match("*").build());
scan.forEachRemaining((bytes) -> {
System.out.println(new String(bytes));
});
//while (scan.hasNext()) {
// System.out.println(new String(scan.next()));
//}
return null;
});
通过上述代码我们可以看到,可以通过迭代器来获取到redis中单库中所有的key,但这样底层是怎么做的呢?我们现在假定有两种方案去实现这种方案。
① redis 客户端(jedis、lettuce)scan执行之后返回了所有的数据,迭代器next()每次都从数据集中取出一条消费。但这样有个疑点 => ScanOptions.scanOptions().count(2).match("*").build()
我们知道count(2)代表着每次只向redis中请求2条返回数据呀。如果全部返回也不符合scan命令的设计!
② 每次迭代next()都要判断是否数据集中还有数据,没有的话去redis中通过游标取下次一的数据集(2条)。然后将获取到数据集迭代器替换到游标中,上一个数据集回收(防止内存过大),使迭代器可以正常流转。
那上面两条都是我自己的猜测,可能两条都不对,也有可能两条对一条,具体我们还是要看源码怎么做的。
分析源码
Cursor<byte[]> scan = connection.scan(ScanOptions.scanOptions().count(2).match("*").build());
这段代码返回了一个Cursor 游标,我们看看他的具体实现是这个类:org.springframework.data.redis.core.ScanCursor
。
这个类实现了Cursor,实现了迭代器的所有方法。
当我们执行forEachRemaining
或者hasNext()
的时候都会去判断
java
@Override
public boolean hasNext() {
assertCursorIsOpen();
// delegate可以理解为数据集的迭代器,是一个list类型,从redis获取到的数据集会放到这个list中
while (!delegate.hasNext() && !CursorState.FINISHED.equals(state)) {
// 可以看到如果说数据集读取完了,会继续去往redis中取下一组
scan(cursorId);
}
if (delegate.hasNext()) {
return true;
}
return cursorId > 0;
}
scan 方法
java
private void scan(long cursorId) {
// 去请求redis获取下一组key
ScanIteration<T> result = doScan(cursorId, this.scanOptions);
// 将数据集
processScanResult(result);
}
processScanResult 方法
java
private void processScanResult(ScanIteration<T> result) {
// redis 中的游标
cursorId = result.getCursorId();
if (isFinished(cursorId)) {
state = CursorState.FINISHED;
}
if (!CollectionUtils.isEmpty(result.getItems())) {
// 替换迭代器 *** 重要代码
delegate = result.iterator();
} else {
resetDelegate();
}
}
可以看到forEachRemaining其实也要去先判断数据集中有值。
自此我们验证了上述的两条推断中第二条是争取的,每次迭代器流转的时候都会去判断是否还有数据,没有的话就会从redis取到新的数据集,然后把数据集的迭代器替换到游标中,从而实现了redis单库取出所有的key。spring-data-redis中实现的很优雅,很巧妙,我们也可以学习这种设计模式来批量获取远程分页数据等。最后我们来看下ScanCursor的整体代码:
java
/*
* Copyright 2014-2022 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
*
* https://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.redis.core;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Redis client agnostic {@link Cursor} implementation continuously loading additional results from Redis server until
* reaching its starting point {@code zero}. <br />
* <strong>Note:</strong> Please note that the {@link ScanCursor} has to be initialized ({@link #open()} prior to usage.
*
* @author Christoph Strobl
* @author Thomas Darimont
* @author Duobiao Ou
* @author Marl Paluch
* @param <T>
* @since 1.4
*/
public abstract class ScanCursor<T> implements Cursor<T> {
private CursorState state; // 游标状态
private long cursorId; // 游标当前位置
private Iterator<T> delegate; // 从redis获取到的结果集的迭代器,只要实现过Iterator的类都可以被代理
private final ScanOptions scanOptions; // 配置信息
private long position; // 位置
/**
* Crates new {@link ScanCursor} with {@code id=0} and {@link ScanOptions#NONE}
*/
public ScanCursor() {
this(ScanOptions.NONE);
}
/**
* Crates new {@link ScanCursor} with {@code id=0}.
*
* @param options the scan options to apply.
*/
public ScanCursor(ScanOptions options) {
this(0, options);
}
/**
* Crates new {@link ScanCursor} with {@link ScanOptions#NONE}
*
* @param cursorId the cursor Id.
*/
public ScanCursor(long cursorId) {
this(cursorId, ScanOptions.NONE);
}
/**
* Crates new {@link ScanCursor}
*
* @param cursorId the cursor Id.
* @param options Defaulted to {@link ScanOptions#NONE} if {@code null}.
*/
public ScanCursor(long cursorId, @Nullable ScanOptions options) {
this.scanOptions = options != null ? options : ScanOptions.NONE;
this.cursorId = cursorId;
this.state = CursorState.READY;
this.delegate = Collections.emptyIterator();
}
private void scan(long cursorId) {
ScanIteration<T> result = doScan(cursorId, this.scanOptions);
processScanResult(result);
}
/**
* Performs the actual scan command using the native client implementation. The given {@literal options} are never
* {@code null}.
*
* @param cursorId
* @param options
* @return
*/
protected abstract ScanIteration<T> doScan(long cursorId, ScanOptions options);
/**
* Initialize the {@link Cursor} prior to usage.
*/
public final ScanCursor<T> open() {
if (!isReady()) {
throw new InvalidDataAccessApiUsageException("Cursor already " + state + ". Cannot (re)open it.");
}
state = CursorState.OPEN;
doOpen(cursorId);
return this;
}
/**
* Customization hook when calling {@link #open()}.
*
* @param cursorId
*/
protected void doOpen(long cursorId) {
scan(cursorId);
}
private void processScanResult(ScanIteration<T> result) {
cursorId = result.getCursorId();
if (isFinished(cursorId)) {
state = CursorState.FINISHED;
}
if (!CollectionUtils.isEmpty(result.getItems())) {
delegate = result.iterator();
} else {
resetDelegate();
}
}
/**
* Check whether {@code cursorId} is finished.
*
* @param cursorId the cursor Id
* @return {@literal true} if the cursor is considered finished, {@literal false} otherwise.s
* @since 2.1
*/
protected boolean isFinished(long cursorId) {
return cursorId == 0;
}
private void resetDelegate() {
delegate = Collections.emptyIterator();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#getCursorId()
*/
@Override
public long getCursorId() {
return cursorId;
}
/*
* (non-Javadoc)
* @see java.util.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
assertCursorIsOpen();
while (!delegate.hasNext() && !CursorState.FINISHED.equals(state)) {
scan(cursorId);
}
if (delegate.hasNext()) {
return true;
}
return cursorId > 0;
}
private void assertCursorIsOpen() {
if (isReady() || isClosed()) {
throw new InvalidDataAccessApiUsageException("Cannot access closed cursor. Did you forget to call open()?");
}
}
/*
* (non-Javadoc)
* @see java.util.Iterator#next()
*/
@Override
public T next() {
assertCursorIsOpen();
if (!hasNext()) {
throw new NoSuchElementException("No more elements available for cursor " + cursorId + ".");
}
T next = moveNext(delegate);
position++;
return next;
}
/**
* Fetch the next item from the underlying {@link Iterable}.
*
* @param source
* @return
*/
protected T moveNext(Iterator<T> source) {
return source.next();
}
/*
* (non-Javadoc)
* @see java.util.Iterator#remove()
*/
@Override
public void remove() {
throw new UnsupportedOperationException("Remove is not supported");
}
/*
* (non-Javadoc)
* @see java.io.Closeable#close()
*/
@Override
public final void close() {
try {
doClose();
} finally {
state = CursorState.CLOSED;
}
}
/**
* Customization hook for cleaning up resources on when calling {@link #close()}.
*/
protected void doClose() {}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#isClosed()
*/
@Override
public boolean isClosed() {
return state == CursorState.CLOSED;
}
protected final boolean isReady() {
return state == CursorState.READY;
}
protected final boolean isOpen() {
return state == CursorState.OPEN;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#getPosition()
*/
@Override
public long getPosition() {
return position;
}
/**
* @author Thomas Darimont
*/
enum CursorState {
READY, OPEN, FINISHED, CLOSED;
}
}