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.
19 #include <sdl/asyncstorage.hpp>
20 #include <sdl/backenderror.hpp>
21 #include <sdl/errorqueries.hpp>
22 #include <sdl/invalidnamespace.hpp>
23 #include <sdl/notconnected.hpp>
24 #include <sdl/operationinterrupted.hpp>
25 #include <sdl/rejectedbybackend.hpp>
26 #include <sdl/rejectedbysdl.hpp>
27 #include "private/redis/asyncredisstorage.hpp"
28 #include "private/syncstorageimpl.hpp"
29 #include "private/system.hpp"
31 using namespace shareddatalayer;
35 void throwExceptionForErrorCode[[ noreturn ]](const std::error_code& ec)
37 if (ec == shareddatalayer::Error::BACKEND_FAILURE)
38 throw BackendError(ec.message());
39 else if (ec == shareddatalayer::Error::NOT_CONNECTED)
40 throw NotConnected(ec.message());
41 else if (ec == shareddatalayer::Error::OPERATION_INTERRUPTED)
42 throw OperationInterrupted(ec.message());
43 else if (ec == shareddatalayer::Error::REJECTED_BY_BACKEND)
44 throw RejectedByBackend(ec.message());
45 else if (ec == AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE)
46 throw InvalidNamespace(ec.message());
47 else if (ec == shareddatalayer::Error::REJECTED_BY_SDL)
48 throw RejectedBySdl(ec.message());
50 std::ostringstream os;
51 os << "No corresponding SDL exception found for error code: " << ec.category().name() << " " << ec.value();
52 throw std::range_error(os.str());
56 SyncStorageImpl::SyncStorageImpl(std::unique_ptr<AsyncStorage> asyncStorage):
57 SyncStorageImpl(std::move(asyncStorage), System::getSystem())
61 SyncStorageImpl::SyncStorageImpl(std::unique_ptr<AsyncStorage> pAsyncStorage,
63 asyncStorage(std::move(pAsyncStorage)),
65 pFd(asyncStorage->fd()),
71 void SyncStorageImpl::modifyAck(const std::error_code& error)
77 void SyncStorageImpl::modifyIfAck(const std::error_code& error, bool status)
84 void SyncStorageImpl::getAck(const std::error_code& error, const DataMap& dataMap)
91 void SyncStorageImpl::findKeysAck(const std::error_code& error, const Keys& keys)
98 void SyncStorageImpl::verifyBackendResponse()
101 throwExceptionForErrorCode(localError);
104 void SyncStorageImpl::waitForCallback()
106 struct pollfd events { pFd, POLLIN, 0 };
108 if (system.poll(&events, 1, -1) > 0 && (events.revents & POLLIN))
109 asyncStorage->handleEvents();
112 void SyncStorageImpl::waitSdlToBeReady(const Namespace& ns)
115 asyncStorage->waitReadyAsync(ns,
116 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
120 verifyBackendResponse();
123 void SyncStorageImpl::set(const Namespace& ns, const DataMap& dataMap)
125 waitSdlToBeReady(ns);
127 asyncStorage->setAsync(ns,
129 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
131 std::placeholders::_1));
133 verifyBackendResponse();
136 bool SyncStorageImpl::setIf(const Namespace& ns, const Key& key, const Data& oldData, const Data& newData)
138 waitSdlToBeReady(ns);
140 asyncStorage->setIfAsync(ns,
144 std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck,
146 std::placeholders::_1,
147 std::placeholders::_2));
149 verifyBackendResponse();
153 bool SyncStorageImpl::setIfNotExists(const Namespace& ns, const Key& key, const Data& data)
155 waitSdlToBeReady(ns);
157 asyncStorage->setIfNotExistsAsync(ns,
160 std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck,
162 std::placeholders::_1,
163 std::placeholders::_2));
165 verifyBackendResponse();
169 SyncStorageImpl::DataMap SyncStorageImpl::get(const Namespace& ns, const Keys& keys)
171 waitSdlToBeReady(ns);
173 asyncStorage->getAsync(ns,
175 std::bind(&shareddatalayer::SyncStorageImpl::getAck,
177 std::placeholders::_1,
178 std::placeholders::_2));
180 verifyBackendResponse();
184 void SyncStorageImpl::remove(const Namespace& ns, const Keys& keys)
186 waitSdlToBeReady(ns);
188 asyncStorage->removeAsync(ns,
190 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
192 std::placeholders::_1));
194 verifyBackendResponse();
197 bool SyncStorageImpl::removeIf(const Namespace& ns, const Key& key, const Data& data)
199 waitSdlToBeReady(ns);
201 asyncStorage->removeIfAsync(ns,
204 std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck,
206 std::placeholders::_1,
207 std::placeholders::_2));
209 verifyBackendResponse();
213 SyncStorageImpl::Keys SyncStorageImpl::findKeys(const Namespace& ns, const std::string& keyPrefix)
215 waitSdlToBeReady(ns);
217 asyncStorage->findKeysAsync(ns,
219 std::bind(&shareddatalayer::SyncStorageImpl::findKeysAck,
221 std::placeholders::_1,
222 std::placeholders::_2));
224 verifyBackendResponse();
228 void SyncStorageImpl::removeAll(const Namespace& ns)
230 waitSdlToBeReady(ns);
232 asyncStorage->removeAllAsync(ns,
233 std::bind(&shareddatalayer::SyncStorageImpl::modifyAck,
235 std::placeholders::_1));
237 verifyBackendResponse();