Add first version
[ric-plt/sdl.git] / src / asyncstorageimpl.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
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
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #include "config.h"
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"
25 #if HAVE_REDIS
26 #include "private/redis/asyncdatabasediscovery.hpp"
27 #include "private/redis/asyncredisstorage.hpp"
28 #endif
29
30 using namespace shareddatalayer;
31
32 AsyncStorageImpl::AsyncStorageImpl(std::shared_ptr<Engine> engine,
33                                    const boost::optional<PublisherId>& pId,
34                                    std::shared_ptr<Logger> logger):
35     engine(engine),
36     databaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
37     namespaceConfigurations(std::make_shared<NamespaceConfigurationsImpl>()),
38     publisherId(pId),
39     logger(logger)
40 {
41     ConfigurationReader configurationReader(logger);
42     configurationReader.readDatabaseConfiguration(std::ref(*databaseConfiguration));
43     configurationReader.readNamespaceConfigurations(std::ref(*namespaceConfigurations));
44 }
45
46 // Meant for UT usage
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):
52     engine(engine),
53     databaseConfiguration(databaseConfiguration),
54     namespaceConfigurations(namespaceConfigurations),
55     publisherId(pId),
56     logger(logger)
57 {
58 }
59
60 AsyncStorage& AsyncStorageImpl::getRedisHandler()
61 {
62 #if HAVE_REDIS
63     static AsyncRedisStorage redisHandler{engine,
64                                           redis::AsyncDatabaseDiscovery::create(
65                                               engine,
66                                               boost::none,
67                                               std::ref(*databaseConfiguration),
68                                               logger),
69                                           publisherId,
70                                           namespaceConfigurations,
71                                           logger};
72
73     return redisHandler;
74 #else
75     logger->error() << "Redis operations cannot be performed, Redis not enabled";
76     SHAREDDATALAYER_ABORT("Invalid configuration.");
77 #endif
78 }
79
80 AsyncStorage& AsyncStorageImpl::getDummyHandler()
81 {
82     static AsyncDummyStorage dummyHandler{engine};
83     return dummyHandler;
84 }
85
86 AsyncStorage& AsyncStorageImpl::getOperationHandler(const std::string& ns)
87 {
88     if (namespaceConfigurations->isDbBackendUseEnabled(ns))
89         return getRedisHandler();
90
91     return getDummyHandler();
92 }
93
94 int AsyncStorageImpl::fd() const
95 {
96     return engine->fd();
97 }
98
99 void AsyncStorageImpl::handleEvents()
100 {
101     engine->handleEvents();
102 }
103
104 void AsyncStorageImpl::waitReadyAsync(const Namespace& ns,
105                                       const ReadyAck& readyAck)
106 {
107     getOperationHandler(ns).waitReadyAsync(ns, readyAck);
108 }
109
110 void AsyncStorageImpl::setAsync(const Namespace& ns,
111                                 const DataMap& dataMap,
112                                 const ModifyAck& modifyAck)
113 {
114     getOperationHandler(ns).setAsync(ns, dataMap, modifyAck);
115 }
116
117 void AsyncStorageImpl::setIfAsync(const Namespace& ns,
118                                   const Key& key,
119                                   const Data& oldData,
120                                   const Data& newData,
121                                   const ModifyIfAck& modifyIfAck)
122 {
123     getOperationHandler(ns).setIfAsync(ns, key, oldData, newData, modifyIfAck);
124 }
125
126 void AsyncStorageImpl::removeIfAsync(const Namespace& ns,
127                                      const Key& key,
128                                      const Data& data,
129                                      const ModifyIfAck& modifyIfAck)
130 {
131     getOperationHandler(ns).removeIfAsync(ns, key, data, modifyIfAck);
132 }
133
134 void AsyncStorageImpl::setIfNotExistsAsync(const Namespace& ns,
135                                            const Key& key,
136                                            const Data& data,
137                                            const ModifyIfAck& modifyIfAck)
138 {
139     getOperationHandler(ns).setIfNotExistsAsync(ns, key, data, modifyIfAck);
140 }
141
142 void AsyncStorageImpl::getAsync(const Namespace& ns,
143                                 const Keys& keys,
144                                 const GetAck& getAck)
145 {
146     getOperationHandler(ns).getAsync(ns, keys, getAck);
147 }
148
149 void AsyncStorageImpl::removeAsync(const Namespace& ns,
150                                    const Keys& keys,
151                                    const ModifyAck& modifyAck)
152 {
153     getOperationHandler(ns).removeAsync(ns, keys, modifyAck);
154 }
155
156 void AsyncStorageImpl::findKeysAsync(const Namespace& ns,
157                                      const std::string& keyPrefix,
158                                      const FindKeysAck& findKeysAck)
159 {
160     getOperationHandler(ns).findKeysAsync(ns, keyPrefix, findKeysAck);
161 }
162
163 void AsyncStorageImpl::removeAllAsync(const Namespace& ns,
164                                        const ModifyAck& modifyAck)
165 {
166     getOperationHandler(ns).removeAllAsync(ns, modifyAck);
167 }