Squashed 'src/leveldb/' changes from a31c8aa40..196962ff0
[bitcoinplatinum.git] / doc / log_format.md
blobf32cb5d7dac482e71a103f85b1a2667b92e6120e
1 leveldb Log format
2 ==================
3 The log file contents are a sequence of 32KB blocks.  The only exception is that
4 the tail of the file may contain a partial block.
6 Each block consists of a sequence of records:
8     block := record* trailer?
9     record :=
10       checksum: uint32     // crc32c of type and data[] ; little-endian
11       length: uint16       // little-endian
12       type: uint8          // One of FULL, FIRST, MIDDLE, LAST
13       data: uint8[length]
15 A record never starts within the last six bytes of a block (since it won't fit).
16 Any leftover bytes here form the trailer, which must consist entirely of zero
17 bytes and must be skipped by readers.
19 Aside: if exactly seven bytes are left in the current block, and a new non-zero
20 length record is added, the writer must emit a FIRST record (which contains zero
21 bytes of user data) to fill up the trailing seven bytes of the block and then
22 emit all of the user data in subsequent blocks.
24 More types may be added in the future.  Some Readers may skip record types they
25 do not understand, others may report that some data was skipped.
27     FULL == 1
28     FIRST == 2
29     MIDDLE == 3
30     LAST == 4
32 The FULL record contains the contents of an entire user record.
34 FIRST, MIDDLE, LAST are types used for user records that have been split into
35 multiple fragments (typically because of block boundaries).  FIRST is the type
36 of the first fragment of a user record, LAST is the type of the last fragment of
37 a user record, and MIDDLE is the type of all interior fragments of a user
38 record.
40 Example: consider a sequence of user records:
42     A: length 1000
43     B: length 97270
44     C: length 8000
46 **A** will be stored as a FULL record in the first block.
48 **B** will be split into three fragments: first fragment occupies the rest of
49 the first block, second fragment occupies the entirety of the second block, and
50 the third fragment occupies a prefix of the third block.  This will leave six
51 bytes free in the third block, which will be left empty as the trailer.
53 **C** will be stored as a FULL record in the fourth block.
55 ----
57 ## Some benefits over the recordio format:
59 1. We do not need any heuristics for resyncing - just go to next block boundary
60    and scan.  If there is a corruption, skip to the next block.  As a
61    side-benefit, we do not get confused when part of the contents of one log
62    file are embedded as a record inside another log file.
64 2. Splitting at approximate boundaries (e.g., for mapreduce) is simple: find the
65    next block boundary and skip records until we hit a FULL or FIRST record.
67 3. We do not need extra buffering for large records.
69 ## Some downsides compared to recordio format:
71 1. No packing of tiny records.  This could be fixed by adding a new record type,
72    so it is a shortcoming of the current implementation, not necessarily the
73    format.
75 2. No compression.  Again, this could be fixed by adding new record types.