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_DATAMGR_SERVICE_RDB_GENERAL_STORE_H 17 #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H 18 #include <atomic> 19 #include <functional> 20 #include <shared_mutex> 21 22 #include "metadata/store_meta_data.h" 23 #include "rdb_asset_loader.h" 24 #include "rdb_cloud.h" 25 #include "rdb_store.h" 26 #include "relational_store_delegate.h" 27 #include "relational_store_manager.h" 28 #include "store/general_store.h" 29 #include "store/general_value.h" 30 #include "snapshot/snapshot.h" 31 namespace OHOS::DistributedRdb { 32 class RdbGeneralStore : public DistributedData::GeneralStore { 33 public: 34 using Cursor = DistributedData::Cursor; 35 using GenQuery = DistributedData::GenQuery; 36 using VBucket = DistributedData::VBucket; 37 using VBuckets = DistributedData::VBuckets; 38 using Value = DistributedData::Value; 39 using Values = DistributedData::Values; 40 using StoreMetaData = DistributedData::StoreMetaData; 41 using Database = DistributedData::Database; 42 using GenErr = DistributedData::GeneralError; 43 using RdbStore = OHOS::NativeRdb::RdbStore; 44 using Reference = DistributedData::Reference; 45 using Snapshot = DistributedData::Snapshot; 46 using BindAssets = DistributedData::BindAssets; 47 48 explicit RdbGeneralStore(const StoreMetaData &meta); 49 ~RdbGeneralStore(); 50 int32_t Bind(const Database &database, BindInfo bindInfo) override; 51 bool IsBound() override; 52 bool IsValid(); 53 int32_t Execute(const std::string &table, const std::string &sql) override; 54 int32_t SetDistributedTables(const std::vector<std::string> &tables, int32_t type, 55 const std::vector<Reference> &references) override; 56 int32_t SetTrackerTable(const std::string& tableName, const std::set<std::string>& trackerColNames, 57 const std::string& extendColName) override; 58 int32_t Insert(const std::string &table, VBuckets &&values) override; 59 int32_t Update(const std::string &table, const std::string &setSql, Values &&values, const std::string &whereSql, 60 Values &&conditions) override; 61 int32_t Replace(const std::string &table, VBucket &&value) override; 62 int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; 63 std::shared_ptr<Cursor> Query(const std::string &table, const std::string &sql, Values &&args) override; 64 std::shared_ptr<Cursor> Query(const std::string &table, GenQuery &query) override; 65 int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) override; 66 std::shared_ptr<Cursor> PreSharing(GenQuery &query) override; 67 int32_t Clean(const std::vector<std::string> &devices, int32_t mode, const std::string &tableName) override; 68 int32_t Watch(int32_t origin, Watcher &watcher) override; 69 int32_t Unwatch(int32_t origin, Watcher &watcher) override; 70 int32_t RegisterDetailProgressObserver(DetailAsync async) override; 71 int32_t UnregisterDetailProgressObserver() override; 72 int32_t Close() override; 73 int32_t AddRef() override; 74 int32_t Release() override; 75 int32_t BindSnapshots(std::shared_ptr<std::map<std::string, std::shared_ptr<Snapshot>>> bindAssets) override; 76 int32_t MergeMigratedData(const std::string &tableName, VBuckets&& values) override; 77 78 private: 79 RdbGeneralStore(const RdbGeneralStore& rdbGeneralStore); 80 RdbGeneralStore& operator=(const RdbGeneralStore& rdbGeneralStore); 81 using RdbDelegate = DistributedDB::RelationalStoreDelegate; 82 using RdbManager = DistributedDB::RelationalStoreManager; 83 using SyncProcess = DistributedDB::SyncProcess; 84 using DBBriefCB = DistributedDB::SyncStatusCallback; 85 using DBProcessCB = std::function<void(const std::map<std::string, SyncProcess> &processes)>; 86 static GenErr ConvertStatus(DistributedDB::DBStatus status); 87 static constexpr inline uint64_t REMOTE_QUERY_TIME_OUT = 30 * 1000; 88 static constexpr const char* CLOUD_GID = "cloud_gid"; 89 static constexpr const char* DATE_KEY = "data_key"; 90 static constexpr uint32_t ITER_V0 = 10000; 91 static constexpr uint32_t ITER_V1 = 5000; 92 static constexpr uint32_t ITERS[] = {ITER_V0, ITER_V1}; 93 static constexpr uint32_t ITERS_COUNT = sizeof(ITERS) / sizeof(ITERS[0]); 94 class ObserverProxy : public DistributedDB::StoreObserver { 95 public: 96 using DBChangedIF = DistributedDB::StoreChangedData; 97 using DBChangedData = DistributedDB::ChangedData; 98 using DBOrigin = DistributedDB::Origin; 99 using GenOrigin = Watcher::Origin; 100 void OnChange(const DistributedDB::StoreChangedData &data) override; 101 void OnChange(DBOrigin origin, const std::string &originalId, DBChangedData &&data) override; HasWatcher()102 bool HasWatcher() const 103 { 104 return watcher_ != nullptr; 105 } 106 private: 107 friend RdbGeneralStore; 108 Watcher *watcher_ = nullptr; 109 std::string storeId_; 110 }; 111 DBBriefCB GetDBBriefCB(DetailAsync async); 112 DBProcessCB GetDBProcessCB(DetailAsync async, uint32_t highMode = AUTO_SYNC_MODE); 113 std::shared_ptr<Cursor> RemoteQuery(const std::string &device, 114 const DistributedDB::RemoteCondition &remoteCondition); 115 std::string BuildSql(const std::string& table, const std::string& statement, 116 const std::vector<std::string>& columns) const; 117 VBuckets QuerySql(const std::string& sql, Values &&args); 118 VBuckets ExtractExtend(VBuckets& values) const; 119 size_t SqlConcatenate(VBucket &value, std::string &strColumnSql, std::string &strRowValueSql); 120 121 ObserverProxy observer_; 122 RdbManager manager_; 123 RdbDelegate *delegate_ = nullptr; 124 DetailAsync async_ = nullptr; 125 std::shared_ptr<RdbCloud> rdbCloud_ {}; 126 std::shared_ptr<RdbAssetLoader> rdbLoader_ {}; 127 BindInfo bindInfo_; 128 std::atomic<bool> isBound_ = false; 129 std::mutex mutex_; 130 int32_t ref_ = 1; 131 mutable std::shared_mutex rwMutex_; 132 133 BindAssets snapshots_; 134 DistributedData::StoreInfo storeInfo_; 135 }; 136 } // namespace OHOS::DistributedRdb 137 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H 138