xref: /aosp_15_r20/external/leveldb/doc/table_format.md (revision 9507f98c5f32dee4b5f9e4a38cd499f3ff5c4490)
1*9507f98cSAndroid Build Coastguard Workerleveldb File format
2*9507f98cSAndroid Build Coastguard Worker===================
3*9507f98cSAndroid Build Coastguard Worker
4*9507f98cSAndroid Build Coastguard Worker    <beginning_of_file>
5*9507f98cSAndroid Build Coastguard Worker    [data block 1]
6*9507f98cSAndroid Build Coastguard Worker    [data block 2]
7*9507f98cSAndroid Build Coastguard Worker    ...
8*9507f98cSAndroid Build Coastguard Worker    [data block N]
9*9507f98cSAndroid Build Coastguard Worker    [meta block 1]
10*9507f98cSAndroid Build Coastguard Worker    ...
11*9507f98cSAndroid Build Coastguard Worker    [meta block K]
12*9507f98cSAndroid Build Coastguard Worker    [metaindex block]
13*9507f98cSAndroid Build Coastguard Worker    [index block]
14*9507f98cSAndroid Build Coastguard Worker    [Footer]        (fixed size; starts at file_size - sizeof(Footer))
15*9507f98cSAndroid Build Coastguard Worker    <end_of_file>
16*9507f98cSAndroid Build Coastguard Worker
17*9507f98cSAndroid Build Coastguard WorkerThe file contains internal pointers.  Each such pointer is called
18*9507f98cSAndroid Build Coastguard Workera BlockHandle and contains the following information:
19*9507f98cSAndroid Build Coastguard Worker
20*9507f98cSAndroid Build Coastguard Worker    offset:   varint64
21*9507f98cSAndroid Build Coastguard Worker    size:     varint64
22*9507f98cSAndroid Build Coastguard Worker
23*9507f98cSAndroid Build Coastguard WorkerSee [varints](https://developers.google.com/protocol-buffers/docs/encoding#varints)
24*9507f98cSAndroid Build Coastguard Workerfor an explanation of varint64 format.
25*9507f98cSAndroid Build Coastguard Worker
26*9507f98cSAndroid Build Coastguard Worker1.  The sequence of key/value pairs in the file are stored in sorted
27*9507f98cSAndroid Build Coastguard Workerorder and partitioned into a sequence of data blocks.  These blocks
28*9507f98cSAndroid Build Coastguard Workercome one after another at the beginning of the file.  Each data block
29*9507f98cSAndroid Build Coastguard Workeris formatted according to the code in `block_builder.cc`, and then
30*9507f98cSAndroid Build Coastguard Workeroptionally compressed.
31*9507f98cSAndroid Build Coastguard Worker
32*9507f98cSAndroid Build Coastguard Worker2. After the data blocks we store a bunch of meta blocks.  The
33*9507f98cSAndroid Build Coastguard Workersupported meta block types are described below.  More meta block types
34*9507f98cSAndroid Build Coastguard Workermay be added in the future.  Each meta block is again formatted using
35*9507f98cSAndroid Build Coastguard Worker`block_builder.cc` and then optionally compressed.
36*9507f98cSAndroid Build Coastguard Worker
37*9507f98cSAndroid Build Coastguard Worker3. A "metaindex" block.  It contains one entry for every other meta
38*9507f98cSAndroid Build Coastguard Workerblock where the key is the name of the meta block and the value is a
39*9507f98cSAndroid Build Coastguard WorkerBlockHandle pointing to that meta block.
40*9507f98cSAndroid Build Coastguard Worker
41*9507f98cSAndroid Build Coastguard Worker4. An "index" block.  This block contains one entry per data block,
42*9507f98cSAndroid Build Coastguard Workerwhere the key is a string >= last key in that data block and before
43*9507f98cSAndroid Build Coastguard Workerthe first key in the successive data block.  The value is the
44*9507f98cSAndroid Build Coastguard WorkerBlockHandle for the data block.
45*9507f98cSAndroid Build Coastguard Worker
46*9507f98cSAndroid Build Coastguard Worker5. At the very end of the file is a fixed length footer that contains
47*9507f98cSAndroid Build Coastguard Workerthe BlockHandle of the metaindex and index blocks as well as a magic number.
48*9507f98cSAndroid Build Coastguard Worker
49*9507f98cSAndroid Build Coastguard Worker        metaindex_handle: char[p];     // Block handle for metaindex
50*9507f98cSAndroid Build Coastguard Worker        index_handle:     char[q];     // Block handle for index
51*9507f98cSAndroid Build Coastguard Worker        padding:          char[40-p-q];// zeroed bytes to make fixed length
52*9507f98cSAndroid Build Coastguard Worker                                       // (40==2*BlockHandle::kMaxEncodedLength)
53*9507f98cSAndroid Build Coastguard Worker        magic:            fixed64;     // == 0xdb4775248b80fb57 (little-endian)
54*9507f98cSAndroid Build Coastguard Worker
55*9507f98cSAndroid Build Coastguard Worker## "filter" Meta Block
56*9507f98cSAndroid Build Coastguard Worker
57*9507f98cSAndroid Build Coastguard WorkerIf a `FilterPolicy` was specified when the database was opened, a
58*9507f98cSAndroid Build Coastguard Workerfilter block is stored in each table.  The "metaindex" block contains
59*9507f98cSAndroid Build Coastguard Workeran entry that maps from `filter.<N>` to the BlockHandle for the filter
60*9507f98cSAndroid Build Coastguard Workerblock where `<N>` is the string returned by the filter policy's
61*9507f98cSAndroid Build Coastguard Worker`Name()` method.
62*9507f98cSAndroid Build Coastguard Worker
63*9507f98cSAndroid Build Coastguard WorkerThe filter block stores a sequence of filters, where filter i contains
64*9507f98cSAndroid Build Coastguard Workerthe output of `FilterPolicy::CreateFilter()` on all keys that are stored
65*9507f98cSAndroid Build Coastguard Workerin a block whose file offset falls within the range
66*9507f98cSAndroid Build Coastguard Worker
67*9507f98cSAndroid Build Coastguard Worker    [ i*base ... (i+1)*base-1 ]
68*9507f98cSAndroid Build Coastguard Worker
69*9507f98cSAndroid Build Coastguard WorkerCurrently, "base" is 2KB.  So for example, if blocks X and Y start in
70*9507f98cSAndroid Build Coastguard Workerthe range `[ 0KB .. 2KB-1 ]`, all of the keys in X and Y will be
71*9507f98cSAndroid Build Coastguard Workerconverted to a filter by calling `FilterPolicy::CreateFilter()`, and the
72*9507f98cSAndroid Build Coastguard Workerresulting filter will be stored as the first filter in the filter
73*9507f98cSAndroid Build Coastguard Workerblock.
74*9507f98cSAndroid Build Coastguard Worker
75*9507f98cSAndroid Build Coastguard WorkerThe filter block is formatted as follows:
76*9507f98cSAndroid Build Coastguard Worker
77*9507f98cSAndroid Build Coastguard Worker    [filter 0]
78*9507f98cSAndroid Build Coastguard Worker    [filter 1]
79*9507f98cSAndroid Build Coastguard Worker    [filter 2]
80*9507f98cSAndroid Build Coastguard Worker    ...
81*9507f98cSAndroid Build Coastguard Worker    [filter N-1]
82*9507f98cSAndroid Build Coastguard Worker
83*9507f98cSAndroid Build Coastguard Worker    [offset of filter 0]                  : 4 bytes
84*9507f98cSAndroid Build Coastguard Worker    [offset of filter 1]                  : 4 bytes
85*9507f98cSAndroid Build Coastguard Worker    [offset of filter 2]                  : 4 bytes
86*9507f98cSAndroid Build Coastguard Worker    ...
87*9507f98cSAndroid Build Coastguard Worker    [offset of filter N-1]                : 4 bytes
88*9507f98cSAndroid Build Coastguard Worker
89*9507f98cSAndroid Build Coastguard Worker    [offset of beginning of offset array] : 4 bytes
90*9507f98cSAndroid Build Coastguard Worker    lg(base)                              : 1 byte
91*9507f98cSAndroid Build Coastguard Worker
92*9507f98cSAndroid Build Coastguard WorkerThe offset array at the end of the filter block allows efficient
93*9507f98cSAndroid Build Coastguard Workermapping from a data block offset to the corresponding filter.
94*9507f98cSAndroid Build Coastguard Worker
95*9507f98cSAndroid Build Coastguard Worker## "stats" Meta Block
96*9507f98cSAndroid Build Coastguard Worker
97*9507f98cSAndroid Build Coastguard WorkerThis meta block contains a bunch of stats.  The key is the name
98*9507f98cSAndroid Build Coastguard Workerof the statistic.  The value contains the statistic.
99*9507f98cSAndroid Build Coastguard Worker
100*9507f98cSAndroid Build Coastguard WorkerTODO(postrelease): record following stats.
101*9507f98cSAndroid Build Coastguard Worker
102*9507f98cSAndroid Build Coastguard Worker    data size
103*9507f98cSAndroid Build Coastguard Worker    index size
104*9507f98cSAndroid Build Coastguard Worker    key size (uncompressed)
105*9507f98cSAndroid Build Coastguard Worker    value size (uncompressed)
106*9507f98cSAndroid Build Coastguard Worker    number of entries
107*9507f98cSAndroid Build Coastguard Worker    number of data blocks
108