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).
24 #include <sdl/asyncstorage.hpp>
25 #include <sdl/backenderror.hpp>
26 #include <sdl/errorqueries.hpp>
27 #include <sdl/invalidnamespace.hpp>
28 #include <sdl/notconnected.hpp>
29 #include <sdl/operationinterrupted.hpp>
30 #include <sdl/rejectedbybackend.hpp>
31 #include <sdl/rejectedbysdl.hpp>
32 #include "private/redis/asyncredisstorage.hpp"
33 #include "private/syncstorageimpl.hpp"
34 #include "private/system.hpp"
36 using namespace shareddatalayer;
40 void throwExceptionForErrorCode[[ noreturn ]](const std::error_code& ec)
42 if (ec == shareddatalayer::Error::BACKEND_FAILURE)
43 throw BackendError(ec.message());
44 else if (ec == shareddatalayer::Error::NOT_CONNECTED)
45 throw NotConnected(ec.message());
46 else if (ec == shareddatalayer::Error::OPERATION_INTERRUPTED)
47 throw OperationInterrupted(ec.message());
48 else if (ec == shareddatalayer::Error::REJECTED_BY_BACKEND)
49 throw RejectedByBackend(ec.message());
50 else if (ec == AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE)
51 throw InvalidNamespace(ec.message());
52 else if (ec == shareddatalayer::Error::REJECTED_BY_SDL)
53 throw RejectedBySdl(ec.message());
55 std::ostringstream os;
56 os << "No corresponding SDL exception found for error code: " << ec.category().name() << " " << ec.value();
57 throw std::range_error(os.str());
61 SyncStorageImpl::SyncStorageImpl(std::unique_ptr<AsyncStorage> asyncStorage):
62 SyncStorageImpl(std::move(asyncStorage), System::getSystem())
66 SyncStorageImpl::SyncStorageImpl(std::unique_ptr<AsyncStorage> pAsyncStorage,
68 asyncStorage(std::move(pAsyncStorage)),
70 pFd(asyncStorage->fd()),
76 void SyncStorageImpl::modifyAck(const std::error_code& error)
82 void SyncStorageImpl::modifyIfAck(const std::error_code& error, bool status)
89 void SyncStorageImpl::getAck(const std::error_code& error, const DataMap& dataMap)
96 void SyncStorageImpl::findKeysAck(const std::error_code& error, const Keys& keys)
103 void SyncStorageImpl::verifyBackendResponse()
106 throwExceptionForErrorCode(localError);
109 void SyncStorageImpl::waitForCallback()
111 struct pollfd events { pFd, POLLIN, 0 };
113 if (system.poll(&events, 1, -1) > 0 && (events.revents & POLLIN))
114 asyncStorage->handleEvents();
117 void SyncStorageImpl::waitSdlToBeReady(const Namespace& ns)
120 asyncStorage->waitReadyAsync(ns,
121 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
125 verifyBackendResponse();
128 void SyncStorageImpl::set(const Namespace& ns, const DataMap& dataMap)
130 waitSdlToBeReady(ns);
132 asyncStorage->setAsync(ns,
134 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
136 std::placeholders::_1));
138 verifyBackendResponse();
141 bool SyncStorageImpl::setIf(const Namespace& ns, const Key& key, const Data& oldData, const Data& newData)
143 waitSdlToBeReady(ns);
145 asyncStorage->setIfAsync(ns,
149 std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck,
151 std::placeholders::_1,
152 std::placeholders::_2));
154 verifyBackendResponse();
158 bool SyncStorageImpl::setIfNotExists(const Namespace& ns, const Key& key, const Data& data)
160 waitSdlToBeReady(ns);
162 asyncStorage->setIfNotExistsAsync(ns,
165 std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck,
167 std::placeholders::_1,
168 std::placeholders::_2));
170 verifyBackendResponse();
174 SyncStorageImpl::DataMap SyncStorageImpl::get(const Namespace& ns, const Keys& keys)
176 waitSdlToBeReady(ns);
178 asyncStorage->getAsync(ns,
180 std::bind(&shareddatalayer::SyncStorageImpl::getAck,
182 std::placeholders::_1,
183 std::placeholders::_2));
185 verifyBackendResponse();
189 void SyncStorageImpl::remove(const Namespace& ns, const Keys& keys)
191 waitSdlToBeReady(ns);
193 asyncStorage->removeAsync(ns,
195 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
197 std::placeholders::_1));
199 verifyBackendResponse();
202 bool SyncStorageImpl::removeIf(const Namespace& ns, const Key& key, const Data& data)
204 waitSdlToBeReady(ns);
206 asyncStorage->removeIfAsync(ns,
209 std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck,
211 std::placeholders::_1,
212 std::placeholders::_2));
214 verifyBackendResponse();
218 SyncStorageImpl::Keys SyncStorageImpl::findKeys(const Namespace& ns, const std::string& keyPrefix)
220 waitSdlToBeReady(ns);
222 asyncStorage->findKeysAsync(ns,
224 std::bind(&shareddatalayer::SyncStorageImpl::findKeysAck,
226 std::placeholders::_1,
227 std::placeholders::_2));
229 verifyBackendResponse();
233 void SyncStorageImpl::removeAll(const Namespace& ns)
235 waitSdlToBeReady(ns);
237 asyncStorage->removeAllAsync(ns,
238 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
240 std::placeholders::_1));
242 verifyBackendResponse();