• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AUTO_LAUNCH_H
17 #define AUTO_LAUNCH_H
18 
19 #include <set>
20 #include <map>
21 #include <mutex>
22 #include "auto_launch_export.h"
23 #include "db_properties.h"
24 #include "ikvdb_connection.h"
25 #include "icommunicator_aggregator.h"
26 #include "kv_store_observer.h"
27 #include "kvdb_properties.h"
28 #include "types_export.h"
29 #include "relational_store_connection.h"
30 #include "relationaldb_properties.h"
31 #include "store_observer.h"
32 
33 namespace DistributedDB {
34 enum class AutoLaunchItemState {
35     UN_INITIAL = 0,
36     IN_ENABLE,
37     IN_LIFE_CYCLE_CALL_BACK, // in LifeCycleCallback
38     IN_COMMUNICATOR_CALL_BACK, // in OnConnectCallback or CommunicatorLackCallback
39     IDLE,
40 };
41 
42 enum class DBType {
43     DB_KV = 0,
44     DB_RELATION,
45     DB_INVALID,
46 };
47 
48 struct AutoLaunchItem {
49     std::shared_ptr<DBProperties> propertiesPtr;
50     AutoLaunchNotifier notifier;
51     KvStoreObserver *observer = nullptr;
52     int conflictType = 0;
53     KvStoreNbConflictNotifier conflictNotifier;
54     void *conn = nullptr;
55     KvDBObserverHandle *observerHandle = nullptr;
56     bool isWriteOpenNotified = false;
57     AutoLaunchItemState state = AutoLaunchItemState::UN_INITIAL;
58     bool isDisable = false;
59     bool inObserver = false;
60     bool isAutoSync = true;
61     DBType type = DBType::DB_INVALID;
62     StoreObserver *storeObserver = nullptr;
63 };
64 
65 class AutoLaunch {
66 public:
67     static int GetAutoLaunchProperties(const AutoLaunchParam &param, const DBType &openType, bool checkDir,
68         std::shared_ptr<DBProperties> &propertiesPtr);
69 
70     AutoLaunch() = default;
71 
72     virtual ~AutoLaunch();
73 
74     DISABLE_COPY_ASSIGN_MOVE(AutoLaunch);
75 
76     void SetCommunicatorAggregator(ICommunicatorAggregator *aggregator);
77 
78     int EnableKvStoreAutoLaunch(const KvDBProperties &properties, AutoLaunchNotifier notifier,
79         const AutoLaunchOption &option);
80 
81     int DisableKvStoreAutoLaunch(const std::string &normalIdentifier, const std::string &dualTupleIdentifier,
82         const std::string &userId);
83 
84     void GetAutoLaunchSyncDevices(const std::string &identifier, std::vector<std::string> &devices) const;
85 
86     void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback, DBType type);
87 
88 protected:
89     static int OpenOneConnection(AutoLaunchItem &autoLaunchItem);
90 
91     // we will return errCode, if errCode != E_OK
92     static int CloseConnectionStrict(AutoLaunchItem &autoLaunchItem);
93 
94     static void CloseNotifier(const AutoLaunchItem &autoLaunchItem);
95 
96     static int SetConflictNotifier(AutoLaunchItem &autoLaunchItem);
97 
98     static int GetAutoLaunchKVProperties(const AutoLaunchParam &param,
99         const std::shared_ptr<KvDBProperties> &propertiesPtr, bool checkDir);
100 
101     static int GetAutoLaunchRelationProperties(const AutoLaunchParam &param,
102         const std::shared_ptr<RelationalDBProperties> &propertiesPtr);
103 
104     static int OpenKvConnection(AutoLaunchItem &autoLaunchItem);
105 
106     static int OpenRelationalConnection(AutoLaunchItem &autoLaunchItem);
107 
108     static int PragmaAutoSync(AutoLaunchItem &autoLaunchItem);
109 
110     int EnableKvStoreAutoLaunchParmCheck(AutoLaunchItem &autoLaunchItem, const std::string &normalIdentifier,
111         const std::string &dualTupleIdentifier, bool isDualTupleMode);
112 
113     int GetKVConnectionInEnable(AutoLaunchItem &autoLaunchItem, const std::string &identifier);
114 
115     // before ReleaseDatabaseConnection, if errCode != E_OK, we not return, we try close more
116     virtual void TryCloseConnection(AutoLaunchItem &autoLaunchItem);
117 
118     int RegisterObserverAndLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier,
119         bool isExt);
120 
121     int RegisterObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt);
122 
123     void ObserverFunc(const KvDBCommitNotifyData &notifyData, const std::string &identifier,
124         const std::string &userId);
125 
126     void ConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId);
127 
128     void OnlineCallBackTask();
129 
130     void GetDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap);
131 
132     void GetConnInDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap);
133 
134     void UpdateGlobalMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap);
135 
136     void ReceiveUnknownIdentifierCallBackTask(const std::string &identifier, const std::string userId);
137 
138     void ConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId);
139 
140     void OnlineCallBack(const std::string &device, bool isConnect);
141 
142     int ReceiveUnknownIdentifierCallBack(const LabelType &label, const std::string &originalUserId);
143 
144     int AutoLaunchExt(const std::string &identifier, const std::string &userId);
145 
146     void AutoLaunchExtTask(const std::string identifier, const std::string userId, AutoLaunchItem autoLaunchItem);
147 
148     void ExtObserverFunc(const KvDBCommitNotifyData &notifyData, const std::string &identifier,
149         const std::string &userId);
150 
151     void ExtConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId);
152 
153     void ExtConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId);
154 
155     int ExtAutoLaunchRequestCallBack(const std::string &identifier, AutoLaunchParam &param, DBType &openType);
156 
157     int RegisterLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt);
158 
159     void TryCloseKvConnection(AutoLaunchItem &autoLaunchItem);
160 
161     void TryCloseRelationConnection(AutoLaunchItem &autoLaunchItem);
162 
163     void EraseAutoLauchItem(const std::string &identifier, const std::string &userId);
164 
165     void NotifyInvalidParam(const AutoLaunchItem &autoLaunchItem);
166 
167     int CheckAutoLaunchRealPath(const AutoLaunchItem &autoLaunchItem);
168 
169     mutable std::mutex dataLock_;
170     mutable std::mutex communicatorLock_;
171     std::set<std::string> onlineDevices_;
172     // key: label, value: <userId, AutoLaunchItem>
173     std::map<std::string, std::map<std::string, AutoLaunchItem>> autoLaunchItemMap_;
174     ICommunicatorAggregator *communicatorAggregator_ = nullptr;
175     std::condition_variable cv_;
176 
177     std::mutex extLock_;
178     std::map<DBType, AutoLaunchRequestCallback> autoLaunchRequestCallbackMap_;
179     // key: label, value: <userId, AutoLaunchItem>
180     std::map<std::string, std::map<std::string, AutoLaunchItem>> extItemMap_;
181 };
182 } // namespace DistributedDB
183 #endif // AUTO_LAUNCH_H
184