Skip to content

Commit

Permalink
Add missing const specifiers to methods (#1744)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Sep 7, 2023
1 parent 15b4d8f commit 45d741a
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/commands/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ class CommandLMPop : public Commander {
}

while (parser.Good()) {
if (parser.EatEqICase("count") && count_ == -1) {
if (parser.EatEqICase("count") && count_ == static_cast<uint32_t>(-1)) {
count_ = GET_OR_RET(parser.TakeInt<uint32_t>());
} else {
return parser.InvalidSyntax();
}
}
if (count_ == -1) {
if (count_ == static_cast<uint32_t>(-1)) {
count_ = 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/commander.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RegisterToCommandTable::RegisterToCommandTable(std::initializer_list<CommandAttr
}
}

int GetCommandNum() { return (int)command_details::redis_command_table.size(); }
size_t GetCommandNum() { return command_details::redis_command_table.size(); }

const CommandMap *GetOriginalCommands() { return &command_details::original_commands; }

Expand Down
4 changes: 2 additions & 2 deletions src/commands/commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ enum CommandFlags : uint64_t {
class Commander {
public:
void SetAttributes(const CommandAttributes *attributes) { attributes_ = attributes; }
const CommandAttributes *GetAttributes() { return attributes_; }
const CommandAttributes *GetAttributes() const { return attributes_; }
void SetArgs(const std::vector<std::string> &args) { args_ = args; }
virtual Status Parse() { return Parse(args_); }
virtual Status Parse(const std::vector<std::string> &args) { return Status::OK(); }
Expand Down Expand Up @@ -268,7 +268,7 @@ inline CommandMap commands;
#define REDIS_REGISTER_COMMANDS(...) \
static RegisterToCommandTable KVROCKS_CONCAT2(register_to_command_table_, __LINE__){__VA_ARGS__};

int GetCommandNum();
size_t GetCommandNum();
CommandMap *GetCommands();
void ResetCommands();
const CommandMap *GetOriginalCommands();
Expand Down
4 changes: 2 additions & 2 deletions src/common/cron.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ bool Cron::IsTimeMatch(struct tm *tm) {
return false;
}

bool Cron::IsEnabled() { return !schedulers_.empty(); }
bool Cron::IsEnabled() const { return !schedulers_.empty(); }

std::string Cron::ToString() {
std::string Cron::ToString() const {
std::string ret;
for (size_t i = 0; i < schedulers_.size(); i++) {
ret += schedulers_[i].ToString();
Expand Down
4 changes: 2 additions & 2 deletions src/common/cron.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Cron {

Status SetScheduleTime(const std::vector<std::string> &args);
bool IsTimeMatch(struct tm *tm);
std::string ToString();
bool IsEnabled();
std::string ToString() const;
bool IsEnabled() const;

private:
std::vector<Scheduler> schedulers_;
Expand Down
2 changes: 1 addition & 1 deletion src/common/task_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class TaskRunner {
return Status::OK();
}

size_t Size() { return task_queue_.size(); }
size_t Size() const { return task_queue_.size(); }
void Cancel() {
state_ = Stopping;
task_queue_.abort();
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ Status Config::Load(const CLIOptions &opts) {
return finish();
}

void Config::Get(const std::string &key, std::vector<std::string> *values) {
void Config::Get(const std::string &key, std::vector<std::string> *values) const {
values->clear();
for (const auto &iter : fields_) {
if (key == "*" || util::ToLower(key) == iter.first) {
Expand Down Expand Up @@ -900,7 +900,7 @@ Status Config::Rewrite() {
return Status::OK();
}

Status Config::GetNamespace(const std::string &ns, std::string *token) {
Status Config::GetNamespace(const std::string &ns, std::string *token) const {
token->clear();
for (const auto &iter : tokens) {
if (iter.second == ns) {
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ struct Config {
std::string NodesFilePath() const;
Status Rewrite();
Status Load(const CLIOptions &path);
void Get(const std::string &key, std::vector<std::string> *values);
void Get(const std::string &key, std::vector<std::string> *values) const;
Status Set(Server *svr, std::string key, const std::string &value);
void SetMaster(const std::string &host, uint32_t port);
void ClearMaster();
Status GetNamespace(const std::string &ns, std::string *token);
Status GetNamespace(const std::string &ns, std::string *token) const;
Status AddNamespace(const std::string &ns, const std::string &token);
Status SetNamespace(const std::string &ns, const std::string &token);
Status DelNamespace(const std::string &ns);
Expand Down
33 changes: 17 additions & 16 deletions src/config/config_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ class ConfigField {
public:
ConfigField() = default;
virtual ~ConfigField() = default;
virtual std::string ToString() = 0;
virtual std::string ToString() const = 0;
virtual Status Set(const std::string &v) = 0;
virtual Status ToNumber(int64_t *n) { return {Status::NotOK, "not supported"}; }
virtual Status ToBool(bool *b) { return {Status::NotOK, "not supported"}; }
virtual ConfigType GetConfigType() { return config_type; }
virtual bool IsMultiConfig() { return config_type == ConfigType::MultiConfig; }
virtual bool IsSingleConfig() { return config_type == ConfigType::SingleConfig; }
virtual Status ToNumber(int64_t *n) const { return {Status::NotOK, "not supported"}; }
virtual Status ToBool(bool *b) const { return {Status::NotOK, "not supported"}; }

ConfigType GetConfigType() const { return config_type; }
bool IsMultiConfig() const { return config_type == ConfigType::MultiConfig; }
bool IsSingleConfig() const { return config_type == ConfigType::SingleConfig; }

int line_number = 0;
bool readonly = true;
Expand All @@ -77,7 +78,7 @@ class StringField : public ConfigField {
public:
StringField(std::string *receiver, std::string s) : receiver_(receiver) { *receiver_ = std::move(s); }
~StringField() override = default;
std::string ToString() override { return *receiver_; }
std::string ToString() const override { return *receiver_; }
Status Set(const std::string &v) override {
*receiver_ = v;
return Status::OK();
Expand All @@ -94,7 +95,7 @@ class MultiStringField : public ConfigField {
this->config_type = ConfigType::MultiConfig;
}
~MultiStringField() override = default;
std::string ToString() override {
std::string ToString() const override {
std::string tmp;
for (auto &p : *receiver_) {
tmp += p + "\n";
Expand All @@ -118,8 +119,8 @@ class IntegerField : public ConfigField {
*receiver_ = n;
}
~IntegerField() override = default;
std::string ToString() override { return std::to_string(*receiver_); }
Status ToNumber(int64_t *n) override {
std::string ToString() const override { return std::to_string(*receiver_); }
Status ToNumber(int64_t *n) const override {
*n = *receiver_;
return Status::OK();
}
Expand All @@ -140,8 +141,8 @@ class OctalField : public ConfigField {
public:
OctalField(int *receiver, int n, int min, int max) : receiver_(receiver), min_(min), max_(max) { *receiver_ = n; }
~OctalField() override = default;
std::string ToString() override { return fmt::format("{:o}", *receiver_); }
Status ToNumber(int64_t *n) override {
std::string ToString() const override { return fmt::format("{:o}", *receiver_); }
Status ToNumber(int64_t *n) const override {
*n = *receiver_;
return Status::OK();
}
Expand All @@ -162,8 +163,8 @@ class YesNoField : public ConfigField {
public:
YesNoField(bool *receiver, bool b) : receiver_(receiver) { *receiver_ = b; }
~YesNoField() override = default;
std::string ToString() override { return *receiver_ ? "yes" : "no"; }
Status ToBool(bool *b) override {
std::string ToString() const override { return *receiver_ ? "yes" : "no"; }
Status ToBool(bool *b) const override {
*b = *receiver_;
return Status::OK();
}
Expand All @@ -189,14 +190,14 @@ class EnumField : public ConfigField {
}
~EnumField() override = default;

std::string ToString() override {
std::string ToString() const override {
for (const auto &e : enums_) {
if (e.val == *receiver_) return e.name;
}
return {};
}

Status ToNumber(int64_t *n) override {
Status ToNumber(int64_t *n) const override {
*n = *receiver_;
return Status::OK();
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/redis_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ uint64_t Connection::GetIdleTime() const { return static_cast<uint64_t>(util::Ge
// kTypeSlave -> Slave
// kTypeNormal -> Normal client
// kTypePubsub -> Client subscribed to Pub/Sub channels
uint64_t Connection::GetClientType() {
uint64_t Connection::GetClientType() const {
if (IsFlagEnabled(kSlave)) return kTypeSlave;

if (!subscribe_channels_.empty() || !subscribe_patterns_.empty()) return kTypePubsub;

return kTypeNormal;
}

std::string Connection::GetFlags() {
std::string Connection::GetFlags() const {
std::string flags;
if (IsFlagEnabled(kSlave)) flags.append("S");
if (IsFlagEnabled(kCloseAfterReply)) flags.append("c");
Expand All @@ -174,7 +174,7 @@ void Connection::EnableFlag(Flag flag) { flags_ |= flag; }

void Connection::DisableFlag(Flag flag) { flags_ &= (~flag); }

bool Connection::IsFlagEnabled(Flag flag) { return (flags_ & flag) > 0; }
bool Connection::IsFlagEnabled(Flag flag) const { return (flags_ & flag) > 0; }

void Connection::SubscribeChannel(const std::string &channel) {
for (const auto &chan : subscribe_channels_) {
Expand Down
14 changes: 7 additions & 7 deletions src/server/redis_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ class Connection : public EvbufCallbackBase<Connection> {
uint64_t GetAge() const;
uint64_t GetIdleTime() const;
void SetLastInteraction();
std::string GetFlags();
std::string GetFlags() const;
void EnableFlag(Flag flag);
void DisableFlag(Flag flag);
bool IsFlagEnabled(Flag flag);
bool IsFlagEnabled(Flag flag) const;

uint64_t GetID() const { return id_; }
void SetID(uint64_t id) { id_ = id; }
std::string GetName() const { return name_; }
void SetName(std::string name) { name_ = std::move(name); }
std::string GetAddr() { return addr_; }
std::string GetAddr() const { return addr_; }
void SetAddr(std::string ip, uint32_t port);
void SetLastCmd(std::string cmd) { last_cmd_ = std::move(cmd); }
std::string GetIP() const { return ip_; }
uint32_t GetPort() const { return port_; }
void SetListeningPort(int port) { listening_port_ = port; }
int GetListeningPort() const { return listening_port_; }
void SetAnnounceIP(std::string ip) { announce_ip_ = std::move(ip); }
std::string GetAnnounceIP() { return !announce_ip_.empty() ? announce_ip_ : ip_; }
std::string GetAnnounceAddr() { return GetAnnounceIP() + ":" + std::to_string(listening_port_); }
uint64_t GetClientType();
std::string GetAnnounceIP() const { return !announce_ip_.empty() ? announce_ip_ : ip_; }
std::string GetAnnounceAddr() const { return GetAnnounceIP() + ":" + std::to_string(listening_port_); }
uint64_t GetClientType() const;
Server *GetServer() { return svr_; }

bool IsAdmin() const { return is_admin_; }
void BecomeAdmin() { is_admin_ = true; }
void BecomeUser() { is_admin_ = false; }
std::string GetNamespace() { return ns_; }
std::string GetNamespace() const { return ns_; }
void SetNamespace(std::string ns) { ns_ = std::move(ns); }

void NeedFreeBufferEvent(bool need_free = true) { need_free_bev_ = need_free; }
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ void Server::AdjustOpenFilesLimit() {
}
}

std::string ServerLogData::Encode() {
std::string ServerLogData::Encode() const {
if (type_ == kReplIdLog) {
return std::string(1, kReplIdTag) + " " + content_;
}
Expand Down
16 changes: 8 additions & 8 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ class ServerLogData {
ServerLogData() = default;
explicit ServerLogData(ServerLogType type, std::string content) : type_(type), content_(std::move(content)) {}

ServerLogType GetType() { return type_; }
std::string GetContent() { return content_; }
std::string Encode();
ServerLogType GetType() const { return type_; }
std::string GetContent() const { return content_; }
std::string Encode() const;
Status Decode(const rocksdb::Slice &blob);

private:
Expand All @@ -172,8 +172,8 @@ class Server {
Status Start();
void Stop();
void Join();
bool IsStopped() { return stop_; }
bool IsLoading() { return is_loading_; }
bool IsStopped() const { return stop_; }
bool IsLoading() const { return is_loading_; }
Config *GetConfig() { return config_; }
static Status LookupAndCreateCommand(const std::string &cmd_name, std::unique_ptr<redis::Commander> *cmd);
void AdjustOpenFilesLimit();
Expand All @@ -183,11 +183,11 @@ class Server {
Status AddSlave(redis::Connection *conn, rocksdb::SequenceNumber next_repl_seq);
void DisconnectSlaves();
void CleanupExitedSlaves();
bool IsSlave() { return !master_host_.empty(); }
bool IsSlave() const { return !master_host_.empty(); }
void FeedMonitorConns(redis::Connection *conn, const std::vector<std::string> &tokens);
void IncrFetchFileThread() { fetch_file_threads_num_++; }
void DecrFetchFileThread() { fetch_file_threads_num_--; }
int GetFetchFileThreadNum() { return fetch_file_threads_num_; }
int GetFetchFileThreadNum() const { return fetch_file_threads_num_; }

int PublishMessage(const std::string &channel, const std::string &msg);
void SubscribeChannel(const std::string &channel, redis::Connection *conn);
Expand All @@ -197,7 +197,7 @@ class Server {
std::vector<ChannelSubscribeNum> *channel_subscribe_nums);
void PSubscribeChannel(const std::string &pattern, redis::Connection *conn);
void PUnsubscribeChannel(const std::string &pattern, redis::Connection *conn);
int GetPubSubPatternSize() { return static_cast<int>(pubsub_patterns_.size()); }
size_t GetPubSubPatternSize() const { return pubsub_patterns_.size(); }

void BlockOnKey(const std::string &key, redis::Connection *conn);
void UnblockOnKey(const std::string &key, redis::Connection *conn);
Expand Down
2 changes: 1 addition & 1 deletion src/stats/stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void Stats::TrackInstantaneousMetric(int metric, uint64_t current_reading) {
inst_metrics[metric].last_sample_count = current_reading;
}

uint64_t Stats::GetInstantaneousMetric(int metric) {
uint64_t Stats::GetInstantaneousMetric(int metric) const {
uint64_t sum = 0;
for (uint64_t sample : inst_metrics[metric].samples) sum += sample;
return sum / STATS_METRIC_SAMPLES;
Expand Down
2 changes: 1 addition & 1 deletion src/stats/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ class Stats {
void IncrPSyncOKCounter() { psync_ok_counter.fetch_add(1, std::memory_order_relaxed); }
static int64_t GetMemoryRSS();
void TrackInstantaneousMetric(int metric, uint64_t current_reading);
uint64_t GetInstantaneousMetric(int metric);
uint64_t GetInstantaneousMetric(int metric) const;
};
4 changes: 2 additions & 2 deletions src/storage/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,11 @@ rocksdb::Status SubKeyScanner::Scan(RedisType type, const Slice &user_key, const
return rocksdb::Status::OK();
}

RedisType WriteBatchLogData::GetRedisType() { return type_; }
RedisType WriteBatchLogData::GetRedisType() const { return type_; }

std::vector<std::string> *WriteBatchLogData::GetArguments() { return &args_; }

std::string WriteBatchLogData::Encode() {
std::string WriteBatchLogData::Encode() const {
std::string ret = std::to_string(type_);
for (const auto &arg : args_) {
ret += " " + arg;
Expand Down
6 changes: 3 additions & 3 deletions src/storage/redis_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Database {
explicit LatestSnapShot(engine::Storage *storage)
: storage_(storage), snapshot_(storage_->GetDB()->GetSnapshot()) {}
~LatestSnapShot() { storage_->GetDB()->ReleaseSnapshot(snapshot_); }
const rocksdb::Snapshot *GetSnapShot() { return snapshot_; }
const rocksdb::Snapshot *GetSnapShot() const { return snapshot_; }

LatestSnapShot(const LatestSnapShot &) = delete;
LatestSnapShot &operator=(const LatestSnapShot &) = delete;
Expand All @@ -94,9 +94,9 @@ class WriteBatchLogData {
explicit WriteBatchLogData(RedisType type) : type_(type) {}
explicit WriteBatchLogData(RedisType type, std::vector<std::string> &&args) : type_(type), args_(std::move(args)) {}

RedisType GetRedisType();
RedisType GetRedisType() const;
std::vector<std::string> *GetArguments();
std::string Encode();
std::string Encode() const;
Status Decode(const rocksdb::Slice &blob);

private:
Expand Down
Loading

0 comments on commit 45d741a

Please sign in to comment.