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 virtual ~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 int GetKVConnectionInEnable(AutoLaunchItem &autoLaunchItem, const std::string &identifier); 122 123 // before ReleaseDatabaseConnection, if errCode != E_OK, we not return, we try close more 124 virtual void TryCloseConnection(AutoLaunchItem &autoLaunchItem); 125 126 int RegisterObserverAndLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier, 127 bool isExt); 128 129 int RegisterObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 130 131 void ObserverFunc(const KvDBCommitNotifyData ¬ifyData, const std::string &identifier, 132 const std::string &userId); 133 134 void ConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId); 135 136 void OnlineCallBackTask(); 137 138 void GetDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap); 139 140 void GetConnInDoOpenMapInner(std::pair<const std::string, std::map<std::string, AutoLaunchItem>> &items, 141 SemaphoreUtils &sema); 142 143 void GetConnInDoOpenMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap); 144 145 void UpdateGlobalMap(std::map<std::string, std::map<std::string, AutoLaunchItem>> &doOpenMap); 146 147 void ReceiveUnknownIdentifierCallBackTask(const std::string &identifier, const std::string &userId); 148 149 void ConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId); 150 151 void OnlineCallBack(const std::string &device, bool isConnect); 152 153 int ReceiveUnknownIdentifierCallBack(const LabelType &label, const std::string &originalUserId); 154 155 int AutoLaunchExt(const std::string &identifier, const std::string &userId); 156 157 void AutoLaunchExtTask(const std::string &identifier, const std::string &userId, AutoLaunchItem &autoLaunchItem); 158 159 void ExtObserverFunc(const KvDBCommitNotifyData ¬ifyData, const std::string &identifier, 160 const std::string &userId); 161 162 void ExtConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId); 163 164 void ExtConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId); 165 166 int ExtAutoLaunchRequestCallBack(const std::string &identifier, AutoLaunchParam ¶m, DBTypeInner &openType); 167 168 int RegisterLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 169 170 void TryCloseKvConnection(AutoLaunchItem &autoLaunchItem); 171 172 void TryCloseRelationConnection(AutoLaunchItem &autoLaunchItem); 173 174 void EraseAutoLaunchItem(const std::string &identifier, const std::string &userId); 175 176 void NotifyInvalidParam(const AutoLaunchItem &autoLaunchItem); 177 178 int CheckAutoLaunchRealPath(const AutoLaunchItem &autoLaunchItem); 179 180 int RegisterKvObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 181 182 int RegisterRelationalObserver(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); 183 184 std::string GetAutoLaunchItemUid(const std::string &identifier, const std::string &originalUserId, 185 bool &handleByCallback); 186 187 bool ChkAutoLaunchAbort(const std::string &identifier, AutoLaunchItem &autoLaunchItem); 188 189 void NotifyAutoLaunch(const std::string &userId, AutoLaunchItem &autoLaunchItem, AutoLaunchNotifier ¬ifier); 190 191 void AutoLaunchOnChange(const std::string &changedDevice, std::string &userId, std::string &appId, 192 std::string &storeId, AutoLaunchItem autoLaunchItem); 193 194 mutable std::mutex dataLock_; 195 mutable std::mutex communicatorLock_; 196 std::set<std::string> onlineDevices_; 197 // key: label, value: <userId, AutoLaunchItem> 198 std::map<std::string, std::map<std::string, AutoLaunchItem>> autoLaunchItemMap_; 199 ICommunicatorAggregator *communicatorAggregator_ = nullptr; 200 std::condition_variable cv_; 201 202 #ifdef USE_FFRT 203 ffrt::mutex extLock_; 204 #else 205 std::mutex extLock_; 206 #endif 207 std::map<DBTypeInner, AutoLaunchRequestCallback> autoLaunchRequestCallbackMap_; 208 // key: label, value: <userId, AutoLaunchItem> 209 std::map<std::string, std::map<std::string, AutoLaunchItem>> extItemMap_; 210 }; 211 } // namespace DistributedDB 212 #endif // AUTO_LAUNCH_H 213