1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
11 #include <unordered_map>
13 #include "util/coding.h"
14 #include "utilities/col_buf_encoder.h"
18 struct ColDeclaration
;
20 // ColBufDecoder is a class to decode column buffers. It can be populated from a
21 // ColDeclaration. Before starting decoding, a Init() method should be called.
22 // Each time it takes a column value into Decode() method.
25 virtual ~ColBufDecoder() = 0;
26 virtual size_t Init(const char* src
) { return 0; }
27 virtual size_t Decode(const char* src
, char** dest
) = 0;
28 static ColBufDecoder
* NewColBufDecoder(const ColDeclaration
& col_declaration
);
32 static inline bool IsRunLength(ColCompressionType type
) {
33 return type
== kColRle
|| type
== kColRleVarint
||
34 type
== kColRleDeltaVarint
|| type
== kColRleDict
;
38 class FixedLengthColBufDecoder
: public ColBufDecoder
{
40 explicit FixedLengthColBufDecoder(
41 size_t size
, ColCompressionType col_compression_type
= kColNoCompression
,
42 bool nullable
= false, bool big_endian
= false)
44 col_compression_type_(col_compression_type
),
46 big_endian_(big_endian
) {}
48 size_t Init(const char* src
) override
;
49 size_t Decode(const char* src
, char** dest
) override
;
50 ~FixedLengthColBufDecoder() {}
54 ColCompressionType col_compression_type_
;
59 std::vector
<uint64_t> dict_vec_
;
60 uint64_t remain_runs_
;
65 class LongFixedLengthColBufDecoder
: public ColBufDecoder
{
67 LongFixedLengthColBufDecoder(size_t size
, bool nullable
)
68 : size_(size
), nullable_(nullable
) {}
70 size_t Decode(const char* src
, char** dest
) override
;
71 ~LongFixedLengthColBufDecoder() {}
78 class VariableLengthColBufDecoder
: public ColBufDecoder
{
80 size_t Decode(const char* src
, char** dest
) override
;
81 ~VariableLengthColBufDecoder() {}
84 class VariableChunkColBufDecoder
: public VariableLengthColBufDecoder
{
86 size_t Init(const char* src
) override
;
87 size_t Decode(const char* src
, char** dest
) override
;
88 explicit VariableChunkColBufDecoder(ColCompressionType col_compression_type
)
89 : col_compression_type_(col_compression_type
) {}
90 VariableChunkColBufDecoder() : col_compression_type_(kColNoCompression
) {}
93 ColCompressionType col_compression_type_
;
94 std::unordered_map
<uint64_t, uint64_t> dictionary_
;
95 std::vector
<uint64_t> dict_vec_
;
98 struct KVPairColBufDecoders
{
99 std::vector
<std::unique_ptr
<ColBufDecoder
>> key_col_bufs
;
100 std::vector
<std::unique_ptr
<ColBufDecoder
>> value_col_bufs
;
101 std::unique_ptr
<ColBufDecoder
> value_checksum_buf
;
103 explicit KVPairColBufDecoders(const KVPairColDeclarations
& kvp_cd
) {
104 for (auto kcd
: *kvp_cd
.key_col_declarations
) {
105 key_col_bufs
.emplace_back(
106 std::move(ColBufDecoder::NewColBufDecoder(kcd
)));
108 for (auto vcd
: *kvp_cd
.value_col_declarations
) {
109 value_col_bufs
.emplace_back(
110 std::move(ColBufDecoder::NewColBufDecoder(vcd
)));
112 value_checksum_buf
.reset(
113 ColBufDecoder::NewColBufDecoder(*kvp_cd
.value_checksum_declaration
));
116 } // namespace rocksdb