📄 fileio.html
字号:
<h2 id=fs_performance>Performance Related Assumptions</h2> <p> SQLite uses the assumptions in this section to try to speed up reading from and writing to the database file.<p class=req id=A21010> It is assumed that writing a series of sequential blocks of data to a file in order is faster than writing the same blocks in an arbitrary order.</p> <h2 id=fs_characteristics>System Failure Related Assumptions</h2> <p> In the event of an operating system or power failure, the various combinations of file-system software and storage hardware available provide varying levels of guarantee as to the integrity of the data written to the file system just before or during the failure. The exact combination of IO operations that SQLite is required to perform in order to safely modify a database file depend on the exact characteristics of the target platform. <p> This section describes the assumptions that SQLite makes about the the content of a file-system following a power or system failure. In other words, it describes the extent of file and file-system corruption that such an event may cause. <p> SQLite queries an implementation for file-system characteristics using the xDeviceCharacteristics() and xSectorSize() methods of the database file file-handle. These two methods are only ever called on file-handles open on database files. They are not called for <i>journal files</i>, <i>master-journal files</i> or <i>temporary database files</i>. <p> The file-system <i>sector size</i> value determined by calling the xSectorSize() method is a power of 2 value between 512 and 32768, inclusive <span class=todo>reference to exactly how this is determined</span>. SQLite assumes that the underlying storage device stores data in blocks of <i>sector-size</i> bytes each, sectors. It is also assumed that each aligned block of <i>sector-size</i> bytes of each file is stored in a single device sector. If the file is not an exact multiple of <i>sector-size</i> bytes in size, then the final device sector is partially empty. <p> Normally, SQLite assumes that if a power failure occurs while updating any portion of a sector then the contents of the entire device sector is suspect following recovery. After writing to any part of a sector within a file, it is assumed that the modified sector contents are held in a volatile buffer somewhere within the system (main memory, disk cache etc.). SQLite does not assume that the updated data has reached the persistent storage media, until after it has successfully <i>synced</i> the corresponding file by invoking the VFS xSync() method. <i>Syncing</i> a file causes all modifications to the file up until that point to be committed to persistent storage. <p> Based on the above, SQLite is designed around a model of the file-system whereby any sector of a file written to is considered to be in a transient state until after the file has been successfully <i>synced</i>. Should a power or system failure occur while a sector is in a transient state, it is impossible to predict its contents following recovery. It may be written correctly, not written at all, overwritten with random data, or any combination thereof. <p> For example, if the <i>sector-size</i> of a given file-system is 2048 bytes, and SQLite opens a file and writes a 1024 byte block of data to offset 3072 of the file, then according to the model the second sector of the file is in the transient state. If a power failure or operating system crash occurs before or during the next call to xSync() on the file handle, then following system recovery SQLite assumes that all file data between byte offsets 2048 and 4095, inclusive, is invalid. It also assumes that since the first sector of the file, containing the data from byte offset 0 to 2047 inclusive, is valid, since it was not in a transient state when the crash occured. <p> Assuming that any and all sectors in the transient state may be corrupted following a power or system failure is a very pessimistic approach. Some modern systems provide more sophisticated guarantees than this. SQLite allows the VFS implementation to specify at runtime that the current platform supports zero or more of the following properties: <ul> <li><p>The <b>safe-append</b> property. If a system supports the <i>safe-append</i> property, it means that when a file is extended the new data is written to the persistent media before the size of the file itself is updated. This guarantees that if a failure occurs after a file has been extended, following recovery the write operations that extended the file will appear to have succeeded or not occurred at all. It is not possible for invalid or garbage data to appear in the extended region of the file. <li><p>The <b>atomic-write</b> property. A system that supports this property also specifies the size or sizes of the blocks that it is capable of writing. Valid sizes are powers of two greater than 512. If a write operation modifies a block of <i>n</i> bytes, where <i>n</i> is one of the block sizes for which <i>atomic-write</i> is supported, then it is impossible for an aligned write of <i>n</i> bytes to cause data corruption. If a failure occurs after such a write operation and before the applicable file handle is <i>synced</i>, then following recovery it will appear as if the write operation succeeded or did not take place at all. It is not possible that only part of the data specified by the write operation was written to persistent media, nor is it possible for any content of the sectors spanned by the write operation to be replaced with garbage data, as it is normally assumed to be. <li><p>The <b>sequential-write</b> property. A system that supports the <i>sequential-write</i> property guarantees that the various write operations on files within the same file-system are written to the persistent media in the same order that they are performed by the application and that each operation is concluded before the next is begun. If a system supports the <i>sequential-write</i> property, then the model used to determine the possible states of the file-system following a failure is different. <p>If a system supports <i>sequential-write</i> it is assumed that <i>syncing</i> any file within the file system flushes all write operations on all files (not just the <i>synced</i> file) to the persistent media. If a failure does occur, it is not known whether or not any of the write operations performed by SQLite since the last time a file was <i>synced</i>. SQLite is able to assume that if the write operations of unknown status are arranged in the order that they occured: <ol> <li> the first <i>n</i> operations will have been executed successfully, <li> the next operation puts all device sectors that it modifies into the transient state, so that following recovery each sector may be partially written, completely written, not written at all or populated with garbage data, <li> the remaining operations will not have had any effect on the contents of the file-system. </ol> </ul> <h3 id=fs_assumption_details>Failure Related Assumption Details</h3> <p> This section describes how the assumptions presented in the parent section apply to the individual API functions and operations provided by the VFS to SQLite for the purposes of modifying the contents of the file-system. <p> SQLite manipulates the contents of the file-system using a combination of the following four types of operation: <ul> <li> <b>Create file</b> operations. SQLite may create new files within the file-system by invoking the xOpen() method of the sqlite3_io_methods object. <li> <b>Delete file</b> operations. SQLite may remove files from the file system by calling the xDelete() method of the sqlite3_io_methods object. <li> <b>Truncate file</b> operations. SQLite may truncate existing files by invoking the xTruncate() method of the sqlite3_file object. <li> <b>Write file</b> operations. SQLite may modify the contents and increase the size of a file by files by invoking the xWrite() method of the sqlite3_file object. </ul> <p> Additionally, all VFS implementations are required to provide the <i>sync file</i> operation, accessed via the xSync() method of the sqlite3_file object, used to flush create, write and truncate operations on a file to the persistent storage medium. <p> The formalized assumptions in this section refer to <i>system failure</i> events. In this context, this should be interpreted as any failure that causes the system to stop operating. For example a power failure or operating system crash. <p> SQLite does not assume that a <b>create file</b> operation has actually modified the file-system records within perisistent storage until after the file has been successfully <i>synced</i>.<p class=req id=A21001> If a system failure occurs during or after a "create file" operation, but before the created file has been <i>synced</i>, then SQLite assumes that it is possible that the created file may not exist following system recovery.</p> <p> Of course, it is also possible that it does exist following system recovery.<p class=req id=A21002> If a "create file" operation is executed by SQLite, and then the created file <i>synced</i>, then SQLite assumes that the file-system modifications corresponding to the "create file" operation have been committed to persistent media. It is assumed that if a system failure occurs any time after the file has been successfully <i>synced</i>, then the file is guaranteed to appear in the file-system following system recovery.</p> <p> A <b>delete file</b> operation (invoked by a call to the VFS xDelete() method) is assumed to be an atomic and durable operation. </p><p class=req id=A21003> If a system failure occurs at any time after a "delete file" operation (call to the VFS xDelete() method) returns successfully, it is assumed that the file-system will not contain the deleted file following system recovery.</p><p class=req id=A21004> If a system failure occurs during a "delete file" operation, it is assumed that following system recovery the file-system will either contain the file being deleted in the state it was in before the operation was attempted, or not contain the file at all. It is assumed that it is not possible for the file to have become corrupted purely as a result of a failure occuring during a "delete file" operation.</p> <p> The effects of a <b>truncate file</b> operation are not assumed to be made persistent until after the corresponding file has been <i>synced</i>.<p class=req id=A21005> If a system failure occurs during or after a "truncate file" operation, but before the truncated file has been <i>synced</i>, then SQLite assumes that the size of the truncated file is either as large or larger than the size that it was to be truncated to.</p><p class=req id=A21006> If a system failure occurs during or after a "truncate file" operation, but before the truncated file has been <i>synced</i>, then it is assumed that the contents of the file up to the size that the file was to be truncated to are not corrupted.</p> <p> The above two assumptions may be interpreted to mean that if a system failure occurs after file truncation but before the truncated file is <i>synced</i>, the contents of the file following the point at which it was to be truncated may not be trusted. They may contain the original file data, or may contain garbage.<p class=req id=A21007> If a "truncate file" operation is executed by SQLite, and then the truncated file <i>synced</i>, then SQLite assumes that the file-system modifications corresponding to the "truncate file" operation have been committed to persistent media. It is assumed that if a system failure occurs any time after the file has been successfully <i>synced</i>, then the effects of the file truncation are guaranteed to appear in the file system following recovery.</p> <p> A <b>write file</b> operation modifies the contents of an existing file within the file-system. It may also increase the size of the file. The effects of a <i>write file</i> operation are not assumed to be made persistent until after the corresponding file has been <i>synced</i>.<p class=req id=A21008> If a system failure occurs during or after a "write file" operation, but before the corresponding file has been <i>synced</i>, then it is assumed that the content of all sectors spanned by the <i>write file</i> operation are untrustworthy following system
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -