2 * Copyright 2015-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.buffer;
18 import io.atomix.utils.AtomixIOException;
19 import io.atomix.utils.memory.BufferCleaner;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 import java.io.IOException;
25 import java.io.RandomAccessFile;
26 import java.nio.ByteBuffer;
27 import java.nio.MappedByteBuffer;
28 import java.nio.channels.FileChannel;
29 import java.nio.file.Files;
32 * {@link ByteBuffer} based mapped bytes.
34 public class MappedBytes extends ByteBufferBytes {
36 private static final Logger LOGGER = LoggerFactory.getLogger(MappedBytes.class);
39 * Allocates a mapped buffer in {@link FileChannel.MapMode#READ_WRITE} mode.
41 * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code count} and mapping the
42 * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}.
44 * @param file The file to map into memory. If the file doesn't exist it will be automatically created.
45 * @param size The count of the buffer to allocate (in bytes).
46 * @return The mapped buffer.
47 * @throws NullPointerException If {@code file} is {@code null}
48 * @throws IllegalArgumentException If {@code count} is greater than {@link MappedBytes#MAX_SIZE}
49 * @see #allocate(File, FileChannel.MapMode, int)
51 public static MappedBytes allocate(File file, int size) {
52 return allocate(file, FileChannel.MapMode.READ_WRITE, size);
56 * Allocates a mapped buffer.
58 * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code count} and mapping the
59 * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}.
61 * @param file The file to map into memory. If the file doesn't exist it will be automatically created.
62 * @param mode The mode with which to map the file.
63 * @param size The count of the buffer to allocate (in bytes).
64 * @return The mapped buffer.
65 * @throws NullPointerException If {@code file} is {@code null}
66 * @throws IllegalArgumentException If {@code count} is greater than {@link Integer#MAX_VALUE}
67 * @see #allocate(File, int)
69 public static MappedBytes allocate(File file, FileChannel.MapMode mode, int size) {
70 return FileBytes.allocate(file, size).map(0, size, mode);
73 private final File file;
74 private final RandomAccessFile randomAccessFile;
75 private final FileChannel.MapMode mode;
77 protected MappedBytes(File file, RandomAccessFile randomAccessFile, MappedByteBuffer buffer, FileChannel.MapMode mode) {
80 this.randomAccessFile = randomAccessFile;
85 protected ByteBuffer newByteBuffer(int size) {
87 return randomAccessFile.getChannel().map(mode, 0, size);
88 } catch (IOException e) {
89 throw new AtomixIOException(e);
94 public boolean isDirect() {
99 public Bytes flush() {
100 ((MappedByteBuffer) buffer).force();
105 public void close() {
107 BufferCleaner.freeBuffer(buffer);
108 } catch (Exception e) {
109 if (LOGGER.isDebugEnabled()) {
110 LOGGER.debug("Failed to unmap direct buffer", e);
114 randomAccessFile.close();
115 } catch (IOException e) {
116 throw new RuntimeException(e);
122 * Deletes the underlying file.
124 public void delete() {
127 Files.delete(file.toPath());
128 } catch (IOException e) {
129 throw new RuntimeException(e);
133 private static String parseMode(FileChannel.MapMode mode) {
134 if (mode == FileChannel.MapMode.READ_ONLY) {
136 } else if (mode == FileChannel.MapMode.READ_WRITE) {
139 throw new IllegalArgumentException("unsupported map mode");