014ff63a6ac30f32ca1f8c48aa7cb0fb328ca056
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappableJournalSegmentWriter.java
1 /*
2  * Copyright 2018-present Open Networking Foundation
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.atomix.storage.journal;
17
18 import io.atomix.storage.journal.index.JournalIndex;
19 import java.nio.MappedByteBuffer;
20 import java.nio.channels.FileChannel;
21
22 /**
23  * Mappable log segment writer.
24  */
25 final class MappableJournalSegmentWriter<E> implements JournalWriter<E> {
26   private final JournalSegment<E> segment;
27   private JournalSegmentWriter<E> writer;
28
29   MappableJournalSegmentWriter(
30       FileChannel channel,
31       JournalSegment<E> segment,
32       int maxEntrySize,
33       JournalIndex index,
34       JournalSerdes namespace) {
35     this.segment = segment;
36     this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
37   }
38
39   /**
40    * Maps the segment writer into memory, returning the mapped buffer.
41    *
42    * @return the buffer that was mapped into memory
43    */
44   MappedByteBuffer map() {
45     final var mapped = writer.toMapped();
46     writer = mapped;
47     return mapped.buffer();
48   }
49
50   /**
51    * Unmaps the mapped buffer.
52    */
53   void unmap() {
54     writer = writer.toFileChannel();
55   }
56
57   MappedByteBuffer buffer() {
58     return writer.buffer();
59   }
60
61   /**
62    * Returns the writer's first index.
63    *
64    * @return the writer's first index
65    */
66   public long firstIndex() {
67     return segment.index();
68   }
69
70   @Override
71   public long getLastIndex() {
72     return writer.getLastIndex();
73   }
74
75   @Override
76   public Indexed<E> getLastEntry() {
77     return writer.getLastEntry();
78   }
79
80   @Override
81   public long getNextIndex() {
82     return writer.getNextIndex();
83   }
84
85   @Override
86   public <T extends E> Indexed<T> append(T entry) {
87     return writer.append(entry);
88   }
89
90   @Override
91   public void append(Indexed<E> entry) {
92     writer.append(entry);
93   }
94
95   @Override
96   public void commit(long index) {
97     writer.commit(index);
98   }
99
100   @Override
101   public void reset(long index) {
102     writer.reset(index);
103   }
104
105   @Override
106   public void truncate(long index) {
107     writer.truncate(index);
108   }
109
110   @Override
111   public void flush() {
112     writer.flush();
113   }
114
115   @Override
116   public void close() {
117     writer.close();
118   }
119 }