1 /* 2 * Copyright (c) 2023 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 OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H 17 #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H 18 #include "eventcenter/event.h" 19 #include "executor_pool.h" 20 #include "store/auto_cache.h" 21 #include "store/general_store.h" 22 #include "store/general_value.h" 23 #include "utils/ref_count.h" 24 #include "concurrent_map.h" 25 #include "cloud/cloud_info.h" 26 namespace OHOS::CloudData { 27 class SyncManager { 28 public: 29 using GenAsync = DistributedData::GenAsync; 30 using GenStore = DistributedData::GeneralStore; 31 using GenQuery = DistributedData::GenQuery; 32 using RefCount = DistributedData::RefCount; 33 using AutoCache = DistributedData::AutoCache; 34 using StoreMetaData = DistributedData::StoreMetaData; 35 static AutoCache::Store GetStore(const StoreMetaData &meta, int32_t user, bool mustBind = true); 36 class SyncInfo final { 37 public: 38 using Store = std::string; 39 using Stores = std::vector<Store>; 40 using Tables = std::vector<std::string>; 41 using MutliStoreTables = std::map<Store, Tables>; 42 SyncInfo(int32_t user, const std::string &bundleName = "", const Store &store = "", const Tables &tables = {}); 43 SyncInfo(int32_t user, const std::string &bundleName, const Stores &stores); 44 SyncInfo(int32_t user, const std::string &bundleName, const MutliStoreTables &tables); 45 void SetMode(int32_t mode); 46 void SetWait(int32_t wait); 47 void SetAsyncDetail(GenAsync asyncDetail); 48 void SetQuery(std::shared_ptr<GenQuery> query); 49 void SetError(int32_t code) const; 50 std::shared_ptr<GenQuery> GenerateQuery(const std::string &store, const Tables &tables); 51 bool Contains(const std::string& storeName); 52 inline static constexpr const char *DEFAULT_ID = "default"; 53 54 private: 55 friend SyncManager; 56 uint64_t syncId_ = 0; 57 int32_t mode_ = GenStore::MixMode(GenStore::CLOUD_TIME_FIRST, GenStore::AUTO_SYNC_MODE); 58 int32_t user_ = 0; 59 int32_t wait_ = 0; 60 std::string id_ = DEFAULT_ID; 61 std::string bundleName_; 62 std::map<std::string, std::vector<std::string>> tables_; 63 GenAsync async_; 64 std::shared_ptr<GenQuery> query_; 65 }; 66 SyncManager(); 67 ~SyncManager(); 68 int32_t Bind(std::shared_ptr<ExecutorPool> executor); 69 int32_t DoCloudSync(SyncInfo syncInfo); 70 int32_t StopCloudSync(int32_t user = 0); 71 72 private: 73 using Event = DistributedData::Event; 74 using Task = ExecutorPool::Task; 75 using TaskId = ExecutorPool::TaskId; 76 using Duration = ExecutorPool::Duration; 77 using Retryer = std::function<bool(Duration interval, int32_t status)>; 78 using CloudInfo = DistributedData::CloudInfo; 79 80 static constexpr ExecutorPool::Duration RETRY_INTERVAL = std::chrono::seconds(10); // second 81 static constexpr ExecutorPool::Duration LOCKED_INTERVAL = std::chrono::seconds(30); // second 82 static constexpr int32_t RETRY_TIMES = 6; // normal retry 83 static constexpr int32_t CLIENT_RETRY_TIMES = 3; // normal retry 84 static constexpr uint64_t USER_MARK = 0xFFFFFFFF00000000; // high 32 bit 85 static constexpr int32_t MV_BIT = 32; 86 87 Task GetSyncTask(int32_t times, bool retry, RefCount ref, SyncInfo &&syncInfo); 88 void UpdateSchema(const SyncInfo &syncInfo); 89 std::function<void(const Event &)> GetSyncHandler(Retryer retryer); 90 std::function<void(const Event &)> GetClientChangeHandler(); 91 Retryer GetRetryer(int32_t times, const SyncInfo &syncInfo); 92 static uint64_t GenerateId(int32_t user); 93 RefCount GenSyncRef(uint64_t syncId); 94 int32_t Compare(uint64_t syncId, int32_t user); 95 bool IsValid(SyncInfo &info, CloudInfo &cloud); 96 97 static std::atomic<uint32_t> genId_; 98 std::shared_ptr<ExecutorPool> executor_; 99 ConcurrentMap<uint64_t, TaskId> actives_; 100 ConcurrentMap<uint64_t, uint64_t> activeInfos_; 101 }; 102 } // namespace OHOS::CloudData 103 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H 104