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 list cmd lset and go ut #141

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -86,6 +86,7 @@ const std::string kCmdNameSUnion = "sunion";
const std::string kCmdNameLPush = "lpush";
const std::string kCmdNameRPush = "rpush";
const std::string kCmdNameRPop = "rpop";
const std::string kCmdNameLSet = "lset";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
Expand Down
34 changes: 34 additions & 0 deletions src/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

#include "cmd_list.h"

#include <pstd_string.h>

#include "store.h"

namespace pikiwidb {
Expand Down Expand Up @@ -47,6 +50,37 @@ void RPushCmd::DoCmd(PClient* client) {
client->SetRes(CmdRes::kSyntaxErr, "rpush cmd error");
}
}
LSetCmd::LSetCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {}
bool LSetCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}
void LSetCmd::DoCmd(PClient* client) {
// isVaildNumber ensures that the string is in decimal format,
// while strtol ensures that the string is within the range of long type
const std::string index_str = client->argv_[2];

if (IsValidNumber(index_str)) {
int64_t val = 0;
if (1 != pstd::String2int(index_str, &val)) {
client->SetRes(CmdRes::kErrOther, "lset cmd error"); // this will not happend in normal case
return;
}
storage::Status s = PSTORE.GetBackend()->LSet(client->Key(), val, client->argv_[3]);
if (s.ok()) {
client->SetRes(CmdRes::kOK);
} else if (s.IsNotFound()) {
client->SetRes(CmdRes::kNotFound);
} else if (s.IsCorruption()) {
client->SetRes(CmdRes::kOutOfRange);
} else {
client->SetRes(CmdRes::kSyntaxErr, "lset cmd error"); // just a safeguard
};
} else {
client->SetRes(CmdRes::kInvalidInt);
}
}

RPopCmd::RPopCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {}
Expand Down
10 changes: 10 additions & 0 deletions src/cmd_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ class RPopCmd : public BaseCmd {
void DoCmd(PClient* client) override;
};

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

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;
};
} // namespace pikiwidb
1 change: 1 addition & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(LPush, -3);
ADD_COMMAND(RPush, -3);
ADD_COMMAND(RPop, 2);
ADD_COMMAND(LSet, 4);
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down
16 changes: 0 additions & 16 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,6 @@ bool Strtol(const char* ptr, size_t nBytes, long* outVal) {
return pEnd == ptr + nBytes;
}

bool Strtoll(const char* ptr, size_t nBytes, long long* outVal) {
if (nBytes == 0 || nBytes > 20) {
return false;
}

errno = 0;
char* pEnd = 0;
*outVal = strtoll(ptr, &pEnd, 0);

if (errno == ERANGE || errno == EINVAL) {
return false;
}

return pEnd == ptr + nBytes;
}

