]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/table_properties_collector.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / db / table_properties_collector.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 // This file defines a collection of statistics collectors.
7 #pragma once
8
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "db/dbformat.h"
14 #include "rocksdb/comparator.h"
15 #include "rocksdb/table_properties.h"
16
17 namespace ROCKSDB_NAMESPACE {
18
19 // Base class for internal table properties collector.
20 class IntTblPropCollector {
21 public:
22 virtual ~IntTblPropCollector() {}
23 virtual Status Finish(UserCollectedProperties* properties) = 0;
24
25 virtual const char* Name() const = 0;
26
27 // @params key the user key that is inserted into the table.
28 // @params value the value that is inserted into the table.
29 virtual Status InternalAdd(const Slice& key, const Slice& value,
30 uint64_t file_size) = 0;
31
32 virtual void BlockAdd(uint64_t block_uncomp_bytes,
33 uint64_t block_compressed_bytes_fast,
34 uint64_t block_compressed_bytes_slow) = 0;
35
36 virtual UserCollectedProperties GetReadableProperties() const = 0;
37
38 virtual bool NeedCompact() const { return false; }
39 };
40
41 // Factory for internal table properties collector.
42 class IntTblPropCollectorFactory {
43 public:
44 virtual ~IntTblPropCollectorFactory() {}
45 // has to be thread-safe
46 virtual IntTblPropCollector* CreateIntTblPropCollector(
47 uint32_t column_family_id, int level_at_creation) = 0;
48
49 // The name of the properties collector can be used for debugging purpose.
50 virtual const char* Name() const = 0;
51 };
52
53 using IntTblPropCollectorFactories =
54 std::vector<std::unique_ptr<IntTblPropCollectorFactory>>;
55
56 // When rocksdb creates a new table, it will encode all "user keys" into
57 // "internal keys", which contains meta information of a given entry.
58 //
59 // This class extracts user key from the encoded internal key when Add() is
60 // invoked.
61 class UserKeyTablePropertiesCollector : public IntTblPropCollector {
62 public:
63 // transfer of ownership
64 explicit UserKeyTablePropertiesCollector(TablePropertiesCollector* collector)
65 : collector_(collector) {}
66
67 virtual ~UserKeyTablePropertiesCollector() {}
68
69 virtual Status InternalAdd(const Slice& key, const Slice& value,
70 uint64_t file_size) override;
71
72 virtual void BlockAdd(uint64_t block_uncomp_bytes,
73 uint64_t block_compressed_bytes_fast,
74 uint64_t block_compressed_bytes_slow) override;
75
76 virtual Status Finish(UserCollectedProperties* properties) override;
77
78 virtual const char* Name() const override { return collector_->Name(); }
79
80 UserCollectedProperties GetReadableProperties() const override;
81
82 virtual bool NeedCompact() const override {
83 return collector_->NeedCompact();
84 }
85
86 protected:
87 std::unique_ptr<TablePropertiesCollector> collector_;
88 };
89
90 class UserKeyTablePropertiesCollectorFactory
91 : public IntTblPropCollectorFactory {
92 public:
93 explicit UserKeyTablePropertiesCollectorFactory(
94 std::shared_ptr<TablePropertiesCollectorFactory> user_collector_factory)
95 : user_collector_factory_(user_collector_factory) {}
96 virtual IntTblPropCollector* CreateIntTblPropCollector(
97 uint32_t column_family_id, int level_at_creation) override {
98 TablePropertiesCollectorFactory::Context context;
99 context.column_family_id = column_family_id;
100 context.level_at_creation = level_at_creation;
101 return new UserKeyTablePropertiesCollector(
102 user_collector_factory_->CreateTablePropertiesCollector(context));
103 }
104
105 virtual const char* Name() const override {
106 return user_collector_factory_->Name();
107 }
108
109 private:
110 std::shared_ptr<TablePropertiesCollectorFactory> user_collector_factory_;
111 };
112
113 // When rocksdb creates a newtable, it will encode all "user keys" into
114 // "internal keys". This class collects min/max timestamp from the encoded
115 // internal key when Add() is invoked.
116 //
117 // @param cmp the user comparator to compare the timestamps in internal key.
118 class TimestampTablePropertiesCollector : public IntTblPropCollector {
119 public:
120 explicit TimestampTablePropertiesCollector(const Comparator* cmp)
121 : cmp_(cmp),
122 timestamp_min_(kDisableUserTimestamp),
123 timestamp_max_(kDisableUserTimestamp) {}
124
125 Status InternalAdd(const Slice& key, const Slice& /* value */,
126 uint64_t /* file_size */) override {
127 auto user_key = ExtractUserKey(key);
128 assert(cmp_ && cmp_->timestamp_size() > 0);
129 if (user_key.size() < cmp_->timestamp_size()) {
130 return Status::Corruption(
131 "User key size mismatch when comparing to timestamp size.");
132 }
133 auto timestamp_in_key =
134 ExtractTimestampFromUserKey(user_key, cmp_->timestamp_size());
135 if (timestamp_max_ == kDisableUserTimestamp ||
136 cmp_->CompareTimestamp(timestamp_in_key, timestamp_max_) > 0) {
137 timestamp_max_.assign(timestamp_in_key.data(), timestamp_in_key.size());
138 }
139 if (timestamp_min_ == kDisableUserTimestamp ||
140 cmp_->CompareTimestamp(timestamp_min_, timestamp_in_key) > 0) {
141 timestamp_min_.assign(timestamp_in_key.data(), timestamp_in_key.size());
142 }
143 return Status::OK();
144 }
145
146 void BlockAdd(uint64_t /* block_uncomp_bytes */,
147 uint64_t /* block_compressed_bytes_fast */,
148 uint64_t /* block_compressed_bytes_slow */) override {
149 return;
150 }
151
152 Status Finish(UserCollectedProperties* properties) override {
153 assert(timestamp_min_.size() == timestamp_max_.size() &&
154 timestamp_max_.size() == cmp_->timestamp_size());
155 properties->insert({"rocksdb.timestamp_min", timestamp_min_});
156 properties->insert({"rocksdb.timestamp_max", timestamp_max_});
157 return Status::OK();
158 }
159
160 const char* Name() const override {
161 return "TimestampTablePropertiesCollector";
162 }
163
164 UserCollectedProperties GetReadableProperties() const override {
165 return {{"rocksdb.timestamp_min", Slice(timestamp_min_).ToString(true)},
166 {"rocksdb.timestamp_max", Slice(timestamp_max_).ToString(true)}};
167 }
168
169 protected:
170 const Comparator* const cmp_;
171 std::string timestamp_min_;
172 std::string timestamp_max_;
173 };
174
175 } // namespace ROCKSDB_NAMESPACE