]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/table_properties_collector.h
add subtree-ish sources for 12.0.3
[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 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 // This file defines a collection of statistics collectors.
7 #pragma once
8
9 #include "rocksdb/table_properties.h"
10
11 #include <memory>
12 #include <string>
13 #include <vector>
14
15 namespace rocksdb {
16
17 struct InternalKeyTablePropertiesNames {
18 static const std::string kDeletedKeys;
19 static const std::string kMergeOperands;
20 };
21
22 // Base class for internal table properties collector.
23 class IntTblPropCollector {
24 public:
25 virtual ~IntTblPropCollector() {}
26 virtual Status Finish(UserCollectedProperties* properties) = 0;
27
28 virtual const char* Name() const = 0;
29
30 // @params key the user key that is inserted into the table.
31 // @params value the value that is inserted into the table.
32 virtual Status InternalAdd(const Slice& key, const Slice& value,
33 uint64_t file_size) = 0;
34
35 virtual UserCollectedProperties GetReadableProperties() const = 0;
36
37 virtual bool NeedCompact() const { return false; }
38 };
39
40 // Factory for internal table properties collector.
41 class IntTblPropCollectorFactory {
42 public:
43 virtual ~IntTblPropCollectorFactory() {}
44 // has to be thread-safe
45 virtual IntTblPropCollector* CreateIntTblPropCollector(
46 uint32_t column_family_id) = 0;
47
48 // The name of the properties collector can be used for debugging purpose.
49 virtual const char* Name() const = 0;
50 };
51
52 // Collecting the statistics for internal keys. Visible only by internal
53 // rocksdb modules.
54 class InternalKeyPropertiesCollector : public IntTblPropCollector {
55 public:
56 virtual Status InternalAdd(const Slice& key, const Slice& value,
57 uint64_t file_size) override;
58
59 virtual Status Finish(UserCollectedProperties* properties) override;
60
61 virtual const char* Name() const override {
62 return "InternalKeyPropertiesCollector";
63 }
64
65 UserCollectedProperties GetReadableProperties() const override;
66
67 private:
68 uint64_t deleted_keys_ = 0;
69 uint64_t merge_operands_ = 0;
70 };
71
72 class InternalKeyPropertiesCollectorFactory
73 : public IntTblPropCollectorFactory {
74 public:
75 virtual IntTblPropCollector* CreateIntTblPropCollector(
76 uint32_t column_family_id) override {
77 return new InternalKeyPropertiesCollector();
78 }
79
80 virtual const char* Name() const override {
81 return "InternalKeyPropertiesCollectorFactory";
82 }
83 };
84
85 // When rocksdb creates a new table, it will encode all "user keys" into
86 // "internal keys", which contains meta information of a given entry.
87 //
88 // This class extracts user key from the encoded internal key when Add() is
89 // invoked.
90 class UserKeyTablePropertiesCollector : public IntTblPropCollector {
91 public:
92 // transfer of ownership
93 explicit UserKeyTablePropertiesCollector(TablePropertiesCollector* collector)
94 : collector_(collector) {}
95
96 virtual ~UserKeyTablePropertiesCollector() {}
97
98 virtual Status InternalAdd(const Slice& key, const Slice& value,
99 uint64_t file_size) override;
100
101 virtual Status Finish(UserCollectedProperties* properties) override;
102
103 virtual const char* Name() const override { return collector_->Name(); }
104
105 UserCollectedProperties GetReadableProperties() const override;
106
107 virtual bool NeedCompact() const override {
108 return collector_->NeedCompact();
109 }
110
111 protected:
112 std::unique_ptr<TablePropertiesCollector> collector_;
113 };
114
115 class UserKeyTablePropertiesCollectorFactory
116 : public IntTblPropCollectorFactory {
117 public:
118 explicit UserKeyTablePropertiesCollectorFactory(
119 std::shared_ptr<TablePropertiesCollectorFactory> user_collector_factory)
120 : user_collector_factory_(user_collector_factory) {}
121 virtual IntTblPropCollector* CreateIntTblPropCollector(
122 uint32_t column_family_id) override {
123 TablePropertiesCollectorFactory::Context context;
124 context.column_family_id = column_family_id;
125 return new UserKeyTablePropertiesCollector(
126 user_collector_factory_->CreateTablePropertiesCollector(context));
127 }
128
129 virtual const char* Name() const override {
130 return user_collector_factory_->Name();
131 }
132
133 private:
134 std::shared_ptr<TablePropertiesCollectorFactory> user_collector_factory_;
135 };
136
137 } // namespace rocksdb