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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #ifndef SHAREDDATALAYER_REDIS_ASYNCREDISSTORAGE_HPP_
23 #define SHAREDDATALAYER_REDIS_ASYNCREDISSTORAGE_HPP_
28 #include <boost/optional.hpp>
29 #include <sdl/asyncstorage.hpp>
30 #include "private/logger.hpp"
31 #include "private/namespaceconfigurationsimpl.hpp"
32 #include "private/timer.hpp"
33 #include "private/redis/databaseinfo.hpp"
34 #include "private/redis/reply.hpp"
36 namespace shareddatalayer
40 class AsyncCommandDispatcher;
41 class AsyncDatabaseDiscovery;
44 class ContentsBuilder;
49 class AsyncRedisStorage: public AsyncStorage
55 REDIS_NOT_YET_DISCOVERED,
57 //Keep this always as last item. Used in unit tests to loop all enum values.
61 using AsyncCommandDispatcherCreator = std::function<std::shared_ptr<redis::AsyncCommandDispatcher>(Engine& engine,
62 const redis::DatabaseInfo& databaseInfo,
63 std::shared_ptr<redis::ContentsBuilder> contentsBuilder,
64 std::shared_ptr<Logger> logger)>;
66 static const std::error_category& errorCategory() noexcept;
68 AsyncRedisStorage(const AsyncRedisStorage&) = delete;
70 AsyncRedisStorage& operator = (const AsyncRedisStorage&) = delete;
72 AsyncRedisStorage(std::shared_ptr<Engine> engine,
73 std::shared_ptr<redis::AsyncDatabaseDiscovery> discovery,
74 const boost::optional<PublisherId>& pId,
75 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations,
76 std::shared_ptr<Logger> logger);
78 AsyncRedisStorage(std::shared_ptr<Engine> engine,
79 std::shared_ptr<redis::AsyncDatabaseDiscovery> discovery,
80 const boost::optional<PublisherId>& pId,
81 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations,
82 const AsyncCommandDispatcherCreator& asyncCommandDispatcherCreator,
83 std::shared_ptr<redis::ContentsBuilder> contentsBuilder,
84 std::shared_ptr<Logger> logger);
86 ~AsyncRedisStorage() override;
88 int fd() const override;
90 void handleEvents() override;
92 void waitReadyAsync(const Namespace& ns, const ReadyAck& readyAck) override;
94 void setAsync(const Namespace& ns, const DataMap& dataMap, const ModifyAck& modifyAck) override;
96 void setIfAsync(const Namespace& ns, const Key& key, const Data& oldData, const Data& newData, const ModifyIfAck& modifyIfAck) override;
98 void setIfNotExistsAsync(const Namespace& ns, const Key& key, const Data& data, const ModifyIfAck& modifyIfAck) override;
100 void getAsync(const Namespace& ns, const Keys& keys, const GetAck& getAck) override;
102 void removeAsync(const Namespace& ns, const Keys& keys, const ModifyAck& modifyAck) override;
104 void removeIfAsync(const Namespace& ns, const Key& key, const Data& data, const ModifyIfAck& modifyIfAck) override;
106 void findKeysAsync(const Namespace& ns, const std::string& keyPrefix, const FindKeysAck& findKeysAck) override;
108 void removeAllAsync(const Namespace& ns, const ModifyAck& modifyAck) override;
110 redis::DatabaseInfo& getDatabaseInfo();
112 std::string buildKeyPrefixSearchPattern(const Namespace& ns, const std::string& keyPrefix) const;
115 std::shared_ptr<Engine> engine;
116 std::shared_ptr<redis::AsyncCommandDispatcher> dispatcher;
117 std::shared_ptr<redis::AsyncDatabaseDiscovery> discovery;
118 const boost::optional<PublisherId> publisherId;
120 AsyncCommandDispatcherCreator asyncCommandDispatcherCreator;
121 std::shared_ptr<redis::ContentsBuilder> contentsBuilder;
122 redis::DatabaseInfo dbInfo;
123 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations;
124 std::shared_ptr<Logger> logger;
126 bool canOperationBePerformed(const Namespace& ns, boost::optional<bool> inputDataIsEmpty, std::error_code& ecToReturn);
128 void serviceStateChanged(const redis::DatabaseInfo& databaseInfo);
130 std::string getPublishMessage() const;
132 void modificationCommandCallback(const std::error_code& error, const redis::Reply&, const ModifyAck&);
134 void conditionalCommandCallback(const std::error_code& error, const redis::Reply&, const ModifyIfAck&);
137 AsyncRedisStorage::ErrorCode& operator++ (AsyncRedisStorage::ErrorCode& ecEnum);
138 std::error_code make_error_code(AsyncRedisStorage::ErrorCode errorCode);
144 struct is_error_code_enum<shareddatalayer::AsyncRedisStorage::ErrorCode>: public true_type { };