]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/col_buf_decoder.cc
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.
6 #include "utilities/col_buf_decoder.h"
13 ColBufDecoder::~ColBufDecoder() {}
17 inline uint64_t EncodeFixed64WithEndian(uint64_t val
, bool big_endian
,
19 if (big_endian
&& port::kLittleEndian
) {
20 val
= EndianTransform(val
, size
);
21 } else if (!big_endian
&& !port::kLittleEndian
) {
22 val
= EndianTransform(val
, size
);
29 ColBufDecoder
* ColBufDecoder::NewColBufDecoder(
30 const ColDeclaration
& col_declaration
) {
31 if (col_declaration
.col_type
== "FixedLength") {
32 return new FixedLengthColBufDecoder(
33 col_declaration
.size
, col_declaration
.col_compression_type
,
34 col_declaration
.nullable
, col_declaration
.big_endian
);
35 } else if (col_declaration
.col_type
== "VariableLength") {
36 return new VariableLengthColBufDecoder();
37 } else if (col_declaration
.col_type
== "VariableChunk") {
38 return new VariableChunkColBufDecoder(col_declaration
.col_compression_type
);
39 } else if (col_declaration
.col_type
== "LongFixedLength") {
40 return new LongFixedLengthColBufDecoder(col_declaration
.size
,
41 col_declaration
.nullable
);
43 // Unrecognized column type
49 void ReadVarint64(const char** src_ptr
, uint64_t* val_ptr
) {
50 const char* q
= GetVarint64Ptr(*src_ptr
, *src_ptr
+ 10, val_ptr
);
56 size_t FixedLengthColBufDecoder::Init(const char* src
) {
59 // Dictionary initialization
61 const char* orig_src
= src
;
62 if (col_compression_type_
== kColDict
||
63 col_compression_type_
== kColRleDict
) {
67 q
= GetVarint64Ptr(src
, src
+ 10, &dict_size
);
72 for (uint64_t i
= 0; i
< dict_size
; ++i
) {
74 ReadVarint64(&src
, &dict_key
);
76 dict_key
= EncodeFixed64WithEndian(dict_key
, big_endian_
, size_
);
77 dict_vec_
.push_back(dict_key
);
80 return src
- orig_src
;
83 size_t FixedLengthColBufDecoder::Decode(const char* src
, char** dest
) {
84 uint64_t read_val
= 0;
85 const char* orig_src
= src
;
86 const char* src_limit
= src
+ 20;
95 if (IsRunLength(col_compression_type_
)) {
96 if (remain_runs_
== 0) {
99 if (col_compression_type_
== kColRle
) {
100 memcpy(&run_val_
, src
, size_
);
103 q
= GetVarint64Ptr(src
, src_limit
, &run_val_
);
104 assert(q
!= nullptr);
108 q
= GetVarint64Ptr(src
, src_limit
, &remain_runs_
);
109 assert(q
!= nullptr);
112 if (col_compression_type_
!= kColRleDeltaVarint
&&
113 col_compression_type_
!= kColRleDict
) {
114 run_val_
= EncodeFixed64WithEndian(run_val_
, big_endian_
, size_
);
119 if (col_compression_type_
== kColNoCompression
) {
120 memcpy(&read_val
, src
, size_
);
123 // Assume a column does not exceed 8 bytes here
124 const char* q
= GetVarint64Ptr(src
, src_limit
, &read_val
);
125 assert(q
!= nullptr);
128 if (col_compression_type_
!= kColDeltaVarint
&&
129 col_compression_type_
!= kColDict
) {
130 read_val
= EncodeFixed64WithEndian(read_val
, big_endian_
, size_
);
134 uint64_t write_val
= read_val
;
135 if (col_compression_type_
== kColDeltaVarint
||
136 col_compression_type_
== kColRleDeltaVarint
) {
137 // does not support 64 bit
139 uint64_t mask
= (write_val
& 1) ? (~uint64_t(0)) : 0;
140 int64_t delta
= (write_val
>> 1) ^ mask
;
141 write_val
= last_val_
+ delta
;
143 uint64_t tmp
= write_val
;
144 write_val
= EncodeFixed64WithEndian(write_val
, big_endian_
, size_
);
146 } else if (col_compression_type_
== kColRleDict
||
147 col_compression_type_
== kColDict
) {
148 uint64_t dict_val
= read_val
;
149 assert(dict_val
< dict_vec_
.size());
150 write_val
= dict_vec_
[dict_val
];
153 // dest->append(reinterpret_cast<char*>(&write_val), size_);
154 memcpy(*dest
, reinterpret_cast<char*>(&write_val
), size_
);
156 if (IsRunLength(col_compression_type_
)) {
159 return src
- orig_src
;
162 size_t LongFixedLengthColBufDecoder::Decode(const char* src
, char** dest
) {
171 memcpy(*dest
, src
, size_
);
176 size_t VariableLengthColBufDecoder::Decode(const char* src
, char** dest
) {
179 memcpy(dest
, reinterpret_cast<char*>(&len
), 1);
182 memcpy(*dest
, src
, len
);
187 size_t VariableChunkColBufDecoder::Init(const char* src
) {
188 // Dictionary initialization
190 const char* orig_src
= src
;
191 if (col_compression_type_
== kColDict
) {
195 q
= GetVarint64Ptr(src
, src
+ 10, &dict_size
);
196 assert(q
!= nullptr);
200 for (uint64_t i
= 0; i
< dict_size
; ++i
) {
202 ReadVarint64(&src
, &dict_key
);
203 dict_vec_
.push_back(dict_key
);
206 return src
- orig_src
;
209 size_t VariableChunkColBufDecoder::Decode(const char* src
, char** dest
) {
210 const char* orig_src
= src
;
212 ReadVarint64(&src
, &size
);
213 int64_t full_chunks
= size
/ 8;
215 size_t chunk_size
= 8;
216 for (int64_t i
= 0; i
< full_chunks
+ 1; ++i
) {
218 if (i
== full_chunks
) {
219 chunk_size
= size
% 8;
221 if (col_compression_type_
== kColDict
) {
223 ReadVarint64(&src
, &dict_val
);
224 assert(dict_val
< dict_vec_
.size());
225 chunk_buf
= dict_vec_
[dict_val
];
227 memcpy(&chunk_buf
, src
, chunk_size
);
230 memcpy(*dest
, reinterpret_cast<char*>(&chunk_buf
), 8);
232 uint8_t mask
= ((0xFF - 8) + chunk_size
) & 0xFF;
233 memcpy(*dest
, reinterpret_cast<char*>(&mask
), 1);
237 return src
- orig_src
;
240 } // namespace rocksdb