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