PragmaTwice commented on code in PR #882: URL: https://github.com/apache/incubator-kvrocks/pull/882#discussion_r973585761
########## src/redis_disk.cc: ########## @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include <vector> +#include <memory> +#include <utility> +#include <algorithm> +#include <string> + +#include "db_util.h" +#include "redis_metadata.h" +#include "redis_sortedint.h" +#include "rocksdb/status.h" +#include "redis_zset.h" +#include "redis_bitmap.h" +#include "redis_disk.h" +#include "status.h" + +namespace Redis { +rocksdb::Status Disk::GetApproximateSizes(const Metadata &metadata, const Slice &ns_key, + rocksdb::ColumnFamilyHandle *column_family, + uint64_t *key_size, Slice subkeyleft, + Slice subkeyright) { + std::string prefix_key, next_version_prefix_key; + InternalKey(ns_key, subkeyleft, metadata.version, + storage_->IsSlotIdEncoded()).Encode(&prefix_key); + InternalKey(ns_key, subkeyright, metadata.version + 1, + storage_->IsSlotIdEncoded()).Encode(&next_version_prefix_key); + auto key_range = rocksdb::Range(prefix_key, next_version_prefix_key); + uint64_t tmp_size = 0; + rocksdb::Status s = db_->GetApproximateSizes(option_, column_family, + &key_range, 1, &tmp_size); + if (!s.ok()) return s; + *key_size += tmp_size; + return rocksdb::Status::OK(); +} +rocksdb::Status Disk::GetStringSize(const Slice &user_key, uint64_t *key_size) { + std::string ns_key; + AppendNamespacePrefix(user_key, &ns_key); + auto key_range = rocksdb::Range(Slice(ns_key), Slice(ns_key + static_cast<char>(0))); + return db_->GetApproximateSizes(option_, metadata_cf_handle_, &key_range, 1, key_size); +} + +rocksdb::Status Disk::GetHashSize(const Slice &user_key, uint64_t *key_size) { + *key_size = 0; + std::string ns_key; + AppendNamespacePrefix(user_key, &ns_key); + HashMetadata metadata(false); + rocksdb::Status s = Database::GetMetadata(kRedisHash, ns_key, &metadata); + if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s; Review Comment: These code appears in almost every method in this file, which is like a copy-and-paste programming pattern, so I think you can just define something like ``` rocksdb::Status GetHashSize(auto ns_key, auto metadata, auto key_size) // fill these types ``` and then ``` RedisType type = ...; std::string ns_key; AppendNamespacePrefix(user_key, &ns_key); Metadata metadata(type, false); rocksdb::Status s = Database::GetMetadata(kRedisHash, ns_key, &metadata); if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s; switch (redisType) { case kRedisHash: GetHashSize(...); break; case kRedisSomething: GetSomethingSize(...) ... } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
