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 "RdbCloud"
17 #include "rdb_cloud.h"
18
19 #include "cloud/schema_meta.h"
20 #include "log_print.h"
21 #include "value_proxy.h"
22 #include "utils/anonymous.h"
23
24 namespace OHOS::DistributedRdb {
25 using namespace DistributedDB;
26 using namespace DistributedData;
RdbCloud(std::shared_ptr<DistributedData::CloudDB> cloudDB)27 RdbCloud::RdbCloud(std::shared_ptr<DistributedData::CloudDB> cloudDB)
28 : cloudDB_(std::move(cloudDB))
29 {
30 }
31
BatchInsert(const std::string & tableName,std::vector<DBVBucket> && record,std::vector<DBVBucket> & extend)32 DBStatus RdbCloud::BatchInsert(
33 const std::string &tableName, std::vector<DBVBucket> &&record, std::vector<DBVBucket> &extend)
34 {
35 DistributedData::VBuckets extends;
36 auto error = cloudDB_->BatchInsert(tableName, ValueProxy::Convert(std::move(record)), extends);
37 if (error == GeneralError::E_OK) {
38 extend = ValueProxy::Convert(std::move(extends));
39 }
40 return ConvertStatus(static_cast<GeneralError>(error));
41 }
42
BatchUpdate(const std::string & tableName,std::vector<DBVBucket> && record,std::vector<DBVBucket> & extend)43 DBStatus RdbCloud::BatchUpdate(
44 const std::string &tableName, std::vector<DBVBucket> &&record, std::vector<DBVBucket> &extend)
45 {
46 DistributedData::VBuckets extends = ValueProxy::Convert(std::move(extend));
47 auto error = cloudDB_->BatchUpdate(tableName, ValueProxy::Convert(std::move(record)), extends);
48 if (error == GeneralError::E_OK) {
49 extend = ValueProxy::Convert(std::move(extends));
50 }
51 return ConvertStatus(static_cast<GeneralError>(error));
52 }
53
BatchDelete(const std::string & tableName,std::vector<DBVBucket> & extend)54 DBStatus RdbCloud::BatchDelete(const std::string &tableName, std::vector<DBVBucket> &extend)
55 {
56 auto error = cloudDB_->BatchDelete(tableName, ValueProxy::Convert(std::move(extend)));
57 return ConvertStatus(static_cast<GeneralError>(error));
58 }
59
Query(const std::string & tableName,DBVBucket & extend,std::vector<DBVBucket> & data)60 DBStatus RdbCloud::Query(const std::string &tableName, DBVBucket &extend, std::vector<DBVBucket> &data)
61 {
62 auto cursor = cloudDB_->Query(tableName, ValueProxy::Convert(std::move(extend)));
63 if (cursor == nullptr) {
64 ZLOGE("cursor is null, table:%{public}s, extend:%{public}zu",
65 Anonymous::Change(tableName).c_str(), extend.size());
66 return ConvertStatus(static_cast<GeneralError>(E_ERROR));
67 }
68 int32_t count = cursor->GetCount();
69 data.reserve(count);
70 auto err = cursor->MoveToFirst();
71 while (err == E_OK && count > 0) {
72 DistributedData::VBucket entry;
73 err = cursor->GetEntry(entry);
74 if (err != E_OK) {
75 break;
76 }
77 data.emplace_back(ValueProxy::Convert(std::move(entry)));
78 err = cursor->MoveToNext();
79 count--;
80 }
81 DistributedData::Value cursorFlag;
82 cursor->Get(SchemaMeta::CURSOR_FIELD, cursorFlag);
83 extend[SchemaMeta::CURSOR_FIELD] = ValueProxy::Convert(std::move(cursorFlag));
84 if (cursor->IsEnd()) {
85 ZLOGD("query end, table:%{public}s", Anonymous::Change(tableName).c_str());
86 return DBStatus::QUERY_END;
87 }
88 return ConvertStatus(static_cast<GeneralError>(err));
89 }
90
Lock()91 std::pair<DBStatus, uint32_t> RdbCloud::Lock()
92 {
93 auto error = cloudDB_->Lock();
94 return std::make_pair( // int64_t <-> uint32_t, s <-> ms
95 ConvertStatus(static_cast<GeneralError>(error)), cloudDB_->AliveTime() * TO_MS);
96 }
97
UnLock()98 DBStatus RdbCloud::UnLock()
99 {
100 auto error = cloudDB_->Unlock();
101 return ConvertStatus(static_cast<GeneralError>(error));
102 }
103
HeartBeat()104 DBStatus RdbCloud::HeartBeat()
105 {
106 auto error = cloudDB_->Heartbeat();
107 return ConvertStatus(static_cast<GeneralError>(error));
108 }
109
Close()110 DBStatus RdbCloud::Close()
111 {
112 auto error = cloudDB_->Close();
113 return ConvertStatus(static_cast<GeneralError>(error));
114 }
115
ConvertStatus(DistributedData::GeneralError error)116 DBStatus RdbCloud::ConvertStatus(DistributedData::GeneralError error)
117 {
118 switch (error) {
119 case GeneralError::E_OK:
120 return DBStatus::OK;
121 case GeneralError::E_NETWORK_ERROR:
122 return DBStatus::CLOUD_NETWORK_ERROR;
123 case GeneralError::E_LOCKED_BY_OTHERS:
124 return DBStatus::CLOUD_LOCK_ERROR;
125 case GeneralError::E_RECODE_LIMIT_EXCEEDED:
126 return DBStatus::CLOUD_FULL_RECORDS;
127 case GeneralError::E_NO_SPACE_FOR_ASSET:
128 return DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT;
129 default:
130 ZLOGI("error:0x%{public}x", error);
131 break;
132 }
133 return DBStatus::CLOUD_ERROR;
134 }
135 } // namespace OHOS::DistributedRdb
136