JournalSegmentFile.maxSize() goes into the algorithm to choose I/O size.
Let's move the method there.
JIRA: CONTROLLER-2115
Change-Id: I9f0810cb9769217965375ee70c7e3e231adbf1d9
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
* A {@link StorageLevel#DISK} implementation of {@link FileReader}. Maintains an internal buffer.
*/
final class DiskFileReader extends FileReader {
- /**
- * Just do not bother with IO smaller than this many bytes.
- */
- private static final int MIN_IO_SIZE = 8192;
-
private final FileChannel channel;
private final ByteBuffer buffer;
private int bufferPosition;
DiskFileReader(final JournalSegmentFile file, final int maxEntrySize) {
- this(file, allocateBuffer(file.maxSize(), maxEntrySize));
+ this(file, file.allocateBuffer(maxEntrySize));
}
// Note: take ownership of the buffer
bufferPosition = 0;
}
- static ByteBuffer allocateBuffer(final int maxSegmentSize, final int maxEntrySize) {
- return ByteBuffer.allocate(chooseBufferSize(maxSegmentSize, maxEntrySize));
- }
-
- private static int chooseBufferSize(final int maxSegmentSize, final int maxEntrySize) {
- if (maxSegmentSize <= MIN_IO_SIZE) {
- // just buffer the entire segment
- return maxSegmentSize;
- }
-
- // one full entry plus its header, or MIN_IO_SIZE, which benefits the read of many small entries
- final int minBufferSize = maxEntrySize + SegmentEntry.HEADER_BYTES;
- return minBufferSize <= MIN_IO_SIZE ? MIN_IO_SIZE : minBufferSize;
- }
-
@Override
void invalidateCache() {
buffer.clear().flip();
DiskFileWriter(final JournalSegmentFile file, final int maxEntrySize) {
super(file, maxEntrySize);
channel = file.channel();
- buffer = DiskFileReader.allocateBuffer(file.maxSize(), maxEntrySize);
+ buffer = file.allocateBuffer(maxEntrySize);
reader = new DiskFileReader(file, buffer);
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import org.eclipse.jdt.annotation.NonNull;
private static final char PART_SEPARATOR = '-';
private static final char EXTENSION_SEPARATOR = '.';
private static final String EXTENSION = "log";
+ /**
+ * Just do not bother with IO smaller than this many bytes.
+ */
+ private static final int MIN_IO_SIZE = 8192;
private final @NonNull JournalSegmentDescriptor descriptor;
private final @NonNull Path path;
file.close();
}
+ ByteBuffer allocateBuffer(final int maxEntrySize) {
+ return ByteBuffer.allocate(chooseBufferSize(maxEntrySize));
+ }
+
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("path", path).add("descriptor", descriptor).toString();
}
+ private int chooseBufferSize(final int maxEntrySize) {
+ final int maxSegmentSize = maxSize();
+ if (maxSegmentSize <= MIN_IO_SIZE) {
+ // just buffer the entire segment
+ return maxSegmentSize;
+ }
+
+ // one full entry plus its header, or MIN_IO_SIZE, which benefits the read of many small entries
+ final int minBufferSize = maxEntrySize + SegmentEntry.HEADER_BYTES;
+ return minBufferSize <= MIN_IO_SIZE ? MIN_IO_SIZE : minBufferSize;
+ }
+
/**
* Returns a boolean value indicating whether the given file appears to be a parsable segment file.
*