Skip to content

Commit

Permalink
Add method to copy to file channel from buffer (elastic#108228)
Browse files Browse the repository at this point in the history
Currently in ShardBytes we only support copying from an inputstream
using an intermediate buffer. This commit adds a method to copy from the
buffer directly without reading from the input stream.
  • Loading branch information
Tim-Brooks authored May 3, 2024
1 parent eb90e36 commit fd3aa53
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,31 @@ public static int copyToCacheFileAligned(IO fc, InputStream input, int fileChann
if (bytesRead <= 0) {
break;
}
if (buffer.hasRemaining()) {
// ensure that last write is aligned on 4k boundaries (= page size)
final int remainder = buffer.position() % PAGE_SIZE;
final int adjustment = remainder == 0 ? 0 : PAGE_SIZE - remainder;
buffer.position(buffer.position() + adjustment);
}
bytesCopied += positionalWrite(fc, fileChannelPos + bytesCopied, buffer);
bytesCopied += copyBufferToCacheFileAligned(fc, fileChannelPos + bytesCopied, buffer);
progressUpdater.accept(bytesCopied);
}
return bytesCopied;
}

/**
* Copy all bytes from {@code buffer} to {@code fc}, only doing writes aligned along {@link #PAGE_SIZE}.
*
* @param fc output cache file reference
* @param fileChannelPos position in {@code fc} to write to
* @param buffer bytebuffer to copy from
* @return the number of bytes copied
* @throws IOException on failure
*/
public static int copyBufferToCacheFileAligned(IO fc, int fileChannelPos, ByteBuffer buffer) throws IOException {
if (buffer.hasRemaining()) {
// ensure the write is aligned on 4k boundaries (= page size)
final int remainder = buffer.position() % PAGE_SIZE;
final int adjustment = remainder == 0 ? 0 : PAGE_SIZE - remainder;
buffer.position(buffer.position() + adjustment);
}
return positionalWrite(fc, fileChannelPos, buffer);
}

private static int positionalWrite(IO fc, int start, ByteBuffer byteBuffer) throws IOException {
byteBuffer.flip();
int written = fc.write(byteBuffer, start);
Expand Down

0 comments on commit fd3aa53

Please sign in to comment.