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.journal.index.JournalIndex;
20 import io.atomix.storage.journal.index.SparseJournalIndex;
21 import io.atomix.utils.serializer.Namespace;
24 import java.io.IOException;
25 import java.nio.MappedByteBuffer;
26 import java.nio.channels.FileChannel;
27 import java.nio.file.Files;
28 import java.nio.file.StandardOpenOption;
30 import java.util.concurrent.atomic.AtomicInteger;
32 import static com.google.common.base.MoreObjects.toStringHelper;
33 import static com.google.common.base.Preconditions.checkState;
38 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
40 public class JournalSegment<E> implements AutoCloseable {
41 private final JournalSegmentFile file;
42 private final JournalSegmentDescriptor descriptor;
43 private final StorageLevel storageLevel;
44 private final int maxEntrySize;
45 private final JournalIndex index;
46 private final Namespace namespace;
47 private final MappableJournalSegmentWriter<E> writer;
48 private final Set<MappableJournalSegmentReader<E>> readers = Sets.newConcurrentHashSet();
49 private final AtomicInteger references = new AtomicInteger();
50 private boolean open = true;
52 public JournalSegment(
53 JournalSegmentFile file,
54 JournalSegmentDescriptor descriptor,
55 StorageLevel storageLevel,
58 Namespace namespace) {
60 this.descriptor = descriptor;
61 this.storageLevel = storageLevel;
62 this.maxEntrySize = maxEntrySize;
63 this.index = new SparseJournalIndex(indexDensity);
64 this.namespace = namespace;
65 this.writer = new MappableJournalSegmentWriter<>(openChannel(file.file()), this, maxEntrySize, index, namespace);
68 private FileChannel openChannel(File file) {
70 return FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
71 } catch (IOException e) {
72 throw new StorageException(e);
77 * Returns the segment ID.
79 * @return The segment ID.
82 return descriptor.id();
86 * Returns the segment version.
88 * @return The segment version.
90 public long version() {
91 return descriptor.version();
95 * Returns the segment's starting index.
97 * @return The segment's starting index.
100 return descriptor.index();
104 * Returns the last index in the segment.
106 * @return The last index in the segment.
108 public long lastIndex() {
109 return writer.getLastIndex();
113 * Returns the size of the segment.
115 * @return the size of the segment
118 return writer.size();
122 * Returns the segment file.
124 * @return The segment file.
126 public JournalSegmentFile file() {
131 * Returns the segment descriptor.
133 * @return The segment descriptor.
135 public JournalSegmentDescriptor descriptor() {
140 * Returns a boolean value indicating whether the segment is empty.
142 * @return Indicates whether the segment is empty.
144 public boolean isEmpty() {
145 return length() == 0;
149 * Returns the segment length.
151 * @return The segment length.
153 public long length() {
154 return writer.getNextIndex() - index();
158 * Acquires a reference to the log segment.
161 if (references.getAndIncrement() == 0 && open) {
167 * Releases a reference to the log segment.
170 if (references.decrementAndGet() == 0 && open) {
176 * Maps the log segment into memory.
179 if (storageLevel == StorageLevel.MAPPED) {
180 MappedByteBuffer buffer = writer.map();
181 readers.forEach(reader -> reader.map(buffer));
186 * Unmaps the log segment from memory.
188 private void unmap() {
189 if (storageLevel == StorageLevel.MAPPED) {
191 readers.forEach(reader -> reader.unmap());
196 * Returns the segment writer.
198 * @return The segment writer.
200 public MappableJournalSegmentWriter<E> writer() {
206 * Creates a new segment reader.
208 * @return A new segment reader.
210 MappableJournalSegmentReader<E> createReader() {
212 MappableJournalSegmentReader<E> reader = new MappableJournalSegmentReader<>(
213 openChannel(file.file()), this, maxEntrySize, index, namespace);
214 MappedByteBuffer buffer = writer.buffer();
215 if (buffer != null) {
223 * Closes a segment reader.
225 * @param reader the closed segment reader
227 void closeReader(MappableJournalSegmentReader<E> reader) {
228 readers.remove(reader);
232 * Checks whether the segment is open.
234 private void checkOpen() {
235 checkState(open, "Segment not open");
239 * Returns a boolean indicating whether the segment is open.
241 * @return indicates whether the segment is open
243 public boolean isOpen() {
248 * Closes the segment.
251 public void close() {
254 readers.forEach(reader -> reader.close());
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())