1 | #pragma once |
2 | |
3 | #include "config_core.h" |
4 | #include <Core/Block.h> |
5 | |
6 | #if USE_POCO_REDIS |
7 | |
8 | # include "DictionaryStructure.h" |
9 | # include "IDictionarySource.h" |
10 | |
11 | namespace Poco |
12 | { |
13 | namespace Util |
14 | { |
15 | class AbstractConfiguration; |
16 | } |
17 | |
18 | namespace Redis |
19 | { |
20 | class Client; |
21 | class Array; |
22 | class Command; |
23 | } |
24 | } |
25 | |
26 | |
27 | namespace DB |
28 | { |
29 | enum class RedisStorageType |
30 | { |
31 | SIMPLE, |
32 | HASH_MAP, |
33 | UNKNOWN |
34 | }; |
35 | |
36 | class RedisDictionarySource final : public IDictionarySource |
37 | { |
38 | RedisDictionarySource( |
39 | const DictionaryStructure & dict_struct, |
40 | const std::string & host, |
41 | UInt16 port, |
42 | UInt8 db_index, |
43 | RedisStorageType storage_type, |
44 | const Block & sample_block); |
45 | |
46 | public: |
47 | using RedisArray = Poco::Redis::Array; |
48 | using RedisCommand = Poco::Redis::Command; |
49 | |
50 | RedisDictionarySource( |
51 | const DictionaryStructure & dict_struct, |
52 | const Poco::Util::AbstractConfiguration & config, |
53 | const std::string & config_prefix, |
54 | Block & sample_block); |
55 | |
56 | RedisDictionarySource(const RedisDictionarySource & other); |
57 | |
58 | ~RedisDictionarySource() override; |
59 | |
60 | BlockInputStreamPtr loadAll() override; |
61 | |
62 | BlockInputStreamPtr loadUpdatedAll() override |
63 | { |
64 | throw Exception{"Method loadUpdatedAll is unsupported for RedisDictionarySource" , ErrorCodes::NOT_IMPLEMENTED}; |
65 | } |
66 | |
67 | bool supportsSelectiveLoad() const override { return true; } |
68 | |
69 | BlockInputStreamPtr loadIds(const std::vector<UInt64> & ids) override; |
70 | |
71 | BlockInputStreamPtr loadKeys(const Columns & /* key_columns */, const std::vector<size_t> & /* requested_rows */) override |
72 | { |
73 | // Redis does not support native indexing |
74 | throw Exception{"Method loadKeys is unsupported for RedisDictionarySource" , ErrorCodes::NOT_IMPLEMENTED}; |
75 | } |
76 | |
77 | bool isModified() const override { return true; } |
78 | |
79 | bool hasUpdateField() const override { return false; } |
80 | |
81 | DictionarySourcePtr clone() const override { return std::make_unique<RedisDictionarySource>(*this); } |
82 | |
83 | std::string toString() const override; |
84 | |
85 | private: |
86 | static RedisStorageType parseStorageType(const std::string& storage_type); |
87 | |
88 | private: |
89 | const DictionaryStructure dict_struct; |
90 | const std::string host; |
91 | const UInt16 port; |
92 | const UInt8 db_index; |
93 | const RedisStorageType storage_type; |
94 | Block sample_block; |
95 | |
96 | std::shared_ptr<Poco::Redis::Client> client; |
97 | }; |
98 | |
99 | } |
100 | #endif |
101 | |