THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MemoryMappedFileWriteExample {
private static String bigTextFile = "test.txt";
public static void main(String[] args) throws Exception
{
// Create file object
File file = new File(bigTextFile);
//Delete the file; we will create a new file
file.delete();
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"))
{
// Get file channel in read-write mode
FileChannel fileChannel = randomAccessFile.getChannel();
// Get direct byte buffer access using channel.map() operation
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 4096 * 8 * 8);
//Write the content using put methods
buffer.put("howtodoinjava.com".getBytes());
}
}
}
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MemoryMappedFileReadExample
{
private static String bigExcelFile = "bigFile.xls";
public static void main(String[] args) throws Exception
{
try (RandomAccessFile file = new RandomAccessFile(new File(bigExcelFile), "r"))
{
//Get file channel in read-only mode
FileChannel fileChannel = file.getChannel();
//Get direct byte buffer access using channel.map() operation
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
// the buffer now reads the file as if it were loaded in memory.
System.out.println(buffer.isLoaded()); //prints false
System.out.println(buffer.capacity()); //Get the size based on content size of file
//You can read the file from this buffer the way you like.
for (int i = 0; i < buffer.limit(); i++)
{
System.out.print((char) buffer.get()); //Print the content of file
}
}
}
}
https://howtodoinjava.com/java/nio/memory-mapped-files-mappedbytebuffer/
https://xunnanxu.github.io/2016/09/10/It-s-all-about-buffers-zero-copy-mmap-and-Java-NIO/
class NamespaceDetails {
DiskLoc firstExtent; // 第一个extent位置
DiskLoc lastExtent; // 最后一个extent位置
DiskLoc deletedListSmall[SmallBuckets];
// 不同大小的删除记录列表
...
};
class DiskLoc {
int _a; // 数据文件编号,如mydb.0编号为0
int ofs; // 文件内部偏移
};
class DataFileHeader {
DataFileVersion version;
int fileLength;
DiskLoc unused;
int unusedLength;
DiskLoc freeListStart;
DiskLoc freeListEnd;
char reserve[];
};
class DeletedRecord {
int _lengthWithHeaders; // record长度
int _extentOfs; // record所在的extent位置指针
DiskLoc _nextDeleted; // 下一个已删除记录的位置
};