Remove limit(long) from Cursor and hint users to stream().limit(...)

Original Pull Request: #2014
This commit is contained in:
Christoph Strobl
2021-03-24 14:35:59 +01:00
parent b0dd3cda6c
commit 3a1fbe1cf7
5 changed files with 17 additions and 156 deletions

View File

@@ -119,13 +119,4 @@ public class ConvertingCursor<S, T> implements Cursor<T> {
public long getPosition() {
return delegate.getPosition();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#limit(long)
*/
@Override
public Cursor<T> limit(long count) {
return new ConvertingCursor<>(delegate.limit(count), converter);
}
}

View File

@@ -15,19 +15,25 @@
*/
package org.springframework.data.redis.core;
import org.springframework.data.redis.util.BoundedIterator;
import org.springframework.data.util.CloseableIterator;
/**
* Cursor abstraction to scan over the keyspace or elements within a data structure using a variant of a {@code SCAN}
* command.
* <p />
* Using a Java 8 {@link #stream() java.util.stream.Stream} allows to apply additional
* {@link java.util.stream.Stream#filter(java.util.function.Predicate) filters} and {@link java.util.stream.Stream#limit(long) limits} to
* the underlying {@link Cursor}.
* <p />
* Make sure to {@link CloseableIterator#close() close} the cursor when done as this allows implementations to clean up
* any resources they need to keep open to iterate over elements (eg. by using a try-with-resource statement).
*
* @author Christoph Strobl
* @author Mark Paluch
* @param <T>
* @since 1.4
*/
public interface Cursor<T> extends BoundedIterator<T>, CloseableIterator<T> {
public interface Cursor<T> extends CloseableIterator<T> {
/**
* Get the reference cursor. <br>
@@ -56,15 +62,4 @@ public interface Cursor<T> extends BoundedIterator<T>, CloseableIterator<T> {
* @return the current position of the cursor.
*/
long getPosition();
/**
* Limit the maximum number of elements to be returned from this cursor. The returned cursor object can be used to
* iterate over the remaining items and to {@link #close() release} associated resources. The returned cursor is not
* attached to the state of {@code this} cursor and this object should be no longer used.
*
* @return a new {@link Cursor} with detached iteration state.
* @since 2.5
*/
@Override
Cursor<T> limit(long count);
}

View File

@@ -43,7 +43,6 @@ public abstract class ScanCursor<T> implements Cursor<T> {
private Iterator<T> delegate;
private final ScanOptions scanOptions;
private long position;
private final long limit;
/**
* Crates new {@link ScanCursor} with {@code id=0} and {@link ScanOptions#NONE}
@@ -82,23 +81,6 @@ public abstract class ScanCursor<T> implements Cursor<T> {
this.cursorId = cursorId;
this.state = CursorState.READY;
this.delegate = Collections.emptyIterator();
this.limit = -1;
}
/**
* Crates a new {@link ScanCursor}.
*
* @param source source cursor.
* @param limit
* @since 2.5
*/
private ScanCursor(ScanCursor<T> source, long limit) {
this.scanOptions = source.scanOptions;
this.cursorId = source.cursorId;
this.state = source.state;
this.delegate = source.delegate;
this.limit = limit;
}
private void scan(long cursorId) {
@@ -189,10 +171,6 @@ public abstract class ScanCursor<T> implements Cursor<T> {
assertCursorIsOpen();
if (limit != -1 && getPosition() > limit - 1) {
return false;
}
while (!delegate.hasNext() && !CursorState.FINISHED.equals(state)) {
scan(cursorId);
}
@@ -294,54 +272,10 @@ public abstract class ScanCursor<T> implements Cursor<T> {
return position;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#limit(long)
*/
@Override
public ScanCursor<T> limit(long count) {
Assert.isTrue(count >= 0, "Count must be greater or equal to zero");
return new ScanCursorWrapper<>(this, count);
}
/**
* @author Thomas Darimont
*/
enum CursorState {
READY, OPEN, FINISHED, CLOSED;
}
/**
* Wrapper for a concrete {@link ScanCursor} forwarding {@link #doScan(long, ScanOptions)}, {@link #doClose()} and
* {@link #isClosed()}.
*
* @param <T>
* @since 2.5
*/
private static class ScanCursorWrapper<T> extends ScanCursor<T> {
private final ScanCursor<T> delegate;
public ScanCursorWrapper(ScanCursor<T> delegate, long limit) {
super(delegate, limit);
this.delegate = delegate;
}
@Override
protected ScanIteration<T> doScan(long cursorId, ScanOptions options) {
return delegate.doScan(cursorId, options);
}
@Override
protected void doClose() {
delegate.close();
}
@Override
public boolean isClosed() {
return delegate.isClosed();
}
}
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright 2021 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.util;
import java.util.Iterator;
/**
* Extension to {@link Iterator} that can be {@link #limit(long) limited} to a maximum number of items.
*
* @author Mark Paluch
* @since 2.5
*/
public interface BoundedIterator<T> extends Iterator<T> {
/**
* Limit the maximum number of elements to return. The limit is only applied to the returned instance and not applied
* to {@code this} iterator.
*
* @param count the maximum number of elements of iterator to return. Must be greater or equal to zero.
* @return a new instance of {@link BoundedIterator} with {@code count} applied.
*/
BoundedIterator<T> limit(long count);
}