import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.CRC32;
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
-class FileChannelJournalSegmentWriter<E> implements JournalWriter<E> {
+final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
private static final ByteBuffer ZERO_ENTRY_HEADER = ByteBuffer.wrap(new byte[Integer.BYTES + Integer.BYTES]);
- private final FileChannel channel;
- private final JournalSegment<E> segment;
- private final int maxEntrySize;
- private final JournalIndex index;
- private final JournalSerdes namespace;
private final ByteBuffer memory;
- private final long firstIndex;
private Indexed<E> lastEntry;
private long currentPosition;
int maxEntrySize,
JournalIndex index,
JournalSerdes namespace) {
- this.channel = channel;
- this.segment = segment;
- this.maxEntrySize = maxEntrySize;
- this.index = index;
- this.memory = ByteBuffer.allocate((maxEntrySize + Integer.BYTES + Integer.BYTES) * 2);
- memory.limit(0);
- this.namespace = namespace;
- this.firstIndex = segment.index();
+ super(channel, segment, maxEntrySize, index, namespace);
+ memory = allocMemory(maxEntrySize);
reset(0);
}
+ FileChannelJournalSegmentWriter(JournalSegmentWriter<E> previous, int position) {
+ super(previous);
+ memory = allocMemory(maxEntrySize);
+ lastEntry = previous.getLastEntry();
+ currentPosition = position;
+ }
+
+ private static ByteBuffer allocMemory(int maxEntrySize) {
+ final var buf = ByteBuffer.allocate((maxEntrySize + Integer.BYTES + Integer.BYTES) * 2);
+ buf.limit(0);
+ return buf;
+ }
+
+ @Override
+ MappedByteBuffer buffer() {
+ return null;
+ }
+
+ @Override
+ MappedJournalSegmentWriter<E> toMapped() {
+ return new MappedJournalSegmentWriter<>(this, (int) currentPosition);
+ }
+
+ @Override
+ FileChannelJournalSegmentWriter<E> toFileChannel() {
+ return this;
+ }
+
@Override
- public void reset(long index) {
+ void reset(long index) {
long nextIndex = firstIndex;
// Clear the buffer indexes.
}
@Override
- public long getLastIndex() {
- return lastEntry != null ? lastEntry.index() : segment.index() - 1;
+ long getLastIndex() {
+ return lastEntry != null ? lastEntry.index() : firstIndex - 1;
}
@Override
- public Indexed<E> getLastEntry() {
+ Indexed<E> getLastEntry() {
return lastEntry;
}
@Override
- public long getNextIndex() {
+ long getNextIndex() {
if (lastEntry != null) {
return lastEntry.index() + 1;
} else {
}
}
- @Override
- public void append(Indexed<E> entry) {
- final long nextIndex = getNextIndex();
-
- // If the entry's index is greater than the next index in the segment, skip some entries.
- if (entry.index() > nextIndex) {
- throw new IndexOutOfBoundsException("Entry index is not sequential");
- }
-
- // If the entry's index is less than the next index, truncate the segment.
- if (entry.index() < nextIndex) {
- truncate(entry.index() - 1);
- }
- append(entry.entry());
- }
-
@Override
@SuppressWarnings("unchecked")
- public <T extends E> Indexed<T> append(T entry) {
+ <T extends E> Indexed<T> append(T entry) {
// Store the entry index.
final long index = getNextIndex();
final int length = memory.limit() - (Integer.BYTES + Integer.BYTES);
// Ensure there's enough space left in the buffer to store the entry.
- if (segment.descriptor().maxSegmentSize() - currentPosition < length + Integer.BYTES + Integer.BYTES) {
+ if (maxSegmentSize - currentPosition < length + Integer.BYTES + Integer.BYTES) {
throw new BufferOverflowException();
}
}
@Override
- public void commit(long index) {
-
- }
-
- @Override
- public void truncate(long index) {
+ void truncate(long index) {
// If the index is greater than or equal to the last index, skip the truncate.
if (index >= getLastIndex()) {
return;
this.index.truncate(index);
try {
- if (index < segment.index()) {
+ if (index < firstIndex) {
// Reset the writer to the first entry.
currentPosition = JournalSegmentDescriptor.BYTES;
} else {
}
@Override
- public void flush() {
+ void flush() {
try {
if (channel.isOpen()) {
channel.force(true);
}
@Override
- public void close() {
+ void close() {
flush();
}
}