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 #ifdef RELATIONAL_STORE 16 #include "relational_row_data_set.h" 17 #include "relational_row_data_impl.h" 18 19 namespace DistributedDB { RelationalRowDataSet()20RelationalRowDataSet::RelationalRowDataSet() : serialLength_(Parcel::GetUInt32Len() + Parcel::GetUInt32Len()) {} 21 ~RelationalRowDataSet()22RelationalRowDataSet::~RelationalRowDataSet() 23 { 24 RelationalRowData::Release(data_); 25 } 26 operator =(RelationalRowDataSet && r)27RelationalRowDataSet &RelationalRowDataSet::operator=(RelationalRowDataSet &&r) noexcept 28 { 29 if (&r == this) { 30 return *this; 31 } 32 33 colNames_ = std::move(r.colNames_); 34 data_ = std::move(r.data_); 35 serialLength_ = r.serialLength_; 36 37 r.serialLength_ = Parcel::GetUInt32Len() + Parcel::GetUInt32Len(); 38 return *this; 39 } 40 GetSize() const41int RelationalRowDataSet::GetSize() const 42 { 43 return data_.size(); 44 } 45 CalcLength() const46int RelationalRowDataSet::CalcLength() const 47 { 48 if (serialLength_ > static_cast<size_t>(INT32_MAX)) { 49 return 0; 50 } 51 return static_cast<int>(Parcel::GetEightByteAlign(serialLength_)); 52 } 53 Serialize(Parcel & parcel) const54int RelationalRowDataSet::Serialize(Parcel &parcel) const 55 { 56 if (serialLength_ > static_cast<size_t>(INT32_MAX) || parcel.IsError()) { 57 return -E_PARSE_FAIL; 58 } 59 60 (void)parcel.WriteUInt32(colNames_.size()); 61 for (const auto &colName : colNames_) { 62 (void)parcel.WriteString(colName); 63 } 64 (void)parcel.WriteUInt32(data_.size()); 65 for (const auto &rowData : data_) { 66 rowData->Serialize(parcel); 67 } 68 69 parcel.EightByteAlign(); 70 if (parcel.IsError()) { 71 return -E_PARSE_FAIL; 72 } 73 return E_OK; 74 }; 75 DeSerialize(Parcel & parcel)76int RelationalRowDataSet::DeSerialize(Parcel &parcel) 77 { 78 Clear(); 79 uint32_t size = 0; 80 parcel.ReadUInt32(size); 81 if (parcel.IsError() || size > DBConstant::MAX_REMOTEDATA_SIZE / parcel.GetStringLen(std::string {})) { 82 return -E_PARSE_FAIL; 83 } 84 while (size-- > 0) { 85 std::string str; 86 parcel.ReadString(str); 87 if (parcel.IsError()) { 88 return -E_PARSE_FAIL; 89 } 90 colNames_.emplace_back(std::move(str)); 91 } 92 93 parcel.ReadUInt32(size); 94 if (parcel.IsError() || size > DBConstant::MAX_REMOTEDATA_SIZE / parcel.GetUInt32Len()) { 95 return -E_PARSE_FAIL; 96 } 97 while (size-- > 0) { 98 auto rowData = new (std::nothrow) RelationalRowDataImpl(); 99 if (rowData == nullptr) { 100 return -E_OUT_OF_MEMORY; 101 } 102 103 if (rowData->DeSerialize(parcel) != E_OK) { 104 return -E_PARSE_FAIL; 105 } 106 data_.push_back(rowData); 107 } 108 parcel.EightByteAlign(); 109 if (parcel.IsError()) { 110 return -E_PARSE_FAIL; 111 } 112 return E_OK; 113 }; 114 SetColNames(std::vector<std::string> && colNames)115void RelationalRowDataSet::SetColNames(std::vector<std::string> &&colNames) 116 { 117 for (const auto &colName : colNames_) { 118 serialLength_ -= Parcel::GetStringLen(colName); 119 } 120 colNames_ = std::move(colNames); 121 for (const auto &colName : colNames_) { 122 serialLength_ += Parcel::GetStringLen(colName); 123 } 124 } 125 SetRowData(std::vector<RelationalRowData * > && data)126void RelationalRowDataSet::SetRowData(std::vector<RelationalRowData *> &&data) 127 { 128 for (const auto &rowData : data_) { 129 serialLength_ -= static_cast<size_t>(rowData->CalcLength()); 130 } 131 data_ = data; 132 for (const auto &rowData : data_) { 133 serialLength_ += static_cast<size_t>(rowData->CalcLength()); 134 } 135 } 136 Insert(RelationalRowData * rowData)137int RelationalRowDataSet::Insert(RelationalRowData *rowData) 138 { 139 if (rowData == nullptr) { 140 return -E_INVALID_ARGS; 141 } 142 if ((serialLength_ + static_cast<size_t>(rowData->CalcLength())) > static_cast<size_t>(INT32_MAX)) { 143 return -E_INVALID_ARGS; 144 } 145 data_.push_back(rowData); 146 serialLength_ += static_cast<size_t>(rowData->CalcLength()); 147 return E_OK; 148 } 149 Clear()150void RelationalRowDataSet::Clear() 151 { 152 colNames_.clear(); 153 RelationalRowData::Release(data_); 154 serialLength_ = Parcel::GetUInt32Len() + Parcel::GetUInt32Len(); 155 } 156 GetColNames() const157const std::vector<std::string> &RelationalRowDataSet::GetColNames() const 158 { 159 return colNames_; 160 } 161 Get(int index) const162const RelationalRowData *RelationalRowDataSet::Get(int index) const 163 { 164 if (index < 0 || index >= static_cast<int>(data_.size())) { 165 return nullptr; 166 } 167 return data_.at(index); 168 } 169 Merge(RelationalRowDataSet && rowDataSet)170int RelationalRowDataSet::Merge(RelationalRowDataSet &&rowDataSet) 171 { 172 if (this == &rowDataSet) { 173 return -E_INVALID_ARGS; 174 } 175 176 if (!rowDataSet.colNames_.empty()) { 177 if (colNames_.empty()) { 178 colNames_ = std::move(rowDataSet.colNames_); 179 } else if (colNames_ != rowDataSet.colNames_) { 180 return -E_INVALID_ARGS; 181 } 182 } 183 184 data_.insert(data_.end(), rowDataSet.data_.begin(), rowDataSet.data_.end()); 185 rowDataSet.data_.clear(); 186 rowDataSet.serialLength_ = Parcel::GetUInt32Len() + Parcel::GetUInt32Len(); 187 return E_OK; 188 } 189 } 190 #endif