• 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_RDB_SERVICE_PROXY_H
17 #define DISTRIBUTED_RDB_RDB_SERVICE_PROXY_H
18 
19 #include <atomic>
20 #include <list>
21 #include <iremote_proxy.h>
22 #include "irdb_service.h"
23 #include "rdb_notifier_stub.h"
24 #include "concurrent_map.h"
25 
26 namespace OHOS::DistributedRdb {
27 class RdbServiceProxy : public IRemoteProxy<IRdbService> {
28 public:
29     struct ObserverParam {
30         RdbStoreObserver *observer = nullptr;
31         std::string bundleName;
32         SubscribeOption subscribeOption{ SubscribeMode::REMOTE };
33     };
34     using Observers = ConcurrentMap<std::string, std::list<ObserverParam>>;
35 
36     explicit RdbServiceProxy(const sptr<IRemoteObject>& object);
37 
38     std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override;
39 
40     int32_t InitNotifier(const RdbSyncerParam &param);
41 
42     int32_t InitNotifier(const RdbSyncerParam &param, sptr<IRemoteObject> notifier) override;
43 
44     int32_t SetDistributedTables(const RdbSyncerParam &param, const std::vector<std::string> &tables,
45         int32_t type = DISTRIBUTED_DEVICE) override;
46 
47     int32_t Sync(const RdbSyncerParam& param, const Option& option,
48                  const PredicatesMemo& predicates, const AsyncDetail &async) override;
49 
50     int32_t Subscribe(const RdbSyncerParam& param, const SubscribeOption& option,
51                       RdbStoreObserver *observer) override;
52 
53     int32_t UnSubscribe(const RdbSyncerParam& param, const SubscribeOption& option,
54                         RdbStoreObserver *observer) override;
55     int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql,
56                         const std::vector<std::string>& selectionArgs, sptr<IRemoteObject>& resultSet) override;
57 
58     Observers ExportObservers();
59 
60     void ImportObservers(Observers &observers);
61 
62     int32_t GetSchema(const RdbSyncerParam &param) override;
63 
64     int32_t Delete(const RdbSyncerParam &param) override;
65 private:
66     using ChangeInfo = RdbStoreObserver::ChangeInfo;
67     using PrimaryFields = RdbStoreObserver::PrimaryFields;
68     std::pair<int32_t, Details> DoSync(const RdbSyncerParam &param, const Option &option,
69         const PredicatesMemo &predicates);
70 
71     int32_t DoAsync(const RdbSyncerParam &param, const Option &option, const PredicatesMemo &predicates);
72 
73     int32_t DoSync(const RdbSyncerParam &param, const Option &option, const PredicatesMemo &predicates,
74         const AsyncDetail &async);
75 
76     int32_t DoAsync(const RdbSyncerParam &param, const Option &option, const PredicatesMemo &predicates,
77         const AsyncDetail &async);
78 
79     int32_t DoSubscribe(const RdbSyncerParam& param, const SubscribeOption &option);
80 
81     int32_t DoUnSubscribe(const RdbSyncerParam& param);
82 
83     uint32_t GetSeqNum();
84 
85     void OnSyncComplete(uint32_t seqNum, Details &&result);
86 
87     void OnDataChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo);
88 
89     std::string RemoveSuffix(const std::string& name);
90 
91     std::atomic<uint32_t> seqNum_ {};
92 
93     ConcurrentMap<uint32_t, AsyncDetail> syncCallbacks_;
94     Observers observers_;
95     sptr<RdbNotifierStub> notifier_;
96 
97     sptr<IRemoteObject> remote_;
98     static inline BrokerDelegator<RdbServiceProxy> delegator_;
99 };
100 } // namespace OHOS::DistributedRdb
101 #endif