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 #ifdef RELATIONAL_STORE
16 #include "relational_row_data_impl.h"
17
18 namespace DistributedDB {
GetColSize()19 std::size_t RelationalRowDataImpl::GetColSize()
20 {
21 return data_.size();
22 }
23
CalcLength() const24 int RelationalRowDataImpl::CalcLength() const
25 {
26 size_t length = Parcel::GetUInt32Len(); // For save the count.
27 for (const auto &dataValue : data_) {
28 length += Parcel::GetUInt32Len(); // For save the dataValue's type.
29 length += DataTransformer::CalDataValueLength(dataValue);
30 if (length > static_cast<size_t>(INT_MAX)) {
31 return 0;
32 }
33 }
34 return static_cast<int>(Parcel::GetEightByteAlign(length));
35 }
36
Serialize(Parcel & parcel) const37 int RelationalRowDataImpl::Serialize(Parcel &parcel) const
38 {
39 parcel.WriteUInt32(data_.size());
40 for (const auto &dataValue : data_) {
41 if (DataTransformer::SerializeDataValue(dataValue, parcel) != E_OK) {
42 return -E_PARSE_FAIL;
43 }
44 }
45 parcel.EightByteAlign();
46 if (parcel.IsError()) {
47 return -E_PARSE_FAIL;
48 }
49 return E_OK;
50 }
51
DeSerialize(Parcel & parcel)52 int RelationalRowDataImpl::DeSerialize(Parcel &parcel)
53 {
54 uint32_t size = 0;
55 (void)parcel.ReadUInt32(size);
56 if (parcel.IsError() ||
57 size > DBConstant::MAX_REMOTEDATA_SIZE / DataTransformer::CalDataValueLength(DataValue {})) {
58 return -E_PARSE_FAIL;
59 }
60 while (size-- > 0u) {
61 DataValue value;
62 if (DataTransformer::DeserializeDataValue(value, parcel) != E_OK) {
63 return -E_PARSE_FAIL;
64 }
65 data_.emplace_back(std::move(value));
66 }
67 parcel.EightByteAlign();
68 if (parcel.IsError()) {
69 return -E_PARSE_FAIL;
70 }
71 return E_OK;
72 }
73
GetType(int index,StorageType & type) const74 int RelationalRowDataImpl::GetType(int index, StorageType &type) const
75 {
76 if (index < 0 || index >= static_cast<int>(data_.size())) {
77 return -E_NONEXISTENT;
78 }
79 type = data_.at(index).GetType();
80 return E_OK;
81 }
82
Get(int index,int64_t & value) const83 int RelationalRowDataImpl::Get(int index, int64_t &value) const
84 {
85 if (index < 0 || index >= static_cast<int>(data_.size())) {
86 return -E_NONEXISTENT;
87 }
88 if (data_.at(index).GetInt64(value) != E_OK) {
89 return -E_TYPE_MISMATCH;
90 }
91 return E_OK;
92 }
93
Get(int index,double & value) const94 int RelationalRowDataImpl::Get(int index, double &value) const
95 {
96 if (index < 0 || index >= static_cast<int>(data_.size())) {
97 return -E_NONEXISTENT;
98 }
99 if (data_.at(index).GetDouble(value) != E_OK) {
100 return -E_TYPE_MISMATCH;
101 }
102 return E_OK;
103 }
104
Get(int index,std::string & value) const105 int RelationalRowDataImpl::Get(int index, std::string &value) const
106 {
107 if (index < 0 || index >= static_cast<int>(data_.size())) {
108 return -E_NONEXISTENT;
109 }
110 if (data_.at(index).GetText(value) != E_OK) {
111 return -E_TYPE_MISMATCH;
112 }
113 return E_OK;
114 }
115
Get(int index,std::vector<uint8_t> & value) const116 int RelationalRowDataImpl::Get(int index, std::vector<uint8_t> &value) const
117 {
118 if (index < 0 || index >= static_cast<int>(data_.size())) {
119 return -E_NONEXISTENT;
120 }
121 Blob blob;
122 int errCode = data_.at(index).GetBlob(blob);
123 if (errCode != E_OK) {
124 return errCode == -E_NOT_SUPPORT ? -E_TYPE_MISMATCH : errCode;
125 }
126 value = blob.ToVector();
127 return errCode;
128 }
129 } // namespace DistributedDB
130 #endif