Skip to content

Commit 4cf643e

Browse files
wendanilguohan
authored andcommitted
Add multiple fields hdel support (#267)
* Add multiple fields hdel support Signed-off-by: Wenda Ni <wenni@microsoft.com> * Add unit test for multiple fields hdel Signed-off-by: Wenda Ni <wenni@microsoft.com>
1 parent a710529 commit 4cf643e

5 files changed

Lines changed: 75 additions & 4 deletions

File tree

common/redisclient.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ int64_t RedisClient::hdel(const string &key, const string &field)
4141
return r.getContext()->integer;
4242
}
4343

44+
int64_t RedisClient::hdel(const std::string &key, const std::vector<std::string> &fields)
45+
{
46+
RedisCommand shdel;
47+
shdel.formatHDEL(key, fields);
48+
RedisReply r(m_db, shdel, REDIS_REPLY_INTEGER);
49+
return r.getContext()->integer;
50+
}
51+
4452
void RedisClient::hset(const string &key, const string &field, const string &value)
4553
{
4654
RedisCommand shset;

common/redisclient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class RedisClient
3030

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

33+
int64_t hdel(const std::string &key, const std::vector<std::string> &fields);
34+
3335
std::unordered_map<std::string, std::string> hgetall(const std::string &key);
3436

3537
template <typename OutputIterator>

common/rediscommand.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ void RedisCommand::formatHDEL(const std::string& key, const std::string& field)
6363
return format("HDEL %s %s", key.c_str(), field.c_str());
6464
}
6565

66+
/* Format HDEL key multiple fields command */
67+
void RedisCommand::formatHDEL(const std::string& key, const std::vector<std::string>& fields)
68+
{
69+
if (fields.empty()) throw std::invalid_argument("empty values");
70+
71+
std::vector<const char *> args = {"HDEL", key.c_str()};
72+
for (const std::string &f : fields)
73+
{
74+
args.push_back(f.c_str());
75+
}
76+
formatArgv(static_cast<int>(args.size()), args.data(), NULL);
77+
}
78+
6679
const char *RedisCommand::c_str() const
6780
{
6881
return temp;

common/rediscommand.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class RedisCommand {
4444
/* Format HDEL key field command */
4545
void formatHDEL(const std::string& key, const std::string& field);
4646

47+
/* Format HDEL key multiple fields command */
48+
void formatHDEL(const std::string& key, const std::vector<std::string>& fields);
49+
4750
const char *c_str() const;
4851

4952
size_t length() const;

tests/redis_ut.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,37 @@ TEST(DBConnector, RedisClient)
331331
cout << endl;
332332
}
333333

334-
cout << "- Step 4. DEL" << endl;
334+
cout << "- Step 4. HDEL single field" << endl;
335+
cout << "Delete field_2 under key [a]" << endl;
336+
int64_t rval = redic.hdel(key_1, "field_2");
337+
EXPECT_EQ(rval, 1);
338+
339+
auto fvs = redic.hgetall(key_1);
340+
EXPECT_EQ(fvs.size(), 2);
341+
for (auto fv: fvs)
342+
{
343+
string value_1 = "1", value_3 = "3";
344+
cout << " " << fvField(fv) << ":" << fvValue(fv) << flush;
345+
if (fvField(fv) == "field_1")
346+
{
347+
EXPECT_EQ(fvValue(fv), value_1);
348+
}
349+
if (fvField(fv) == "field_3")
350+
{
351+
EXPECT_EQ(fvValue(fv), value_3);
352+
}
353+
354+
ASSERT_FALSE(fvField(fv) == "2");
355+
}
356+
cout << endl;
357+
358+
cout << "- Step 5. DEL" << endl;
335359
cout << "Delete key [a]" << endl;
336360
redic.del(key_1);
337361

338-
cout << "- Step 5. GET" << endl;
362+
cout << "- Step 6. GET" << endl;
339363
cout << "Get key [a] and key [b]" << endl;
340-
auto fvs = redic.hgetall(key_1);
364+
fvs = redic.hgetall(key_1);
341365
EXPECT_TRUE(fvs.empty());
342366
fvs = redic.hgetall(key_2);
343367

@@ -357,7 +381,28 @@ TEST(DBConnector, RedisClient)
357381
}
358382
cout << endl;
359383

360-
cout << "- Step 6. DEL and GET_TABLE_CONTENT" << endl;
384+
cout << "- Step 7. HDEL multiple fields" << endl;
385+
cout << "Delete field_2, field_3 under key [b]" << endl;
386+
rval = redic.hdel(key_2, vector<string>({"field_2", "field_3"}));
387+
EXPECT_EQ(rval, 2);
388+
389+
fvs = redic.hgetall(key_2);
390+
EXPECT_EQ(fvs.size(), 1);
391+
for (auto fv: fvs)
392+
{
393+
string value_1 = "1";
394+
cout << " " << fvField(fv) << ":" << fvValue(fv) << flush;
395+
if (fvField(fv) == "field_1")
396+
{
397+
EXPECT_EQ(fvValue(fv), value_1);
398+
}
399+
400+
ASSERT_FALSE(fvField(fv) == "field_2");
401+
ASSERT_FALSE(fvField(fv) == "field_3");
402+
}
403+
cout << endl;
404+
405+
cout << "- Step 8. DEL and GET_TABLE_CONTENT" << endl;
361406
cout << "Delete key [b]" << endl;
362407
redic.del(key_2);
363408
fvs = redic.hgetall(key_2);

0 commit comments

Comments
 (0)