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 io.atomix.utils.serializer.Namespace;
21 import java.io.IOException;
22 import java.nio.MappedByteBuffer;
23 import java.nio.channels.FileChannel;
26 * Mappable log segment writer.
28 class MappableJournalSegmentWriter<E> implements JournalWriter<E> {
29 private final FileChannel channel;
30 private final JournalSegment<E> segment;
31 private final int maxEntrySize;
32 private final JournalIndex index;
33 private final Namespace namespace;
34 private JournalWriter<E> writer;
36 MappableJournalSegmentWriter(
38 JournalSegment<E> segment,
41 Namespace namespace) {
42 this.channel = channel;
43 this.segment = segment;
44 this.maxEntrySize = maxEntrySize;
46 this.namespace = namespace;
47 this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
51 * Maps the segment writer into memory, returning the mapped buffer.
53 * @return the buffer that was mapped into memory
55 MappedByteBuffer map() {
56 if (writer instanceof MappedJournalSegmentWriter) {
57 return ((MappedJournalSegmentWriter<E>) writer).buffer();
61 JournalWriter<E> writer = this.writer;
62 MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, segment.descriptor().maxSegmentSize());
63 this.writer = new MappedJournalSegmentWriter<>(buffer, segment, maxEntrySize, index, namespace);
66 } catch (IOException e) {
67 throw new StorageException(e);
72 * Unmaps the mapped buffer.
75 if (writer instanceof MappedJournalSegmentWriter) {
76 JournalWriter<E> writer = this.writer;
77 this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
82 MappedByteBuffer buffer() {
83 JournalWriter<E> writer = this.writer;
84 if (writer instanceof MappedJournalSegmentWriter) {
85 return ((MappedJournalSegmentWriter<E>) writer).buffer();
91 * Returns the writer's first index.
93 * @return the writer's first index
95 public long firstIndex() {
96 return segment.index();
100 * Returns the size of the segment.
102 * @return the size of the segment
106 return (int) channel.size();
107 } catch (IOException e) {
108 throw new StorageException(e);
113 public long getLastIndex() {
114 return writer.getLastIndex();
118 public Indexed<E> getLastEntry() {
119 return writer.getLastEntry();
123 public long getNextIndex() {
124 return writer.getNextIndex();
128 public <T extends E> Indexed<T> append(T entry) {
129 return writer.append(entry);
133 public void append(Indexed<E> entry) {
134 writer.append(entry);
138 public void commit(long index) {
139 writer.commit(index);
143 public void reset(long index) {
148 public void truncate(long index) {
149 writer.truncate(index);
153 public void flush() {
158 public void close() {
162 } catch (IOException e) {
163 throw new StorageException(e);