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.ByteBuffer;
23 import java.nio.channels.FileChannel;
26 * Mappable log segment reader.
28 class MappableJournalSegmentReader<E> implements JournalReader<E> {
29 private final JournalSegment<E> segment;
30 private final FileChannel channel;
31 private final int maxEntrySize;
32 private final JournalIndex index;
33 private final Namespace namespace;
34 private JournalReader<E> reader;
36 MappableJournalSegmentReader(
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.reader = new FileChannelJournalSegmentReader<>(channel, segment, maxEntrySize, index, namespace);
51 * Converts the reader to a mapped reader using the given buffer.
53 * @param buffer the mapped buffer
55 void map(ByteBuffer buffer) {
56 if (!(reader instanceof MappedJournalSegmentReader)) {
57 JournalReader<E> reader = this.reader;
58 this.reader = new MappedJournalSegmentReader<>(buffer, segment, maxEntrySize, index, namespace);
59 this.reader.reset(reader.getNextIndex());
65 * Converts the reader to an unmapped reader.
68 if (reader instanceof MappedJournalSegmentReader) {
69 JournalReader<E> reader = this.reader;
70 this.reader = new FileChannelJournalSegmentReader<>(channel, segment, maxEntrySize, index, namespace);
71 this.reader.reset(reader.getNextIndex());
77 public long getFirstIndex() {
78 return reader.getFirstIndex();
82 public long getCurrentIndex() {
83 return reader.getCurrentIndex();
87 public Indexed<E> getCurrentEntry() {
88 return reader.getCurrentEntry();
92 public long getNextIndex() {
93 return reader.getNextIndex();
97 public boolean hasNext() {
98 return reader.hasNext();
102 public Indexed<E> next() {
103 return reader.next();
107 public void reset() {
112 public void reset(long index) {
117 public void close() {
121 } catch (IOException e) {
122 throw new StorageException(e);
124 segment.closeReader(this);