in src/storage/redis_db.cc [249:316]
rocksdb::Status Database::Keys(engine::Context &ctx, const std::string &prefix, const std::string &suffix_glob,
std::vector<std::string> *keys, KeyNumStats *stats) {
uint16_t slot_id = 0;
std::string ns_prefix;
if (namespace_ != kDefaultNamespace || keys != nullptr) {
if (storage_->IsSlotIdEncoded()) {
ns_prefix = ComposeNamespaceKey(namespace_, "", false);
if (!prefix.empty()) {
PutFixed16(&ns_prefix, slot_id);
ns_prefix.append(prefix);
}
} else {
ns_prefix = AppendNamespacePrefix(prefix);
}
}
uint64_t ttl_sum = 0;
auto iter = util::UniqueIterator(ctx, ctx.GetReadOptions(), metadata_cf_handle_);
while (true) {
ns_prefix.empty() ? iter->SeekToFirst() : iter->Seek(ns_prefix);
for (; iter->Valid(); iter->Next()) {
if (!ns_prefix.empty() && !iter->key().starts_with(ns_prefix)) {
break;
}
auto [_, user_key] = ExtractNamespaceKey(iter->key(), storage_->IsSlotIdEncoded());
if (!util::StringMatch(suffix_glob, user_key.ToString().substr(prefix.size()))) {
continue;
}
Metadata metadata(kRedisNone, false);
auto s = metadata.Decode(iter->value());
if (!s.ok()) continue;
if (metadata.Expired()) {
if (stats) stats->n_expired++;
continue;
}
if (stats) {
int64_t ttl = metadata.TTL();
stats->n_key++;
if (ttl != -1) {
stats->n_expires++;
if (ttl > 0) ttl_sum += ttl;
}
}
if (keys) {
keys->emplace_back(user_key.ToString());
}
}
if (auto s = iter->status(); !s.ok()) {
return s;
}
if (!storage_->IsSlotIdEncoded()) break;
if (prefix.empty()) break;
if (++slot_id >= HASH_SLOTS_SIZE) break;
ns_prefix = ComposeNamespaceKey(namespace_, "", false);
PutFixed16(&ns_prefix, slot_id);
ns_prefix.append(prefix);
}
if (stats && stats->n_expires > 0) {
stats->avg_ttl = ttl_sum / stats->n_expires / 1000;
}
return rocksdb::Status::OK();
}