• 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     void Dump(int fd);
89 
90     void CloseConnection(DBType type, const DBProperties &properties);
91 
92 protected:
93     static int OpenOneConnection(AutoLaunchItem &autoLaunchItem);
94 
95     // we will return errCode, if errCode != E_OK
96     static int CloseConnectionStrict(AutoLaunchItem &autoLaunchItem);
97 
98     static void CloseNotifier(const AutoLaunchItem &autoLaunchItem);
99 
100     static int SetConflictNotifier(AutoLaunchItem &autoLaunchItem);
101 
102     static int GetAutoLaunchKVProperties(const AutoLaunchParam &param,
103         const std::shared_ptr<KvDBProperties> &propertiesPtr, bool checkDir);
104 
105     static int GetAutoLaunchRelationProperties(const AutoLaunchParam &param,
106         const std::shared_ptr<RelationalDBProperties> &propertiesPtr);
107 
108     static int OpenKvConnection(AutoLaunchItem &autoLaunchItem);
109 
110     static int OpenRelationalConnection(AutoLaunchItem &autoLaunchItem);
111 
112     static int PragmaAutoSync(AutoLaunchItem &autoLaunchItem);
113 
114     int EnableKvStoreAutoLaunchParmCheck(AutoLaunchItem &autoLaunchItem, const std::string &normalIdentifier,
115         const std::string &dualTupleIdentifier, bool isDualTupleMode);
116 
117     int GetKVConnectionInEnable(AutoLaunchItem &autoLaunchItem, const std::string &identifier);
118 
119     // before ReleaseDatabaseConnection, if errCode != E_OK, we not return, we try close more
120     virtual void TryCloseConnection(AutoLaunchItem &autoLaunchItem);
121 
122     int RegisterObserverAndLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier,
123         bool isExt);
124 
125     int RegisterObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt);
126 
127     void ObserverFunc(const KvDBCommitNotifyData &notifyData, const std::string &identifier,
128         const std::string &userId);
129 
130     void ConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId);
131 
132     void OnlineCallBackTask();
133 
134     void GetDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap);
135 
136     void GetConnInDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap);
137 
138     void UpdateGlobalMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap);
139 
140     void ReceiveUnknownIdentifierCallBackTask(const std::string &identifier, const std::string &userId);
141 
142     void ConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId);
143 
144     void OnlineCallBack(const std::string &device, bool isConnect);
145 
146     int ReceiveUnknownIdentifierCallBack(const LabelType &label, const std::string &originalUserId);
147 
148     int AutoLaunchExt(const std::string &identifier, const std::string &userId);
149 
150     void AutoLaunchExtTask(const std::string &identifier, const std::string &userId, AutoLaunchItem &autoLaunchItem);
151 
152     void ExtObserverFunc(const KvDBCommitNotifyData &notifyData, const std::string &identifier,
153         const std::string &userId);
154 
155     void ExtConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId);
156 
157     void ExtConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId);
158 
159     int ExtAutoLaunchRequestCallBack(const std::string &identifier, AutoLaunchParam &param, DBType &openType);
160 
161     int RegisterLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt);
162 
163     void TryCloseKvConnection(AutoLaunchItem &autoLaunchItem);
164 
165     void TryCloseRelationConnection(AutoLaunchItem &autoLaunchItem);
166 
167     void EraseAutoLauchItem(const std::string &identifier, const std::string &userId);
168 
169     void NotifyInvalidParam(const AutoLaunchItem &autoLaunchItem);
170 
171     int CheckAutoLaunchRealPath(const AutoLaunchItem &autoLaunchItem);
172 
173     int RegisterKvObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt);
174 
175     int RegisterRelationalObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt);
176 
177     mutable std::mutex dataLock_;
178     mutable std::mutex communicatorLock_;
179     std::set<std::string> onlineDevices_;
180     // key: label, value: <userId, AutoLaunchItem>
181     std::map<std::string, std::map<std::string, AutoLaunchItem>> autoLaunchItemMap_;
182     ICommunicatorAggregator *communicatorAggregator_ = nullptr;
183     std::condition_variable cv_;
184 
185     std::mutex extLock_;
186     std::map<DBType, AutoLaunchRequestCallback> autoLaunchRequestCallbackMap_;
187     // key: label, value: <userId, AutoLaunchItem>
188     std::map<std::string, std::map<std::string, AutoLaunchItem>> extItemMap_;
189 };
190 } // namespace DistributedDB
191 #endif // AUTO_LAUNCH_H
192