Skip to content
Merged
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
8 changes: 8 additions & 0 deletions common/redisclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ int64_t RedisClient::hdel(const string &key, const string &field)
return r.getContext()->integer;
}

int64_t RedisClient::hdel(const std::string &key, const std::vector<std::string> &fields)
{
RedisCommand shdel;
shdel.formatHDEL(key, fields);
RedisReply r(m_db, shdel, REDIS_REPLY_INTEGER);
return r.getContext()->integer;
}

void RedisClient::hset(const string &key, const string &field, const string &value)
{
RedisCommand shset;
Expand Down
2 changes: 2 additions & 0 deletions common/redisclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class RedisClient

int64_t hdel(const std::string &key, const std::string &field);

int64_t hdel(const std::string &key, const std::vector<std::string> &fields);

std::unordered_map<std::string, std::string> hgetall(const std::string &key);

template <typename OutputIterator>
Expand Down
13 changes: 13 additions & 0 deletions common/rediscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ void RedisCommand::formatHDEL(const std::string& key, const std::string& field)
return format("HDEL %s %s", key.c_str(), field.c_str());
}

/* Format HDEL key multiple fields command */
void RedisCommand::formatHDEL(const std::string& key, const std::vector<std::string>& fields)
{
if (fields.empty()) throw std::invalid_argument("empty values");

std::vector<const char *> args = {"HDEL", key.c_str()};
for (const std::string &f : fields)
{
args.push_back(f.c_str());
}
formatArgv(static_cast<int>(args.size()), args.data(), NULL);
}

const char *RedisCommand::c_str() const
{
return temp;
Expand Down
3 changes: 3 additions & 0 deletions common/rediscommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class RedisCommand {
/* Format HDEL key field command */
void formatHDEL(const std::string& key, const std::string& field);

/* Format HDEL key multiple fields command */
void formatHDEL(const std::string& key, const std::vector<std::string>& fields);

const char *c_str() const;

size_t length() const;
Expand Down
53 changes: 49 additions & 4 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,37 @@ TEST(DBConnector, RedisClient)
cout << endl;
}

cout << "- Step 4. DEL" << endl;
cout << "- Step 4. HDEL single field" << endl;
cout << "Delete field_2 under key [a]" << endl;
int64_t rval = redic.hdel(key_1, "field_2");
EXPECT_EQ(rval, 1);

auto fvs = redic.hgetall(key_1);
EXPECT_EQ(fvs.size(), 2);
for (auto fv: fvs)
{
string value_1 = "1", value_3 = "3";
cout << " " << fvField(fv) << ":" << fvValue(fv) << flush;
if (fvField(fv) == "field_1")
{
EXPECT_EQ(fvValue(fv), value_1);
}
if (fvField(fv) == "field_3")
{
EXPECT_EQ(fvValue(fv), value_3);
}

ASSERT_FALSE(fvField(fv) == "2");
}
cout << endl;

cout << "- Step 5. DEL" << endl;
cout << "Delete key [a]" << endl;
redic.del(key_1);

cout << "- Step 5. GET" << endl;
cout << "- Step 6. GET" << endl;
cout << "Get key [a] and key [b]" << endl;
auto fvs = redic.hgetall(key_1);
fvs = redic.hgetall(key_1);
EXPECT_TRUE(fvs.empty());
fvs = redic.hgetall(key_2);

Expand All @@ -357,7 +381,28 @@ TEST(DBConnector, RedisClient)
}
cout << endl;

cout << "- Step 6. DEL and GET_TABLE_CONTENT" << endl;
cout << "- Step 7. HDEL multiple fields" << endl;
cout << "Delete field_2, field_3 under key [b]" << endl;
rval = redic.hdel(key_2, vector<string>({"field_2", "field_3"}));
EXPECT_EQ(rval, 2);

fvs = redic.hgetall(key_2);
EXPECT_EQ(fvs.size(), 1);
for (auto fv: fvs)
{
string value_1 = "1";
cout << " " << fvField(fv) << ":" << fvValue(fv) << flush;
if (fvField(fv) == "field_1")
{
EXPECT_EQ(fvValue(fv), value_1);
}

ASSERT_FALSE(fvField(fv) == "field_2");
ASSERT_FALSE(fvField(fv) == "field_3");
}
cout << endl;

cout << "- Step 8. DEL and GET_TABLE_CONTENT" << endl;
cout << "Delete key [b]" << endl;
redic.del(key_2);
fvs = redic.hgetall(key_2);
Expand Down