• 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 DISTRIBUTEDDATASERVICE_RDB_SERVICE_H
17 #define DISTRIBUTEDDATASERVICE_RDB_SERVICE_H
18 
19 #include "rdb_service_stub.h"
20 
21 #include <map>
22 #include <mutex>
23 #include <string>
24 #include "metadata/store_meta_data.h"
25 #include "metadata/secret_key_meta_data.h"
26 #include "rdb_syncer.h"
27 #include "concurrent_map.h"
28 #include "store_observer.h"
29 #include "timer.h"
30 #include "visibility.h"
31 
32 namespace OHOS::DistributedRdb {
33 class API_EXPORT RdbServiceImpl : public RdbServiceStub {
34 public:
35     using StoreMetaData = OHOS::DistributedData::StoreMetaData;
36     using SecretKeyMetaData = DistributedData::SecretKeyMetaData;
37     RdbServiceImpl();
38 
39     void OnClientDied(pid_t pid);
40 
41     /* IPC interface */
42     std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override;
43 
44     int32_t InitNotifier(const RdbSyncerParam& param, const sptr<IRemoteObject> notifier) override;
45 
46     int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector<std::string>& tables) override;
47 
48     int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql,
49                         const std::vector<std::string>& selectionArgs, sptr<IRemoteObject>& resultSet) override;
50 
51     void OnDataChange(pid_t pid, const DistributedDB::StoreChangedData& data);
52 
53     int32_t CreateRDBTable(
54         const RdbSyncerParam &param, const std::string &writePermission, const std::string &readPermission) override;
55     int32_t DestroyRDBTable(const RdbSyncerParam &param) override;
56 
57     int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param) override;
58 
59 protected:
60     int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option,
61                    const RdbPredicates& predicates, SyncResult& result) override;
62 
63     int32_t DoAsync(const RdbSyncerParam& param, uint32_t seqNum, const SyncOption& option,
64                     const RdbPredicates& predicates) override;
65 
66     int32_t DoSubscribe(const RdbSyncerParam& param) override;
67 
68     int32_t DoUnSubscribe(const RdbSyncerParam& param) override;
69 
70 private:
71     std::string GenIdentifier(const RdbSyncerParam& param);
72 
73     bool CheckAccess(const RdbSyncerParam& param);
74 
75     void SyncerTimeout(std::shared_ptr<RdbSyncer> syncer);
76 
77     std::shared_ptr<RdbSyncer> GetRdbSyncer(const RdbSyncerParam& param);
78 
79     void OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult& result);
80 
81     class DeathRecipientImpl : public IRemoteObject::DeathRecipient {
82     public:
83         using DeathCallback = std::function<void()>;
84         explicit DeathRecipientImpl(const DeathCallback& callback);
85         ~DeathRecipientImpl() override;
86         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
87     private:
88         const DeathCallback callback_;
89     };
90     class Factory {
91     public:
92         Factory();
93         ~Factory();
94     };
95 
96     using StoreSyncersType = std::map<std::string, std::shared_ptr<RdbSyncer>>;
97     int32_t syncerNum_ {};
98     ConcurrentMap<pid_t, StoreSyncersType> syncers_;
99     ConcurrentMap<pid_t, sptr<RdbNotifierProxy>> notifiers_;
100     ConcurrentMap<std::string, pid_t> identifiers_;
101     Utils::Timer timer_;
102     RdbStoreObserverImpl autoLaunchObserver_;
103 
104     static Factory factory_;
105 
106     static std::string TransferStringToHex(const std::string& origStr);
107 
108     static constexpr int32_t MAX_SYNCER_NUM = 50;
109     static constexpr int32_t MAX_SYNCER_PER_PROCESS = 10;
110     static constexpr int32_t SYNCER_TIMEOUT = 60 * 1000; // ms
111 };
112 } // namespace OHOS::DistributedRdb
113 #endif
114