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 ConvertKeyInfoData(Rdb_KeyInfo::Rdb_KeyData *keyInfoData,
56         std::vector<RdbStoreObserver::PrimaryKey> &primaryKey);
57     size_t GetKeyInfoSize(RdbStoreObserver::ChangeInfo &&changeInfo);
58     int32_t GetKeyDataType(std::vector<RdbStoreObserver::PrimaryKey> &primaryKey);
59     int mode_ = Rdb_SubscribeType::RDB_SUBSCRIBE_TYPE_CLOUD;
60     const Rdb_DataObserver *observer_;
61 };
62 
63 class RelationalStore : public OH_Rdb_Store {
64 public:
65     explicit RelationalStore(std::shared_ptr<OHOS::NativeRdb::RdbStore> store);
66     ~RelationalStore();
GetStore()67     std::shared_ptr<OHOS::NativeRdb::RdbStore> GetStore()
68     {
69         return store_;
70     }
71     int SubscribeAutoSyncProgress(const Rdb_ProgressObserver *callback);
72     int UnsubscribeAutoSyncProgress(const Rdb_ProgressObserver *callback);
73     int DoSubScribe(Rdb_SubscribeType type, const Rdb_DataObserver *observer);
74     int DoUnsubScribe(Rdb_SubscribeType type, const Rdb_DataObserver *observer);
75 
76 private:
77     std::shared_ptr<OHOS::NativeRdb::RdbStore> store_;
78     std::mutex mutex_;
79     std::list<std::shared_ptr<NDKDetailProgressObserver>> callbacks_;
80     std::map<Rdb_SubscribeType, std::vector<std::shared_ptr<NDKStoreObserver>>> dataObservers_;
81 };
82 
83 class NDKUtils {
84 public:
85     static OHOS::DistributedRdb::SyncMode TransformMode(Rdb_SyncMode &mode);
86     static OHOS::DistributedRdb::SubscribeMode GetSubscribeType(Rdb_SubscribeType &type);
87 };
88 } // namespace RdbNdk
89 } // namespace OHOS
90 #endif // RELATIONAL_STORE_IMPL_H
91