Introduce JournalSegmentWriter
[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.io.IOException;
20 import java.nio.MappedByteBuffer;
21 import java.nio.channels.FileChannel;
22
23 /**
24  * Mappable log segment writer.
25  */
26 final class MappableJournalSegmentWriter<E> implements JournalWriter<E> {
27   private final FileChannel channel;
28   private final JournalSegment<E> segment;
29   private final int maxEntrySize;
30   private final JournalIndex index;
31   private final JournalSerdes namespace;
32   private JournalSegmentWriter<E> writer;
33
34   MappableJournalSegmentWriter(
35       FileChannel channel,
36       JournalSegment<E> segment,
37       int maxEntrySize,
38       JournalIndex index,
39       JournalSerdes namespace) {
40     this.channel = channel;
41     this.segment = segment;
42     this.maxEntrySize = maxEntrySize;
43     this.index = index;
44     this.namespace = namespace;
45     this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
46   }
47
48   /**
49    * Maps the segment writer into memory, returning the mapped buffer.
50    *
51    * @return the buffer that was mapped into memory
52    */
53   MappedByteBuffer map() {
54     if (writer instanceof MappedJournalSegmentWriter) {
55       return ((MappedJournalSegmentWriter<E>) writer).buffer();
56     }
57
58     try {
59       JournalSegmentWriter<E> writer = this.writer;
60       MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, segment.descriptor().maxSegmentSize());
61       this.writer = new MappedJournalSegmentWriter<>(buffer, segment, maxEntrySize, index, namespace);
62       writer.close();
63       return buffer;
64     } catch (IOException e) {
65       throw new StorageException(e);
66     }
67   }
68
69   /**
70    * Unmaps the mapped buffer.
71    */
72   void unmap() {
73     if (writer instanceof MappedJournalSegmentWriter) {
74       JournalSegmentWriter<E> writer = this.writer;
75       this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
76       writer.close();
77     }
78   }
79
80   MappedByteBuffer buffer() {
81     return writer.buffer();
82   }
83
84   /**
85    * Returns the writer's first index.
86    *
87    * @return the writer's first index
88    */
89   public long firstIndex() {
90     return segment.index();
91   }
92
93   /**
94    * Returns the size of the segment.
95    *
96    * @return the size of the segment
97    */
98   public int size() {
99     try {
100       return (int) channel.size();
101     } catch (IOException e) {
102       throw new StorageException(e);
103     }
104   }
105
106   @Override
107   public long getLastIndex() {
108     return writer.getLastIndex();
109   }
110
111   @Override
112   public Indexed<E> getLastEntry() {
113     return writer.getLastEntry();
114   }
115
116   @Override
117   public long getNextIndex() {
118     return writer.getNextIndex();
119   }
120
121   @Override
122   public <T extends E> Indexed<T> append(T entry) {
123     return writer.append(entry);
124   }
125
126   @Override
127   public void append(Indexed<E> entry) {
128     writer.append(entry);
129   }
130
131   @Override
132   public void commit(long index) {
133     writer.commit(index);
134   }
135
136   @Override
137   public void reset(long index) {
138     writer.reset(index);
139   }
140
141   @Override
142   public void truncate(long index) {
143     writer.truncate(index);
144   }
145
146   @Override
147   public void flush() {
148     writer.flush();
149   }
150
151   @Override
152   public void close() {
153     writer.close();
154     try {
155       channel.close();
156     } catch (IOException e) {
157       throw new StorageException(e);
158     }
159   }
160 }