2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #ifndef SHAREDDATALAYER_REDIS_ASYNCREDISSTORAGE_HPP_
18 #define SHAREDDATALAYER_REDIS_ASYNCREDISSTORAGE_HPP_
23 #include <boost/optional.hpp>
24 #include <sdl/asyncstorage.hpp>
25 #include "private/logger.hpp"
26 #include "private/namespaceconfigurationsimpl.hpp"
27 #include "private/timer.hpp"
28 #include "private/redis/databaseinfo.hpp"
29 #include "private/redis/reply.hpp"
31 namespace shareddatalayer
35 class AsyncCommandDispatcher;
36 class AsyncDatabaseDiscovery;
39 class ContentsBuilder;
44 class AsyncRedisStorage: public AsyncStorage
50 REDIS_NOT_YET_DISCOVERED,
52 //Keep this always as last item. Used in unit tests to loop all enum values.
56 using AsyncCommandDispatcherCreator = std::function<std::shared_ptr<redis::AsyncCommandDispatcher>(Engine& engine,
57 const redis::DatabaseInfo& databaseInfo,
58 std::shared_ptr<redis::ContentsBuilder> contentsBuilder,
59 std::shared_ptr<Logger> logger)>;
61 static const std::error_category& errorCategory() noexcept;
63 AsyncRedisStorage(const AsyncRedisStorage&) = delete;
65 AsyncRedisStorage& operator = (const AsyncRedisStorage&) = delete;
67 AsyncRedisStorage(std::shared_ptr<Engine> engine,
68 std::shared_ptr<redis::AsyncDatabaseDiscovery> discovery,
69 const boost::optional<PublisherId>& pId,
70 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations,
71 std::shared_ptr<Logger> logger);
73 AsyncRedisStorage(std::shared_ptr<Engine> engine,
74 std::shared_ptr<redis::AsyncDatabaseDiscovery> discovery,
75 const boost::optional<PublisherId>& pId,
76 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations,
77 const AsyncCommandDispatcherCreator& asyncCommandDispatcherCreator,
78 std::shared_ptr<redis::ContentsBuilder> contentsBuilder,
79 std::shared_ptr<Logger> logger);
81 ~AsyncRedisStorage() override;
83 int fd() const override;
85 void handleEvents() override;
87 void waitReadyAsync(const Namespace& ns, const ReadyAck& readyAck) override;
89 void setAsync(const Namespace& ns, const DataMap& dataMap, const ModifyAck& modifyAck) override;
91 void setIfAsync(const Namespace& ns, const Key& key, const Data& oldData, const Data& newData, const ModifyIfAck& modifyIfAck) override;
93 void setIfNotExistsAsync(const Namespace& ns, const Key& key, const Data& data, const ModifyIfAck& modifyIfAck) override;
95 void getAsync(const Namespace& ns, const Keys& keys, const GetAck& getAck) override;
97 void removeAsync(const Namespace& ns, const Keys& keys, const ModifyAck& modifyAck) override;
99 void removeIfAsync(const Namespace& ns, const Key& key, const Data& data, const ModifyIfAck& modifyIfAck) override;
101 void findKeysAsync(const Namespace& ns, const std::string& keyPrefix, const FindKeysAck& findKeysAck) override;
103 void removeAllAsync(const Namespace& ns, const ModifyAck& modifyAck) override;
105 redis::DatabaseInfo& getDatabaseInfo();
107 std::string buildKeyPrefixSearchPattern(const Namespace& ns, const std::string& keyPrefix) const;
110 std::shared_ptr<Engine> engine;
111 std::shared_ptr<redis::AsyncCommandDispatcher> dispatcher;
112 std::shared_ptr<redis::AsyncDatabaseDiscovery> discovery;
113 const boost::optional<PublisherId> publisherId;
115 AsyncCommandDispatcherCreator asyncCommandDispatcherCreator;
116 std::shared_ptr<redis::ContentsBuilder> contentsBuilder;
117 redis::DatabaseInfo dbInfo;
118 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations;
119 std::shared_ptr<Logger> logger;
121 bool canOperationBePerformed(const Namespace& ns, boost::optional<bool> inputDataIsEmpty, std::error_code& ecToReturn);
123 void serviceStateChanged(const redis::DatabaseInfo& databaseInfo);
125 std::string getPublishMessage() const;
127 void modificationCommandCallback(const std::error_code& error, const redis::Reply&, const ModifyAck&);
129 void conditionalCommandCallback(const std::error_code& error, const redis::Reply&, const ModifyIfAck&);
132 AsyncRedisStorage::ErrorCode& operator++ (AsyncRedisStorage::ErrorCode& ecEnum);
133 std::error_code make_error_code(AsyncRedisStorage::ErrorCode errorCode);
139 struct is_error_code_enum<shareddatalayer::AsyncRedisStorage::ErrorCode>: public true_type { };