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.SparseJournalIndex;
20 import java.io.IOException;
21 import java.nio.channels.FileChannel;
22 import java.nio.file.Files;
23 import java.nio.file.StandardOpenOption;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.atomic.AtomicInteger;
28 import static com.google.common.base.MoreObjects.toStringHelper;
29 import static com.google.common.base.Preconditions.checkState;
34 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
36 final class JournalSegment<E> implements AutoCloseable {
37 private final JournalSegmentFile file;
38 private final JournalSegmentDescriptor descriptor;
39 private final StorageLevel storageLevel;
40 private final int maxEntrySize;
41 private final JournalIndex index;
42 private final JournalSerdes namespace;
43 private final MappableJournalSegmentWriter<E> writer;
44 private final Set<JournalSegmentReader<E>> readers = ConcurrentHashMap.newKeySet();
45 private final AtomicInteger references = new AtomicInteger();
46 private final FileChannel channel;
47 private boolean open = true;
50 JournalSegmentFile file,
51 JournalSegmentDescriptor descriptor,
52 StorageLevel storageLevel,
55 JournalSerdes namespace) {
57 this.descriptor = descriptor;
58 this.storageLevel = storageLevel;
59 this.maxEntrySize = maxEntrySize;
60 this.namespace = namespace;
61 index = new SparseJournalIndex(indexDensity);
63 channel = FileChannel.open(file.file().toPath(),
64 StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
65 } catch (IOException e) {
66 throw new StorageException(e);
68 writer = new MappableJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
72 * Returns the segment ID.
74 * @return The segment ID.
77 return descriptor.id();
81 * Returns the segment version.
83 * @return The segment version.
85 public long version() {
86 return descriptor.version();
90 * Returns the segment's starting index.
92 * @return The segment's starting index.
95 return descriptor.index();
99 * Returns the last index in the segment.
101 * @return The last index in the segment.
103 public long lastIndex() {
104 return writer.getLastIndex();
108 * Returns the size of the segment.
110 * @return the size of the segment
114 return (int) channel.size();
115 } catch (IOException e) {
116 throw new StorageException(e);
121 * Returns the segment file.
123 * @return The segment file.
125 public JournalSegmentFile file() {
130 * Returns the segment descriptor.
132 * @return The segment descriptor.
134 public JournalSegmentDescriptor descriptor() {
139 * Returns a boolean value indicating whether the segment is empty.
141 * @return Indicates whether the segment is empty.
143 public boolean isEmpty() {
144 return length() == 0;
148 * Returns the segment length.
150 * @return The segment length.
152 public long length() {
153 return writer.getNextIndex() - index();
157 * Acquires a reference to the log segment.
160 if (references.getAndIncrement() == 0 && storageLevel == StorageLevel.MAPPED) {
166 * Releases a reference to the log segment.
169 if (references.decrementAndGet() == 0) {
170 if (storageLevel == StorageLevel.MAPPED) {
180 * Returns the segment writer.
182 * @return The segment writer.
184 public MappableJournalSegmentWriter<E> writer() {
190 * Creates a new segment reader.
192 * @return A new segment reader.
194 JournalSegmentReader<E> createReader() {
198 final var buffer = writer.buffer();
199 final var reader = buffer == null
200 ? new FileChannelJournalSegmentReader<>(channel, this, maxEntrySize, index, namespace)
201 : new MappedJournalSegmentReader<>(buffer, this, maxEntrySize, index, namespace);
207 * Closes a segment reader.
209 * @param reader the closed segment reader
211 void closeReader(JournalSegmentReader<E> reader) {
212 if (readers.remove(reader)) {
218 * Checks whether the segment is open.
220 private void checkOpen() {
221 checkState(open, "Segment not open");
225 * Returns a boolean indicating whether the segment is open.
227 * @return indicates whether the segment is open
229 public boolean isOpen() {
234 * Closes the segment.
237 public void close() {
243 readers.forEach(JournalSegmentReader::close);
244 if (references.get() == 0) {
249 private void finishClose() {
253 } catch (IOException e) {
254 throw new StorageException(e);
259 * Deletes the segment.
261 public void delete() {
263 Files.deleteIfExists(file.file().toPath());
264 } catch (IOException e) {
265 throw new StorageException(e);
270 public String toString() {
271 return toStringHelper(this)
273 .add("version", version())
274 .add("index", index())