Skip to content

Commit 03a5e44

Browse files
author
Jinglei Ren
committed
Fix bug in handling null string and freeing redis reply
1 parent f59c7d1 commit 03a5e44

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

db/redis_db.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ int RedisDB::Read(const string &table, const string &key,
3131
assert(reply->type == REDIS_REPLY_ARRAY);
3232
assert(fields->size() == reply->elements);
3333
for (size_t i = 0; i < reply->elements; ++i) {
34-
result.push_back(make_pair(
35-
fields->at(i), string(reply->element[i]->str)));
34+
const char *value = reply->element[i]->str;
35+
result.push_back(make_pair(fields->at(i), string(value ? value : "")));
3636
}
3737
freeReplyObject(reply);
3838
} else {

redis/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ LDFLAGS=-lhiredis
66

77
all: hiredis sample
88

9-
sample: sample.cc $(HEADERS)
9+
sample: sample.cc ../db/redis_db.o $(HEADERS)
1010
$(MAKE) -C ./hiredis
11-
$(CC) $(CFLAGS) $(INCLUDES) $< $(LDFLAGS) -o $@
11+
$(MAKE) -C ../db
12+
$(CC) $(CFLAGS) $(INCLUDES) $< ../db/redis_db.o $(LDFLAGS) -o $@
1213

1314
clean:
1415
$(RM) sample

redis/redis_client.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ inline int RedisClient::Command(std::string cmd, int n, int t) {
5959
HandleError(reply, cmd.c_str());
6060
}
6161
freeReplyObject(reply);
62-
if (sync_ && redisGetReply(context_, (void **)&reply) == REDIS_ERR) {
63-
HandleError(reply, "WAIT");
62+
if (sync_) {
63+
if (redisGetReply(context_, (void **)&reply) == REDIS_ERR) {
64+
HandleError(reply, "WAIT");
65+
}
66+
freeReplyObject(reply);
6467
}
65-
freeReplyObject(reply);
6668
return 0;
6769
}
6870

redis/run_sample.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#! /bin/bash
22

3-
REDIS=~/redis
4-
53
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
64

75
./sample
86

9-
$REDIS/src/redis-cli hmget Ren field1 field2
10-

redis/sample.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
#include "redis_client.h"
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include "redis/redis_client.h"
5+
#include "db/redis_db.h"
26

7+
using namespace std;
38
using namespace ycsbc;
49

510
int main(int argc, const char *argv[]) {
@@ -10,5 +15,31 @@ int main(int argc, const char *argv[]) {
1015

1116
client.Command("HMSET Ren field1 jinglei@ren.systems field2 Jinglei");
1217

18+
RedisDB db(host, port, false);
19+
db.Init();
20+
string key = "Ren";
21+
vector<string> fields;
22+
fields.push_back("field1");
23+
fields.push_back("field3");
24+
vector<DB::KVPair> result;
25+
26+
db.Read(key, key, &fields, result);
27+
for (auto &p : result) {
28+
cout << p.first << '\t' << p.second << endl;
29+
}
30+
31+
result[1].second = "HelloWorld!";
32+
db.Update(key, key, result);
33+
34+
result.clear();
35+
db.Read(key, key, nullptr, result);
36+
for (auto &p : result) {
37+
cout << p.first << '\t' << p.second << endl;
38+
}
39+
40+
db.Delete(key, key);
41+
result.clear();
42+
db.Read(key, key, nullptr, result);
43+
cout << "After delete: " << result.size() << endl;
1344
return 0;
1445
}

0 commit comments

Comments
 (0)