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