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: floyd supports one key for one data structure #240

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
6 changes: 3 additions & 3 deletions pikiwidb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ logfile stdout
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 3
databases 16

################################ SNAPSHOTTING #################################
#
Expand All @@ -57,7 +57,7 @@ databases 3
# points by adding a save directive with a single empty string argument
# like in the following example:
#
save ""
# save ""

#save 900 1
#save 300 10
Expand Down Expand Up @@ -348,4 +348,4 @@ db-instance-num 5
# default 86400 * 7
rocksdb-ttl-second 604800
# default 86400 * 3
rocksdb-periodic-second 259200;
rocksdb-periodic-second 259200
61 changes: 61 additions & 0 deletions pikiwidbtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# clear the log file
function cleanup() {
rm -rf ./log[0-9]*
rm -rf ./db[0-9]*
rm -rf dbsync/
rm src/redis-server
}

# check if tcl is installed
function check_tcl {
if [ -z "$(which tclsh)" ]; then
echo "tclsh is not installed"
exit 1
fi
}

# handle different build directories.
function setup_build_dir {
BUILD_DIR="./bin"
echo "BUILD_DIR: $BUILD_DIR"
}

# setup pikiwidb bin and conf
function setup_pika_bin {
PIKIWIDB_BIN="./$BUILD_DIR/pikiwidb"
if [ ! -f "$PIKIWIDB_BIN" ]; then
echo "pikiwidb bin not found"
exit 1
fi
cp $PIKIWIDB_BIN src/redis-server
cp ./pikiwidb.conf tests/assets/default.conf
}


cleanup

check_tcl

setup_build_dir

setup_pika_bin

echo "run pikiwidb tests $1"

if [ "$1" == "all" ]; then
tclsh tests/test_helper.tcl --clients 1
else
tclsh tests/test_helper.tcl --clients 1 --single unit/$1
fi

if [ $? -ne 0 ]; then
echo "pikiwidb tests failed"
cleanup
exit 1
fi

#if [ "$2" == "clean" ];
# cleanup
#fi
4 changes: 4 additions & 0 deletions src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ void CmdRes::SetRes(CmdRes::CmdRet _ret, const std::string& content) {
case kInvalidCursor:
AppendStringRaw("-ERR invalid cursor");
break;
case kmultikey:
AppendStringRaw("-WRONGTYPE Operation against a key holding the wrong kind of value");
AppendStringRaw(CRLF);
break;
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CmdRes {
kErrOther,
KIncrByOverFlow,
kInvalidCursor,
kmultikey,
};

CmdRes() = default;
Expand Down
30 changes: 29 additions & 1 deletion src/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ void HSetCmd::DoCmd(PClient* client) {
s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HSet(client->Key(), field, value, &temp);
if (s.ok()) {
ret += temp;
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
return;
} else {
// FIXME(century): need txn, if bw crashes, it should rollback
client->SetRes(CmdRes::kErrOther);
return;
}
}

client->AppendInteger(ret);
}

