1 // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 // Package table allows read and write sorted key/value.
17 Table is consist of one or more data blocks, an optional filter block
18 a metaindex block, an index block and a table footer. Metaindex block
19 is a special block used to keep parameters of the table, such as filter
20 block name and its block handle. Index block is a special block used to
21 keep record of data blocks offset and length, index block use one as
22 restart interval. The key used by index block are the last key of preceding
23 block, shorter separator of adjacent blocks or shorter successor of the
24 last key of the last block. Filter block is an optional block contains
25 sequence of filter data generated by a filter generator.
30 +--------------+--------------+--------------+------+-------+-----------------+-------------+--------+
31 | data block 1 | ... | data block n | filter block | metaindex block | index block | footer |
32 +--------------+--------------+--------------+--------------+-----------------+-------------+--------+
34 Each block followed by a 5-bytes trailer contains compression type and checksum.
38 +---------------------------+-------------------+
39 | compression type (1-byte) | checksum (4-byte) |
40 +---------------------------+-------------------+
42 The checksum is a CRC-32 computed using Castagnoli's polynomial. Compression
43 type also included in the checksum.
47 +------------------- 40-bytes -------------------+
49 +------------------------+--------------------+------+-----------------+
50 | metaindex block handle / index block handle / ---- | magic (8-bytes) |
51 +------------------------+--------------------+------+-----------------+
53 The magic are first 64-bit of SHA-1 sum of "http://code.google.com/p/leveldb/".
55 NOTE: All fixed-length integer are little-endian.
61 Block is consist of one or more key/value entries and a block trailer.
62 Block entry shares key prefix with its preceding key until a restart
63 point reached. A block should contains at least one restart point.
64 First restart point are always zero.
68 + restart point + restart point (depends on restart interval)
70 +---------------+---------------+---------------+---------------+---------+
71 | block entry 1 | block entry 2 | ... | block entry n | trailer |
72 +---------------+---------------+---------------+---------------+---------+
78 +-------+---------+-----------+---------+--------------------+--------------+----------------+
79 | shared (varint) | not shared (varint) | value len (varint) | key (varlen) | value (varlen) |
80 +-----------------+---------------------+--------------------+--------------+----------------+
82 Block entry shares key prefix with its preceding key:
85 entry one : key=deck,value=v1
86 entry two : key=dock,value=v2
87 entry three: key=duck,value=v3
88 The entries will be encoded as follow:
90 + restart point (offset=0) + restart point (offset=16)
92 +-----+-----+-----+----------+--------+-----+-----+-----+---------+--------+-----+-----+-----+----------+--------+
93 | 0 | 4 | 2 | "deck" | "v1" | 1 | 3 | 2 | "ock" | "v2" | 0 | 4 | 2 | "duck" | "v3" |
94 +-----+-----+-----+----------+--------+-----+-----+-----+---------+--------+-----+-----+-----+----------+--------+
96 +----------- entry one -----------+ +----------- entry two ----------+ +---------- entry three ----------+
98 The block trailer will contains two restart points:
100 +------------+-----------+--------+
102 +------------+-----------+---+----+
104 +-- restart points --+ + restart points length
110 +-----------------+-----------------+-----------------+------------------------------+
111 | restart point 1 | .... | restart point n | restart points len (4-bytes) |
112 +-----------------+-----------------+-----------------+------------------------------+
115 NOTE: All fixed-length integer are little-endian.
121 Filter block consist of one or more filter data and a filter block trailer.
122 The trailer contains filter data offsets, a trailer offset and a 1-byte base Lg.
124 Filter block data structure:
126 + offset 1 + offset 2 + offset n + trailer offset
128 +---------------+---------------+---------------+---------+
129 | filter data 1 | ... | filter data n | trailer |
130 +---------------+---------------+---------------+---------+
132 Filter block trailer:
136 +---------------+---------------+---------------+-------------------------------+------------------+
137 | data 1 offset | .... | data n offset | data-offsets offset (4-bytes) | base Lg (1-byte) |
138 +-------------- +---------------+---------------+-------------------------------+------------------+
141 NOTE: All fixed-length integer are little-endian.
148 magic = "\x57\xfb\x80\x8b\x24\x75\x47\xdb"
150 // The block type gives the per-block compression format.
151 // These constants are part of the file format and should not be changed.
152 blockTypeNoCompression = 0
153 blockTypeSnappyCompression = 1
155 // Generate new filter every 2KB of data
157 filterBase = 1 << filterBaseLg
160 type blockHandle struct {
161 offset, length uint64
164 func decodeBlockHandle(src []byte) (blockHandle, int) {
165 offset, n := binary.Uvarint(src)
166 length, m := binary.Uvarint(src[n:])
167 if n == 0 || m == 0 {
168 return blockHandle{}, 0
170 return blockHandle{offset, length}, n + m
173 func encodeBlockHandle(dst []byte, b blockHandle) int {
174 n := binary.PutUvarint(dst, b.offset)
175 m := binary.PutUvarint(dst[n:], b.length)