2 * Copyright 2018-present Open Networking Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.storage.journal;
18 import io.atomix.storage.journal.index.JournalIndex;
19 import java.nio.MappedByteBuffer;
20 import java.nio.channels.FileChannel;
23 * Mappable log segment writer.
25 final class MappableJournalSegmentWriter<E> implements JournalWriter<E> {
26 private final JournalSegment<E> segment;
27 private JournalSegmentWriter<E> writer;
29 MappableJournalSegmentWriter(
31 JournalSegment<E> segment,
34 JournalSerdes namespace) {
35 this.segment = segment;
36 this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
40 * Maps the segment writer into memory, returning the mapped buffer.
42 * @return the buffer that was mapped into memory
44 MappedByteBuffer map() {
45 final var mapped = writer.toMapped();
47 return mapped.buffer();
51 * Unmaps the mapped buffer.
54 writer = writer.toFileChannel();
57 MappedByteBuffer buffer() {
58 return writer.buffer();
62 * Returns the writer's first index.
64 * @return the writer's first index
66 public long firstIndex() {
67 return segment.index();
71 public long getLastIndex() {
72 return writer.getLastIndex();
76 public Indexed<E> getLastEntry() {
77 return writer.getLastEntry();
81 public long getNextIndex() {
82 return writer.getNextIndex();
86 public <T extends E> Indexed<T> append(T entry) {
87 return writer.append(entry);
91 public void append(Indexed<E> entry) {
96 public void commit(long index) {
101 public void reset(long index) {
106 public void truncate(long index) {
107 writer.truncate(index);
111 public void flush() {
116 public void close() {