1 /*
2 * Copyright (c) 2021 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 OMIT_MULTI_VER
17 #include "multi_ver_natural_store_commit_notify_data.h"
18 #include "db_errno.h"
19 #include "log_print.h"
20
21 namespace DistributedDB {
MultiVerNaturalStoreCommitNotifyData(MultiVerNaturalStore * db,const CommitID & startCommitID,const CommitID & endCommitID,Version curVersion)22 MultiVerNaturalStoreCommitNotifyData::MultiVerNaturalStoreCommitNotifyData(MultiVerNaturalStore *db,
23 const CommitID &startCommitID, const CommitID &endCommitID, Version curVersion)
24 : db_(db),
25 startCommitID_(startCommitID),
26 endCommitID_(endCommitID),
27 isFilled_(false),
28 version_(curVersion)
29 {}
30
~MultiVerNaturalStoreCommitNotifyData()31 MultiVerNaturalStoreCommitNotifyData::~MultiVerNaturalStoreCommitNotifyData()
32 {
33 if (db_ != nullptr) {
34 db_->RemoveVersionConstraintFromList(version_);
35 }
36
37 db_ = nullptr;
38 }
39
GetInsertedEntries(int & errCode) const40 const std::list<Entry> MultiVerNaturalStoreCommitNotifyData::GetInsertedEntries(int &errCode) const
41 {
42 errCode = FillInnerData();
43 if (errCode != E_OK) {
44 LOGE("Failed to fill inner data in GetInsertedEntries(), err:%d", errCode);
45 }
46 return diffData_.inserted;
47 }
48
GetUpdatedEntries(int & errCode) const49 const std::list<Entry> MultiVerNaturalStoreCommitNotifyData::GetUpdatedEntries(int &errCode) const
50 {
51 errCode = FillInnerData();
52 if (errCode != E_OK) {
53 LOGE("Failed to fill inner data in GetUpdatedEntries(), err:%d", errCode);
54 }
55 return diffData_.updated;
56 }
57
GetDeletedEntries(int & errCode) const58 const std::list<Entry> MultiVerNaturalStoreCommitNotifyData::GetDeletedEntries(int &errCode) const
59 {
60 errCode = FillInnerData();
61 if (errCode != E_OK) {
62 LOGE("Failed to fill inner data in GetDeletedEntries(), err:%d", errCode);
63 }
64 return diffData_.deleted;
65 }
66
IsCleared() const67 bool MultiVerNaturalStoreCommitNotifyData::IsCleared() const
68 {
69 int errCode = FillInnerData();
70 if (errCode != E_OK) {
71 LOGE("Failed to fill inner data in IsCleared(), err:%d", errCode);
72 }
73 return diffData_.isCleared;
74 }
75
IsChangedDataEmpty() const76 bool MultiVerNaturalStoreCommitNotifyData::IsChangedDataEmpty() const
77 {
78 int errCode = FillInnerData();
79 if (errCode != E_OK) {
80 LOGE("Failed to fill inner data in IsEmpty(), err:%d", errCode);
81 }
82 return !diffData_.isCleared &&
83 diffData_.inserted.empty() &&
84 diffData_.updated.empty() &&
85 diffData_.deleted.empty();
86 }
87
FillInnerData() const88 int MultiVerNaturalStoreCommitNotifyData::FillInnerData() const
89 {
90 std::lock_guard<std::mutex> lock(fillMutex_);
91 if (isFilled_) {
92 return E_OK;
93 }
94 if (db_ == nullptr) {
95 LOGE("Failed to fill inner data, db is nullptr");
96 return -E_INVALID_DB;
97 }
98
99 int errCode = db_->GetDiffEntries(startCommitID_, endCommitID_, diffData_);
100 if (errCode != E_OK) {
101 LOGE("Failed to get diff entries when filling inner data, err:%d", errCode);
102 return errCode;
103 }
104 isFilled_ = true;
105 return E_OK;
106 }
107
108 DEFINE_OBJECT_TAG_FACILITIES(MultiVerNaturalStoreCommitNotifyData)
109 }
110 #endif