private final int maxEntrySize;
private final JournalIndex index;
private final JournalSerdes namespace;
- private final MappableJournalSegmentWriter<E> writer;
private final Set<JournalSegmentReader<E>> readers = ConcurrentHashMap.newKeySet();
private final AtomicInteger references = new AtomicInteger();
private final FileChannel channel;
+
+ private JournalSegmentWriter<E> writer;
private boolean open = true;
JournalSegment(
} catch (IOException e) {
throw new StorageException(e);
}
- writer = new MappableJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
+ writer = new FileChannelJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
}
/**
*/
private void acquire() {
if (references.getAndIncrement() == 0 && storageLevel == StorageLevel.MAPPED) {
- writer.map();
+ writer = writer.toMapped();
}
}
private void release() {
if (references.decrementAndGet() == 0) {
if (storageLevel == StorageLevel.MAPPED) {
- writer.unmap();
+ writer = writer.toFileChannel();
}
if (!open) {
finishClose();
*
* @return The segment writer.
*/
- MappableJournalSegmentWriter<E> acquireWriter() {
+ JournalSegmentWriter<E> acquireWriter() {
checkOpen();
acquire();
+++ /dev/null
-/*
- * Copyright 2018-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.storage.journal;
-
-import io.atomix.storage.journal.index.JournalIndex;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-
-/**
- * Mappable log segment writer.
- */
-final class MappableJournalSegmentWriter<E> implements JournalWriter<E> {
- private JournalSegmentWriter<E> writer;
-
- MappableJournalSegmentWriter(
- FileChannel channel,
- JournalSegment<E> segment,
- int maxEntrySize,
- JournalIndex index,
- JournalSerdes namespace) {
- this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
- }
-
- /**
- * Maps the segment writer into memory, returning the mapped buffer.
- *
- * @return the buffer that was mapped into memory
- */
- MappedByteBuffer map() {
- final var mapped = writer.toMapped();
- writer = mapped;
- return mapped.buffer();
- }
-
- /**
- * Unmaps the mapped buffer.
- */
- void unmap() {
- writer = writer.toFileChannel();
- }
-
- MappedByteBuffer buffer() {
- return writer.buffer();
- }
-
- @Override
- public long getLastIndex() {
- return writer.getLastIndex();
- }
-
- @Override
- public Indexed<E> getLastEntry() {
- return writer.getLastEntry();
- }
-
- @Override
- public long getNextIndex() {
- return writer.getNextIndex();
- }
-
- @Override
- public <T extends E> Indexed<T> append(T entry) {
- return writer.append(entry);
- }
-
- @Override
- public void append(Indexed<E> entry) {
- writer.append(entry);
- }
-
- @Override
- public void commit(long index) {
- writer.commit(index);
- }
-
- @Override
- public void reset(long index) {
- writer.reset(index);
- }
-
- @Override
- public void truncate(long index) {
- writer.truncate(index);
- }
-
- @Override
- public void flush() {
- writer.flush();
- }
-
- @Override
- public void close() {
- writer.close();
- }
-}