bool Strtof(const char* ptr, size_t nBytes, float* outVal) {
if (nBytes == 0 || nBytes > 20) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/pstring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace pikiwidb {

PObject PObject::CreateString(const PString& value) {
PObject obj(kPTypeString);
long val;
long val = 0;
578223592 marked this conversation as resolved.
Show resolved Hide resolved

// isVaildNumber ensures that the string is in decimal format,
// while strtol ensures that the string is within the range of long type
Expand Down
3 changes: 3 additions & 0 deletions tests/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ var _ = Describe("Keyspace", Ordered, func() {
n, err = client.Exists(ctx, "key1", "key2", "notExistKey").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))

_, err = client.Del(ctx, "key1", "key2").Result()
Expect(err).NotTo(HaveOccurred())
})

It("Del", func() {
Expand Down
26 changes: 26 additions & 0 deletions tests/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ var _ = Describe("List", Ordered, func() {
Expect(client.LPush(ctx, "mylistLPUSH", "hello", ).Val()).To(Equal(int64(2)))

// Expect(client.LRange(ctx,"mylistLPUSH",0,-1).Val()).To(Equal([]string{"hello", "world"})) //After the LRange command is developed, uncomment it to test LRange command.
Expect(client.Del(ctx,"mylistLPUSH").Err()).NotTo(HaveOccurred());
})
It("Cmd RPUSH", func() {
log.Println("Cmd RPUSH Begin")
Expect(client.LPush(ctx, "mylistRPUSH", "hello", ).Val()).To(Equal(int64(1)))
Expect(client.LPush(ctx, "mylistRPUSH", "world", ).Val()).To(Equal(int64(2)))

// Expect(client.LRange(ctx,"mylistRPUSH",0,-1).Val()).To(Equal([]string{"hello", "world"})) //After the LRange command is developed, uncomment it to test LRange command.
Expect(client.Del(ctx,"mylistRPUSH").Err()).NotTo(HaveOccurred());
})

It("should RPop", func() {
Expand All @@ -99,4 +101,28 @@ var _ = Describe("List", Ordered, func() {
err := client.Do(ctx, "RPOP", "list", 1, 2).Err()
Expect(err).To(MatchError(ContainSubstring("ERR wrong number of arguments for 'rpop' command")))
})

It("should LSet", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())

lSet := client.LSet(ctx, "list", 0, "four")
Expect(lSet.Err()).NotTo(HaveOccurred())
Expect(lSet.Val()).To(Equal("OK"))

lSet = client.LSet(ctx, "list", -2, "five")
Expect(lSet.Err()).NotTo(HaveOccurred())
Expect(lSet.Val()).To(Equal("OK"))

// lRange := client.LRange(ctx, "list", 0, -1) //After the LRange command is developed, uncomment it to test command.
// Expect(lRange.Err()).NotTo(HaveOccurred())
// Expect(lRange.Val()).To(Equal([]string{"four", "five", "three"}))

Expect(client.Del(ctx,"list").Err()).NotTo(HaveOccurred());
})

})
94 changes: 47 additions & 47 deletions tests/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var _ = Describe("Set", Ordered, func() {
BeforeEach(func() {
client = s.NewClient()
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
time.Sleep(1 * time.Second)
time.Sleep(1 * time.Second)
})

// nodes that run after the spec's subject(It).
Expand All @@ -67,52 +67,52 @@ var _ = Describe("Set", Ordered, func() {

//TODO(dingxiaoshuai) Add more test cases.
It("SUnion", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sUnion := client.SUnion(ctx, "set1", "set2")
Expect(sUnion.Err()).NotTo(HaveOccurred())
Expect(sUnion.Val()).To(HaveLen(5))

sUnion = client.SUnion(ctx, "nonexistent_set1", "nonexistent_set2")
Expect(sUnion.Err()).NotTo(HaveOccurred())
Expect(sUnion.Val()).To(HaveLen(0))
})

It("should SUnionStore", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sUnionStore := client.SUnionStore(ctx, "set", "set1", "set2")
Expect(sUnionStore.Err()).NotTo(HaveOccurred())
Expect(sUnionStore.Val()).To(Equal(int64(5)))

//sMembers := client.SMembers(ctx, "set")
//Expect(sMembers.Err()).NotTo(HaveOccurred())
//Expect(sMembers.Val()).To(HaveLen(5))
})
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sUnion := client.SUnion(ctx, "set1", "set2")
Expect(sUnion.Err()).NotTo(HaveOccurred())
Expect(sUnion.Val()).To(HaveLen(5))

sUnion = client.SUnion(ctx, "nonexistent_set1", "nonexistent_set2")
Expect(sUnion.Err()).NotTo(HaveOccurred())
Expect(sUnion.Val()).To(HaveLen(0))
})

It("should SUnionStore", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sUnionStore := client.SUnionStore(ctx, "set", "set1", "set2")
Expect(sUnionStore.Err()).NotTo(HaveOccurred())
Expect(sUnionStore.Val()).To(Equal(int64(5)))

//sMembers := client.SMembers(ctx, "set")
//Expect(sMembers.Err()).NotTo(HaveOccurred())
//Expect(sMembers.Val()).To(HaveLen(5))
})
It("Cmd SADD", func() {
log.Println("Cmd SADD Begin")
Expect(client.SAdd(ctx, "myset", "one", "two").Val()).NotTo(Equal("FooBar"))
Expand Down
Loading