• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 DISTRIBUTED_RDB_SYNCER_H
17 #define DISTRIBUTED_RDB_SYNCER_H
18 
19 #include <mutex>
20 #include <string>
21 
22 #include "metadata/store_meta_data.h"
23 #include "rdb_notifier.h"
24 #include "rdb_store_observer_impl.h"
25 #include "rdb_types.h"
26 #include "relational_store_delegate.h"
27 #include "relational_store_manager.h"
28 #include "metadata/secret_key_meta_data.h"
29 namespace OHOS::DistributedRdb {
30 class RdbSyncer {
31 public:
32     using StoreMetaData = OHOS::DistributedData::StoreMetaData;
33     using SecretKeyMetaData = DistributedData::SecretKeyMetaData;
34     RdbSyncer(const RdbSyncerParam& param, RdbStoreObserverImpl* observer);
35     ~RdbSyncer() noexcept;
36 
37     int32_t Init(pid_t pid, pid_t uid, uint32_t token, const std::string &writePermission = "",
38         const std::string &readPermission = "");
39 
40     pid_t GetPid() const;
41 
42     void SetTimerId(uint32_t timerId);
43 
44     uint32_t GetTimerId() const;
45 
46     std::string GetStoreId() const;
47 
48     std::string GetIdentifier() const;
49 
50     int32_t SetDistributedTables(const std::vector<std::string>& tables);
51 
52     int32_t DoSync(const SyncOption& option, const RdbPredicates& predicates, SyncResult& result);
53 
54     int32_t DoAsync(const SyncOption& option, const RdbPredicates& predicates, const SyncCallback& callback);
55 
56     int32_t RemoteQuery(const std::string& device, const std::string& sql,
57                         const std::vector<std::string>& selectionArgs, sptr<IRemoteObject>& resultSet);
58 
59     int32_t DestroyMetaData(StoreMetaData &meta);
60     static std::string RemoveSuffix(const std::string& name);
61 
62     static int32_t GetInstIndex(uint32_t tokenId, const std::string &bundleName);
63 
64     static bool GetPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password);
65 
66 private:
67     std::string GetUserId() const;
68 
69     std::string GetBundleName() const;
70 
71     std::string GetAppId() const;
72 
73     int32_t CreateMetaData(StoreMetaData &meta);
74     void FillMetaData(StoreMetaData &meta);
75     int32_t InitDBDelegate(const StoreMetaData &meta);
76     bool SetSecretKey(const StoreMetaData &meta);
77 
78     DistributedDB::RelationalStoreDelegate* GetDelegate();
79 
80     std::mutex mutex_;
81     DistributedDB::RelationalStoreManager* manager_ {};
82     DistributedDB::RelationalStoreDelegate* delegate_ {};
83     RdbSyncerParam param_;
84     RdbStoreObserverImpl *observer_ {};
85     pid_t pid_ {};
86     pid_t uid_ {};
87     uint32_t token_ {};
88     uint32_t timerId_ {};
89 
90     static std::vector<std::string> GetConnectDevices();
91     static std::vector<std::string> NetworkIdToUUID(const std::vector<std::string>& networkIds);
92 
93     static void HandleSyncStatus(const std::map<std::string, std::vector<DistributedDB::TableStatus>>& SyncStatus,
94                                  SyncResult& result);
95     static DistributedDB::Query MakeQuery(const RdbPredicates& predicates);
96     static void EqualTo(const RdbPredicateOperation& operation, DistributedDB::Query& query);
97     static void NotEqualTo(const RdbPredicateOperation& operation, DistributedDB::Query& query);
98     static void And(const RdbPredicateOperation& operation, DistributedDB::Query& query);
99     static void Or(const RdbPredicateOperation& operation, DistributedDB::Query& query);
100     static void OrderBy(const RdbPredicateOperation& operation, DistributedDB::Query& query);
101     static void Limit(const RdbPredicateOperation& operation, DistributedDB::Query& query);
102 
103     using PredicateHandle = void(*)(const RdbPredicateOperation& operation, DistributedDB::Query& query);
104     static inline PredicateHandle HANDLES[OPERATOR_MAX] = {
105         [EQUAL_TO] = &RdbSyncer::EqualTo,
106         [NOT_EQUAL_TO] = &RdbSyncer::NotEqualTo,
107         [AND] = &RdbSyncer::And,
108         [OR] = &RdbSyncer::Or,
109         [ORDER_BY] = &RdbSyncer::OrderBy,
110         [LIMIT] = &RdbSyncer::Limit,
111     };
112 
113     static constexpr int DECIMAL_BASE = 10;
114     static constexpr uint64_t REMOTE_QUERY_TIME_OUT = 30 * 1000;
115 };
116 } // namespace OHOS::DistributedRdb
117 #endif
118