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 #define LOG_TAG "RdbWatcher"
17 
18 #include "rdb_watcher.h"
19 
20 #include "error/general_error.h"
21 #include "log_print.h"
22 #include "utils/anonymous.h"
23 
24 namespace OHOS::DistributedRdb {
25 using namespace DistributedData;
26 using Error = DistributedData::GeneralError;
RdbWatcher()27 RdbWatcher::RdbWatcher() {}
28 
OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values)29 int32_t RdbWatcher::OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values)
30 {
31     auto notifier = GetNotifier();
32     if (notifier == nullptr) {
33         return E_NOT_INIT;
34     }
35     DistributedRdb::Origin rdbOrigin;
36     rdbOrigin.origin = origin.origin;
37     rdbOrigin.dataType = origin.dataType;
38     rdbOrigin.id = origin.id;
39     rdbOrigin.store = origin.store;
40     ZLOGD("store:%{public}s data change from :%{public}s, dataType:%{public}d, origin:%{public}d.",
41         Anonymous::Change(rdbOrigin.store).c_str(),
42         rdbOrigin.id.empty() ? "empty" : Anonymous::Change(*rdbOrigin.id.begin()).c_str(),
43         rdbOrigin.dataType, rdbOrigin.origin);
44     notifier->OnChange(rdbOrigin, primaryFields, std::move(values));
45     return E_OK;
46 }
47 
OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas)48 int32_t RdbWatcher::OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas)
49 {
50     return E_OK;
51 }
52 
GetNotifier() const53 sptr<RdbNotifierProxy> RdbWatcher::GetNotifier() const
54 {
55     std::shared_lock<decltype(mutex_)> lock(mutex_);
56     return notifier_;
57 }
58 
SetNotifier(sptr<RdbNotifierProxy> notifier)59 void RdbWatcher::SetNotifier(sptr<RdbNotifierProxy> notifier)
60 {
61     std::unique_lock<decltype(mutex_)> lock(mutex_);
62     if (notifier_ == notifier) {
63         return;
64     }
65     notifier_ = notifier;
66 }
67 } // namespace OHOS::DistributedRdb
68