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).
23 #include "private/error.hpp"
24 #include "private/abort.hpp"
25 #include "private/asyncstorageimpl.hpp"
26 #include "private/configurationreader.hpp"
27 #include "private/asyncdummystorage.hpp"
28 #include "private/engine.hpp"
29 #include "private/logger.hpp"
31 #include "private/redis/asyncredisstorage.hpp"
34 using namespace shareddatalayer;
35 using namespace shareddatalayer::redis;
39 std::shared_ptr<AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine> engine,
40 const DatabaseConfiguration& databaseConfiguration,
41 std::shared_ptr<Logger> logger)
43 return AsyncDatabaseDiscovery::create(engine,
45 databaseConfiguration,
50 AsyncStorageImpl::AsyncStorageImpl(std::shared_ptr<Engine> engine,
51 const boost::optional<PublisherId>& pId,
52 std::shared_ptr<Logger> logger):
54 databaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
55 namespaceConfigurations(std::make_shared<NamespaceConfigurationsImpl>()),
58 asyncDatabaseDiscoveryCreator(::asyncDatabaseDiscoveryCreator)
60 ConfigurationReader configurationReader(logger);
61 configurationReader.readDatabaseConfiguration(std::ref(*databaseConfiguration));
62 configurationReader.readNamespaceConfigurations(std::ref(*namespaceConfigurations));
66 AsyncStorageImpl::AsyncStorageImpl(std::shared_ptr<Engine> engine,
67 const boost::optional<PublisherId>& pId,
68 std::shared_ptr<DatabaseConfiguration> databaseConfiguration,
69 std::shared_ptr<NamespaceConfigurations> namespaceConfigurations,
70 std::shared_ptr<Logger> logger,
71 const AsyncDatabaseDiscoveryCreator& asyncDatabaseDiscoveryCreator):
73 databaseConfiguration(databaseConfiguration),
74 namespaceConfigurations(namespaceConfigurations),
77 asyncDatabaseDiscoveryCreator(asyncDatabaseDiscoveryCreator)
81 AsyncStorage& AsyncStorageImpl::getRedisHandler()
84 static AsyncRedisStorage redisHandler{engine,
85 asyncDatabaseDiscoveryCreator(
87 std::ref(*databaseConfiguration),
90 namespaceConfigurations,
95 logger->error() << "Redis operations cannot be performed, Redis not enabled";
96 SHAREDDATALAYER_ABORT("Invalid configuration.");
100 AsyncStorage& AsyncStorageImpl::getDummyHandler()
102 static AsyncDummyStorage dummyHandler{engine};
106 AsyncStorage& AsyncStorageImpl::getOperationHandler(const std::string& ns)
108 if (namespaceConfigurations->isDbBackendUseEnabled(ns))
109 return getRedisHandler();
111 return getDummyHandler();
114 int AsyncStorageImpl::fd() const
119 void AsyncStorageImpl::handleEvents()
121 engine->handleEvents();
124 void AsyncStorageImpl::waitReadyAsync(const Namespace& ns,
125 const ReadyAck& readyAck)
127 getOperationHandler(ns).waitReadyAsync(ns, readyAck);
130 void AsyncStorageImpl::setAsync(const Namespace& ns,
131 const DataMap& dataMap,
132 const ModifyAck& modifyAck)
134 getOperationHandler(ns).setAsync(ns, dataMap, modifyAck);
137 void AsyncStorageImpl::setIfAsync(const Namespace& ns,
141 const ModifyIfAck& modifyIfAck)
143 getOperationHandler(ns).setIfAsync(ns, key, oldData, newData, modifyIfAck);
146 void AsyncStorageImpl::removeIfAsync(const Namespace& ns,
149 const ModifyIfAck& modifyIfAck)
151 getOperationHandler(ns).removeIfAsync(ns, key, data, modifyIfAck);
154 void AsyncStorageImpl::setIfNotExistsAsync(const Namespace& ns,
157 const ModifyIfAck& modifyIfAck)
159 getOperationHandler(ns).setIfNotExistsAsync(ns, key, data, modifyIfAck);
162 void AsyncStorageImpl::getAsync(const Namespace& ns,
164 const GetAck& getAck)
166 getOperationHandler(ns).getAsync(ns, keys, getAck);
169 void AsyncStorageImpl::removeAsync(const Namespace& ns,
171 const ModifyAck& modifyAck)
173 getOperationHandler(ns).removeAsync(ns, keys, modifyAck);
176 void AsyncStorageImpl::findKeysAsync(const Namespace& ns,
177 const std::string& keyPrefix,
178 const FindKeysAck& findKeysAck)
180 getOperationHandler(ns).findKeysAsync(ns, keyPrefix, findKeysAck);
183 void AsyncStorageImpl::removeAllAsync(const Namespace& ns,
184 const ModifyAck& modifyAck)
186 getOperationHandler(ns).removeAllAsync(ns, modifyAck);