Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support commands returning vector<int> #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ a `WRONG_TYPE` or `NIL_REPLY` status.
* `<std::vector<std::string>>`: Arrays of Simple Strings or Bulk Strings (in received order)
* `<std::set<std::string>>`: Arrays of Simple Strings or Bulk Strings (in sorted order)
* `<std::unordered_set<std::string>>`: Arrays of Simple Strings or Bulk Strings (in no order)
* `<std::vector<int>>`: Arrays of integers (in received order)

## Installation
Instructions provided are for Ubuntu, but all components are platform-independent.
Expand Down
12 changes: 12 additions & 0 deletions examples/data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ int main(int argc, char* argv[]) {
for (const string& s : c.reply()) cout << s << " ";
cout << endl;
}
}
);

rdx.del("myset");
rdx.commandSync(rdx.strToVec("SADD myset 2 3 5 7 11 13 17 19"));
rdx.command<vector<int>>(rdx.strToVec("SMISMEMBER myset 1 2 3 4 5"),
[&rdx](Command<vector<int>>& c) {
if(c.ok()) {
cout << "Are {1, 2, 3, 4, 5} in the set, respectively: ";
for (int i : c.reply()) cout << i << " ";
cout << endl;
}
rdx.stop();
}
);
Expand Down
1 change: 1 addition & 0 deletions include/redox/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ class Redox {
std::unordered_map<long, Command<long long int> *> commands_long_long_int_;
std::unordered_map<long, Command<std::nullptr_t> *> commands_null_;
std::unordered_map<long, Command<std::vector<std::string>> *> commands_vector_string_;
std::unordered_map<long, Command<std::vector<int>> *> commands_vector_int_;
std::unordered_map<long, Command<std::set<std::string>> *> commands_set_string_;
std::unordered_map<long, Command<std::unordered_set<std::string>> *>
commands_unordered_set_string_;
Expand Down
7 changes: 7 additions & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ void Redox::processQueuedCommands(struct ev_loop *loop, ev_async *async, int rev
} else if (rdx->processQueuedCommand<long long int>(id)) {
} else if (rdx->processQueuedCommand<nullptr_t>(id)) {
} else if (rdx->processQueuedCommand<vector<string>>(id)) {
} else if (rdx->processQueuedCommand<vector<int>>(id)) {
} else if (rdx->processQueuedCommand<std::set<string>>(id)) {
} else if (rdx->processQueuedCommand<unordered_set<string>>(id)) {
} else
Expand All @@ -483,6 +484,7 @@ void Redox::freeQueuedCommands(struct ev_loop *loop, ev_async *async, int revent
} else if (rdx->freeQueuedCommand<long long int>(id)) {
} else if (rdx->freeQueuedCommand<nullptr_t>(id)) {
} else if (rdx->freeQueuedCommand<vector<string>>(id)) {
} else if (rdx->freeQueuedCommand<vector<int>>(id)) {
} else if (rdx->freeQueuedCommand<std::set<string>>(id)) {
} else if (rdx->freeQueuedCommand<unordered_set<string>>(id)) {
} else {
Expand Down Expand Up @@ -515,6 +517,7 @@ long Redox::freeAllCommands() {
freeAllCommandsOfType<char *>() + freeAllCommandsOfType<int>() +
freeAllCommandsOfType<long long int>() + freeAllCommandsOfType<nullptr_t>() +
freeAllCommandsOfType<vector<string>>() + freeAllCommandsOfType<std::set<string>>() +
freeAllCommandsOfType<vector<int>>() +
freeAllCommandsOfType<unordered_set<string>>();
}

Expand Down Expand Up @@ -579,6 +582,10 @@ template <> unordered_map<long, Command<vector<string>> *> &Redox::getCommandMap
return commands_vector_string_;
}

template <> unordered_map<long, Command<vector<int>> *> &Redox::getCommandMap<vector<int>>() {
return commands_vector_int_;
}

template <> unordered_map<long, Command<set<string>> *> &Redox::getCommandMap<set<string>>() {
return commands_set_string_;
}
Expand Down
12 changes: 12 additions & 0 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ template <> void Command<vector<string>>::parseReplyObject() {
}
}

template <> void Command<vector<int>>::parseReplyObject() {

if (!isExpectedReply(REDIS_REPLY_ARRAY))
return;

for (size_t i = 0; i < reply_obj_->elements; i++) {
redisReply *r = *(reply_obj_->element + i);
reply_val_.push_back(r->integer);
}
}

template <> void Command<unordered_set<string>>::parseReplyObject() {

if (!isExpectedReply(REDIS_REPLY_ARRAY))
Expand Down Expand Up @@ -268,6 +279,7 @@ template class Command<int>;
template class Command<long long int>;
template class Command<nullptr_t>;
template class Command<vector<string>>;
template class Command<vector<int>>;
template class Command<set<string>>;
template class Command<unordered_set<string>>;

Expand Down