2 * Copyright 2017-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.storage.journal.index.Position;
20 import io.atomix.utils.serializer.Namespace;
22 import java.nio.BufferUnderflowException;
23 import java.nio.ByteBuffer;
24 import java.util.NoSuchElementException;
25 import java.util.zip.CRC32;
30 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
32 class MappedJournalSegmentReader<E> implements JournalReader<E> {
33 private final ByteBuffer buffer;
34 private final int maxEntrySize;
35 private final JournalIndex index;
36 private final Namespace namespace;
37 private final long firstIndex;
38 private Indexed<E> currentEntry;
39 private Indexed<E> nextEntry;
41 MappedJournalSegmentReader(
43 JournalSegment<E> segment,
46 Namespace namespace) {
47 this.buffer = buffer.slice();
48 this.maxEntrySize = maxEntrySize;
50 this.namespace = namespace;
51 this.firstIndex = segment.index();
56 public long getFirstIndex() {
61 public long getCurrentIndex() {
62 return currentEntry != null ? currentEntry.index() : 0;
66 public Indexed<E> getCurrentEntry() {
71 public long getNextIndex() {
72 return currentEntry != null ? currentEntry.index() + 1 : firstIndex;
76 public void reset(long index) {
78 Position position = this.index.lookup(index - 1);
79 if (position != null) {
80 currentEntry = new Indexed<>(position.index() - 1, null, 0);
81 buffer.position(position.position());
84 while (getNextIndex() < index && hasNext()) {
91 buffer.position(JournalSegmentDescriptor.BYTES);
98 public boolean hasNext() {
99 // If the next entry is null, check whether a next entry exists.
100 if (nextEntry == null) {
103 return nextEntry != null;
107 public Indexed<E> next() {
109 throw new NoSuchElementException();
112 // Set the current entry to the next entry.
113 currentEntry = nextEntry;
115 // Reset the next entry to null.
118 // Read the next entry in the segment.
121 // Return the current entry.
126 * Reads the next entry in the segment.
128 private void readNext() {
129 // Compute the index of the next entry in the segment.
130 final long index = getNextIndex();
132 // Mark the buffer so it can be reset if necessary.
136 // Read the length of the entry.
137 final int length = buffer.getInt();
139 // If the buffer length is zero then return.
140 if (length <= 0 || length > maxEntrySize) {
146 // Read the checksum of the entry.
147 long checksum = buffer.getInt() & 0xFFFFFFFFL;
149 // Compute the checksum for the entry bytes.
150 final CRC32 crc32 = new CRC32();
151 ByteBuffer slice = buffer.slice();
155 // If the stored checksum equals the computed checksum, return the entry.
156 if (checksum == crc32.getValue()) {
158 E entry = namespace.deserialize(slice);
159 nextEntry = new Indexed<>(index, entry, length);
160 buffer.position(buffer.position() + length);
165 } catch (BufferUnderflowException e) {
172 public void close() {
173 // Do nothing. The writer is responsible for cleaning the mapped buffer.