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 namespace OHOS::DistributedRdb { 31 class RdbGeneralStore : public DistributedData::GeneralStore { 32 public: 33 using Cursor = DistributedData::Cursor; 34 using GenQuery = DistributedData::GenQuery; 35 using VBucket = DistributedData::VBucket; 36 using VBuckets = DistributedData::VBuckets; 37 using Value = DistributedData::Value; 38 using Values = DistributedData::Values; 39 using StoreMetaData = DistributedData::StoreMetaData; 40 using Database = DistributedData::Database; 41 using GenErr = DistributedData::GeneralError; 42 using RdbStore = OHOS::NativeRdb::RdbStore; 43 44 explicit RdbGeneralStore(const StoreMetaData &meta); 45 ~RdbGeneralStore(); 46 int32_t Bind(const Database &database, BindInfo bindInfo) override; 47 bool IsBound() override; 48 int32_t Execute(const std::string &table, const std::string &sql) override; 49 int32_t BatchInsert(const std::string &table, VBuckets &&values) override; 50 int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) override; 51 int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; 52 std::shared_ptr<Cursor> Query(const std::string &table, const std::string &sql, Values &&args) override; 53 std::shared_ptr<Cursor> Query(const std::string &table, GenQuery &query) override; 54 int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) override; 55 int32_t Clean(const std::vector<std::string> &devices, int32_t mode, const std::string &tableName) override; 56 int32_t Watch(int32_t origin, Watcher &watcher) override; 57 int32_t Unwatch(int32_t origin, Watcher &watcher) override; 58 int32_t Close() override; 59 int32_t AddRef() override; 60 int32_t Release() override; 61 int32_t SetDistributedTables(const std::vector<std::string> &tables, int32_t type) override; 62 63 private: 64 using RdbDelegate = DistributedDB::RelationalStoreDelegate; 65 using RdbManager = DistributedDB::RelationalStoreManager; 66 using SyncProcess = DistributedDB::SyncProcess; 67 using DBBriefCB = DistributedDB::SyncStatusCallback; 68 using DBProcessCB = std::function<void(const std::map<std::string, SyncProcess> &processes)>; 69 static GenErr ConvertStatus(DistributedDB::DBStatus status); 70 static constexpr inline uint32_t ITERATE_TIMES = 10000; 71 static constexpr inline uint64_t REMOTE_QUERY_TIME_OUT = 30 * 1000; 72 class ObserverProxy : public DistributedDB::StoreObserver { 73 public: 74 using DBChangedIF = DistributedDB::StoreChangedData; 75 using DBChangedData = DistributedDB::ChangedData; 76 using DBOrigin = DistributedDB::Origin; 77 using GenOrigin = Watcher::Origin; 78 void OnChange(const DistributedDB::StoreChangedData &data) override; 79 void OnChange(DBOrigin origin, const std::string &originalId, DBChangedData &&data) override; HasWatcher()80 bool HasWatcher() const 81 { 82 return watcher_ != nullptr; 83 } 84 private: 85 friend RdbGeneralStore; 86 Watcher *watcher_ = nullptr; 87 std::string storeId_; 88 }; 89 DBBriefCB GetDBBriefCB(DetailAsync async); 90 DBProcessCB GetDBProcessCB(DetailAsync async); 91 std::shared_ptr<Cursor> RemoteQuery(const std::string &device, 92 const DistributedDB::RemoteCondition &remoteCondition); 93 94 ObserverProxy observer_; 95 RdbManager manager_; 96 RdbDelegate *delegate_ = nullptr; 97 std::shared_ptr<RdbCloud> rdbCloud_ {}; 98 std::shared_ptr<RdbAssetLoader> rdbLoader_ {}; 99 BindInfo bindInfo_; 100 std::atomic<bool> isBound_ = false; 101 std::mutex mutex_; 102 int32_t ref_ = 1; 103 mutable std::shared_mutex rwMutex_; 104 }; 105 } // namespace OHOS::DistributedRdb 106 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H 107