Skip to content

Commit 7cd30ec

Browse files
committed
Add ResultCursor.limit()
o This is the dual of skip - and was IMHO missing
1 parent 6cef322 commit 7cd30ec

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

driver/src/main/java/org/neo4j/driver/internal/InternalResultCursor.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class InternalResultCursor extends InternalRecordAccessor implements Resu
4545
private boolean open = true;
4646
private Record current = null;
4747
private long position = -1;
48+
private long limit = -1;
4849

4950
public InternalResultCursor( List<String> keys, List<Record> body, ResultSummary summary )
5051
{
@@ -137,6 +138,10 @@ public boolean next()
137138
{
138139
current = iter.next();
139140
position += 1;
141+
if ( position == limit )
142+
{
143+
discard();
144+
}
140145
return true;
141146
}
142147
else
@@ -163,6 +168,22 @@ public long skip( long elements )
163168
}
164169
}
165170

171+
@Override
172+
public long limit( long records )
173+
{
174+
if ( records < 0 )
175+
{
176+
throw new ClientException( "Cannot limit negative number of elements" );
177+
}
178+
else if ( records == 0) {
179+
this.limit = position;
180+
discard();
181+
} else {
182+
this.limit = records + position;
183+
}
184+
return this.limit;
185+
}
186+
166187
@Override
167188
public boolean first()
168189
{

driver/src/main/java/org/neo4j/driver/v1/ResultCursor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,23 @@ public interface ResultCursor extends RecordAccessor, Resource
7272
/**
7373
* Advance the cursor as if calling next multiple times.
7474
*
75-
* @throws IllegalArgumentException if records is negative
75+
* @throws ClientException if records is negative
7676
* @param records amount of records to be skipped
7777
* @return the actual number of records successfully skipped
7878
*/
7979
long skip( long records );
8080

81+
/**
82+
* Limit this cursor to return no more than the given number of records after the current record.
83+
* As soon as the described amount of records have been returned, all further records are discarded.
84+
* Calling limit again before the described amount of records have been returned, replaces the limit (overwriting the previous limit).
85+
*
86+
* @throws ClientException if records is negative
87+
* @param records the maximum number of records to return from future calls to {@link #next()}
88+
* @return the actual position of the last record to be returned
89+
*/
90+
long limit( long records );
91+
8192
/**
8293
* Move to the first record if possible, otherwise do nothing.
8394
*

driver/src/test/java/org/neo4j/driver/internal/InternalResultCursorTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,64 @@ public void skipThrowsIfNegativeNumber()
147147
result.skip( -1 );
148148
}
149149

150+
@Test
151+
public void limitShouldWorkAsExpected()
152+
{
153+
// GIVEN
154+
ResultCursor result = createResult( 42 );
155+
result.limit( 10 );
156+
157+
// THEN
158+
assertThat( result.retain().size(), equalTo( 10 ) );
159+
}
160+
161+
@Test
162+
public void limitZeroShouldWorkAsExpected1()
163+
{
164+
// GIVEN
165+
ResultCursor result = createResult( 42 );
166+
result.limit( 0 );
167+
168+
// THEN
169+
assertThat( result.retain().size(), equalTo( 0 ) );
170+
}
171+
172+
@Test
173+
public void limitZeroShouldWorkAsExpected2()
174+
{
175+
// GIVEN
176+
ResultCursor result = createResult( 10 );
177+
result.skip( 4 );
178+
result.limit( 0 );
179+
180+
// THEN
181+
assertTrue( result.atEnd() );
182+
assertFalse( result.next() );
183+
}
184+
185+
@Test
186+
public void limitOnEmptyResultShouldWorkAsExpected()
187+
{
188+
// GIVEN
189+
ResultCursor result = createResult( 0 );
190+
result.limit( 10 );
191+
192+
// THEN
193+
assertThat( result.retain().size(), equalTo( 0 ) );
194+
}
195+
196+
@Test
197+
public void changingLimitShouldWorkAsExpected()
198+
{
199+
// GIVEN
200+
ResultCursor result = createResult( 6 );
201+
result.limit( 1 );
202+
result.limit( 60 );
203+
204+
// THEN
205+
assertThat( result.retain().size(), equalTo( 6 ) );
206+
}
207+
150208
@Test
151209
public void retainShouldWorkAsExpected()
152210
{

0 commit comments

Comments
 (0)