@Override
public long getLastIndex() {
- return lastEntry != null ? lastEntry.index() : segment.index() - 1;
+ return lastEntry != null ? lastEntry.index() : firstIndex - 1;
}
@Override
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();
}
this.index.truncate(index);
try {
- if (index < segment.index()) {
+ if (index < firstIndex) {
// Reset the writer to the first entry.
currentPosition = JournalSegmentDescriptor.BYTES;
} else {
abstract sealed class JournalSegmentWriter<E> implements JournalWriter<E>
permits FileChannelJournalSegmentWriter, MappedJournalSegmentWriter {
final @NonNull FileChannel channel;
- final @NonNull JournalSegment<E> segment;
- final int maxEntrySize;
final @NonNull JournalIndex index;
final @NonNull JournalSerdes namespace;
+ final int maxSegmentSize;
+ final int maxEntrySize;
final long firstIndex;
JournalSegmentWriter(final FileChannel channel, final JournalSegment<E> segment, final int maxEntrySize,
final JournalIndex index, final JournalSerdes namespace) {
this.channel = requireNonNull(channel);
- this.segment = requireNonNull(segment);
- this.maxEntrySize = maxEntrySize;
this.index = requireNonNull(index);
this.namespace = requireNonNull(namespace);
+ this.maxSegmentSize = segment.descriptor().maxSegmentSize();
+ this.maxEntrySize = maxEntrySize;
this.firstIndex = segment.index();
}
JournalSegmentWriter(JournalSegmentWriter<E> previous) {
this.channel = previous.channel;
- this.segment = previous.segment;
- this.maxEntrySize = previous.maxEntrySize;
this.index = previous.index;
this.namespace = previous.namespace;
+ this.maxSegmentSize = previous.maxSegmentSize;
+ this.maxEntrySize = previous.maxEntrySize;
this.firstIndex = previous.firstIndex;
}
JournalIndex index,
JournalSerdes namespace) {
super(channel, segment, maxEntrySize, index, namespace);
- mappedBuffer = mapBuffer(channel, segment.descriptor().maxSegmentSize());
+ mappedBuffer = mapBuffer(channel, maxSegmentSize);
buffer = mappedBuffer.slice();
reset(0);
}
MappedJournalSegmentWriter(JournalSegmentWriter<E> previous, int position) {
super(previous);
- mappedBuffer = mapBuffer(channel, segment.descriptor().maxSegmentSize());
+ mappedBuffer = mapBuffer(channel, maxSegmentSize);
buffer = mappedBuffer.slice();
lastEntry = previous.getLastEntry();
buffer.position(position);
@Override
public long getLastIndex() {
- return lastEntry != null ? lastEntry.index() : segment.index() - 1;
+ return lastEntry != null ? lastEntry.index() : firstIndex - 1;
}
@Override
// Truncate the index.
this.index.truncate(index);
- if (index < segment.index()) {
+ if (index < firstIndex) {
// Reset the writer to the first entry.
buffer.position(JournalSegmentDescriptor.BYTES);
} else {