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 #define LOG_TAG "StoreResultSet"
16 #include "store_result_set.h"
17
18 #include "log_print.h"
19 #include "store_util.h"
20 namespace OHOS::DistributedKv {
StoreResultSet(DBResultSet * impl,std::shared_ptr<DBStore> dbStore,const Convertor & convert)21 StoreResultSet::StoreResultSet(DBResultSet *impl, std::shared_ptr<DBStore> dbStore, const Convertor &convert)
22 : impl_(impl), dbStore_(std::move(dbStore)), convert_(convert)
23 {
24 }
25
~StoreResultSet()26 StoreResultSet::~StoreResultSet()
27 {
28 if (impl_ != nullptr && dbStore_ != nullptr) {
29 dbStore_->CloseResultSet(impl_);
30 impl_ = nullptr;
31 }
32 }
33
GetCount() const34 int StoreResultSet::GetCount() const
35 {
36 std::shared_lock<decltype(mutex_)> lock(mutex_);
37 if (impl_ == nullptr) {
38 ZLOGW("already closed");
39 return INVALID_COUNT;
40 }
41
42 return impl_->GetCount();
43 }
44
GetPosition() const45 int StoreResultSet::GetPosition() const
46 {
47 std::shared_lock<decltype(mutex_)> lock(mutex_);
48 if (impl_ == nullptr) {
49 ZLOGW("already closed");
50 return INVALID_POSITION;
51 }
52
53 return impl_->GetPosition();
54 }
55
MoveToFirst()56 bool StoreResultSet::MoveToFirst()
57 {
58 std::shared_lock<decltype(mutex_)> lock(mutex_);
59 if (impl_ == nullptr) {
60 ZLOGW("already closed");
61 return false;
62 }
63
64 return impl_->MoveToFirst();
65 }
MoveToLast()66 bool StoreResultSet::MoveToLast()
67 {
68 std::shared_lock<decltype(mutex_)> lock(mutex_);
69 if (impl_ == nullptr) {
70 ZLOGW("already closed");
71 return false;
72 }
73
74 return impl_->MoveToLast();
75 }
76
MoveToNext()77 bool StoreResultSet::MoveToNext()
78 {
79 std::shared_lock<decltype(mutex_)> lock(mutex_);
80 if (impl_ == nullptr) {
81 ZLOGW("already closed");
82 return false;
83 }
84
85 return impl_->MoveToNext();
86 }
87
MoveToPrevious()88 bool StoreResultSet::MoveToPrevious()
89 {
90 std::shared_lock<decltype(mutex_)> lock(mutex_);
91 if (impl_ == nullptr) {
92 ZLOGW("already closed");
93 return false;
94 }
95
96 return impl_->MoveToPrevious();
97 }
98
Move(int offset)99 bool StoreResultSet::Move(int offset)
100 {
101 std::shared_lock<decltype(mutex_)> lock(mutex_);
102 if (impl_ == nullptr) {
103 ZLOGW("already closed");
104 return false;
105 }
106
107 return impl_->Move(offset);
108 }
109
MoveToPosition(int position)110 bool StoreResultSet::MoveToPosition(int position)
111 {
112 std::shared_lock<decltype(mutex_)> lock(mutex_);
113 if (impl_ == nullptr) {
114 ZLOGW("already closed");
115 return false;
116 }
117
118 return impl_->MoveToPosition(position);
119 }
120
IsFirst() const121 bool StoreResultSet::IsFirst() const
122 {
123 std::shared_lock<decltype(mutex_)> lock(mutex_);
124 if (impl_ == nullptr) {
125 ZLOGW("already closed");
126 return false;
127 }
128
129 return impl_->IsFirst();
130 }
IsLast() const131 bool StoreResultSet::IsLast() const
132 {
133 std::shared_lock<decltype(mutex_)> lock(mutex_);
134 if (impl_ == nullptr) {
135 ZLOGW("already closed");
136 return false;
137 }
138
139 return impl_->IsLast();
140 }
141
IsBeforeFirst() const142 bool StoreResultSet::IsBeforeFirst() const
143 {
144 std::shared_lock<decltype(mutex_)> lock(mutex_);
145 if (impl_ == nullptr) {
146 ZLOGW("already closed");
147 return false;
148 }
149
150 return impl_->IsBeforeFirst();
151 }
152
IsAfterLast() const153 bool StoreResultSet::IsAfterLast() const
154 {
155 std::shared_lock<decltype(mutex_)> lock(mutex_);
156 if (impl_ == nullptr) {
157 ZLOGW("already closed");
158 return false;
159 }
160
161 return impl_->IsAfterLast();
162 }
163
GetEntry(Entry & entry) const164 Status StoreResultSet::GetEntry(Entry &entry) const
165 {
166 std::shared_lock<decltype(mutex_)> lock(mutex_);
167 if (impl_ == nullptr) {
168 ZLOGW("already closed");
169 return ALREADY_CLOSED;
170 }
171
172 DBEntry dbEntry;
173 auto dbStatus = impl_->GetEntry(dbEntry);
174 auto status = StoreUtil::ConvertStatus(dbStatus);
175 if (status != SUCCESS) {
176 ZLOGE("failed! status:%{public}d, position:%{public}d", status, impl_->GetPosition());
177 return status;
178 }
179 std::string deviceId;
180 entry.key = convert_.ToKey(std::move(dbEntry.key), deviceId);
181 entry.value = std::move(dbEntry.value);
182 return SUCCESS;
183 }
184
Close()185 Status StoreResultSet::Close()
186 {
187 std::unique_lock<decltype(mutex_)> lock(mutex_);
188 if (impl_ == nullptr || dbStore_ == nullptr) {
189 return SUCCESS;
190 }
191 auto dbStatus = dbStore_->CloseResultSet(impl_);
192 auto status = StoreUtil::ConvertStatus(dbStatus);
193 if (status == SUCCESS) {
194 impl_ = nullptr;
195 dbStore_ = nullptr;
196 }
197 return status;
198 }
199 } // namespace OHOS::DistributedKv