Move JournalSegmentWriter switchover logic
[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 JournalSegmentWriter<E> writer;
30
31   MappableJournalSegmentWriter(
32       FileChannel channel,
33       JournalSegment<E> segment,
34       int maxEntrySize,
35       JournalIndex index,
36       JournalSerdes namespace) {
37     this.channel = channel;
38     this.segment = segment;
39     this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
40   }
41
42   /**
43    * Maps the segment writer into memory, returning the mapped buffer.
44    *
45    * @return the buffer that was mapped into memory
46    */
47   MappedByteBuffer map() {
48     final var mapped = writer.toMapped();
49     writer = mapped;
50     return mapped.buffer();
51   }
52
53   /**
54    * Unmaps the mapped buffer.
55    */
56   void unmap() {
57     writer = writer.toFileChannel();
58   }
59
60   MappedByteBuffer buffer() {
61     return writer.buffer();
62   }
63
64   /**
65    * Returns the writer's first index.
66    *
67    * @return the writer's first index
68    */
69   public long firstIndex() {
70     return segment.index();
71   }
72
73   /**
74    * Returns the size of the segment.
75    *
76    * @return the size of the segment
77    */
78   public int size() {
79     try {
80       return (int) channel.size();
81     } catch (IOException e) {
82       throw new StorageException(e);
83     }
84   }
85
86   @Override
87   public long getLastIndex() {
88     return writer.getLastIndex();
89   }
90
91   @Override
92   public Indexed<E> getLastEntry() {
93     return writer.getLastEntry();
94   }
95
96   @Override
97   public long getNextIndex() {
98     return writer.getNextIndex();
99   }
100
101   @Override
102   public <T extends E> Indexed<T> append(T entry) {
103     return writer.append(entry);
104   }
105
106   @Override
107   public void append(Indexed<E> entry) {
108     writer.append(entry);
109   }
110
111   @Override
112   public void commit(long index) {
113     writer.commit(index);
114   }
115
116   @Override
117   public void reset(long index) {
118     writer.reset(index);
119   }
120
121   @Override
122   public void truncate(long index) {
123     writer.truncate(index);
124   }
125
126   @Override
127   public void flush() {
128     writer.flush();
129   }
130
131   @Override
132   public void close() {
133     writer.close();
134     try {
135       channel.close();
136     } catch (IOException e) {
137       throw new StorageException(e);
138     }
139   }
140 }