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 {
29 }
30
OnChange(const Origin & origin,const PRIFields & primaryFields,ChangeInfo && values)31 int32_t RdbWatcher::OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values)
32 {
33 auto notifier = GetNotifier();
34 if (notifier == nullptr) {
35 return E_NOT_INIT;
36 }
37 DistributedRdb::Origin rdbOrigin;
38 rdbOrigin.origin = origin.origin;
39 rdbOrigin.dataType = origin.dataType;
40 rdbOrigin.id = origin.id;
41 rdbOrigin.store = origin.store;
42 ZLOGD("store:%{public}s data change from :%{public}s, dataType:%{public}d, origin:%{public}d.",
43 Anonymous::Change(rdbOrigin.store).c_str(),
44 rdbOrigin.id.empty() ? "empty" : Anonymous::Change(*rdbOrigin.id.begin()).c_str(),
45 rdbOrigin.dataType, rdbOrigin.origin);
46 notifier->OnChange(rdbOrigin, primaryFields, std::move(values));
47 return E_OK;
48 }
49
OnChange(const Origin & origin,const Fields & fields,ChangeData && datas)50 int32_t RdbWatcher::OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas)
51 {
52 return E_OK;
53 }
54
GetNotifier() const55 sptr<RdbNotifierProxy> RdbWatcher::GetNotifier() const
56 {
57 std::shared_lock<decltype(mutex_)> lock(mutex_);
58 return notifier_;
59 }
60
SetNotifier(sptr<RdbNotifierProxy> notifier)61 void RdbWatcher::SetNotifier(sptr<RdbNotifierProxy> notifier)
62 {
63 std::unique_lock<decltype(mutex_)> lock(mutex_);
64 if (notifier_ == notifier) {
65 return;
66 }
67 notifier_ = notifier;
68 }
69 } // namespace OHOS::DistributedRdb
70