| 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.mvstore.cache; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.nio.ByteBuffer; |
| 10 |
import java.nio.channels.FileChannel; |
| 11 |
import java.nio.channels.FileLock; |
| 12 |
import org.h2.store.fs.FileBase; |
| 13 |
import org.h2.store.fs.FilePath; |
| 14 |
import org.h2.store.fs.FilePathWrapper; |
| 15 |
|
| 16 |
/** |
| 17 |
* A file with a read cache. |
| 18 |
*/ |
| 19 |
public class FilePathCache extends FilePathWrapper { |
| 20 |
|
| 21 |
/** |
| 22 |
* The instance. |
| 23 |
*/ |
| 24 |
public static final FilePathCache INSTANCE = new FilePathCache(); |
| 25 |
|
| 26 |
/** |
| 27 |
* Register the file system. |
| 28 |
*/ |
| 29 |
static { |
| 30 |
FilePath.register(INSTANCE); |
| 31 |
} |
| 32 |
|
| 33 |
public static FileChannel wrap(FileChannel f) { |
| 34 |
return new FileCache(f); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public FileChannel open(String mode) throws IOException { |
| 39 |
return new FileCache(getBase().open(mode)); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public String getScheme() { |
| 44 |
return "cache"; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* A file with a read cache. |
| 49 |
*/ |
| 50 |
public static class FileCache extends FileBase { |
| 51 |
|
| 52 |
private static final int CACHE_BLOCK_SIZE = 4 * 1024; |
| 53 |
private final FileChannel base; |
| 54 |
|
| 55 |
private final CacheLongKeyLIRS<ByteBuffer> cache; |
| 56 |
|
| 57 |
{ |
| 58 |
CacheLongKeyLIRS.Config cc = new CacheLongKeyLIRS.Config(); |
| 59 |
// 1 MB cache size |
| 60 |
cc.maxMemory = 1024 * 1024; |
| 61 |
cache = new CacheLongKeyLIRS<>(cc); |
| 62 |
} |
| 63 |
|
| 64 |
FileCache(FileChannel base) { |
| 65 |
this.base = base; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
protected void implCloseChannel() throws IOException { |
| 70 |
base.close(); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public FileChannel position(long newPosition) throws IOException { |
| 75 |
base.position(newPosition); |
| 76 |
return this; |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public long position() throws IOException { |
| 81 |
return base.position(); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public int read(ByteBuffer dst) throws IOException { |
| 86 |
return base.read(dst); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public synchronized int read(ByteBuffer dst, long position) throws IOException { |
| 91 |
long cachePos = getCachePos(position); |
| 92 |
int off = (int) (position - cachePos); |
| 93 |
int len = CACHE_BLOCK_SIZE - off; |
| 94 |
len = Math.min(len, dst.remaining()); |
| 95 |
ByteBuffer buff = cache.get(cachePos); |
| 96 |
if (buff == null) { |
| 97 |
buff = ByteBuffer.allocate(CACHE_BLOCK_SIZE); |
| 98 |
long pos = cachePos; |
| 99 |
while (true) { |
| 100 |
int read = base.read(buff, pos); |
| 101 |
if (read <= 0) { |
| 102 |
break; |
| 103 |
} |
| 104 |
if (buff.remaining() == 0) { |
| 105 |
break; |
| 106 |
} |
| 107 |
pos += read; |
| 108 |
} |
| 109 |
int read = buff.position(); |
| 110 |
if (read == CACHE_BLOCK_SIZE) { |
| 111 |
cache.put(cachePos, buff, CACHE_BLOCK_SIZE + 80); |
| 112 |
} else { |
| 113 |
if (read <= 0) { |
| 114 |
return -1; |
| 115 |
} |
| 116 |
len = Math.min(len, read - off); |
| 117 |
} |
| 118 |
} |
| 119 |
dst.put(buff.array(), off, len); |
| 120 |
return len == 0 ? -1 : len; |
| 121 |
} |
| 122 |
|
| 123 |
private static long getCachePos(long pos) { |
| 124 |
return (pos / CACHE_BLOCK_SIZE) * CACHE_BLOCK_SIZE; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public long size() throws IOException { |
| 129 |
return base.size(); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public synchronized FileChannel truncate(long newSize) throws IOException { |
| 134 |
cache.clear(); |
| 135 |
base.truncate(newSize); |
| 136 |
return this; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public synchronized int write(ByteBuffer src, long position) throws IOException { |
| 141 |
clearCache(src, position); |
| 142 |
return base.write(src, position); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public synchronized int write(ByteBuffer src) throws IOException { |
| 147 |
clearCache(src, position()); |
| 148 |
return base.write(src); |
| 149 |
} |
| 150 |
|
| 151 |
private void clearCache(ByteBuffer src, long position) { |
| 152 |
if (cache.size() > 0) { |
| 153 |
int len = src.remaining(); |
| 154 |
long p = getCachePos(position); |
| 155 |
while (len > 0) { |
| 156 |
cache.remove(p); |
| 157 |
p += CACHE_BLOCK_SIZE; |
| 158 |
len -= CACHE_BLOCK_SIZE; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public void force(boolean metaData) throws IOException { |
| 165 |
base.force(metaData); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public FileLock tryLock(long position, long size, boolean shared) |
| 170 |
throws IOException { |
| 171 |
return base.tryLock(position, size, shared); |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public String toString() { |
| 176 |
return "cache:" + base.toString(); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
} |