]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/col_buf_decoder.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / utilities / col_buf_decoder.h
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.
5
6 #pragma once
7 #include <cstdio>
8 #include <cstring>
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13 #include "util/coding.h"
14 #include "utilities/col_buf_encoder.h"
15
16 namespace rocksdb {
17
18 struct ColDeclaration;
19
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.
23 class ColBufDecoder {
24 public:
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);
29
30 protected:
31 std::string buffer_;
32 static inline bool IsRunLength(ColCompressionType type) {
33 return type == kColRle || type == kColRleVarint ||
34 type == kColRleDeltaVarint || type == kColRleDict;
35 }
36 };
37
38 class FixedLengthColBufDecoder : public ColBufDecoder {
39 public:
40 explicit FixedLengthColBufDecoder(
41 size_t size, ColCompressionType col_compression_type = kColNoCompression,
42 bool nullable = false, bool big_endian = false)
43 : size_(size),
44 col_compression_type_(col_compression_type),
45 nullable_(nullable),
46 big_endian_(big_endian) {}
47
48 size_t Init(const char* src) override;
49 size_t Decode(const char* src, char** dest) override;
50 ~FixedLengthColBufDecoder() {}
51
52 private:
53 size_t size_;
54 ColCompressionType col_compression_type_;
55 bool nullable_;
56 bool big_endian_;
57
58 // for decoding
59 std::vector<uint64_t> dict_vec_;
60 uint64_t remain_runs_;
61 uint64_t run_val_;
62 uint64_t last_val_;
63 };
64
65 class LongFixedLengthColBufDecoder : public ColBufDecoder {
66 public:
67 LongFixedLengthColBufDecoder(size_t size, bool nullable)
68 : size_(size), nullable_(nullable) {}
69
70 size_t Decode(const char* src, char** dest) override;
71 ~LongFixedLengthColBufDecoder() {}
72
73 private:
74 size_t size_;
75 bool nullable_;
76 };
77
78 class VariableLengthColBufDecoder : public ColBufDecoder {
79 public:
80 size_t Decode(const char* src, char** dest) override;
81 ~VariableLengthColBufDecoder() {}
82 };
83
84 class VariableChunkColBufDecoder : public VariableLengthColBufDecoder {
85 public:
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) {}
91
92 private:
93 ColCompressionType col_compression_type_;
94 std::unordered_map<uint64_t, uint64_t> dictionary_;
95 std::vector<uint64_t> dict_vec_;
96 };
97
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;
102
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)));
107 }
108 for (auto vcd : *kvp_cd.value_col_declarations) {
109 value_col_bufs.emplace_back(
110 std::move(ColBufDecoder::NewColBufDecoder(vcd)));
111 }
112 value_checksum_buf.reset(
113 ColBufDecoder::NewColBufDecoder(*kvp_cd.value_checksum_declaration));
114 }
115 };
116 } // namespace rocksdb