Expand All @@ -64,6 +66,8 @@ void HGetCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HGet(client->Key(), field, &value);
if (s.ok()) {
client->AppendString(value);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else if (s.IsNotFound()) {
client->AppendString("");
} else {
Expand All @@ -83,6 +87,10 @@ void HDelCmd::DoCmd(PClient* client) {
int32_t res{};
std::vector<std::string> fields(client->argv_.begin() + 2, client->argv_.end());
auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HDel(client->Key(), fields, &res);
if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
return;
}
if (!s.ok() && !s.IsNotFound()) {
client->SetRes(CmdRes::kErrOther, s.ToString());
return;
Expand All @@ -107,6 +115,8 @@ void HMSetCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HMSet(client->Key(), client->Fvs());
if (s.ok()) {
client->SetRes(CmdRes::kOK);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
Expand Down Expand Up @@ -135,6 +145,8 @@ void HMGetCmd::DoCmd(PClient* client) {
client->AppendString("");
}
}
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
Expand Down Expand Up @@ -185,6 +197,8 @@ void HGetAllCmd::DoCmd(PClient* client) {
if (s.ok() || s.IsNotFound()) {
client->AppendArrayLen(total_fv * 2);
client->AppendStringRaw(raw);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
Expand All @@ -209,6 +223,8 @@ void HKeysCmd::DoCmd(PClient* client) {
}
// update fields
client->Fields() = std::move(fields);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
Expand All @@ -227,6 +243,8 @@ void HLenCmd::DoCmd(PClient* client) {
auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HLen(client->Key(), &len);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(len);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "something wrong in hlen");
}
Expand All @@ -245,6 +263,8 @@ void HStrLenCmd::DoCmd(PClient* client) {
auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HStrlen(client->Key(), client->argv_[2], &len);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(len);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "something wrong in hstrlen");
}
Expand Down Expand Up @@ -320,6 +340,8 @@ void HValsCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HVals(client->Key(), &valueVec);
if (s.ok() || s.IsNotFound()) {
client->AppendStringVector(valueVec);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "hvals cmd error");
}
Expand Down Expand Up @@ -350,6 +372,8 @@ void HIncrbyFloatCmd::DoCmd(PClient* client) {
->HIncrbyfloat(client->Key(), client->argv_[2], client->argv_[3], &newValue);
if (s.ok() || s.IsNotFound()) {
client->AppendString(newValue);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "hvals cmd error");
}
Expand All @@ -371,6 +395,8 @@ void HSetNXCmd::DoCmd(PClient* client) {
->HSetnx(client->Key(), client->argv_[2], client->argv_[3], &temp);
if (s.ok()) {
client->AppendInteger(temp);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kSyntaxErr, "hsetnx cmd error");
}
Expand All @@ -397,6 +423,8 @@ void HIncrbyCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HIncrby(client->Key(), client->argv_[2], int_by, &temp);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(temp);
} else if (s.IsInvalidArgument()) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "hincrby cmd error");
}
Expand Down
60 changes: 4 additions & 56 deletions src/cmd_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,8 @@ bool PersistCmd::DoInitial(PClient* client) {

void PersistCmd::DoCmd(PClient* client) {
std::map<storage::DataType, storage::Status> type_status;
auto res = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Persist(client->Key(), &type_status);
if (res != -1) {
client->AppendInteger(res);
} else {
std::string cnt;
for (auto const& s : type_status) {
cnt.append(storage::DataTypeToString[s.first]);
cnt.append(" - ");
cnt.append(s.second.ToString());
cnt.append(";");
}
client->SetRes(CmdRes::kErrOther, cnt);
}
auto res = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Persist(client->Key());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除 map

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

client->AppendInteger(res);
}

KeysCmd::KeysCmd(const std::string& name, int16_t arity)
Expand Down Expand Up @@ -177,49 +166,8 @@ bool PttlCmd::DoInitial(PClient* client) {

// like Blackwidow , Floyd still possible has same key in different data structure
void PttlCmd::DoCmd(PClient* client) {
std::map<storage::DataType, rocksdb::Status> type_status;
auto type_timestamp = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->TTL(client->Key(), &type_status);
for (const auto& item : type_timestamp) {
// mean operation exception errors happen in database
if (item.second == -3) {
client->SetRes(CmdRes::kErrOther, "ttl internal error");
return;
}
}
if (type_timestamp[storage::kStrings] != -2) {
if (type_timestamp[storage::kStrings] == -1) {
client->AppendInteger(-1);
} else {
client->AppendInteger(type_timestamp[storage::kStrings] * 1000);
}
} else if (type_timestamp[storage::kHashes] != -2) {
if (type_timestamp[storage::kHashes] == -1) {
client->AppendInteger(-1);
} else {
client->AppendInteger(type_timestamp[storage::kHashes] * 1000);
}
} else if (type_timestamp[storage::kLists] != -2) {
if (type_timestamp[storage::kLists] == -1) {
client->AppendInteger(-1);
} else {
client->AppendInteger(type_timestamp[storage::kLists] * 1000);
}
} else if (type_timestamp[storage::kSets] != -2) {
if (type_timestamp[storage::kSets] == -1) {
client->AppendInteger(-1);
} else {
client->AppendInteger(type_timestamp[storage::kSets] * 1000);
}
} else if (type_timestamp[storage::kZSets] != -2) {
if (type_timestamp[storage::kZSets] == -1) {
client->AppendInteger(-1);
} else {
client->AppendInteger(type_timestamp[storage::kZSets] * 1000);
}
} else {
// this key not exist
client->AppendInteger(-2);
}
auto timestamp = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->TTL(client->Key());
client->AppendInteger(timestamp);
}

} // namespace pikiwidb
Loading
Loading