]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/db/table_properties_collector.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / db / table_properties_collector.cc
CommitLineData
7c673cae
FG
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#include "db/table_properties_collector.h"
7
8#include "db/dbformat.h"
9#include "util/coding.h"
10#include "util/string_util.h"
11
12namespace rocksdb {
13
14Status InternalKeyPropertiesCollector::InternalAdd(const Slice& key,
15 const Slice& value,
16 uint64_t file_size) {
17 ParsedInternalKey ikey;
18 if (!ParseInternalKey(key, &ikey)) {
19 return Status::InvalidArgument("Invalid internal key");
20 }
21
22 // Note: We count both, deletions and single deletions here.
23 if (ikey.type == ValueType::kTypeDeletion ||
24 ikey.type == ValueType::kTypeSingleDeletion) {
25 ++deleted_keys_;
26 } else if (ikey.type == ValueType::kTypeMerge) {
27 ++merge_operands_;
28 }
29
30 return Status::OK();
31}
32
33Status InternalKeyPropertiesCollector::Finish(
34 UserCollectedProperties* properties) {
35 assert(properties);
36 assert(properties->find(
37 InternalKeyTablePropertiesNames::kDeletedKeys) == properties->end());
38 assert(properties->find(InternalKeyTablePropertiesNames::kMergeOperands) ==
39 properties->end());
40
41 std::string val_deleted_keys;
42 PutVarint64(&val_deleted_keys, deleted_keys_);
43 properties->insert(
44 {InternalKeyTablePropertiesNames::kDeletedKeys, val_deleted_keys});
45
46 std::string val_merge_operands;
47 PutVarint64(&val_merge_operands, merge_operands_);
48 properties->insert(
49 {InternalKeyTablePropertiesNames::kMergeOperands, val_merge_operands});
50
51 return Status::OK();
52}
53
54UserCollectedProperties
55InternalKeyPropertiesCollector::GetReadableProperties() const {
56 return {{"kDeletedKeys", ToString(deleted_keys_)},
57 {"kMergeOperands", ToString(merge_operands_)}};
58}
59
60namespace {
61
62EntryType GetEntryType(ValueType value_type) {
63 switch (value_type) {
64 case kTypeValue:
65 return kEntryPut;
66 case kTypeDeletion:
67 return kEntryDelete;
68 case kTypeSingleDeletion:
69 return kEntrySingleDelete;
70 case kTypeMerge:
71 return kEntryMerge;
72 default:
73 return kEntryOther;
74 }
75}
76
77uint64_t GetUint64Property(const UserCollectedProperties& props,
78 const std::string property_name,
79 bool* property_present) {
80 auto pos = props.find(property_name);
81 if (pos == props.end()) {
82 *property_present = false;
83 return 0;
84 }
85 Slice raw = pos->second;
86 uint64_t val = 0;
87 *property_present = true;
88 return GetVarint64(&raw, &val) ? val : 0;
89}
90
91} // namespace
92
93Status UserKeyTablePropertiesCollector::InternalAdd(const Slice& key,
94 const Slice& value,
95 uint64_t file_size) {
96 ParsedInternalKey ikey;
97 if (!ParseInternalKey(key, &ikey)) {
98 return Status::InvalidArgument("Invalid internal key");
99 }
100
101 return collector_->AddUserKey(ikey.user_key, value, GetEntryType(ikey.type),
102 ikey.sequence, file_size);
103}
104
105Status UserKeyTablePropertiesCollector::Finish(
106 UserCollectedProperties* properties) {
107 return collector_->Finish(properties);
108}
109
110UserCollectedProperties
111UserKeyTablePropertiesCollector::GetReadableProperties() const {
112 return collector_->GetReadableProperties();
113}
114
115
116const std::string InternalKeyTablePropertiesNames::kDeletedKeys
117 = "rocksdb.deleted.keys";
118const std::string InternalKeyTablePropertiesNames::kMergeOperands =
119 "rocksdb.merge.operands";
120
121uint64_t GetDeletedKeys(
122 const UserCollectedProperties& props) {
123 bool property_present_ignored;
124 return GetUint64Property(props, InternalKeyTablePropertiesNames::kDeletedKeys,
125 &property_present_ignored);
126}
127
128uint64_t GetMergeOperands(const UserCollectedProperties& props,
129 bool* property_present) {
130 return GetUint64Property(
131 props, InternalKeyTablePropertiesNames::kMergeOperands, property_present);
132}
133
134} // namespace rocksdb