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 #include "observer_bridge.h"
16 #include "kvdb_service_client.h"
17 #include "kvstore_observer_client.h"
18 namespace OHOS::DistributedKv {
ObserverBridge(AppId appId,StoreId store,std::shared_ptr<Observer> observer,const Convertor & cvt)19 ObserverBridge::ObserverBridge(AppId appId, StoreId store, std::shared_ptr<Observer> observer, const Convertor &cvt)
20 : appId_(std::move(appId)), storeId_(std::move(store)), observer_(std::move(observer)), convert_(cvt)
21 {
22 }
23
~ObserverBridge()24 ObserverBridge::~ObserverBridge()
25 {
26 if (remote_ == nullptr) {
27 return;
28 }
29 auto service = KVDBServiceClient::GetInstance();
30 if (service == nullptr) {
31 return;
32 }
33 service->Unsubscribe(appId_, storeId_, remote_);
34 }
35
RegisterRemoteObserver()36 Status ObserverBridge::RegisterRemoteObserver()
37 {
38 if (remote_ != nullptr) {
39 return SUCCESS;
40 }
41
42 auto service = KVDBServiceClient::GetInstance();
43 if (service == nullptr) {
44 return SERVER_UNAVAILABLE;
45 }
46
47 remote_ = new (std::nothrow) ObserverClient(observer_, convert_);
48 auto status = service->Subscribe(appId_, storeId_, remote_);
49 if (status != SUCCESS) {
50 remote_ = nullptr;
51 }
52 return status;
53 }
54
UnregisterRemoteObserver()55 Status ObserverBridge::UnregisterRemoteObserver()
56 {
57 if (remote_ == nullptr) {
58 return SUCCESS;
59 }
60
61 auto service = KVDBServiceClient::GetInstance();
62 if (service == nullptr) {
63 return SERVER_UNAVAILABLE;
64 }
65
66 auto status = service->Unsubscribe(appId_, storeId_, remote_);
67 remote_ = nullptr;
68 return status;
69 }
70
OnChange(const DBChangedData & data)71 void ObserverBridge::OnChange(const DBChangedData &data)
72 {
73 std::string deviceId;
74 auto inserted = ConvertDB(data.GetEntriesInserted(), deviceId, convert_);
75 auto updated = ConvertDB(data.GetEntriesUpdated(), deviceId, convert_);
76 auto deleted = ConvertDB(data.GetEntriesDeleted(), deviceId, convert_);
77 ChangeNotification notice(std::move(inserted), std::move(updated), std::move(deleted), deviceId, false);
78 observer_->OnChange(notice);
79 }
80
ObserverClient(std::shared_ptr<Observer> observer,const Convertor & cvt)81 ObserverBridge::ObserverClient::ObserverClient(std::shared_ptr<Observer> observer, const Convertor &cvt)
82 : KvStoreObserverClient(observer), convert_(cvt)
83 {
84 }
85
OnChange(const ChangeNotification & data)86 void ObserverBridge::ObserverClient::OnChange(const ChangeNotification &data)
87 {
88 std::string deviceId;
89 auto inserted = ObserverBridge::ConvertDB(data.GetInsertEntries(), deviceId, convert_);
90 auto updated = ObserverBridge::ConvertDB(data.GetUpdateEntries(), deviceId, convert_);
91 auto deleted = ObserverBridge::ConvertDB(data.GetDeleteEntries(), deviceId, convert_);
92 ChangeNotification notice(std::move(inserted), std::move(updated), std::move(deleted), deviceId, false);
93 KvStoreObserverClient::OnChange(notice);
94 }
95
96 template<class T>
ConvertDB(const T & dbEntries,std::string & deviceId,const Convertor & convert)97 std::vector<Entry> ObserverBridge::ConvertDB(const T &dbEntries, std::string &deviceId, const Convertor &convert)
98 {
99 std::vector<Entry> entries(dbEntries.size());
100 auto it = entries.begin();
101 for (const auto &dbEntry : dbEntries) {
102 Entry &entry = *it;
103 entry.key = convert.ToKey(DBKey(dbEntry.key), deviceId);
104 entry.value = dbEntry.value;
105 ++it;
106 }
107 return entries;
108 }
109
OnServiceDeath()110 void ObserverBridge::OnServiceDeath()
111 {
112 if (remote_ == nullptr) {
113 return;
114 }
115 remote_ = nullptr;
116 }
117 } // namespace OHOS::DistributedKv
118