代码分析:
一开始就认为该文件主要是为了事务的回滚,而生成日志文件记录下数据,但是没有找到任何的证明。
如下是函数的翻译:
当atomic-write 优化选项被启用的时候,SQLite会调用sqlite3_file对象创建日志文件。
sqlite3_file文件的显著特点是延迟写磁盘。当创建该文件之后,如果用户调用read或者
write函数,调用者会指定缓存区去缓存用户数据,并没有真正创建磁盘文件,除非如下的
条件发生:
1)缓存的数据量过大
2)调用sqlite3JournalCreate函数
/*
JournalFile对象是sqliqte3_file的子类,作为日志文件(journal files)的句柄
*/
struct JournalFile {
sqlite3_io_methods *pMethod; /* I/O methods on journal files */
int nBuf; /* Size of zBuf[] in bytes */
char *zBuf; /* Space to buffer journal writes */
int iSize; /* Amount of zBuf[] currently used */
int flags; /* xOpen flags */
sqlite3_vfs *pVfs; /* The "real" underlying VFS */
sqlite3_file *pReal; /* The "real" underlying file descriptor */
const char *zJournal; /* Name of the journal file */
};
typedef struct JournalFile JournalFile;
/*
如果不存在,为JournalFile对象p创建磁盘文件,然后填充数据。
*/
static int createFile(JournalFile *p)
/*
JournalFile对象的函数列表
*/
static struct sqlite3_io_methods JournalFileMethods = {
1, /* iVersion */
jrnlClose, /* xClose */
jrnlRead, /* xRead */
jrnlWrite, /* xWrite */
jrnlTruncate, /* xTruncate */
jrnlSync, /* xSync */
jrnlFileSize, /* xFileSize */
0, /* xLock */
0, /* xUnlock */
0, /* xCheckReservedLock */
0, /* xFileControl */
0, /* xSectorSize */
0, /* xDeviceCharacteristics */
0, /* xShmMap */
0, /* xShmLock */
0, /* xShmBarrier */
0 /* xShmUnmap */
};
/*
如果参数p指针指向一个JournalFile结构体,该结构体指向的文件尚未创建,
立即创建。
*/
int sqlite3JournalCreate(sqlite3_file *p)
/*
文件句柄作为唯一的参数传递进来确保了打开一个文件。她是否是JournalFile已经无关
紧要。如果是一个JournalFile,说明底层的磁盘文件尚未打开,返回0,否则返回1.
*/
int sqlite3JournalExists(sqlite3_file *p)
/*
返回pVfs创建底层磁盘文件需要的字节个数
*/
int sqlite3JournalSize(sqlite3_vfs *pVfs)