| 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.rec; |
| 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 java.util.Arrays; |
| 13 |
import org.h2.store.fs.FileBase; |
| 14 |
import org.h2.store.fs.Recorder; |
| 15 |
|
| 16 |
/** |
| 17 |
* A file object that records all write operations and can re-play them. |
| 18 |
*/ |
| 19 |
class FileRec extends FileBase { |
| 20 |
|
| 21 |
private final FilePathRec rec; |
| 22 |
private final FileChannel channel; |
| 23 |
private final String name; |
| 24 |
|
| 25 |
FileRec(FilePathRec rec, FileChannel file, String fileName) { |
| 26 |
this.rec = rec; |
| 27 |
this.channel = file; |
| 28 |
this.name = fileName; |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public void implCloseChannel() throws IOException { |
| 33 |
channel.close(); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public long position() throws IOException { |
| 38 |
return channel.position(); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public long size() throws IOException { |
| 43 |
return channel.size(); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public int read(ByteBuffer dst) throws IOException { |
| 48 |
return channel.read(dst); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public int read(ByteBuffer dst, long position) throws IOException { |
| 53 |
return channel.read(dst, position); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public FileChannel position(long pos) throws IOException { |
| 58 |
channel.position(pos); |
| 59 |
return this; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public FileChannel truncate(long newLength) throws IOException { |
| 64 |
rec.log(Recorder.TRUNCATE, name, null, newLength); |
| 65 |
channel.truncate(newLength); |
| 66 |
return this; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void force(boolean metaData) throws IOException { |
| 71 |
channel.force(metaData); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public int write(ByteBuffer src) throws IOException { |
| 76 |
byte[] buff = src.array(); |
| 77 |
int len = src.remaining(); |
| 78 |
if (src.position() != 0 || len != buff.length) { |
| 79 |
int offset = src.arrayOffset() + src.position(); |
| 80 |
buff = Arrays.copyOfRange(buff, offset, offset + len); |
| 81 |
} |
| 82 |
int result = channel.write(src); |
| 83 |
rec.log(Recorder.WRITE, name, buff, channel.position()); |
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public int write(ByteBuffer src, long position) throws IOException { |
| 89 |
byte[] buff = src.array(); |
| 90 |
int len = src.remaining(); |
| 91 |
if (src.position() != 0 || len != buff.length) { |
| 92 |
int offset = src.arrayOffset() + src.position(); |
| 93 |
buff = Arrays.copyOfRange(buff, offset, offset + len); |
| 94 |
} |
| 95 |
int result = channel.write(src, position); |
| 96 |
rec.log(Recorder.WRITE, name, buff, position); |
| 97 |
return result; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public synchronized FileLock tryLock(long position, long size, |
| 102 |
boolean shared) throws IOException { |
| 103 |
return channel.tryLock(position, size, shared); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public String toString() { |
| 108 |
return name; |
| 109 |
} |
| 110 |
|
| 111 |
} |