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 com.google.common.base.MoreObjects;
19 import io.atomix.storage.journal.index.JournalIndex;
20 import io.atomix.storage.journal.index.SparseJournalIndex;
21 import java.io.IOException;
22 import java.nio.channels.FileChannel;
23 import java.nio.file.Files;
24 import java.nio.file.StandardOpenOption;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.atomic.AtomicInteger;
32 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
34 final class JournalSegment<E> implements AutoCloseable {
35 private final JournalSegmentFile file;
36 private final JournalSegmentDescriptor descriptor;
37 private final StorageLevel storageLevel;
38 private final int maxEntrySize;
39 private final JournalIndex index;
40 private final JournalSerdes namespace;
41 private final Set<JournalSegmentReader<E>> readers = ConcurrentHashMap.newKeySet();
42 private final AtomicInteger references = new AtomicInteger();
43 private final FileChannel channel;
45 private JournalSegmentWriter<E> writer;
46 private boolean open = true;
49 JournalSegmentFile file,
50 JournalSegmentDescriptor descriptor,
51 StorageLevel storageLevel,
54 JournalSerdes namespace) {
56 this.descriptor = descriptor;
57 this.storageLevel = storageLevel;
58 this.maxEntrySize = maxEntrySize;
59 this.namespace = namespace;
60 index = new SparseJournalIndex(indexDensity);
62 channel = FileChannel.open(file.file().toPath(),
63 StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
64 } catch (IOException e) {
65 throw new StorageException(e);
67 writer = switch (storageLevel) {
68 case DISK -> new FileChannelJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
69 case MAPPED -> new MappedJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace).toFileChannel();
74 * Returns the segment's starting index.
76 * @return The segment's starting index.
79 return descriptor.index();
83 * Returns the last index in the segment.
85 * @return The last index in the segment.
88 return writer.getLastIndex();
92 * Returns the size of the segment.
94 * @return the size of the segment
98 return (int) channel.size();
99 } catch (IOException e) {
100 throw new StorageException(e);
105 * Returns the segment file.
107 * @return The segment file.
109 JournalSegmentFile file() {
114 * Returns the segment descriptor.
116 * @return The segment descriptor.
118 JournalSegmentDescriptor descriptor() {
123 * Acquires a reference to the log segment.
125 private void acquire() {
126 if (references.getAndIncrement() == 0 && storageLevel == StorageLevel.MAPPED) {
127 writer = writer.toMapped();
132 * Releases a reference to the log segment.
134 private void release() {
135 if (references.decrementAndGet() == 0) {
136 if (storageLevel == StorageLevel.MAPPED) {
137 writer = writer.toFileChannel();
146 * Acquires a reference to the segment writer.
148 * @return The segment writer.
150 JournalSegmentWriter<E> acquireWriter() {
158 * Releases the reference to the segment writer.
160 void releaseWriter() {
165 * Creates a new segment reader.
167 * @return A new segment reader.
169 JournalSegmentReader<E> createReader() {
173 final var buffer = writer.buffer();
174 final var reader = buffer == null
175 ? new FileChannelJournalSegmentReader<>(channel, this, maxEntrySize, index, namespace)
176 : new MappedJournalSegmentReader<>(buffer, this, maxEntrySize, index, namespace);
182 * Closes a segment reader.
184 * @param reader the closed segment reader
186 void closeReader(JournalSegmentReader<E> reader) {
187 if (readers.remove(reader)) {
193 * Checks whether the segment is open.
195 private void checkOpen() {
197 throw new IllegalStateException("Segment not open");
202 * Returns a boolean indicating whether the segment is open.
204 * @return indicates whether the segment is open
206 public boolean isOpen() {
211 * Closes the segment.
214 public void close() {
220 readers.forEach(JournalSegmentReader::close);
221 if (references.get() == 0) {
226 private void finishClose() {
230 } catch (IOException e) {
231 throw new StorageException(e);
236 * Deletes the segment.
240 Files.deleteIfExists(file.file().toPath());
241 } catch (IOException e) {
242 throw new StorageException(e);
247 public String toString() {
248 return MoreObjects.toStringHelper(this)
249 .add("id", descriptor.id())
250 .add("version", descriptor.version())
251 .add("index", index())