Skip to content

Commit

Permalink
Merge branch 'unstable-fork' into zrevrangebyscore
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/base_cmd.h
#	src/cmd_table_manager.cc
#	src/cmd_zset.cc
#	src/cmd_zset.h
#	tests/zset_test.go
  • Loading branch information
callme-taota committed Mar 10, 2024
2 parents 344aff5 + 2c7aadd commit 6aba952
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 9 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/issue-translator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Issue Translator
on:
issue_comment:
types: [created]
issues:
types: [opened]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: usthe/[email protected]
with:
IS_MODIFY_TITLE: false
CUSTOM_BOT_NOTE: Bot detected the issue body's language is not English, translate it automatically.
3 changes: 3 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace pikiwidb {
const std::string kCmdNameDel = "del";
const std::string kCmdNameExists = "exists";
const std::string kCmdNamePExpire = "pexpire";
const std::string kCmdNameExpireat = "expireat";
const std::string kCmdNamePExpireat = "pexpireat";

// string cmd
const std::string kCmdNameSet = "set";
Expand Down Expand Up @@ -108,6 +110,7 @@ const std::string kCmdNameZAdd = "zadd";
const std::string kCmdNameZRevrange = "zrevrange";
const std::string kCmdNameZRangebyscore = "zrangebyscore";
const std::string kCmdNameZRevRangeByScore = "zrevrangebyscore";
const std::string kCmdNameZCard = "zcard";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
Expand Down
47 changes: 46 additions & 1 deletion src/cmd_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,52 @@ void PExpireCmd::DoCmd(PClient* client) {
if (res != -1) {
client->AppendInteger(res);
} else {
client->SetRes(CmdRes::kErrOther, "exists internal error");
client->SetRes(CmdRes::kErrOther, "pexpire internal error");
}
}

ExpireatCmd::ExpireatCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryKeyspace) {}

bool ExpireatCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ExpireatCmd::DoCmd(PClient* client) {
int64_t time_stamp = 0;
if (pstd::String2int(client->argv_[2], &time_stamp) == 0) {
client->SetRes(CmdRes ::kInvalidInt);
return;
}
auto res = PSTORE.GetBackend(client->GetCurrentDB())->Expireat(client->Key(), time_stamp);
if (res != -1) {
client->AppendInteger(res);
} else {
client->SetRes(CmdRes::kErrOther, "expireat internal error");
}
}

PExpireatCmd::PExpireatCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryKeyspace) {}

bool PExpireatCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

// PExpireatCmd actually invoke Expireat
void PExpireatCmd::DoCmd(PClient* client) {
int64_t time_stamp_ms = 0;
if (pstd::String2int(client->argv_[2], &time_stamp_ms) == 0) {
client->SetRes(CmdRes ::kInvalidInt);
return;
}
auto res = PSTORE.GetBackend(client->GetCurrentDB())->Expireat(client->Key(), time_stamp_ms / 1000);
if (res != -1) {
client->AppendInteger(res);
} else {
client->SetRes(CmdRes::kErrOther, "pexpireat internal error");
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/cmd_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,27 @@ class PExpireCmd : public BaseCmd {
private:
void DoCmd(PClient* client) override;
};

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

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;
};

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

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;
};

} // namespace pikiwidb
2 changes: 1 addition & 1 deletion src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,4 @@ void SetRangeCmd::DoCmd(PClient* client) {
}
client->AppendInteger(static_cast<int>(ret));
}
} // namespace pikiwidb
} // 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 @@ -47,6 +47,8 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(Del, -2);
ADD_COMMAND(Exists, -2);
ADD_COMMAND(PExpire, 3);
ADD_COMMAND(Expireat, 3);
ADD_COMMAND(PExpireat, 3);
// kv
ADD_COMMAND(Get, 2);
ADD_COMMAND(Set, -3);
Expand Down Expand Up @@ -115,6 +117,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(ZRevrange, -4);
ADD_COMMAND(ZRangebyscore, -4);
ADD_COMMAND(ZRevRangeByScore, -4);
ADD_COMMAND(ZCard, 2);
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down
18 changes: 18 additions & 0 deletions src/cmd_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ void ZRangebyscoreCmd::DoCmd(PClient* client) {
}
}

ZCardCmd::ZCardCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategorySortedSet) {}

bool ZCardCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZCardCmd::DoCmd(PClient* client) {
int32_t reply_Num = 0;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->ZCard(client->Key(), &reply_Num);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "ZCard cmd error");
return;
}
client->AppendInteger(reply_Num);
}

ZRevRangeByScoreCmd::ZRevRangeByScoreCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategorySortedSet) {}

