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 RELATIONAL_STORE_IMPL_H 17 #define RELATIONAL_STORE_IMPL_H 18 19 #include <list> 20 #include <memory> 21 22 #include "oh_predicates.h" 23 #include "rdb_store.h" 24 #include "relational_store.h" 25 26 namespace OHOS { 27 namespace RdbNdk { 28 class NDKDetailProgressObserver : public DistributedRdb::DetailProgressObserver { 29 public: 30 explicit NDKDetailProgressObserver(const Rdb_ProgressObserver *callback); 31 void ProgressNotification(const DistributedRdb::Details &details); 32 bool operator==(const Rdb_ProgressObserver *callback); 33 34 private: 35 const Rdb_ProgressObserver *callback_; 36 }; 37 38 class NDKStoreObserver : public OHOS::DistributedRdb::RdbStoreObserver { 39 public: 40 using Origin = DistributedRdb::Origin; 41 using RdbStoreObserver = OHOS::DistributedRdb::RdbStoreObserver; 42 43 NDKStoreObserver(const Rdb_DataObserver *observer, int mode); 44 ~NDKStoreObserver() noexcept override = default; 45 46 void OnChange(const std::vector<std::string> &devices) override; 47 48 void OnChange(const OHOS::DistributedRdb::Origin &origin, const PrimaryFields &fields, 49 ChangeInfo &&changeInfo) override; 50 51 void OnChange() override; 52 bool operator==(const Rdb_DataObserver *other); 53 54 private: 55 void ConvertKeyInfo(Rdb_KeyInfo &keyInfo, std::vector<RdbStoreObserver::PrimaryKey> &primaryKey); 56 int mode_ = Rdb_SubscribeType::RDB_SUBSCRIBE_TYPE_CLOUD; 57 const Rdb_DataObserver *observer_; 58 }; 59 60 class RelationalStore : public OH_Rdb_Store { 61 public: 62 explicit RelationalStore(std::shared_ptr<OHOS::NativeRdb::RdbStore> store); 63 ~RelationalStore(); GetStore()64 std::shared_ptr<OHOS::NativeRdb::RdbStore> GetStore() 65 { 66 return store_; 67 } 68 int SubscribeAutoSyncProgress(const Rdb_ProgressObserver *callback); 69 int UnsubscribeAutoSyncProgress(const Rdb_ProgressObserver *callback); 70 int DoSubScribe(Rdb_SubscribeType type, const Rdb_DataObserver *observer); 71 int DoUnsubScribe(Rdb_SubscribeType type, const Rdb_DataObserver *observer); 72 73 private: 74 std::shared_ptr<OHOS::NativeRdb::RdbStore> store_; 75 std::mutex mutex_; 76 std::list<std::shared_ptr<NDKDetailProgressObserver>> callbacks_; 77 std::map<Rdb_SubscribeType, std::vector<std::shared_ptr<NDKStoreObserver>>> dataObservers_; 78 }; 79 80 class NDKUtils { 81 public: 82 static OHOS::DistributedRdb::SyncMode TransformMode(Rdb_SyncMode &mode); 83 static OHOS::DistributedRdb::SubscribeMode GetSubscribeType(Rdb_SubscribeType &type); 84 }; 85 } // namespace RdbNdk 86 } // namespace OHOS 87 #endif // RELATIONAL_STORE_IMPL_H 88