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.StorageException;
19 import io.atomix.storage.journal.index.JournalIndex;
20 import io.atomix.storage.journal.index.Position;
21 import io.atomix.utils.serializer.Namespace;
23 import java.io.IOException;
24 import java.nio.BufferUnderflowException;
25 import java.nio.ByteBuffer;
26 import java.nio.channels.FileChannel;
27 import java.util.NoSuchElementException;
28 import java.util.zip.CRC32;
29 import java.util.zip.Checksum;
34 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
36 class FileChannelJournalSegmentReader<E> implements JournalReader<E> {
37 private final FileChannel channel;
38 private final int maxEntrySize;
39 private final JournalIndex index;
40 private final Namespace namespace;
41 private final ByteBuffer memory;
42 private final long firstIndex;
43 private Indexed<E> currentEntry;
44 private Indexed<E> nextEntry;
46 FileChannelJournalSegmentReader(
48 JournalSegment<E> segment,
51 Namespace namespace) {
52 this.channel = channel;
53 this.maxEntrySize = maxEntrySize;
55 this.namespace = namespace;
56 this.memory = ByteBuffer.allocate((maxEntrySize + Integer.BYTES + Integer.BYTES) * 2);
57 this.firstIndex = segment.index();
62 public long getFirstIndex() {
67 public long getCurrentIndex() {
68 return currentEntry != null ? currentEntry.index() : 0;
72 public Indexed<E> getCurrentEntry() {
77 public long getNextIndex() {
78 return currentEntry != null ? currentEntry.index() + 1 : firstIndex;
82 public void reset(long index) {
84 Position position = this.index.lookup(index - 1);
85 if (position != null) {
86 currentEntry = new Indexed<>(position.index() - 1, null, 0);
88 channel.position(position.position());
89 memory.clear().flip();
90 } catch (IOException e) {
91 throw new StorageException(e);
95 while (getNextIndex() < index && hasNext()) {
101 public void reset() {
103 channel.position(JournalSegmentDescriptor.BYTES);
104 } catch (IOException e) {
105 throw new StorageException(e);
107 memory.clear().limit(0);
114 public boolean hasNext() {
115 // If the next entry is null, check whether a next entry exists.
116 if (nextEntry == null) {
119 return nextEntry != null;
123 public Indexed<E> next() {
125 throw new NoSuchElementException();
128 // Set the current entry to the next entry.
129 currentEntry = nextEntry;
131 // Reset the next entry to null.
134 // Read the next entry in the segment.
137 // Return the current entry.
142 * Reads the next entry in the segment.
144 private void readNext() {
145 // Compute the index of the next entry in the segment.
146 final long index = getNextIndex();
149 // Read more bytes from the segment if necessary.
150 if (memory.remaining() < maxEntrySize) {
151 long position = channel.position() + memory.position();
152 channel.position(position);
154 channel.read(memory);
155 channel.position(position);
159 // Mark the buffer so it can be reset if necessary.
163 // Read the length of the entry.
164 final int length = memory.getInt();
166 // If the buffer length is zero then return.
167 if (length <= 0 || length > maxEntrySize) {
168 memory.reset().limit(memory.position());
173 // Read the checksum of the entry.
174 long checksum = memory.getInt() & 0xFFFFFFFFL;
176 // Compute the checksum for the entry bytes.
177 final Checksum crc32 = new CRC32();
178 crc32.update(memory.array(), memory.position(), length);
180 // If the stored checksum equals the computed checksum, return the entry.
181 if (checksum == crc32.getValue()) {
182 int limit = memory.limit();
183 memory.limit(memory.position() + length);
184 E entry = namespace.deserialize(memory);
186 nextEntry = new Indexed<>(index, entry, length);
188 memory.reset().limit(memory.position());
191 } catch (BufferUnderflowException e) {
192 memory.reset().limit(memory.position());
195 } catch (IOException e) {
196 throw new StorageException(e);
201 public void close() {
202 // Do nothing. The parent reader manages the channel.