Merge branch cassandra-3.11 into cassandra-4.0

This commit is contained in:
Benjamin Lerer 2021-07-08 12:17:49 +02:00
commit bff4e54cdd
3 changed files with 92 additions and 0 deletions

View File

@ -6,6 +6,7 @@
* Cleanup dependency scopes (CASSANDRA-16704)
* Make JmxHistogram#getRecentValues() and JmxTimer#getRecentValues() thread-safe (CASSANDRA-16707)
Merged from 3.11:
* Optimize bytes skipping when reading SSTable files (CASSANDRA-14415)
* Enable tombstone compactions when unchecked_tombstone_compaction is set in TWCS (CASSANDRA-14496)
* Read only the required SSTables for single partition queries (CASSANDRA-16737)
Merged from 3.0:

View File

@ -210,6 +210,20 @@ public class RandomAccessReader extends RebufferingInputStream implements FileDa
reBufferAt(newPosition);
}
@Override
public int skipBytes(int n) throws IOException
{
if (n <= 0)
return 0;
if (buffer == null)
throw new IOException("Attempted skipBytes() on a closed RAR");
long current = current();
long newPosition = Math.min(current + n, length());
n = (int)(newPosition - current);
seek(newPosition);
return n;
}
/**
* Reads a line of text form the current position in this file. A line is
* represented by zero or more characters followed by {@code '\n'}, {@code

View File

@ -499,4 +499,81 @@ public class RandomAccessReaderTest
}
}
}
@Test
public void testSkipBytesLessThanBufferSize() throws IOException
{
testSkipBytes(new Parameters(8192, 1024), 1);
}
@Test
public void testSkipBytesGreaterThanBufferSize() throws IOException
{
int bufferSize = 16;
Parameters params = new Parameters(8192, bufferSize);
int numberOfExpectationsInBufferSize = bufferSize / params.expected.length;
testSkipBytes(params, numberOfExpectationsInBufferSize + 1);
}
public void testSkipBytesNonPositive() throws IOException
{
Parameters params = new Parameters(8192, 4096);
final File f = writeFile(params);
try (FileHandle.Builder builder = new FileHandle.Builder(f.getPath())
.bufferType(params.bufferType).bufferSize(params.bufferSize))
{
builder.mmapped(params.mmappedRegions);
try (FileHandle fh = builder.complete();
RandomAccessReader reader = fh.createReader())
{
assertEquals(0, reader.skipBytes(0));
assertEquals(0, reader.skipBytes(-1));
}
}
}
@Test(expected = IOException.class)
public void testSkipBytesClosed() throws IOException
{
Parameters params = new Parameters(8192, 4096);
final File f = writeFile(params);
try (FileHandle.Builder builder = new FileHandle.Builder(f.getPath())
.bufferType(params.bufferType).bufferSize(params.bufferSize))
{
try (FileHandle fh = builder.complete();
RandomAccessReader reader = fh.createReader())
{
reader.close();
reader.skipBytes(31415);
}
}
}
private static void testSkipBytes(Parameters params, int expectationMultiples) throws IOException
{
final File f = writeFile(params);
try (FileHandle.Builder builder = new FileHandle.Builder(f.getPath())
.bufferType(params.bufferType).bufferSize(params.bufferSize))
{
builder.mmapped(params.mmappedRegions);
try (FileHandle fh = builder.complete();
RandomAccessReader reader = fh.createReader())
{
int toSkip = expectationMultiples * params.expected.length;
byte[] b = new byte[params.expected.length];
long numRead = 0;
while (numRead < params.fileLength)
{
reader.readFully(b);
assertTrue(Arrays.equals(params.expected, b));
numRead += b.length;
int skipped = reader.skipBytes(toSkip);
long expectedSkipped = Math.max(Math.min(toSkip, params.fileLength - numRead), 0);
assertEquals(expectedSkipped, skipped);
numRead += skipped;
}
}
}
}
}