-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from boostorg/233-missing-implementation-for-…
…stdvectorstdoptionalstdstring Adds support for optional fields
- Loading branch information
Showing
8 changed files
with
287 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva ([email protected]) | ||
/* Copyright (c) 2018-2025 Marcelo Zimbres Silva ([email protected]) | ||
* | ||
* Distributed under the Boost Software License, Version 1.0. (See | ||
* accompanying file LICENSE.txt) | ||
|
@@ -24,13 +24,25 @@ using boost::asio::awaitable; | |
using boost::asio::detached; | ||
using boost::asio::consign; | ||
|
||
template<class T> | ||
std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt) | ||
{ | ||
if (opt.has_value()) | ||
std::cout << opt.value(); | ||
else | ||
std::cout << "null"; | ||
|
||
return os; | ||
} | ||
|
||
void print(std::map<std::string, std::string> const& cont) | ||
{ | ||
for (auto const& e: cont) | ||
std::cout << e.first << ": " << e.second << "\n"; | ||
} | ||
|
||
void print(std::vector<int> const& cont) | ||
template <class T> | ||
void print(std::vector<T> const& cont) | ||
{ | ||
for (auto const& e: cont) std::cout << e << " "; | ||
std::cout << "\n"; | ||
|
@@ -48,6 +60,7 @@ auto store(std::shared_ptr<connection> conn) -> awaitable<void> | |
request req; | ||
req.push_range("RPUSH", "rpush-key", vec); | ||
req.push_range("HSET", "hset-key", map); | ||
req.push("SET", "key", "value"); | ||
|
||
co_await conn->async_exec(req, ignore); | ||
} | ||
|
@@ -67,26 +80,48 @@ auto hgetall(std::shared_ptr<connection> conn) -> awaitable<void> | |
print(std::get<0>(resp).value()); | ||
} | ||
|
||
auto mget(std::shared_ptr<connection> conn) -> awaitable<void> | ||
{ | ||
// A request contains multiple commands. | ||
request req; | ||
req.push("MGET", "key", "non-existing-key"); | ||
|
||
// Responses as tuple elements. | ||
response<std::vector<std::optional<std::string>>> resp; | ||
|
||
// Executes the request and reads the response. | ||
co_await conn->async_exec(req, resp); | ||
|
||
print(std::get<0>(resp).value()); | ||
} | ||
|
||
// Retrieves in a transaction. | ||
auto transaction(std::shared_ptr<connection> conn) -> awaitable<void> | ||
{ | ||
request req; | ||
req.push("MULTI"); | ||
req.push("LRANGE", "rpush-key", 0, -1); // Retrieves | ||
req.push("HGETALL", "hset-key"); // Retrieves | ||
req.push("MGET", "key", "non-existing-key"); | ||
req.push("EXEC"); | ||
|
||
response< | ||
ignore_t, // multi | ||
ignore_t, // lrange | ||
ignore_t, // hgetall | ||
response<std::optional<std::vector<int>>, std::optional<std::map<std::string, std::string>>> // exec | ||
ignore_t, // mget | ||
response< | ||
std::optional<std::vector<int>>, | ||
std::optional<std::map<std::string, std::string>>, | ||
std::optional<std::vector<std::optional<std::string>>> | ||
> // exec | ||
> resp; | ||
|
||
co_await conn->async_exec(req, resp); | ||
|
||
print(std::get<0>(std::get<3>(resp).value()).value().value()); | ||
print(std::get<1>(std::get<3>(resp).value()).value().value()); | ||
print(std::get<0>(std::get<4>(resp).value()).value().value()); | ||
print(std::get<1>(std::get<4>(resp).value()).value().value()); | ||
print(std::get<2>(std::get<4>(resp).value()).value().value()); | ||
} | ||
|
||
// Called from the main function (see main.cpp) | ||
|
@@ -98,6 +133,7 @@ awaitable<void> co_main(config cfg) | |
co_await store(conn); | ||
co_await transaction(conn); | ||
co_await hgetall(conn); | ||
co_await mget(conn); | ||
conn->cancel(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.