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.collect.Sets;
19 import io.atomix.storage.StorageException;
20 import io.atomix.storage.StorageLevel;
21 import io.atomix.storage.journal.index.JournalIndex;
22 import io.atomix.storage.journal.index.SparseJournalIndex;
23 import io.atomix.utils.serializer.Namespace;
26 import java.io.IOException;
27 import java.nio.MappedByteBuffer;
28 import java.nio.channels.FileChannel;
29 import java.nio.file.Files;
30 import java.nio.file.StandardOpenOption;
32 import java.util.concurrent.atomic.AtomicInteger;
34 import static com.google.common.base.MoreObjects.toStringHelper;
35 import static com.google.common.base.Preconditions.checkState;
40 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
42 public class JournalSegment<E> implements AutoCloseable {
43 private final JournalSegmentFile file;
44 private final JournalSegmentDescriptor descriptor;
45 private final StorageLevel storageLevel;
46 private final int maxEntrySize;
47 private final JournalIndex index;
48 private final Namespace namespace;
49 private final MappableJournalSegmentWriter<E> writer;
50 private final Set<MappableJournalSegmentReader<E>> readers = Sets.newConcurrentHashSet();
51 private final AtomicInteger references = new AtomicInteger();
52 private boolean open = true;
54 public JournalSegment(
55 JournalSegmentFile file,
56 JournalSegmentDescriptor descriptor,
57 StorageLevel storageLevel,
60 Namespace namespace) {
62 this.descriptor = descriptor;
63 this.storageLevel = storageLevel;
64 this.maxEntrySize = maxEntrySize;
65 this.index = new SparseJournalIndex(indexDensity);
66 this.namespace = namespace;
67 this.writer = new MappableJournalSegmentWriter<>(openChannel(file.file()), this, maxEntrySize, index, namespace);
70 private FileChannel openChannel(File file) {
72 return FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
73 } catch (IOException e) {
74 throw new StorageException(e);
79 * Returns the segment ID.
81 * @return The segment ID.
84 return descriptor.id();
88 * Returns the segment version.
90 * @return The segment version.
92 public long version() {
93 return descriptor.version();
97 * Returns the segment's starting index.
99 * @return The segment's starting index.
101 public long index() {
102 return descriptor.index();
106 * Returns the last index in the segment.
108 * @return The last index in the segment.
110 public long lastIndex() {
111 return writer.getLastIndex();
115 * Returns the size of the segment.
117 * @return the size of the segment
120 return writer.size();
124 * Returns the segment file.
126 * @return The segment file.
128 public JournalSegmentFile file() {
133 * Returns the segment descriptor.
135 * @return The segment descriptor.
137 public JournalSegmentDescriptor descriptor() {
142 * Returns a boolean value indicating whether the segment is empty.
144 * @return Indicates whether the segment is empty.
146 public boolean isEmpty() {
147 return length() == 0;
151 * Returns the segment length.
153 * @return The segment length.
155 public long length() {
156 return writer.getNextIndex() - index();
160 * Acquires a reference to the log segment.
163 if (references.getAndIncrement() == 0 && open) {
169 * Releases a reference to the log segment.
172 if (references.decrementAndGet() == 0 && open) {
178 * Maps the log segment into memory.
181 if (storageLevel == StorageLevel.MAPPED) {
182 MappedByteBuffer buffer = writer.map();
183 readers.forEach(reader -> reader.map(buffer));
188 * Unmaps the log segment from memory.
190 private void unmap() {
191 if (storageLevel == StorageLevel.MAPPED) {
193 readers.forEach(reader -> reader.unmap());
198 * Returns the segment writer.
200 * @return The segment writer.
202 public MappableJournalSegmentWriter<E> writer() {
208 * Creates a new segment reader.
210 * @return A new segment reader.
212 MappableJournalSegmentReader<E> createReader() {
214 MappableJournalSegmentReader<E> reader = new MappableJournalSegmentReader<>(
215 openChannel(file.file()), this, maxEntrySize, index, namespace);
216 MappedByteBuffer buffer = writer.buffer();
217 if (buffer != null) {
225 * Closes a segment reader.
227 * @param reader the closed segment reader
229 void closeReader(MappableJournalSegmentReader<E> reader) {
230 readers.remove(reader);
234 * Checks whether the segment is open.
236 private void checkOpen() {
237 checkState(open, "Segment not open");
241 * Returns a boolean indicating whether the segment is open.
243 * @return indicates whether the segment is open
245 public boolean isOpen() {
250 * Closes the segment.
253 public void close() {
256 readers.forEach(reader -> reader.close());
261 * Deletes the segment.
263 public void delete() {
265 Files.deleteIfExists(file.file().toPath());
266 } catch (IOException e) {
267 throw new StorageException(e);
272 public String toString() {
273 return toStringHelper(this)
275 .add("version", version())
276 .add("index", index())