| 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.mem; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.nio.channels.NonWritableChannelException; |
| 10 |
import java.util.Arrays; |
| 11 |
import java.util.LinkedHashMap; |
| 12 |
import java.util.Map; |
| 13 |
import java.util.concurrent.atomic.AtomicReference; |
| 14 |
import org.h2.compress.CompressLZF; |
| 15 |
import org.h2.util.MathUtils; |
| 16 |
|
| 17 |
/** |
| 18 |
* This class contains the data of an in-memory random access file. |
| 19 |
* Data compression using the LZF algorithm is supported as well. |
| 20 |
*/ |
| 21 |
class FileMemData { |
| 22 |
|
| 23 |
private static final int CACHE_SIZE = 8; |
| 24 |
private static final int BLOCK_SIZE_SHIFT = 10; |
| 25 |
private static final int BLOCK_SIZE = 1 << BLOCK_SIZE_SHIFT; |
| 26 |
private static final int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; |
| 27 |
private static final CompressLZF LZF = new CompressLZF(); |
| 28 |
private static final byte[] BUFFER = new byte[BLOCK_SIZE * 2]; |
| 29 |
private static final byte[] COMPRESSED_EMPTY_BLOCK; |
| 30 |
|
| 31 |
private static final Cache<CompressItem, CompressItem> COMPRESS_LATER = |
| 32 |
new Cache<>(CACHE_SIZE); |
| 33 |
|
| 34 |
private String name; |
| 35 |
private final int id; |
| 36 |
private final boolean compress; |
| 37 |
private volatile long length; |
| 38 |
private AtomicReference<byte[]>[] data; |
| 39 |
private long lastModified; |
| 40 |
private boolean isReadOnly; |
| 41 |
private boolean isLockedExclusive; |
| 42 |
private int sharedLockCount; |
| 43 |
|
| 44 |
static { |
| 45 |
byte[] n = new byte[BLOCK_SIZE]; |
| 46 |
int len = LZF.compress(n, 0, BLOCK_SIZE, BUFFER, 0); |
| 47 |
COMPRESSED_EMPTY_BLOCK = Arrays.copyOf(BUFFER, len); |
| 48 |
} |
| 49 |
|
| 50 |
@SuppressWarnings("unchecked") |
| 51 |
FileMemData(String name, boolean compress) { |
| 52 |
this.name = name; |
| 53 |
this.id = name.hashCode(); |
| 54 |
this.compress = compress; |
| 55 |
this.data = new AtomicReference[0]; |
| 56 |
lastModified = System.currentTimeMillis(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the page if it exists. |
| 61 |
* |
| 62 |
* @param page the page id |
| 63 |
* @return the byte array, or null |
| 64 |
*/ |
| 65 |
private byte[] getPage(int page) { |
| 66 |
AtomicReference<byte[]>[] b = data; |
| 67 |
if (page >= b.length) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
return b[page].get(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Set the page data. |
| 75 |
* |
| 76 |
* @param page the page id |
| 77 |
* @param oldData the old data |
| 78 |
* @param newData the new data |
| 79 |
* @param force whether the data should be overwritten even if the old data |
| 80 |
* doesn't match |
| 81 |
*/ |
| 82 |
private void setPage(int page, byte[] oldData, byte[] newData, boolean force) { |
| 83 |
AtomicReference<byte[]>[] b = data; |
| 84 |
if (page >= b.length) { |
| 85 |
return; |
| 86 |
} |
| 87 |
if (force) { |
| 88 |
b[page].set(newData); |
| 89 |
} else { |
| 90 |
b[page].compareAndSet(oldData, newData); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
int getId() { |
| 95 |
return id; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Lock the file in exclusive mode if possible. |
| 100 |
* |
| 101 |
* @return if locking was successful |
| 102 |
*/ |
| 103 |
synchronized boolean lockExclusive() { |
| 104 |
if (sharedLockCount > 0 || isLockedExclusive) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
isLockedExclusive = true; |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Lock the file in shared mode if possible. |
| 113 |
* |
| 114 |
* @return if locking was successful |
| 115 |
*/ |
| 116 |
synchronized boolean lockShared() { |
| 117 |
if (isLockedExclusive) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
sharedLockCount++; |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Unlock the file. |
| 126 |
*/ |
| 127 |
synchronized void unlock() throws IOException { |
| 128 |
if (isLockedExclusive) { |
| 129 |
isLockedExclusive = false; |
| 130 |
} else if (sharedLockCount > 0) { |
| 131 |
sharedLockCount--; |
| 132 |
} else { |
| 133 |
throw new IOException("not locked"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* This small cache compresses the data if an element leaves the cache. |
| 139 |
*/ |
| 140 |
static class Cache<K, V> extends LinkedHashMap<K, V> { |
| 141 |
|
| 142 |
private static final long serialVersionUID = 1L; |
| 143 |
private final int size; |
| 144 |
|
| 145 |
Cache(int size) { |
| 146 |
super(size, (float) 0.75, true); |
| 147 |
this.size = size; |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public synchronized V put(K key, V value) { |
| 152 |
return super.put(key, value); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { |
| 157 |
if (size() < size) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
CompressItem c = (CompressItem) eldest.getKey(); |
| 161 |
c.file.compress(c.page); |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Points to a block of bytes that needs to be compressed. |
| 168 |
*/ |
| 169 |
static class CompressItem { |
| 170 |
|
| 171 |
/** |
| 172 |
* The file. |
| 173 |
*/ |
| 174 |
FileMemData file; |
| 175 |
|
| 176 |
/** |
| 177 |
* The page to compress. |
| 178 |
*/ |
| 179 |
int page; |
| 180 |
|
| 181 |
@Override |
| 182 |
public int hashCode() { |
| 183 |
return page ^ file.getId(); |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public boolean equals(Object o) { |
| 188 |
if (o instanceof CompressItem) { |
| 189 |
CompressItem c = (CompressItem) o; |
| 190 |
return c.page == page && c.file == file; |
| 191 |
} |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
private void compressLater(int page) { |
| 198 |
CompressItem c = new CompressItem(); |
| 199 |
c.file = this; |
| 200 |
c.page = page; |
| 201 |
synchronized (LZF) { |
| 202 |
COMPRESS_LATER.put(c, c); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private byte[] expand(int page) { |
| 207 |
byte[] d = getPage(page); |
| 208 |
if (d.length == BLOCK_SIZE) { |
| 209 |
return d; |
| 210 |
} |
| 211 |
byte[] out = new byte[BLOCK_SIZE]; |
| 212 |
if (d != COMPRESSED_EMPTY_BLOCK) { |
| 213 |
synchronized (LZF) { |
| 214 |
LZF.expand(d, 0, d.length, out, 0, BLOCK_SIZE); |
| 215 |
} |
| 216 |
} |
| 217 |
setPage(page, d, out, false); |
| 218 |
return out; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Compress the data in a byte array. |
| 223 |
* |
| 224 |
* @param page which page to compress |
| 225 |
*/ |
| 226 |
void compress(int page) { |
| 227 |
byte[] old = getPage(page); |
| 228 |
if (old == null || old.length != BLOCK_SIZE) { |
| 229 |
// not yet initialized or already compressed |
| 230 |
return; |
| 231 |
} |
| 232 |
synchronized (LZF) { |
| 233 |
int len = LZF.compress(old, 0, BLOCK_SIZE, BUFFER, 0); |
| 234 |
if (len <= BLOCK_SIZE) { |
| 235 |
byte[] d = Arrays.copyOf(BUFFER, len); |
| 236 |
// maybe data was changed in the meantime |
| 237 |
setPage(page, old, d, false); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Update the last modified time. |
| 244 |
* |
| 245 |
* @param openReadOnly if the file was opened in read-only mode |
| 246 |
*/ |
| 247 |
void touch(boolean openReadOnly) { |
| 248 |
if (isReadOnly || openReadOnly) { |
| 249 |
throw new NonWritableChannelException(); |
| 250 |
} |
| 251 |
lastModified = System.currentTimeMillis(); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Get the file length. |
| 256 |
* |
| 257 |
* @return the length |
| 258 |
*/ |
| 259 |
long length() { |
| 260 |
return length; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Truncate the file. |
| 265 |
* |
| 266 |
* @param newLength the new length |
| 267 |
*/ |
| 268 |
void truncate(long newLength) { |
| 269 |
changeLength(newLength); |
| 270 |
long end = MathUtils.roundUpLong(newLength, BLOCK_SIZE); |
| 271 |
if (end != newLength) { |
| 272 |
int lastPage = (int) (newLength >>> BLOCK_SIZE_SHIFT); |
| 273 |
byte[] d = expand(lastPage); |
| 274 |
byte[] d2 = Arrays.copyOf(d, d.length); |
| 275 |
for (int i = (int) (newLength & BLOCK_SIZE_MASK); i < BLOCK_SIZE; i++) { |
| 276 |
d2[i] = 0; |
| 277 |
} |
| 278 |
setPage(lastPage, d, d2, true); |
| 279 |
if (compress) { |
| 280 |
compressLater(lastPage); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
private void changeLength(long len) { |
| 286 |
length = len; |
| 287 |
len = MathUtils.roundUpLong(len, BLOCK_SIZE); |
| 288 |
int blocks = (int) (len >>> BLOCK_SIZE_SHIFT); |
| 289 |
if (blocks != data.length) { |
| 290 |
AtomicReference<byte[]>[] n = Arrays.copyOf(data, blocks); |
| 291 |
for (int i = data.length; i < blocks; i++) { |
| 292 |
n[i] = new AtomicReference<>(COMPRESSED_EMPTY_BLOCK); |
| 293 |
} |
| 294 |
data = n; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Read or write. |
| 300 |
* |
| 301 |
* @param pos the position |
| 302 |
* @param b the byte array |
| 303 |
* @param off the offset within the byte array |
| 304 |
* @param len the number of bytes |
| 305 |
* @param write true for writing |
| 306 |
* @return the new position |
| 307 |
*/ |
| 308 |
long readWrite(long pos, byte[] b, int off, int len, boolean write) { |
| 309 |
long end = pos + len; |
| 310 |
if (end > length) { |
| 311 |
if (write) { |
| 312 |
changeLength(end); |
| 313 |
} else { |
| 314 |
len = (int) (length - pos); |
| 315 |
} |
| 316 |
} |
| 317 |
while (len > 0) { |
| 318 |
int l = (int) Math.min(len, BLOCK_SIZE - (pos & BLOCK_SIZE_MASK)); |
| 319 |
int page = (int) (pos >>> BLOCK_SIZE_SHIFT); |
| 320 |
byte[] block = expand(page); |
| 321 |
int blockOffset = (int) (pos & BLOCK_SIZE_MASK); |
| 322 |
if (write) { |
| 323 |
byte[] p2 = Arrays.copyOf(block, block.length); |
| 324 |
System.arraycopy(b, off, p2, blockOffset, l); |
| 325 |
setPage(page, block, p2, true); |
| 326 |
} else { |
| 327 |
System.arraycopy(block, blockOffset, b, off, l); |
| 328 |
} |
| 329 |
if (compress) { |
| 330 |
compressLater(page); |
| 331 |
} |
| 332 |
off += l; |
| 333 |
pos += l; |
| 334 |
len -= l; |
| 335 |
} |
| 336 |
return pos; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Set the file name. |
| 341 |
* |
| 342 |
* @param name the name |
| 343 |
*/ |
| 344 |
void setName(String name) { |
| 345 |
this.name = name; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Get the file name |
| 350 |
* |
| 351 |
* @return the name |
| 352 |
*/ |
| 353 |
String getName() { |
| 354 |
return name; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Get the last modified time. |
| 359 |
* |
| 360 |
* @return the time |
| 361 |
*/ |
| 362 |
long getLastModified() { |
| 363 |
return lastModified; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Check whether writing is allowed. |
| 368 |
* |
| 369 |
* @return true if it is |
| 370 |
*/ |
| 371 |
boolean canWrite() { |
| 372 |
return !isReadOnly; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Set the read-only flag. |
| 377 |
* |
| 378 |
* @return true |
| 379 |
*/ |
| 380 |
boolean setReadOnly() { |
| 381 |
isReadOnly = true; |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
} |