testCompact.h3文件生成方式
H2數據庫源碼自帶的測試類TestMVStoreTool
private void testCompact() {
String fileName = getBaseDir() + "/testCompact.h3";
FileUtils.createDirectories(getBaseDir());
//刪除舊的文件
FileUtils.delete(fileName);
// store with a very small page size, to make sure
// there are many leaf pages
MVStore s = new MVStore.Builder().
pageSplitSize(1000).
//調用MVStore的open方法
fileName(fileName).autoCommitDisabled().open();
MVMap<Integer, String> map = s.openMap("data");
for (int i = 0; i < 10; i++) {
map.put(i, "Hello World " + i * 10);
if (i % 3 == 0) {
s.commit();
}
}
public MVStore open() {
return new MVStore(config);
}
我們的主要目的是找到創建文件的方法其他的就不看了
// 19 KB memory is about 1 KB storage
autoCommitMemory = kb * 1024 * 19;
o = config.get("autoCompactFillRate");
autoCompactFillRate = o == null ? 50 : (Integer) o;
char[] encryptionKey = (char[]) config.get("encryptionKey");
try {
if (!fileStoreIsProvided) {
//調用了 fileStore的open方法
fileStore.open(fileName, readOnly, encryptionKey);
}
if (fileStore.size() == 0) {
creationTime = getTimeAbsolute();
lastCommitTime = creationTime;
storeHeader.put("H", 2);
storeHeader.put("blockSize", BLOCK_SIZE);
storeHeader.put("format", FORMAT_WRITE);
storeHeader.put("created", creationTime);
writeStoreHeader();
馬上要到了
public void open(String fileName, boolean readOnly, char[] encryptionKey) {
if (file != null) {
return;
}
if (fileName != null) {
// ensure the Cache file system is registered
FilePathCache.INSTANCE.getScheme();
FilePath p = FilePath.get(fileName);
// if no explicit scheme was specified, NIO is used
if (p instanceof FilePathDisk &&
!fileName.startsWith(p.getScheme() + ":")) {
// ensure the NIO file system is registered
//確保NIO文件系統已註冊
FilePathNio.class.getName();
fileName = "nio:" + fileName;
}
}
this.fileName = fileName;
FilePath f = FilePath.get(fileName);
FilePath parent = f.getParent();
if (parent != null && !parent.exists()) {
throw DataUtils.newIllegalArgumentException(
"Directory does not exist: {0}", parent);
}
if (f.exists() && !f.canWrite()) {
readOnly = true;
}
this.readOnly = readOnly;
try {
//真正要創建文件的地方
file = f.open(readOnly ? "r" : "rw");
if (encryptionKey != null) {
byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey);
encryptedFile = file;
file = new FilePathEncrypt.FileEncrypt(fileName, key, file);
}
好吧就是這裏了RandomAccessFile類關於他的解釋自己去找資料吧
FileNio(String fileName, String mode) throws IOException {
this.name = fileName;
//RandomAccessFile 專門處理文件的類
//創建隨機存儲文件流,文件屬性由參數File對象指定
channel = new RandomAccessFile(fileName, mode).getChannel();
}