From 8db3fb218561964aea2e5590f02f3dbdb208d55d Mon Sep 17 00:00:00 2001 From: Marcelo Zimbres Date: Wed, 20 Nov 2019 22:23:35 +0100 Subject: [PATCH] Adds some examples. --- Makefile | 17 ++++ examples.cpp | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 Makefile create mode 100644 examples.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..e0fb501e --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +# Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com) +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +CPPFLAGS += -std=c++17 -g +CPPFLAGS += -I/opt/boost_1_71_0/include +CPPFLAGS += -I/opt/aedis-1.0.0 + +examples: % : %.o + $(CXX) -o $@ $^ $(CPPFLAGS) -lfmt -lpthread + +.PHONY: clean +clean: + rm -f examples examples.o + diff --git a/examples.cpp b/examples.cpp new file mode 100644 index 00000000..96037dd2 --- /dev/null +++ b/examples.cpp @@ -0,0 +1,219 @@ +/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace aedis; + +using args_type = std::initializer_list; + +void set_get() +{ + net::io_context ioc; + session ss {ioc}; + + auto c = set({"Name", "Marcelo"}) + + get("Name"); + + ss.send(std::move(c)); + + ss.run(); + ioc.run(); +} + +void transaction() +{ + net::io_context ioc; + session ss {ioc}; + + auto o = multi() + + set({"Age", "39"}) + + incr("Age") + + get("Age") + + expire("Age", 10) + + exec(); + + ss.send(std::move(o)); + ss.run(); + ioc.run(); +} + +void rpush_vector() +{ + net::io_context ioc; + session ss {ioc}; + + std::vector v1 {1 , 2, 3, 4, 5, 6, 7}; + ss.send(rpush("a", v1) + lrange("a") + del("a")); + + std::list v2 {"one" ,"two", "three"}; + ss.send(rpush("b", v2) + lrange("b") + del("b")); + + std::set v3 {"a" ,"b", "c"}; + ss.send(rpush("c", v3) + lrange("c") + del("c")); + + ss.run(); + ioc.run(); +} + +void pub(int count, char const* channel) +{ + net::io_context ioc; + session pub_session(ioc); + for (auto i = 0; i < count; ++i) + pub_session.send(publish(channel, std::to_string(i))); + + pub_session.run(); + + ioc.run(); +} + +auto msg_handler2 = [i = 0](auto ec, auto const& res) mutable +{ + if (ec) + throw std::runtime_error(ec.message()); + + auto const n = std::stoi(res.back()); + if (n != i + 1) + std::cout << "===============> Error." << std::endl; + std::cout << "Counter: " << n << std::endl; + + i = n; + + //for (auto const& o : res) + // std::cout << o << " "; + + //std::cout << std::endl; +}; + +struct sub_arena { + session s; + + sub_arena( net::io_context& ioc + , std::string channel) + : s(ioc) + { + s.set_msg_handler(msg_handler2); + + auto const on_conn_handler = [this, channel]() + { s.send(subscribe(channel)); }; + + s.set_on_conn_handler(on_conn_handler); + s.run(); + } +}; + +void sub(char const* channel) +{ + net::io_context ioc; + sub_arena arena(ioc, channel); + ioc.run(); +} + +void zadd() +{ + net::io_context ioc; + session ss {ioc}; + + auto c1 = zadd("foo", 1, "bar1") + + zadd("foo", 2, "bar2") + + zadd("foo", 3, "bar3") + + zadd("foo", 4, "bar4") + + zadd("foo", 5, "bar5"); + + ss.send(c1); + + ss.run(); + ioc.run(); +} + +void zrangebyscore() +{ + net::io_context ioc; + session ss(ioc); + + auto c1 = zrangebyscore("foo", 2, -1); + + ss.send(c1); + + ss.run(); + ioc.run(); +} + +void zrange() +{ + net::io_context ioc; + session ss(ioc); + + ss.send(zrange("foo", 2, -1)); + + ss.run(); + ioc.run(); +} + +void read_msg_op() +{ + net::io_context ioc; + session ss(ioc); + + auto c1 = multi() + + lrange("foo") + + del("foo") + + exec(); + + ss.send(c1); + + ss.run(); + ioc.run(); +} + +int main(int argc, char* argv[]) +{ + try { + if (argc == 1) { + std::cerr << "Usage: " << argv[0] << " n host port" << std::endl; + return 1; + } + + session::config cfg; + auto const n = std::stoi(argv[1]); + + if (argc == 3) { + cfg.host = argv[2]; + cfg.port = argv[3]; + } + + char const* channel = "foo"; + + switch (n) { + case 0: set_get(); break; + case 1: transaction(); break; + case 2: pub(10, channel); break; + case 3: sub(channel); break; + case 4: zadd(); break; + case 5: zrangebyscore(); break; + case 6: zrange(); break; + case 7: read_msg_op(); break; + case 8: rpush_vector(); break; + default: + std::cerr << "Option not available." << std::endl; + } + } catch (std::exception const& e) { + std::cerr << e.what() << "\n"; + } + + return 0; +} +