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).
6 #include "db/table_properties_collector.h"
8 #include "db/dbformat.h"
9 #include "util/coding.h"
10 #include "util/string_util.h"
14 Status
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");
22 // Note: We count both, deletions and single deletions here.
23 if (ikey
.type
== ValueType::kTypeDeletion
||
24 ikey
.type
== ValueType::kTypeSingleDeletion
) {
26 } else if (ikey
.type
== ValueType::kTypeMerge
) {
33 Status
InternalKeyPropertiesCollector::Finish(
34 UserCollectedProperties
* properties
) {
36 assert(properties
->find(
37 InternalKeyTablePropertiesNames::kDeletedKeys
) == properties
->end());
38 assert(properties
->find(InternalKeyTablePropertiesNames::kMergeOperands
) ==
41 std::string val_deleted_keys
;
42 PutVarint64(&val_deleted_keys
, deleted_keys_
);
44 {InternalKeyTablePropertiesNames::kDeletedKeys
, val_deleted_keys
});
46 std::string val_merge_operands
;
47 PutVarint64(&val_merge_operands
, merge_operands_
);
49 {InternalKeyTablePropertiesNames::kMergeOperands
, val_merge_operands
});
54 UserCollectedProperties
55 InternalKeyPropertiesCollector::GetReadableProperties() const {
56 return {{"kDeletedKeys", ToString(deleted_keys_
)},
57 {"kMergeOperands", ToString(merge_operands_
)}};
62 uint64_t GetUint64Property(const UserCollectedProperties
& props
,
63 const std::string
& property_name
,
64 bool* property_present
) {
65 auto pos
= props
.find(property_name
);
66 if (pos
== props
.end()) {
67 *property_present
= false;
70 Slice raw
= pos
->second
;
72 *property_present
= true;
73 return GetVarint64(&raw
, &val
) ? val
: 0;
78 Status
UserKeyTablePropertiesCollector::InternalAdd(const Slice
& key
,
81 ParsedInternalKey ikey
;
82 if (!ParseInternalKey(key
, &ikey
)) {
83 return Status::InvalidArgument("Invalid internal key");
86 return collector_
->AddUserKey(ikey
.user_key
, value
, GetEntryType(ikey
.type
),
87 ikey
.sequence
, file_size
);
90 Status
UserKeyTablePropertiesCollector::Finish(
91 UserCollectedProperties
* properties
) {
92 return collector_
->Finish(properties
);
95 UserCollectedProperties
96 UserKeyTablePropertiesCollector::GetReadableProperties() const {
97 return collector_
->GetReadableProperties();
101 const std::string
InternalKeyTablePropertiesNames::kDeletedKeys
102 = "rocksdb.deleted.keys";
103 const std::string
InternalKeyTablePropertiesNames::kMergeOperands
=
104 "rocksdb.merge.operands";
106 uint64_t GetDeletedKeys(
107 const UserCollectedProperties
& props
) {
108 bool property_present_ignored
;
109 return GetUint64Property(props
, InternalKeyTablePropertiesNames::kDeletedKeys
,
110 &property_present_ignored
);
113 uint64_t GetMergeOperands(const UserCollectedProperties
& props
,
114 bool* property_present
) {
115 return GetUint64Property(
116 props
, InternalKeyTablePropertiesNames::kMergeOperands
, property_present
);
119 } // namespace rocksdb