|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com> |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#ifndef CPP_REDIS_CORE_TYPES_HPP |
| 24 | +#define CPP_REDIS_CORE_TYPES_HPP |
| 25 | + |
| 26 | +#include <chrono> |
| 27 | +#include <cpp_redis/core/reply.hpp> |
| 28 | +#include <cpp_redis/impl/types.hpp> |
| 29 | +#include <ctime> |
| 30 | +#include <functional> |
| 31 | +#include <map> |
| 32 | +#include <string> |
| 33 | +#include <vector> |
| 34 | + |
| 35 | +namespace cpp_redis { |
| 36 | + |
| 37 | +//! |
| 38 | +//! @brief first array is the session name, second is ids |
| 39 | +//! |
| 40 | +using streams_t = std::pair<std::vector<string_t>, std::vector<string_t>>; |
| 41 | + |
| 42 | +//! |
| 43 | +//! @brief Options |
| 44 | +//! |
| 45 | +typedef struct xread_options { |
| 46 | + streams_t Streams; |
| 47 | + int_t Count; |
| 48 | + int_t Block; |
| 49 | +} xread_options_t; |
| 50 | + |
| 51 | +typedef struct xreadgroup_options { |
| 52 | + string_t Group; |
| 53 | + string_t Consumer; |
| 54 | + streams_t Streams; |
| 55 | + int_t Count; |
| 56 | + int_t Block; |
| 57 | + bool NoAck; |
| 58 | +} xreadgroup_options_t; |
| 59 | + |
| 60 | +typedef struct range_options { |
| 61 | + string_t Start; |
| 62 | + string_t Stop; |
| 63 | + int_t Count; |
| 64 | +} range_options_t; |
| 65 | + |
| 66 | +typedef struct xclaim_options { |
| 67 | + int_t Idle; |
| 68 | + std::time_t *Time; |
| 69 | + int_t RetryCount; |
| 70 | + bool Force; |
| 71 | + bool JustId; |
| 72 | +} xclaim_options_t; |
| 73 | + |
| 74 | +typedef struct xpending_options { |
| 75 | + range_options_t Range; |
| 76 | + string_t Consumer; |
| 77 | +} xpending_options_t; |
| 78 | + |
| 79 | +//! |
| 80 | +//! @brief Replies |
| 81 | +//! |
| 82 | +class xmessage : public virtual message_type { |
| 83 | +public: |
| 84 | + xmessage(); |
| 85 | + |
| 86 | + explicit xmessage(const reply_t &data); |
| 87 | + |
| 88 | + friend std::ostream &operator<<(std::ostream &os, const xmessage &xm); |
| 89 | +}; |
| 90 | + |
| 91 | +using xmessage_t = xmessage; |
| 92 | + |
| 93 | +class xstream { |
| 94 | +public: |
| 95 | + explicit xstream(const reply_t &data); |
| 96 | + |
| 97 | + friend std::ostream &operator<<(std::ostream &os, const xstream &xs); |
| 98 | + |
| 99 | + string_t Stream; |
| 100 | + std::vector<xmessage_t> Messages; |
| 101 | +}; |
| 102 | + |
| 103 | +using xstream_t = xstream; |
| 104 | + |
| 105 | +class xinfo_reply { |
| 106 | +public: |
| 107 | + explicit xinfo_reply(const cpp_redis::reply_t &data); |
| 108 | + |
| 109 | + int_t Length; |
| 110 | + int_t RadixTreeKeys; |
| 111 | + int_t RadixTreeNodes; |
| 112 | + int_t Groups; |
| 113 | + string_t LastGeneratedId; |
| 114 | + xmessage_t FirstEntry; |
| 115 | + xmessage_t LastEntry; |
| 116 | +}; |
| 117 | + |
| 118 | +class xstream_reply : public std::vector<xstream_t> { |
| 119 | +public: |
| 120 | + explicit xstream_reply(const reply_t &data); |
| 121 | + |
| 122 | + friend std::ostream &operator<<(std::ostream &os, const xstream_reply &xs); |
| 123 | + |
| 124 | + bool is_null() const { |
| 125 | + if (empty()) |
| 126 | + return true; |
| 127 | + for (auto &v : *this) { |
| 128 | + if (v.Messages.empty()) |
| 129 | + return true; |
| 130 | + } |
| 131 | + return false; |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +using xstream_reply_t = xstream_reply; |
| 136 | + |
| 137 | +//! |
| 138 | +//! @brief Callbacks |
| 139 | +//! |
| 140 | + |
| 141 | +//! |
| 142 | +//! acknowledgment callback called whenever a subscribe completes |
| 143 | +//! takes as parameter the int returned by the redis server (usually the number |
| 144 | +//! of channels you are subscribed to) |
| 145 | +//! |
| 146 | +//! |
| 147 | +using acknowledgement_callback_t = std::function<void(const int_t &)>; |
| 148 | + |
| 149 | +//! |
| 150 | +//! high availability (re)connection states |
| 151 | +//! * dropped: connection has dropped |
| 152 | +//! * start: attempt of connection has started |
| 153 | +//! * sleeping: sleep between two attempts |
| 154 | +//! * ok: connected |
| 155 | +//! * failed: failed to connect |
| 156 | +//! * lookup failed: failed to retrieve master sentinel |
| 157 | +//! * stopped: stop to try to reconnect |
| 158 | +//! |
| 159 | +//! |
| 160 | +enum class connect_state { |
| 161 | + dropped, |
| 162 | + start, |
| 163 | + sleeping, |
| 164 | + ok, |
| 165 | + failed, |
| 166 | + lookup_failed, |
| 167 | + stopped |
| 168 | +}; |
| 169 | + |
| 170 | +using connect_state_t = connect_state; |
| 171 | + |
| 172 | +//! |
| 173 | +//! connect handler, called whenever a new connection even occurred |
| 174 | +//! |
| 175 | +//! |
| 176 | +typedef std::function<void(const string_t &host, std::size_t port, |
| 177 | + connect_state status)> |
| 178 | + connect_callback_t; |
| 179 | + |
| 180 | +using message_callback_t = std::function<void(const cpp_redis::message_type &)>; |
| 181 | +} // namespace cpp_redis |
| 182 | + |
| 183 | +#endif // CPP_REDIS_TYPES_HPP |
0 commit comments