| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store.fs.niomapped; |
| 7 |
|
| 8 |
import java.io.EOFException; |
| 9 |
import java.io.IOException; |
| 10 |
import java.lang.ref.WeakReference; |
| 11 |
import java.nio.BufferUnderflowException; |
| 12 |
import java.nio.ByteBuffer; |
| 13 |
import java.nio.MappedByteBuffer; |
| 14 |
import java.nio.channels.FileChannel; |
| 15 |
import java.nio.channels.FileLock; |
| 16 |
import java.nio.channels.NonWritableChannelException; |
| 17 |
import java.nio.file.Paths; |
| 18 |
import org.h2.engine.SysProperties; |
| 19 |
import org.h2.store.fs.FileBaseDefault; |
| 20 |
import org.h2.store.fs.FileUtils; |
| 21 |
import org.h2.util.MemoryUnmapper; |
| 22 |
|
| 23 |
/** |
| 24 |
* Uses memory mapped files. |
| 25 |
* The file size is limited to 2 GB. |
| 26 |
*/ |
| 27 |
class FileNioMapped extends FileBaseDefault { |
| 28 |
|
| 29 |
private static final int GC_TIMEOUT_MS = 10_000; |
| 30 |
private final String name; |
| 31 |
private final MapMode mode; |
| 32 |
private FileChannel channel; |
| 33 |
private MappedByteBuffer mapped; |
| 34 |
private long fileLength; |
| 35 |
|
| 36 |
FileNioMapped(String fileName, String mode) throws IOException { |
| 37 |
if ("r".equals(mode)) { |
| 38 |
this.mode = MapMode.READ_ONLY; |
| 39 |
} else { |
| 40 |
this.mode = MapMode.READ_WRITE; |
| 41 |
} |
| 42 |
this.name = fileName; |
| 43 |
channel = FileChannel.open(Paths.get(fileName), FileUtils.modeToOptions(mode), FileUtils.NO_ATTRIBUTES); |
| 44 |
reMap(); |
| 45 |
} |
| 46 |
|
| 47 |
private void unMap() throws IOException { |
| 48 |
if (mapped == null) { |
| 49 |
return; |
| 50 |
} |
| 51 |
// first write all data |
| 52 |
mapped.force(); |
| 53 |
|
| 54 |
// need to dispose old direct buffer, see bug |
| 55 |
// https://bugs.openjdk.java.net/browse/JDK-4724038 |
| 56 |
|
| 57 |
if (SysProperties.NIO_CLEANER_HACK) { |
| 58 |
if (MemoryUnmapper.unmap(mapped)) { |
| 59 |
mapped = null; |
| 60 |
return; |
| 61 |
} |
| 62 |
} |
| 63 |
WeakReference<MappedByteBuffer> bufferWeakRef = new WeakReference<>(mapped); |
| 64 |
mapped = null; |
| 65 |
long stopAt = System.nanoTime() + GC_TIMEOUT_MS * 1_000_000L; |
| 66 |
while (bufferWeakRef.get() != null) { |
| 67 |
if (System.nanoTime() - stopAt > 0L) { |
| 68 |
throw new IOException("Timeout (" + GC_TIMEOUT_MS + " ms) reached while trying to GC mapped buffer"); |
| 69 |
} |
| 70 |
System.gc(); |
| 71 |
Thread.yield(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Re-map byte buffer into memory, called when file size has changed or file |
| 77 |
* was created. |
| 78 |
*/ |
| 79 |
private void reMap() throws IOException { |
| 80 |
if (mapped != null) { |
| 81 |
unMap(); |
| 82 |
} |
| 83 |
fileLength = channel.size(); |
| 84 |
checkFileSizeLimit(fileLength); |
| 85 |
// maps new MappedByteBuffer; the old one is disposed during GC |
| 86 |
mapped = channel.map(mode, 0, fileLength); |
| 87 |
int limit = mapped.limit(); |
| 88 |
int capacity = mapped.capacity(); |
| 89 |
if (limit < fileLength || capacity < fileLength) { |
| 90 |
throw new IOException("Unable to map: length=" + limit + |
| 91 |
" capacity=" + capacity + " length=" + fileLength); |
| 92 |
} |
| 93 |
if (SysProperties.NIO_LOAD_MAPPED) { |
| 94 |
mapped.load(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private static void checkFileSizeLimit(long length) throws IOException { |
| 99 |
if (length > Integer.MAX_VALUE) { |
| 100 |
throw new IOException( |
| 101 |
"File over 2GB is not supported yet when using this file system"); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void implCloseChannel() throws IOException { |
| 107 |
if (channel != null) { |
| 108 |
unMap(); |
| 109 |
channel.close(); |
| 110 |
channel = null; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public String toString() { |
| 116 |
return "nioMapped:" + name; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public synchronized long size() throws IOException { |
| 121 |
return fileLength; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public synchronized int read(ByteBuffer dst, long pos) throws IOException { |
| 126 |
checkFileSizeLimit(pos); |
| 127 |
try { |
| 128 |
int len = dst.remaining(); |
| 129 |
if (len == 0) { |
| 130 |
return 0; |
| 131 |
} |
| 132 |
len = (int) Math.min(len, fileLength - pos); |
| 133 |
if (len <= 0) { |
| 134 |
return -1; |
| 135 |
} |
| 136 |
mapped.position((int)pos); |
| 137 |
mapped.get(dst.array(), dst.arrayOffset() + dst.position(), len); |
| 138 |
dst.position(dst.position() + len); |
| 139 |
pos += len; |
| 140 |
return len; |
| 141 |
} catch (IllegalArgumentException | BufferUnderflowException e) { |
| 142 |
EOFException e2 = new EOFException("EOF"); |
| 143 |
e2.initCause(e); |
| 144 |
throw e2; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
protected void implTruncate(long newLength) throws IOException { |
| 150 |
// compatibility with JDK FileChannel#truncate |
| 151 |
if (mode == MapMode.READ_ONLY) { |
| 152 |
throw new NonWritableChannelException(); |
| 153 |
} |
| 154 |
if (newLength < size()) { |
| 155 |
setFileLength(newLength); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public synchronized void setFileLength(long newLength) throws IOException { |
| 160 |
if (mode == MapMode.READ_ONLY) { |
| 161 |
throw new NonWritableChannelException(); |
| 162 |
} |
| 163 |
checkFileSizeLimit(newLength); |
| 164 |
unMap(); |
| 165 |
for (int i = 0;; i++) { |
| 166 |
try { |
| 167 |
long length = channel.size(); |
| 168 |
if (length >= newLength) { |
| 169 |
channel.truncate(newLength); |
| 170 |
} else { |
| 171 |
channel.write(ByteBuffer.wrap(new byte[1]), newLength - 1); |
| 172 |
} |
| 173 |
break; |
| 174 |
} catch (IOException e) { |
| 175 |
if (i > 16 || !e.toString().contains("user-mapped section open")) { |
| 176 |
throw e; |
| 177 |
} |
| 178 |
} |
| 179 |
System.gc(); |
| 180 |
} |
| 181 |
reMap(); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void force(boolean metaData) throws IOException { |
| 186 |
mapped.force(); |
| 187 |
channel.force(metaData); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public synchronized int write(ByteBuffer src, long position) throws IOException { |
| 192 |
checkFileSizeLimit(position); |
| 193 |
int len = src.remaining(); |
| 194 |
// check if need to expand file |
| 195 |
if (mapped.capacity() < position + len) { |
| 196 |
setFileLength(position + len); |
| 197 |
} |
| 198 |
mapped.position((int)position); |
| 199 |
mapped.put(src); |
| 200 |
return len; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public synchronized FileLock tryLock(long position, long size, |
| 205 |
boolean shared) throws IOException { |
| 206 |
return channel.tryLock(position, size, shared); |
| 207 |
} |
| 208 |
|
| 209 |
} |