Skip to content

Commit

Permalink
fix incr cmd time to millionsseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyuecai committed Aug 9, 2024
1 parent b77a048 commit 0f28299
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 71 deletions.
8 changes: 4 additions & 4 deletions include/pika_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class IncrCmd : public Cmd {
int64_t new_value_ = 0;
void DoInitial() override;
rocksdb::Status s_;
int64_t expired_timestamp_sec_ = 0;
int64_t expired_timestamp_millsec_ = 0;
std::string ToRedisProtocol() override;
};

Expand All @@ -140,7 +140,7 @@ class IncrbyCmd : public Cmd {
int64_t by_ = 0, new_value_ = 0;
void DoInitial() override;
rocksdb::Status s_;
int64_t expired_timestamp_sec_ = 0;
int64_t expired_timestamp_millsec_ = 0;
std::string ToRedisProtocol() override;
};

Expand All @@ -165,7 +165,7 @@ class IncrbyfloatCmd : public Cmd {
double by_ = 0;
void DoInitial() override;
rocksdb::Status s_;
int64_t expired_timestamp_sec_ = 0;
int64_t expired_timestamp_millsec_ = 0;
std::string ToRedisProtocol() override;
};

Expand Down Expand Up @@ -260,7 +260,7 @@ class AppendCmd : public Cmd {
std::string new_value_;
void DoInitial() override;
rocksdb::Status s_;
int64_t expired_timestamp_sec_ = 0;
int64_t expired_timestamp_millsec_ = 0;
std::string ToRedisProtocol() override;
};

Expand Down
16 changes: 8 additions & 8 deletions src/pika_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void IncrCmd::DoInitial() {
}

