1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
12 #include "options/options_helper.h"
13 #include "rocksdb/options.h"
14 #include "rocksdb/table.h"
21 class RandomAccessFile
;
26 // IndexedTable requires fixed length key, configured as a constructor
27 // parameter of the factory class. Output file format:
28 // +-------------+-----------------+
29 // | version | user_key_length |
30 // +------------++------------+-----------------+ <= key1 offset
31 // | encoded key1 | value_size | |
32 // +------------+-------------+-------------+ |
35 // +--------------------------+-------------+---+ <= key2 offset
36 // | encoded key2 | value_size | |
37 // +------------+-------------+-------------+ |
41 // +-----------------+--------------------------+
43 // When the key encoding type is kPlain. Key part is encoded as:
44 // +------------+--------------------+
45 // | [key_size] | internal key |
46 // +------------+--------------------+
47 // for the case of user_key_len = kPlainTableVariableLength case,
49 // +----------------------+
51 // +----------------------+
52 // for user_key_len != kPlainTableVariableLength case.
54 // If key encoding type is kPrefix. Keys are encoding in this format.
55 // There are three ways to encode a key:
57 // +---------------+---------------+-------------------+
58 // | Full Key Flag | Full Key Size | Full Internal Key |
59 // +---------------+---------------+-------------------+
60 // which simply encodes a full key
62 // (2) A key shared the same prefix as the previous key, which is encoded as
64 // +-------------+-------------+-------------+-------------+------------+
65 // | Prefix Flag | Prefix Size | Suffix Flag | Suffix Size | Key Suffix |
66 // +-------------+-------------+-------------+-------------+------------+
67 // where key is the suffix part of the key, including the internal bytes.
68 // the actual key will be constructed by concatenating prefix part of the
69 // previous key, with the suffix part of the key here, with sizes given here.
71 // (3) A key shared the same prefix as the previous key, which is encoded as
73 // +-----------------+-----------------+------------------------+
74 // | Key Suffix Flag | Key Suffix Size | Suffix of Internal Key |
75 // +-----------------+-----------------+------------------------+
76 // The key will be constructed by concatenating previous key's prefix (which is
77 // also a prefix which the last key encoded in the format of (1)) and the
80 // For example, we for following keys (prefix and suffix are separated by
87 // Will be encoded like this:
93 // (where FK means full key flag, PF means prefix flag and SF means suffix flag)
95 // All those "key flag + key size" shown above are in this format:
96 // The 8 bits of the first byte:
97 // +----+----+----+----+----+----+----+----+
99 // +----+----+----+----+----+----+----+----+
100 // Type indicates: full key, prefix, or suffix.
101 // The last 6 bits are for size. If the size bits are not all 1, it means the
102 // size of the key. Otherwise, varint32 is read after this byte. This varint
103 // value + 0x3F (the value of all 1) will be the key size.
105 // For example, full key with length 16 will be encoded as (binary):
107 // (00 means full key)
108 // and a prefix with 100 bytes will be encoded as:
109 // 01 111111 00100101
111 // (01 means key suffix)
113 // All the internal keys above (including kPlain and kPrefix) are encoded in
115 // There are two types:
116 // (1) normal internal key format
117 // +----------- ...... -------------+----+---+---+---+---+---+---+---+
118 // | user key |type| sequence ID |
119 // +----------- ..... --------------+----+---+---+---+---+---+---+---+
120 // (2) Special case for keys whose sequence ID is 0 and is value type
121 // +----------- ...... -------------+----+
123 // +----------- ..... --------------+----+
124 // To save 7 bytes for the special case where sequence ID = 0.
127 class PlainTableFactory
: public TableFactory
{
129 ~PlainTableFactory() {}
130 // user_key_len is the length of the user key. If it is set to be
131 // kPlainTableVariableLength, then it means variable length. Otherwise, all
132 // the keys need to have the fix length of this value. bloom_bits_per_key is
133 // number of bits used for bloom filer per key. hash_table_ratio is
134 // the desired utilization of the hash table used for prefix hashing.
135 // hash_table_ratio = number of prefixes / #buckets in the hash table
136 // hash_table_ratio = 0 means skip hash table but only replying on binary
138 // index_sparseness determines index interval for keys
139 // inside the same prefix. It will be the maximum number of linear search
140 // required after hash and binary search.
141 // index_sparseness = 0 means index for every key.
142 // huge_page_tlb_size determines whether to allocate hash indexes from huge
143 // page TLB and the page size if allocating from there. See comments of
144 // Arena::AllocateAligned() for details.
145 explicit PlainTableFactory(
146 const PlainTableOptions
& _table_options
= PlainTableOptions())
147 : table_options_(_table_options
) {}
149 const char* Name() const override
{ return "PlainTable"; }
150 Status
NewTableReader(const TableReaderOptions
& table_reader_options
,
151 std::unique_ptr
<RandomAccessFileReader
>&& file
,
152 uint64_t file_size
, std::unique_ptr
<TableReader
>* table
,
153 bool prefetch_index_and_filter_in_cache
) const override
;
155 TableBuilder
* NewTableBuilder(
156 const TableBuilderOptions
& table_builder_options
,
157 uint32_t column_family_id
, WritableFileWriter
* file
) const override
;
159 std::string
GetPrintableTableOptions() const override
;
161 const PlainTableOptions
& table_options() const;
163 static const char kValueTypeSeqId0
= char(~0);
165 // Sanitizes the specified DB Options.
166 Status
SanitizeOptions(
167 const DBOptions
& /*db_opts*/,
168 const ColumnFamilyOptions
& /*cf_opts*/) const override
{
172 void* GetOptions() override
{ return &table_options_
; }
174 Status
GetOptionString(std::string
* /*opt_string*/,
175 const std::string
& /*delimiter*/) const override
{
180 PlainTableOptions table_options_
;
183 static std::unordered_map
<std::string
, OptionTypeInfo
> plain_table_type_info
= {
185 {offsetof(struct PlainTableOptions
, user_key_len
), OptionType::kUInt32T
,
186 OptionVerificationType::kNormal
, false, 0}},
187 {"bloom_bits_per_key",
188 {offsetof(struct PlainTableOptions
, bloom_bits_per_key
), OptionType::kInt
,
189 OptionVerificationType::kNormal
, false, 0}},
191 {offsetof(struct PlainTableOptions
, hash_table_ratio
), OptionType::kDouble
,
192 OptionVerificationType::kNormal
, false, 0}},
194 {offsetof(struct PlainTableOptions
, index_sparseness
), OptionType::kSizeT
,
195 OptionVerificationType::kNormal
, false, 0}},
196 {"huge_page_tlb_size",
197 {offsetof(struct PlainTableOptions
, huge_page_tlb_size
),
198 OptionType::kSizeT
, OptionVerificationType::kNormal
, false, 0}},
200 {offsetof(struct PlainTableOptions
, encoding_type
),
201 OptionType::kEncodingType
, OptionVerificationType::kByName
, false, 0}},
203 {offsetof(struct PlainTableOptions
, full_scan_mode
), OptionType::kBoolean
,
204 OptionVerificationType::kNormal
, false, 0}},
205 {"store_index_in_file",
206 {offsetof(struct PlainTableOptions
, store_index_in_file
),
207 OptionType::kBoolean
, OptionVerificationType::kNormal
, false, 0}}};
209 } // namespace rocksdb
210 #endif // ROCKSDB_LITE