In order to undo the changes made to a database file, the following information must be known:
This information forms the core of a transaction record in a transaction log file.
- the fully qualified database file's name,
- the mode and permissions used to open the database file,
- the offset in the database file where the data was changed,
- the number of bytes changed,
- a copy of the altered data and
- the type of alteration which took place.
Figure 1 shows the specific format of a transaction record. Since both the length of the filename and the number of bytes in the data area may vary, both of these values are stored in the TRollBackStruct. (Note: the null terminator of the filename is also recorded in the transaction record for use during a rollback.) While it is possible to make both of these areas a fixed size, doing so would result in unnecessary data being written to disk.
Since a transaction record has a fixed maximum size (XFER_BYTES), a single user-initiated file modification may require multiple transaction records.
For a file extension operation (appending new data or changing the file size so it is larger), no bytes are recorded in the data area. Instead, the value in the file offset field of the structure is the old file length.
Transaction records must be rolled back in reverse chronological order. To do so efficiently, we must be able to find the beginning offset of each record. Storing the total record size at the start of each record enables the system to seek to the location where the next record should be located.
Once the end of the last record is found, the fixed size TRollBackStruct at the end of the record can be retrieved. This structure holds enough information to retrieve both the filename and logged data and position the log file at the end of the previous record.
Each transaction record must be recorded in a single file write() call. Multiple writes would open a window in which the system may fail, leaving a partial transaction record in the log file. Multiple writes would also add unnecessary file system overhead.