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.MappedByteBuffer;
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;
29 import static com.google.common.base.MoreObjects.toStringHelper;
30 import static com.google.common.base.Preconditions.checkState;
35 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
37 final class JournalSegment<E> implements AutoCloseable {
38 private final JournalSegmentFile file;
39 private final JournalSegmentDescriptor descriptor;
40 private final StorageLevel storageLevel;
41 private final int maxEntrySize;
42 private final JournalIndex index;
43 private final JournalSerdes namespace;
44 private final MappableJournalSegmentWriter<E> writer;
45 private final Set<MappableJournalSegmentReader<E>> readers = ConcurrentHashMap.newKeySet();
46 private final AtomicInteger references = new AtomicInteger();
47 private final FileChannel channel;
48 private boolean open = true;
51 JournalSegmentFile file,
52 JournalSegmentDescriptor descriptor,
53 StorageLevel storageLevel,
56 JournalSerdes namespace) {
58 this.descriptor = descriptor;
59 this.storageLevel = storageLevel;
60 this.maxEntrySize = maxEntrySize;
61 this.namespace = namespace;
62 index = new SparseJournalIndex(indexDensity);
64 channel = FileChannel.open(file.file().toPath(),
65 StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
66 } catch (IOException e) {
67 throw new StorageException(e);
69 writer = new MappableJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
73 * Returns the segment ID.
75 * @return The segment ID.
78 return descriptor.id();
82 * Returns the segment version.
84 * @return The segment version.
86 public long version() {
87 return descriptor.version();
91 * Returns the segment's starting index.
93 * @return The segment's starting index.
96 return descriptor.index();
100 * Returns the last index in the segment.
102 * @return The last index in the segment.
104 public long lastIndex() {
105 return writer.getLastIndex();
109 * Returns the size of the segment.
111 * @return the size of the segment
115 return (int) channel.size();
116 } catch (IOException e) {
117 throw new StorageException(e);
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<>(channel, this, maxEntrySize, index,
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());
258 } catch (IOException e) {
259 throw new StorageException(e);
264 * Deletes the segment.
266 public void delete() {
268 Files.deleteIfExists(file.file().toPath());
269 } catch (IOException e) {
270 throw new StorageException(e);
275 public String toString() {
276 return toStringHelper(this)
278 .add("version", version())
279 .add("index", index())