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 #include "private/error.hpp"
19 #include "private/abort.hpp"
20 #include "private/asyncstorageimpl.hpp"
21 #include "private/configurationreader.hpp"
22 #include "private/asyncdummystorage.hpp"
23 #include "private/engine.hpp"
24 #include "private/logger.hpp"
26 #include "private/redis/asyncdatabasediscovery.hpp"
27 #include "private/redis/asyncredisstorage.hpp"
30 using namespace shareddatalayer;
32 AsyncStorageImpl::AsyncStorageImpl(std::shared_ptr<Engine> engine,
33 const boost::optional<PublisherId>& pId,
34 std::shared_ptr<Logger> logger):
36 databaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
37 namespaceConfigurations(std::make_shared<NamespaceConfigurationsImpl>()),
41 ConfigurationReader configurationReader(logger);
42 configurationReader.readDatabaseConfiguration(std::ref(*databaseConfiguration));
43 configurationReader.readNamespaceConfigurations(std::ref(*namespaceConfigurations));
47 AsyncStorageImpl::AsyncStorageImpl(std::shared_ptr<Engine> engine,
48 const boost::optional<PublisherId>& pId,
49 std::shared_ptr<DatabaseConfiguration> databaseConfiguration,
50 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations,
51 std::shared_ptr<Logger> logger):
53 databaseConfiguration(databaseConfiguration),
54 namespaceConfigurations(namespaceConfigurations),
60 AsyncStorage& AsyncStorageImpl::getRedisHandler()
63 static AsyncRedisStorage redisHandler{engine,
64 redis::AsyncDatabaseDiscovery::create(
67 std::ref(*databaseConfiguration),
70 namespaceConfigurations,
75 logger->error() << "Redis operations cannot be performed, Redis not enabled";
76 SHAREDDATALAYER_ABORT("Invalid configuration.");
80 AsyncStorage& AsyncStorageImpl::getDummyHandler()
82 static AsyncDummyStorage dummyHandler{engine};
86 AsyncStorage& AsyncStorageImpl::getOperationHandler(const std::string& ns)
88 if (namespaceConfigurations->isDbBackendUseEnabled(ns))
89 return getRedisHandler();
91 return getDummyHandler();
94 int AsyncStorageImpl::fd() const
99 void AsyncStorageImpl::handleEvents()
101 engine->handleEvents();
104 void AsyncStorageImpl::waitReadyAsync(const Namespace& ns,
105 const ReadyAck& readyAck)
107 getOperationHandler(ns).waitReadyAsync(ns, readyAck);
110 void AsyncStorageImpl::setAsync(const Namespace& ns,
111 const DataMap& dataMap,
112 const ModifyAck& modifyAck)
114 getOperationHandler(ns).setAsync(ns, dataMap, modifyAck);
117 void AsyncStorageImpl::setIfAsync(const Namespace& ns,
121 const ModifyIfAck& modifyIfAck)
123 getOperationHandler(ns).setIfAsync(ns, key, oldData, newData, modifyIfAck);
126 void AsyncStorageImpl::removeIfAsync(const Namespace& ns,
129 const ModifyIfAck& modifyIfAck)
131 getOperationHandler(ns).removeIfAsync(ns, key, data, modifyIfAck);
134 void AsyncStorageImpl::setIfNotExistsAsync(const Namespace& ns,
137 const ModifyIfAck& modifyIfAck)
139 getOperationHandler(ns).setIfNotExistsAsync(ns, key, data, modifyIfAck);
142 void AsyncStorageImpl::getAsync(const Namespace& ns,
144 const GetAck& getAck)
146 getOperationHandler(ns).getAsync(ns, keys, getAck);
149 void AsyncStorageImpl::removeAsync(const Namespace& ns,
151 const ModifyAck& modifyAck)
153 getOperationHandler(ns).removeAsync(ns, keys, modifyAck);
156 void AsyncStorageImpl::findKeysAsync(const Namespace& ns,
157 const std::string& keyPrefix,
158 const FindKeysAck& findKeysAck)
160 getOperationHandler(ns).findKeysAsync(ns, keyPrefix, findKeysAck);
163 void AsyncStorageImpl::removeAllAsync(const Namespace& ns,
164 const ModifyAck& modifyAck)
166 getOperationHandler(ns).removeAllAsync(ns, modifyAck);