735274f549f3dc0f855320f13c184cc3c18dab71
[ric-plt/sdl.git] / include / private / syncstorageimpl.hpp
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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #ifndef SHAREDDATALAYER_SYNCSTORAGEIMPL_HPP_
23 #define SHAREDDATALAYER_SYNCSTORAGEIMPL_HPP_
24
25 #include <sdl/asyncstorage.hpp>
26 #include <sdl/syncstorage.hpp>
27 #include <sys/poll.h>
28 #include <system_error>
29
30 namespace shareddatalayer
31 {
32     class System;
33
34     class SyncStorageImpl: public SyncStorage
35     {
36     public:
37         explicit SyncStorageImpl(std::unique_ptr<AsyncStorage> asyncStorage);
38
39         SyncStorageImpl(std::unique_ptr<AsyncStorage> asyncStorage,
40                         System& system);
41
42         virtual void set(const Namespace& ns, const DataMap& dataMap) override;
43
44         virtual bool setIf(const Namespace& ns, const Key& key, const Data& oldData, const Data& newData) override;
45
46         virtual bool setIfNotExists(const Namespace& ns, const Key& key, const Data& data) override;
47
48         virtual DataMap get(const Namespace& ns, const Keys& keys) override;
49
50         virtual void remove(const Namespace& ns, const Keys& keys) override;
51
52         virtual bool removeIf(const Namespace& ns, const Key& key, const Data& data) override;
53
54         virtual Keys findKeys(const Namespace& ns, const std::string& keyPrefix) override;
55
56         virtual void removeAll(const Namespace& ns) override;
57
58         virtual void setOperationTimeout(const std::chrono::steady_clock::duration& timeout) override;
59
60         static constexpr int NO_TIMEOUT = -1;
61
62     private:
63         std::unique_ptr<AsyncStorage> asyncStorage;
64         System& system;
65         DataMap localMap;
66         Keys localKeys;
67         bool localStatus;
68         std::error_code localError;
69         bool synced;
70         bool isReady;
71         struct pollfd events;
72         std::chrono::steady_clock::duration operationTimeout;
73
74         void verifyBackendResponse();
75
76         void pollAndHandleEvents(int timeout_ms);
77
78         void waitForReadinessCheckCallback();
79
80         void waitForOperationCallback();
81
82         void waitSdlToBeReady(const Namespace& ns);
83
84         void waitReadyAck(const std::error_code& error);
85
86         void modifyAck(const std::error_code& error);
87
88         void modifyIfAck(const std::error_code& error, bool status);
89
90         void getAck(const std::error_code& error, const DataMap& dataMap);
91
92         void findKeysAck(const std::error_code& error, const Keys& keys);
93
94         void handlePendingEvents();
95     };
96 }
97
98 #endif