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_remote_query_continue_token.h"
17 
18 namespace DistributedDB {
RelationalRemoteQueryContinueToken(std::vector<std::string> && colNames,std::vector<RelationalRowData * > && data)19 RelationalRemoteQueryContinueToken::RelationalRemoteQueryContinueToken(std::vector<std::string> &&colNames,
20     std::vector<RelationalRowData *> &&data) : colNames_(std::move(colNames)), data_(std::move(data)) {}
21 
~RelationalRemoteQueryContinueToken()22 RelationalRemoteQueryContinueToken::~RelationalRemoteQueryContinueToken()
23 {
24     RelationalRowData::Release(data_);
25 }
26 
27 // Check the magic number at the beginning and end of the RelationalRemoteQueryContinueToken.
CheckValid() const28 bool RelationalRemoteQueryContinueToken::CheckValid() const
29 {
30     bool isValid = (magicBegin_ == MAGIC_BEGIN && magicEnd_ == MAGIC_END);
31     if (!isValid) {
32         LOGE("Invalid continue token.");
33     }
34     return isValid;
35 }
36 
37 // if succeed, return E_UNFINISHED, E_OK; if error happened, return other errCode
GetData(int packetSize,RelationalRowDataSet & dataSet)38 int RelationalRemoteQueryContinueToken::GetData(int packetSize, RelationalRowDataSet &dataSet)
39 {
40     bool isEmpty = true;
41     if (!colNames_.empty()) {
42         dataSet.SetColNames(std::move(colNames_));
43         isEmpty = false;
44     }
45     while (!data_.empty()) {
46         if (isEmpty) {  // at least get one data
47             dataSet.Insert(data_.at(0));
48             data_.erase(data_.begin());
49             isEmpty = false;
50             continue;
51         }
52         if (dataSet.CalcLength() + data_.at(0)->CalcLength() > packetSize) {
53             return -E_UNFINISHED;
54         }
55         dataSet.Insert(data_.at(0));
56         data_.erase(data_.begin());
57     }
58     return E_OK;
59 }
60 }
61 #endif