forked from apache/kvrocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_properties_collector.cc
82 lines (75 loc) · 2.99 KB
/
table_properties_collector.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "table_properties_collector.h"
#include <utility>
#include "encoding.h"
#include "redis_metadata.h"
#include "server.h"
rocksdb::Status
CompactOnExpiredCollector::AddUserKey(const rocksdb::Slice &key, const rocksdb::Slice &value,
rocksdb::EntryType entry_type, rocksdb::SequenceNumber, uint64_t) {
int now;
uint8_t type;
uint32_t expired, subkeys = 0;
uint64_t version;
if (start_key_.empty()) {
start_key_ = key.ToString();
}
stop_key_ = key.ToString();
total_keys_ += 1;
if (entry_type == rocksdb::kEntryDelete) {
deleted_keys_ += 1;
return rocksdb::Status::OK();
}
if (cf_name_ != "metadata") {
return rocksdb::Status::OK();
}
rocksdb::Slice cv = value;
if (entry_type != rocksdb::kEntryPut || cv.size() < 5) {
return rocksdb::Status::OK();
}
GetFixed8(&cv, &type);
GetFixed32(&cv, &expired);
type = type & (uint8_t)0x0f;
if (type == kRedisBitmap || type == kRedisSet || type == kRedisList ||
type == kRedisHash || type == kRedisZSet || type == kRedisSortedint) {
if (cv.size() <= 12) return rocksdb::Status::OK();
GetFixed64(&cv, &version);
GetFixed32(&cv, &subkeys);
}
total_keys_ += subkeys;
now = Server::GetUnixTime();
if ((expired > 0 && expired < static_cast<uint32_t>(now))
|| (type != kRedisString && subkeys == 0)) {
deleted_keys_ += subkeys + 1;
}
return rocksdb::Status::OK();
}
rocksdb::Status
CompactOnExpiredCollector::Finish(rocksdb::UserCollectedProperties *properties) {
properties->insert(std::pair<std::string, std::string>{"total_keys", std::to_string(total_keys_)});
properties->insert(std::pair<std::string, std::string>{"deleted_keys", std::to_string(deleted_keys_)});
properties->insert(std::pair<std::string, std::string>{"start_key", start_key_});
properties->insert(std::pair<std::string, std::string>{"stop_key", stop_key_});
return rocksdb::Status::OK();
}
rocksdb::UserCollectedProperties
CompactOnExpiredCollector::GetReadableProperties() const {
rocksdb::UserCollectedProperties properties;
properties.insert(std::pair<std::string, std::string>{"total_keys", std::to_string(total_keys_)});
properties.insert(std::pair<std::string, std::string>{"deleted_keys", std::to_string(deleted_keys_)});
return properties;
}
bool CompactOnExpiredCollector::NeedCompact() const {
if (total_keys_ == 0) return false;
return static_cast<float>(deleted_keys_)/ static_cast<float>(total_keys_) > trigger_threshold_;
}
rocksdb::TablePropertiesCollector *
CompactOnExpiredTableCollectorFactory::CreateTablePropertiesCollector(
rocksdb::TablePropertiesCollectorFactory::Context context) {
return new CompactOnExpiredCollector(cf_name_, trigger_threshold_);
}
std::shared_ptr<CompactOnExpiredTableCollectorFactory>
NewCompactOnExpiredTableCollectorFactory(const std::string &cf_name,
float trigger_threshold) {
return std::shared_ptr<CompactOnExpiredTableCollectorFactory>(
new CompactOnExpiredTableCollectorFactory(cf_name, trigger_threshold));
}