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.io.IOException;
20 import java.nio.MappedByteBuffer;
21 import java.nio.channels.FileChannel;
24 * Mappable log segment writer.
26 final class MappableJournalSegmentWriter<E> implements JournalWriter<E> {
27 private final FileChannel channel;
28 private final JournalSegment<E> segment;
29 private final int maxEntrySize;
30 private final JournalIndex index;
31 private final JournalSerdes namespace;
32 private JournalSegmentWriter<E> writer;
34 MappableJournalSegmentWriter(
36 JournalSegment<E> segment,
39 JournalSerdes namespace) {
40 this.channel = channel;
41 this.segment = segment;
42 this.maxEntrySize = maxEntrySize;
44 this.namespace = namespace;
45 this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
49 * Maps the segment writer into memory, returning the mapped buffer.
51 * @return the buffer that was mapped into memory
53 MappedByteBuffer map() {
54 if (writer instanceof MappedJournalSegmentWriter) {
55 return ((MappedJournalSegmentWriter<E>) writer).buffer();
59 JournalSegmentWriter<E> writer = this.writer;
60 MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, segment.descriptor().maxSegmentSize());
61 this.writer = new MappedJournalSegmentWriter<>(buffer, segment, maxEntrySize, index, namespace);
64 } catch (IOException e) {
65 throw new StorageException(e);
70 * Unmaps the mapped buffer.
73 if (writer instanceof MappedJournalSegmentWriter) {
74 JournalSegmentWriter<E> writer = this.writer;
75 this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
80 MappedByteBuffer buffer() {
81 return writer.buffer();
85 * Returns the writer's first index.
87 * @return the writer's first index
89 public long firstIndex() {
90 return segment.index();
94 * Returns the size of the segment.
96 * @return the size of the segment
100 return (int) channel.size();
101 } catch (IOException e) {
102 throw new StorageException(e);
107 public long getLastIndex() {
108 return writer.getLastIndex();
112 public Indexed<E> getLastEntry() {
113 return writer.getLastEntry();
117 public long getNextIndex() {
118 return writer.getNextIndex();
122 public <T extends E> Indexed<T> append(T entry) {
123 return writer.append(entry);
127 public void append(Indexed<E> entry) {
128 writer.append(entry);
132 public void commit(long index) {
133 writer.commit(index);
137 public void reset(long index) {
142 public void truncate(long index) {
143 writer.truncate(index);
147 public void flush() {
152 public void close() {
156 } catch (IOException e) {
157 throw new StorageException(e);