Skip to content

Commit

Permalink
feat: support cmd persist
Browse files Browse the repository at this point in the history
issue: #30

Signed-off-by: HappyUncle <[email protected]>
  • Loading branch information
happy-v587 committed Mar 17, 2024
1 parent f6fb756 commit 7971227
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const std::string kCmdNameExists = "exists";
const std::string kCmdNamePExpire = "pexpire";
const std::string kCmdNameExpireat = "expireat";
const std::string kCmdNamePExpireat = "pexpireat";
const std::string kCmdNamePersist = "persist";

// string cmd
const std::string kCmdNameSet = "set";
Expand Down
25 changes: 25 additions & 0 deletions src/cmd_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,29 @@ void PExpireatCmd::DoCmd(PClient* client) {
}
}

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

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

void PersistCmd::DoCmd(PClient* client) {
std::map<storage::DataType, storage::Status> type_status;
auto res = PSTORE.GetBackend(client->GetCurrentDB())->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);
}
}

} // namespace pikiwidb
10 changes: 10 additions & 0 deletions src/cmd_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ class PExpireatCmd : public BaseCmd {
void DoCmd(PClient* client) override;
};

class PersistCmd : public BaseCmd {
public:
PersistCmd(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 @@ -49,6 +49,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(PExpire, 3);
ADD_COMMAND(Expireat, 3);
ADD_COMMAND(PExpireat, 3);
ADD_COMMAND(Persist, 2);
// kv
ADD_COMMAND(Get, 2);
ADD_COMMAND(Set, -3);
Expand Down
1 change: 1 addition & 0 deletions src/storage/include/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ enum BeforeOrAfter { Before, After };

enum DataType { kAll, kStrings, kHashes, kSets, kLists, kZSets };

const std::string DataTypeToString[] = {"all", "string", "hash", "set", "list", "zset"};
const char DataTypeTag[] = {'a', 'k', 'h', 's', 'l', 'z'};

enum class OptionType {
Expand Down
23 changes: 23 additions & 0 deletions tests/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,27 @@ var _ = Describe("Keyspace", Ordered, func() {

})

It("persist", func() {
// return 0 if key does not exist
Expect(client.Persist(ctx, DefaultKey).Val()).To(Equal(false))

// return 0 if key does not have an associated timeout
Expect(client.Set(ctx, DefaultKey, DefaultValue, 0).Val()).To(Equal(OK))
Expect(client.Persist(ctx, DefaultKey).Val()).To(Equal(false))

// return 1 if the timueout was set
Expect(client.PExpireAt(ctx, DefaultKey, time.Now().Add(time.Second*3)).Val()).To(Equal(true))
Expect(client.Persist(ctx, DefaultKey).Val()).To(Equal(true))
time.Sleep(5 * time.Second)
Expect(client.Exists(ctx, DefaultKey).Val()).To(Equal(int64(1)))

// multi data type
Expect(client.LPush(ctx, DefaultKey, "l").Err()).NotTo(HaveOccurred())
Expect(client.HSet(ctx, DefaultKey, "h", "h").Err()).NotTo(HaveOccurred())
Expect(client.SAdd(ctx, DefaultKey, "s").Err()).NotTo(HaveOccurred())
Expect(client.ZAdd(ctx, DefaultKey, redis.Z{Score: 1, Member: "z"}).Err()).NotTo(HaveOccurred())
Expect(client.Set(ctx, DefaultKey, DefaultValue, 0).Val()).To(Equal(OK))
Expect(client.PExpireAt(ctx, DefaultKey, time.Now().Add(time.Second*1000)).Err()).NotTo(HaveOccurred())
Expect(client.Persist(ctx, DefaultKey).Err()).NotTo(HaveOccurred())
})
})

0 comments on commit 7971227

Please sign in to comment.