Expand Down
11 changes: 11 additions & 0 deletions src/cmd_zset.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ class ZRevRangeByScoreCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

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

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;
};

} // namespace pikiwidb
2 changes: 1 addition & 1 deletion src/storage/include/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ class Storage {
// return -1 operation exception errors happen in database
// return 0 if key does not exist
// return >=1 if the timueout was set
int32_t Expireat(const Slice& key, uint64_t timestamp, std::map<DataType, Status>* type_status);
int32_t Expireat(const Slice& key, uint64_t timestamp);

// Remove the existing timeout on key, turning the key from volatile (a key
// with an expire set) to persistent (a key that will never expire as no
Expand Down
7 changes: 1 addition & 6 deletions src/storage/src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ Status Storage::Scanx(const DataType& data_type, const std::string& start_key, c
return Status::OK();
}

int32_t Storage::Expireat(const Slice& key, uint64_t timestamp, std::map<DataType, Status>* type_status) {
int32_t Storage::Expireat(const Slice& key, uint64_t timestamp) {
Status s;
int32_t count = 0;
bool is_corruption = false;
Expand All @@ -1519,39 +1519,34 @@ int32_t Storage::Expireat(const Slice& key, uint64_t timestamp, std::map<DataTyp
count++;
} else if (!s.IsNotFound()) {
is_corruption = true;
(*type_status)[DataType::kStrings] = s;
}

s = inst->HashesExpireat(key, timestamp);
if (s.ok()) {
count++;
} else if (!s.IsNotFound()) {
is_corruption = true;
(*type_status)[DataType::kHashes] = s;
}

s = inst->SetsExpireat(key, timestamp);
if (s.ok()) {
count++;
} else if (!s.IsNotFound()) {
is_corruption = true;
(*type_status)[DataType::kSets] = s;
}

s = inst->ListsExpireat(key, timestamp);
if (s.ok()) {
count++;
} else if (!s.IsNotFound()) {
is_corruption = true;
(*type_status)[DataType::kLists] = s;
}

s = inst->ZsetsExpireat(key, timestamp);
if (s.ok()) {
count++;
} else if (!s.IsNotFound()) {
is_corruption = true;
(*type_status)[DataType::kZSets] = s;
}

if (is_corruption) {
Expand Down
38 changes: 38 additions & 0 deletions tests/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,42 @@ var _ = Describe("Keyspace", Ordered, func() {

Expect(client.Do(ctx, "pexpire", DefaultKey, "err").Err()).To(MatchError("ERR value is not an integer or out of range"))
})

It("should expireat", func() {
Expect(client.Set(ctx, DefaultKey, DefaultValue, 0).Val()).To(Equal(OK))
Expect(client.ExpireAt(ctx, DefaultKey, time.Now().Add(time.Second*-1)).Val()).To(Equal(true))
Expect(client.Exists(ctx, DefaultKey).Val()).To(Equal(int64(0)))

})

It("should expireat", func() {
Expect(client.Set(ctx, DefaultKey, DefaultValue, 0).Val()).To(Equal(OK))
Expect(client.ExpireAt(ctx, DefaultKey, time.Now().Add(time.Second*3)).Val()).To(Equal(true))
Expect(client.Exists(ctx, DefaultKey).Val()).To(Equal(int64(1)))

time.Sleep(4 * time.Second)

Expect(client.Get(ctx, DefaultKey).Err()).To(MatchError(redis.Nil))
Expect(client.Exists(ctx, DefaultKey).Val()).To(Equal(int64(0)))
})

It("should pexpirat", func() {
Expect(client.Set(ctx, DefaultKey, DefaultValue, 0).Val()).To(Equal(OK))
Expect(client.PExpireAt(ctx, DefaultKey, time.Now().Add(time.Second*-1)).Val()).To(Equal(true))
Expect(client.Exists(ctx, DefaultKey).Val()).To(Equal(int64(0)))

})

It("should pexpirat", func() {
Expect(client.Set(ctx, DefaultKey, DefaultValue, 0).Val()).To(Equal(OK))
Expect(client.PExpireAt(ctx, DefaultKey, time.Now().Add(time.Second*3)).Val()).To(Equal(true))
Expect(client.Exists(ctx, DefaultKey).Val()).To(Equal(int64(1)))

time.Sleep(4 * time.Second)

Expect(client.Get(ctx, DefaultKey).Err()).To(MatchError(redis.Nil))
Expect(client.Exists(ctx, DefaultKey).Val()).To(Equal(int64(0)))

})

})
16 changes: 16 additions & 0 deletions tests/zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,20 @@ var _ = Describe("Zset", Ordered, func() {
Expect(vals).To(Equal([]string{}))
})

It("should ZCard", func() {
err := client.ZAdd(ctx, "zsetZCard", redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zsetZCard", redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())

card, err := client.ZCard(ctx, "zsetZCard").Result()
Expect(err).NotTo(HaveOccurred())
Expect(card).To(Equal(int64(2)))
})
})

0 comments on commit 6aba952

Please sign in to comment.