void IncrCmd::Do() {
s_ = db_->storage()->Incrby(key_, 1, &new_value_, &expired_timestamp_sec_);
s_ = db_->storage()->Incrby(key_, 1, &new_value_, &expired_timestamp_millsec_);
if (s_.ok()) {
res_.AppendContent(":" + std::to_string(new_value_));
AddSlotKey("k", key_, db_);
Expand Down Expand Up @@ -296,7 +296,7 @@ std::string IncrCmd::ToRedisProtocol() {
RedisAppendContent(content, key_);
// time_stamp
char buf[100];
auto time_stamp = expired_timestamp_sec_;
auto time_stamp = expired_timestamp_millsec_ > 0 ? expired_timestamp_millsec_ / 1000 : expired_timestamp_millsec_;
pstd::ll2string(buf, sizeof(buf), time_stamp);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
Expand All @@ -321,7 +321,7 @@ void IncrbyCmd::DoInitial() {
}

void IncrbyCmd::Do() {
s_ = db_->storage()->Incrby(key_, by_, &new_value_, &expired_timestamp_sec_);
s_ = db_->storage()->Incrby(key_, by_, &new_value_, &expired_timestamp_millsec_);
if (s_.ok()) {
res_.AppendContent(":" + std::to_string(new_value_));
AddSlotKey("k", key_, db_);
Expand Down Expand Up @@ -360,7 +360,7 @@ std::string IncrbyCmd::ToRedisProtocol() {
RedisAppendContent(content, key_);
// time_stamp
char buf[100];
auto time_stamp = expired_timestamp_sec_;
auto time_stamp = expired_timestamp_millsec_ > 0 ? expired_timestamp_millsec_ / 1000 : expired_timestamp_millsec_;
pstd::ll2string(buf, sizeof(buf), time_stamp);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
Expand All @@ -386,7 +386,7 @@ void IncrbyfloatCmd::DoInitial() {
}

void IncrbyfloatCmd::Do() {
s_ = db_->storage()->Incrbyfloat(key_, value_, &new_value_, &expired_timestamp_sec_);
s_ = db_->storage()->Incrbyfloat(key_, value_, &new_value_, &expired_timestamp_millsec_);
if (s_.ok()) {
res_.AppendStringLenUint64(new_value_.size());
res_.AppendContent(new_value_);
Expand Down Expand Up @@ -429,7 +429,7 @@ std::string IncrbyfloatCmd::ToRedisProtocol() {
RedisAppendContent(content, key_);
// time_stamp
char buf[100];
auto time_stamp = expired_timestamp_sec_;
auto time_stamp = expired_timestamp_millsec_ > 0 ? expired_timestamp_millsec_ / 1000 : expired_timestamp_millsec_;
pstd::ll2string(buf, sizeof(buf), time_stamp);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
Expand Down Expand Up @@ -560,7 +560,7 @@ void AppendCmd::DoInitial() {

void AppendCmd::Do() {
int32_t new_len = 0;
s_ = db_->storage()->Append(key_, value_, &new_len, &expired_timestamp_sec_, new_value_);
s_ = db_->storage()->Append(key_, value_, &new_len, &expired_timestamp_millsec_, new_value_);
if (s_.ok() || s_.IsNotFound()) {
res_.AppendInteger(new_len);
AddSlotKey("k", key_, db_);
Expand Down Expand Up @@ -595,7 +595,7 @@ std::string AppendCmd::ToRedisProtocol() {
RedisAppendContent(content, key_);
// time_stamp
char buf[100];
auto time_stamp = expired_timestamp_sec_;
auto time_stamp = expired_timestamp_millsec_ > 0 ? expired_timestamp_millsec_ / 1000 : expired_timestamp_millsec_;
pstd::ll2string(buf, sizeof(buf), time_stamp);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
Expand Down
4 changes: 2 additions & 2 deletions src/storage/include/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Storage {
// If key already exists and is a string, this command appends the value at
// the end of the string
// return the length of the string after the append operation
Status Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_sec, std::string& out_new_value);
Status Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_millsec, std::string& out_new_value);

// Count the number of set bits (population counting) in a string.
// return the number of bits set to 1
Expand All @@ -285,7 +285,7 @@ class Storage {

// Increments the number stored at key by increment.
// If the key does not exist, it is set to 0 before performing the operation
Status Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_sec);
Status Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_millsec);

// Increment the string representing a floating point number
// stored at key by the specified increment.
Expand Down
4 changes: 2 additions & 2 deletions src/storage/src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Redis {
virtual Status SetsTTL(const Slice& key, int64_t* ttl_millsec, std::string&& prefetch_meta = {});

// Strings Commands
Status Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_sec, std::string& out_new_value);
Status Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_millsec, std::string& out_new_value);
Status BitCount(const Slice& key, int64_t start_offset, int64_t end_offset, int32_t* ret, bool have_range);
Status BitOp(BitOpType op, const std::string& dest_key, const std::vector<std::string>& src_keys, std::string &value_to_dest, int64_t* ret);
Status Decrby(const Slice& key, int64_t value, int64_t* ret);
Expand All @@ -163,7 +163,7 @@ class Redis {
Status GetrangeWithValue(const Slice& key, int64_t start_offset, int64_t end_offset,
std::string* ret, std::string* value, int64_t* ttl_millsec);
Status GetSet(const Slice& key, const Slice& value, std::string* old_value);
Status Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_sec);
Status Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_millsec);
Status Incrbyfloat(const Slice& key, const Slice& value, std::string* ret, int64_t* expired_timestamp_sec);
Status MSet(const std::vector<KeyValue>& kvs);
Status MSetnx(const std::vector<KeyValue>& kvs, int32_t* ret);
Expand Down
12 changes: 6 additions & 6 deletions src/storage/src/redis_strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ Status Redis::ScanStringsKeyNum(KeyInfo* key_info) {
return Status::OK();
}

Status Redis::Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_sec, std::string& out_new_value) {
Status Redis::Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_millsec, std::string& out_new_value) {
std::string old_value;
*ret = 0;
*expired_timestamp_sec = 0;
*expired_timestamp_millsec = 0;
ScopeRecordLock l(lock_mgr_, key);

BaseKey base_key(key);
Expand Down Expand Up @@ -94,13 +94,13 @@ Status Redis::Append(const Slice& key, const Slice& value, int32_t* ret, int64_t
StringsValue strings_value(new_value);
strings_value.SetEtime(timestamp);
*ret = static_cast<int32_t>(new_value.size());
*expired_timestamp_sec = timestamp;
*expired_timestamp_millsec = timestamp;
return db_->Put(default_write_options_, base_key.Encode(), strings_value.Encode());
}
} else if (s.IsNotFound()) {
*ret = static_cast<int32_t>(value.size());
StringsValue strings_value(value);
*expired_timestamp_sec = 0;
*expired_timestamp_millsec = 0;
return db_->Put(default_write_options_, base_key.Encode(), strings_value.Encode());
}
return s;
Expand Down Expand Up @@ -619,7 +619,7 @@ Status Redis::GetSet(const Slice& key, const Slice& value, std::string* old_valu
return db_->Put(default_write_options_, base_key.Encode(), strings_value.Encode());
}

Status Redis::Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_sec) {
Status Redis::Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_millsec) {
std::string old_value;
std::string new_value;
ScopeRecordLock l(lock_mgr_, key);
Expand Down Expand Up @@ -657,7 +657,7 @@ Status Redis::Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* exp
new_value = std::to_string(*ret);
StringsValue strings_value(new_value);
strings_value.SetEtime(timestamp);
*expired_timestamp_sec = timestamp;
*expired_timestamp_millsec = timestamp;
return db_->Put(default_write_options_, base_key.Encode(), strings_value.Encode());
}
} else if (s.IsNotFound()) {
Expand Down
8 changes: 4 additions & 4 deletions src/storage/src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ Status Storage::GetrangeWithValue(const Slice& key, int64_t start_offset, int64_
return inst->GetrangeWithValue(key, start_offset, end_offset, ret, value, ttl_millsec);
}

Status Storage::Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_sec, std::string& out_new_value) {
Status Storage::Append(const Slice& key, const Slice& value, int32_t* ret, int64_t* expired_timestamp_millsec, std::string& out_new_value) {
auto& inst = GetDBInstance(key);
return inst->Append(key, value, ret, expired_timestamp_sec, out_new_value);
return inst->Append(key, value, ret, expired_timestamp_millsec, out_new_value);
}

Status Storage::BitCount(const Slice& key, int64_t start_offset, int64_t end_offset, int32_t* ret, bool have_range) {
Expand Down Expand Up @@ -345,9 +345,9 @@ Status Storage::Decrby(const Slice& key, int64_t value, int64_t* ret) {
return inst->Decrby(key, value, ret);
}

Status Storage::Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_sec) {
Status Storage::Incrby(const Slice& key, int64_t value, int64_t* ret, int64_t* expired_timestamp_millsec) {
auto& inst = GetDBInstance(key);
return inst->Incrby(key, value, ret, expired_timestamp_sec);
return inst->Incrby(key, value, ret, expired_timestamp_millsec);
}

Status Storage::Incrbyfloat(const Slice& key, const Slice& value, std::string* ret, int64_t* expired_timestamp_sec) {
Expand Down
Loading

0 comments on commit 0f28299

Please sign in to comment.