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 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 JournalWriter<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 JournalWriter<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 JournalWriter<E> writer = this.writer;
75 this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
80 MappedByteBuffer buffer() {
81 JournalWriter<E> writer = this.writer;
82 if (writer instanceof MappedJournalSegmentWriter) {
83 return ((MappedJournalSegmentWriter<E>) writer).buffer();
89 * Returns the writer's first index.
91 * @return the writer's first index
93 public long firstIndex() {
94 return segment.index();
98 * Returns the size of the segment.
100 * @return the size of the segment
104 return (int) channel.size();
105 } catch (IOException e) {
106 throw new StorageException(e);
111 public long getLastIndex() {
112 return writer.getLastIndex();
116 public Indexed<E> getLastEntry() {
117 return writer.getLastEntry();
121 public long getNextIndex() {
122 return writer.getNextIndex();
126 public <T extends E> Indexed<T> append(T entry) {
127 return writer.append(entry);
131 public void append(Indexed<E> entry) {
132 writer.append(entry);
136 public void commit(long index) {
137 writer.commit(index);
141 public void reset(long index) {
146 public void truncate(long index) {
147 writer.truncate(index);
151 public void flush() {
156 public void close() {
160 } catch (IOException e) {
161 throw new StorageException(e);