|
11 | 11 |
|
12 | 12 | import org.apache.lucene.store.ByteBuffersDirectory; |
13 | 13 | import org.apache.lucene.store.Directory; |
| 14 | +import org.apache.lucene.store.FilterIndexInput; |
14 | 15 | import org.apache.lucene.store.IOContext; |
15 | 16 | import org.apache.lucene.store.IndexInput; |
16 | 17 | import org.apache.lucene.store.IndexOutput; |
@@ -267,17 +268,47 @@ public void testSkipBytes() throws Exception { |
267 | 268 | skipBytesExpected |
268 | 269 | ); |
269 | 270 |
|
270 | | - IndexInput input = dir.openInput("test", IOContext.DEFAULT); |
271 | | - InputStreamIndexInput is = new InputStreamIndexInput(input, limit); |
| 271 | + var countingInput = new CountingReadBytesIndexInput("test", dir.openInput("test", IOContext.DEFAULT)); |
| 272 | + InputStreamIndexInput is = new InputStreamIndexInput(countingInput, limit); |
272 | 273 | is.readNBytes(initialReadBytes); |
273 | 274 | assertThat(is.skip(skipBytes), equalTo((long) skipBytesExpected)); |
| 275 | + long expectedActualInitialBytesRead = Math.min(Math.min(initialReadBytes, limit), bytes); |
| 276 | + assertThat(countingInput.getBytesRead(), equalTo(expectedActualInitialBytesRead)); |
274 | 277 |
|
275 | 278 | int remainingBytes = Math.min(bytes, limit) - seekExpected; |
276 | 279 | for (int i = seekExpected; i < seekExpected + remainingBytes; i++) { |
277 | 280 | assertThat(is.read(), equalTo(i)); |
278 | 281 | } |
| 282 | + assertThat(countingInput.getBytesRead(), equalTo(expectedActualInitialBytesRead + remainingBytes)); |
279 | 283 | } |
280 | 284 |
|
| 285 | + protected static class CountingReadBytesIndexInput extends FilterIndexInput { |
| 286 | + private long bytesRead = 0; |
| 287 | + |
| 288 | + public CountingReadBytesIndexInput(String resourceDescription, IndexInput in) { |
| 289 | + super(resourceDescription, in); |
| 290 | + } |
| 291 | + |
| 292 | + @Override |
| 293 | + public byte readByte() throws IOException { |
| 294 | + long filePointerBefore = getFilePointer(); |
| 295 | + byte b = super.readByte(); |
| 296 | + bytesRead += getFilePointer() - filePointerBefore; |
| 297 | + return b; |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + public void readBytes(byte[] b, int offset, int len) throws IOException { |
| 302 | + long filePointerBefore = getFilePointer(); |
| 303 | + super.readBytes(b, offset, len); |
| 304 | + bytesRead += getFilePointer() - filePointerBefore; |
| 305 | + } |
| 306 | + |
| 307 | + public long getBytesRead() { |
| 308 | + return bytesRead; |
| 309 | + } |
| 310 | + }; |
| 311 | + |
281 | 312 | public void testReadZeroShouldReturnZero() throws IOException { |
282 | 313 | try (Directory dir = new ByteBuffersDirectory()) { |
283 | 314 | try (IndexOutput output = dir.createOutput("test", IOContext.DEFAULT)) { |
|
0 commit comments