Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add and implement the incrby commandfeat #44

Merged
merged 9 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace pikiwidb {
// string cmd
const std::string kCmdNameSet = "set";
const std::string kCmdNameGet = "get";
const std::string kCmdNameIncrby = "incrby";

// multi
const std::string kCmdNameMulti = "multi";
Expand Down
37 changes: 37 additions & 0 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "cmd_kv.h"
#include "pstd/pstd_string.h"
#include "store.h"

namespace pikiwidb {
Expand Down Expand Up @@ -48,4 +49,40 @@ void SetCmd::DoCmd(PClient* client) {
client->SetRes(CmdRes::kOk);
}

IncrbyCmd::IncrbyCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString) {}

bool IncrbyCmd::DoInitial(PClient* client) {
if (client->argv_.size() < 3) {
jettcc marked this conversation as resolved.
Show resolved Hide resolved
client->SetRes(CmdRes::kWrongNum, "incrby");
return false;
}
if (!(pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by_))) {
client->SetRes(CmdRes::kInvalidInt);
return false;
}
client->SetKey(client->argv_[1]);
return true;
}

void IncrbyCmd::DoCmd(PClient* client) {
PObject* value;
jettcc marked this conversation as resolved.
Show resolved Hide resolved
PError err = PSTORE.Incrby(client->Key(), by_, &new_value_);
switch (err) {
case PError_type:
client->SetRes(CmdRes::kInvalidInt);
break;
case PError_notExist: // key not exist, set a new value
PSTORE.ClearExpire(client->Key()); // clear key's old ttl
PSTORE.SetValue(client->Key(), PObject::CreateString(by_));
client->SetRes(CmdRes::kOk);
break;
case PError_ok:
client->AppendInteger(new_value_);
break;
default:
client->SetRes(CmdRes::kErrOther, "incrby cmd error");
break;
}
}
} // namespace pikiwidb
12 changes: 12 additions & 0 deletions src/cmd_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ class SetCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

class IncrbyCmd : public BaseCmd {
public:
IncrbyCmd(const std::string &name, int16_t arity);

protected:
bool DoInitial(PClient *client) override;

private:
int64_t by_, new_value_ = 0;
jettcc marked this conversation as resolved.
Show resolved Hide resolved
void DoCmd(PClient *client) override;
};

} // namespace pikiwidb
3 changes: 3 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void CmdTableManager::InitCmdTable() {
cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr)));
std::unique_ptr<BaseCmd> setPtr = std::make_unique<SetCmd>(kCmdNameSet, -3);
cmds_->insert(std::make_pair(kCmdNameSet, std::move(setPtr)));

std::unique_ptr<BaseCmd> incrbyPtr = std::make_unique<IncrbyCmd>(kCmdNameIncrby, -3);
jettcc marked this conversation as resolved.
Show resolved Hide resolved
cmds_->insert(std::make_pair(kCmdNameIncrby, std::move(incrbyPtr)));
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down
37 changes: 35 additions & 2 deletions src/store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ bool PStore::DeleteKey(const PString& key) {
size_t ret = 0;
// erase() from folly ConcurrentHashmap will throw an exception if hash function crashes
try {
ret = db->erase(key);
ret = db->erase(key);
} catch (const std::exception& e) {
return false;
return false;
}

return ret != 0;
Expand Down Expand Up @@ -556,6 +556,39 @@ PObject* PStore::SetValue(const PString& key, PObject&& value) {
return const_cast<PObject*>(&obj);
}

PError PStore::Incrby(const PString& key, int64_t value, int64_t* ret) {
PObject* old_value;
jettcc marked this conversation as resolved.
Show resolved Hide resolved
auto db = &dbs_[dbno_];

// shared when reading
std::shared_lock<std::shared_mutex> lock(mutex_);
jettcc marked this conversation as resolved.
Show resolved Hide resolved
jettcc marked this conversation as resolved.
Show resolved Hide resolved
PError err = getValueByType(key, old_value, PType_string);
if (err != PError_ok) {
return err;
}
char* end = nullptr;
auto str = pikiwidb::GetDecodedString(old_value);
int64_t ival = strtoll(str->c_str(), &end, 10);
if (*end != 0) {
// value is not a integer
return PError_type;
}

PObject new_value;
*ret = ival + value;
new_value = PObject::CreateString((long)(*ret));
new_value.lru = PObject::lruclock;
auto [realObj, status] = db->insert_or_assign(key, std::move(new_value));
const PObject& obj = realObj->second;

// put this key to sync list
if (!waitSyncKeys_.empty()) {
waitSyncKeys_[dbno_].insert_or_assign(key, &obj);
}

return PError_ok;
}

void PStore::SetExpire(const PString& key, uint64_t when) const { expiredDBs_[dbno_].SetExpire(key, when); }

int64_t PStore::TTL(const PString& key, uint64_t now) { return expiredDBs_[dbno_].TTL(key, now); }
Expand Down
12 changes: 9 additions & 3 deletions src/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <folly/concurrency/ConcurrentHashMap.h>
#include <map>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <vector>

namespace pikiwidb {
Expand Down Expand Up @@ -117,6 +119,8 @@ class PStore {
PError GetValueByTypeNoTouch(const PString& key, PObject*& value, PType type = PType_invalid);

PObject* SetValue(const PString& key, PObject&& value);
// incr
PError Incrby(const PString& key, int64_t value, int64_t* ret);

// for expire key
enum ExpireResult : std::int8_t {
Expand Down Expand Up @@ -157,6 +161,8 @@ class PStore {

private:
PStore() : dbno_(0) {}
// mutex
mutable std::shared_mutex mutex_;

PError getValueByType(const PString& key, PObject*& value, PType type = PType_invalid, bool touch = true);

Expand Down Expand Up @@ -187,7 +193,7 @@ class PStore {
size_t Size() const { return blockedClients_.size(); }

private:
using Clients = std::list<std::tuple<std::weak_ptr<PClient>, uint64_t, ListPosition> >;
using Clients = std::list<std::tuple<std::weak_ptr<PClient>, uint64_t, ListPosition>>;
using WaitingList = folly::ConcurrentHashMap<PString, Clients, my_hash, std::equal_to<PString>>;

WaitingList blockedClients_;
Expand All @@ -199,9 +205,9 @@ class PStore {
mutable std::vector<PDB> dbs_;
mutable std::vector<ExpiredDB> expiredDBs_;
std::vector<BlockedClients> blockedClients_;
std::vector<std::unique_ptr<PDumpInterface> > backends_;
std::vector<std::unique_ptr<PDumpInterface>> backends_;

using ToSyncDB = folly::ConcurrentHashMap<PString, const PObject*, my_hash, std::equal_to<PString> >;
using ToSyncDB = folly::ConcurrentHashMap<PString, const PObject*, my_hash, std::equal_to<PString>>;
std::vector<ToSyncDB> waitSyncKeys_;
int dbno_ = -1;
};
Expand Down