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
16 #define LOG_TAG "KvStoreDataShareBridge"
17
18 #include "log_print.h"
19 #include "kvstore_datashare_bridge.h"
20
21 namespace OHOS {
22 namespace DistributedKv {
23 using namespace DataShare;
KvStoreDataShareBridge(std::shared_ptr<KvStoreResultSet> kvResultSet)24 KvStoreDataShareBridge::KvStoreDataShareBridge(std::shared_ptr<KvStoreResultSet> kvResultSet)
25 :kvResultSet_(kvResultSet) {};
26
GetRowCount(int32_t & count)27 int KvStoreDataShareBridge::GetRowCount(int32_t &count)
28 {
29 count = Count();
30 return count == INVALID_COUNT ? E_ERROR : E_OK;
31 }
32
GetAllColumnNames(std::vector<std::string> & columnsName)33 int KvStoreDataShareBridge::GetAllColumnNames(std::vector<std::string> &columnsName)
34 {
35 columnsName = { "key", "value" };
36 return E_OK;
37 }
FillBlock(int pos,ResultSetBridge::Writer & writer)38 bool KvStoreDataShareBridge::FillBlock(int pos, ResultSetBridge::Writer &writer)
39 {
40 if (kvResultSet_ == nullptr) {
41 ZLOGE("kvResultSet_ nullptr");
42 return false;
43 }
44 bool isMoved = kvResultSet_->MoveToPosition(pos);
45 if (!isMoved) {
46 ZLOGE("MoveToPosition failed");
47 return false;
48 }
49 Entry entry;
50 Status status = kvResultSet_->GetEntry(entry);
51 if (status != Status::SUCCESS) {
52 ZLOGE("GetEntry failed %{public}d", status);
53 return false;
54 }
55 int statusAlloc = writer.AllocRow();
56 if (statusAlloc != E_OK) {
57 ZLOGE("SharedBlock is full: %{public}d", statusAlloc);
58 return false;
59 }
60 int keyStatus = writer.Write(0, entry.key.ToString().c_str(), entry.key.Size() + 1);
61 if (keyStatus != E_OK) {
62 ZLOGE("WriteBlob key error: %{public}d", keyStatus);
63 return false;
64 }
65 int valueStatus = writer.Write(1, entry.value.ToString().c_str(), entry.value.Size() + 1);
66 if (valueStatus != E_OK) {
67 ZLOGE("WriteBlob value error: %{public}d", valueStatus);
68 return false;
69 }
70 return true;
71 }
72
Count()73 int KvStoreDataShareBridge::Count()
74 {
75 if (kvResultSet_ == nullptr) {
76 ZLOGE("kvResultSet_ nullptr");
77 return INVALID_COUNT;
78 }
79 if (resultRowCount != INVALID_COUNT) {
80 return resultRowCount;
81 }
82 int count = kvResultSet_->GetCount();
83 if (count < 0) {
84 ZLOGE("kvResultSet count invalid: %{public}d", count);
85 return INVALID_COUNT;
86 }
87 resultRowCount = count;
88 return count;
89 }
OnGo(int32_t start,int32_t target,ResultSetBridge::Writer & writer)90 int KvStoreDataShareBridge::OnGo(int32_t start, int32_t target, ResultSetBridge::Writer &writer)
91 {
92 if ((start < 0) || (target < 0) || (start > target) || (target >= Count())) {
93 ZLOGE("nowRowIndex out of line: %{public}d", target);
94 return -1;
95 }
96 for (int pos = start; pos <= target; pos++) {
97 bool ret = FillBlock(pos, writer);
98 if (!ret) {
99 ZLOGE("nowRowIndex out of line: %{public}d %{public}d", pos, target);
100 return pos - 1;
101 }
102 }
103 return target;
104 }
105 } // namespace DistributedKv
106 } // namespace OHOS