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 #ifdef USE_FFRT 25 #include "ffrt.h" 26 #endif 27 #include "ikvdb_connection.h" 28 #include "icommunicator_aggregator.h" 29 #include "kv_store_observer.h" 30 #include "kvdb_properties.h" 31 #include "types_export.h" 32 #include "relational_store_connection.h" 33 #include "relationaldb_properties.h" 34 #include "semaphore_utils.h" 35 #include "store_observer.h" 36 37 namespace DistributedDB { 38 enum class AutoLaunchItemState { 39 UN_INITIAL = 0, 40 IN_ENABLE, 41 IN_LIFE_CYCLE_CALL_BACK, // in LifeCycleCallback 42 IN_COMMUNICATOR_CALL_BACK, // in OnConnectCallback or CommunicatorLackCallback 43 IDLE, 44 }; 45 46 enum class DBTypeInner { 47 DB_KV = 1, 48 DB_RELATION, 49 DB_INVALID, 50 }; 51 52 struct AutoLaunchItem { 53 std::shared_ptr<DBProperties> propertiesPtr; 54 AutoLaunchNotifier notifier; 55 KvStoreObserver *observer = nullptr; 56 int conflictType = 0; 57 KvStoreNbConflictNotifier conflictNotifier; 58 void *conn = nullptr; 59 KvDBObserverHandle *observerHandle = nullptr; 60 bool isWriteOpenNotified = false; 61 AutoLaunchItemState state = AutoLaunchItemState::UN_INITIAL; 62 bool isDisable = false; 63 bool inObserver = false; 64 bool isAutoSync = true; 65 DBTypeInner type = DBTypeInner::DB_INVALID; 66 StoreObserver *storeObserver = nullptr; 67 }; 68 69 class AutoLaunch { 70 public: 71 static int GetAutoLaunchProperties(const AutoLaunchParam ¶m, const DBTypeInner &openType, bool checkDir, 72 std::shared_ptr<DBProperties> &propertiesPtr); 73 74 AutoLaunch() = default; 75 76 ~AutoLaunch(); 77 78 DISABLE_COPY_ASSIGN_MOVE(AutoLaunch); 79 80 void SetCommunicatorAggregator(ICommunicatorAggregator *aggregator); 81 82 int EnableKvStoreAutoLaunch(const KvDBProperties &properties, AutoLaunchNotifier notifier, 83 const AutoLaunchOption &option); 84 85 int DisableKvStoreAutoLaunch(const std::string &normalIdentifier, const std::string &dualTupleIdentifier, 86 const std::string &userId); 87 88 void GetAutoLaunchSyncDevices(const std::string &identifier, std::vector<std::string> &devices) const; 89 90 void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback, DBTypeInner type); 91 92 void Dump(int fd); 93 94 void CloseConnection(DBTypeInner type, const DBProperties &properties); 95 96 protected: 97 static int OpenOneConnection(AutoLaunchItem &autoLaunchItem); 98 99 // we will return errCode, if errCode != E_OK 100 static int CloseConnectionStrict(AutoLaunchItem &autoLaunchItem); 101 102 static void CloseNotifier(const AutoLaunchItem &autoLaunchItem); 103 104 static int SetConflictNotifier(AutoLaunchItem &autoLaunchItem); 105 106 static int GetAutoLaunchKVProperties(const AutoLaunchParam ¶m, 107 const std::shared_ptr<KvDBProperties> &propertiesPtr, bool checkDir); 108 109 static int GetAutoLaunchRelationProperties(const AutoLaunchParam ¶m, 110 const std::shared_ptr<RelationalDBProperties> &propertiesPtr); 111 112 static int OpenKvConnection(AutoLaunchItem &autoLaunchItem); 113 114 static int OpenRelationalConnection(AutoLaunchItem &autoLaunchItem); 115 116 static int PragmaAutoSync(AutoLaunchItem &autoLaunchItem); 117 118 int EnableKvStoreAutoLaunchParmCheck(AutoLaunchItem &autoLaunchItem, const std::string &normalIdentifier, 119 const std::string &dualTupleIdentifier, bool isDualTupleMode); 120 121 void GetKVConnectionInEnableInner(const AutoLaunchItem &autoLaunchItem, const std::string &identifier, 122 const std::string &userId); 123 124 int GetKVConnectionInEnable(AutoLaunchItem &autoLaunchItem, const std::string &identifier); 125 126 // before ReleaseDatabaseConnection, if errCode != E_OK, we not return, we try close more 127 virtual void TryCloseConnection(AutoLaunchItem &autoLaunchItem); 128 129 int RegisterObserverAndLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier, 130 bool isExt); 131 132 int RegisterObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 133 134 void ObserverFunc(const KvDBCommitNotifyData ¬ifyData, const std::string &identifier, 135 const std::string &userId); 136 137 void ConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId); 138 139 void OnlineCallBackTask(); 140 141 void GetDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap); 142 143 void GetConnInDoOpenMapInner(std::pair<const std::string, std::map<std::string, AutoLaunchItem>> &items, 144 SemaphoreUtils &sema); 145 146 void GetConnInDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap); 147 148 void UpdateGlobalMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap); 149 150 void ReceiveUnknownIdentifierCallBackTask(const std::string &identifier, const std::string &userId); 151 152 void ConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId); 153 154 void OnlineCallBack(const std::string &device, bool isConnect); 155 156 int ReceiveUnknownIdentifierCallBack(const LabelType &label, const std::string &originalUserId); 157 158 int AutoLaunchExt(const std::string &identifier, const std::string &userId); 159 160 void AutoLaunchExtTask(const std::string &identifier, const std::string &userId, AutoLaunchItem &autoLaunchItem); 161 162 void ExtObserverFunc(const KvDBCommitNotifyData ¬ifyData, const std::string &identifier, 163 const std::string &userId); 164 165 void ExtConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId); 166 167 void ExtConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId); 168 169 int ExtAutoLaunchRequestCallBack(const std::string &identifier, AutoLaunchParam ¶m, DBTypeInner &openType); 170 171 int RegisterLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 172 173 void TryCloseKvConnection(AutoLaunchItem &autoLaunchItem); 174 175 void TryCloseRelationConnection(AutoLaunchItem &autoLaunchItem); 176 177 void EraseAutoLaunchItem(const std::string &identifier, const std::string &userId); 178 179 void NotifyInvalidParam(const AutoLaunchItem &autoLaunchItem); 180 181 int CheckAutoLaunchRealPath(const AutoLaunchItem &autoLaunchItem); 182 183 int RegisterKvObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 184 185 int RegisterRelationalObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 186 187 std::string GetAutoLaunchItemUid(const std::string &identifier, const std::string &originalUserId, 188 bool &handleByCallback); 189 190 bool ChkAutoLaunchAbort(const std::string &identifier, AutoLaunchItem &autoLaunchItem); 191 192 void NotifyAutoLaunch(const std::string &userId, AutoLaunchItem &autoLaunchItem, AutoLaunchNotifier ¬ifier); 193 194 void AutoLaunchOnChange(const std::string &changedDevice, std::string &userId, std::string &appId, 195 std::string &storeId, AutoLaunchItem autoLaunchItem); 196 197 mutable std::mutex dataLock_; 198 mutable std::mutex communicatorLock_; 199 std::set<std::string> onlineDevices_; 200 // key: label, value: <userId, AutoLaunchItem> 201 std::map<std::string, std::map<std::string, AutoLaunchItem>> autoLaunchItemMap_; 202 ICommunicatorAggregator *communicatorAggregator_ = nullptr; 203 std::condition_variable cv_; 204 205 #ifdef USE_FFRT 206 ffrt::mutex extLock_; 207 #else 208 std::mutex extLock_; 209 #endif 210 std::map<DBTypeInner, AutoLaunchRequestCallback> autoLaunchRequestCallbackMap_; 211 // key: label, value: <userId, AutoLaunchItem> 212 std::map<std::string, std::map<std::string, AutoLaunchItem>> extItemMap_; 213 }; 214 } // namespace DistributedDB 215 #endif // AUTO_LAUNCH_H 216