1 /*
2  * Copyright (c) 2024 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 #include "query_utils.h"
17 
18 namespace DistributedDB {
FillQueryIn(const std::string & col,const std::vector<Type> & data,size_t valueType,Query & query)19 void QueryUtils::FillQueryIn(const std::string &col, const std::vector<Type> &data, size_t valueType,
20     Query &query)
21 {
22     switch (valueType) {
23         case TYPE_INDEX<int64_t>: {
24             std::vector<int64_t> pkList;
25             for (const auto &pk : data) {
26                 pkList.push_back(std::get<int64_t>(pk));
27             }
28             query.In(col, pkList);
29             break;
30         }
31         case TYPE_INDEX<std::string>: {
32             std::vector<std::string> pkList;
33             for (const auto &pk : data) {
34                 pkList.push_back(std::get<std::string>(pk));
35             }
36             query.In(col, pkList);
37             break;
38         }
39         case TYPE_INDEX<double>: {
40             std::vector<double> pkList;
41             for (const auto &pk : data) {
42                 pkList.push_back(std::get<double>(pk));
43             }
44             query.In(col, pkList);
45             break;
46         }
47         case TYPE_INDEX<bool>: {
48             std::vector<bool> pkList;
49             for (const auto &pk : data) {
50                 pkList.push_back(std::get<bool>(pk));
51             }
52             query.In(col, pkList);
53             break;
54         }
55         default:
56             break;
57     }
58 }
59 
FillQueryInKeys(const std::map<std::string,std::vector<Type>> & syncPk,std::map<std::string,size_t> dataIndex,Query & query)60 void QueryUtils::FillQueryInKeys(const std::map<std::string, std::vector<Type>> &syncPk,
61     std::map<std::string, size_t> dataIndex, Query &query)
62 {
63     std::set<Key> keys;
64     for (const auto &[col, pkList] : syncPk) {
65         switch (dataIndex[col]) {
66             case TYPE_INDEX<std::string>:
67                 FillStringQueryKeys(keys, pkList);
68                 break;
69             case TYPE_INDEX<Bytes>:
70                 FillByteQueryKeys(keys, pkList);
71                 break;
72             default:
73                 break;
74         }
75     }
76     if (!keys.empty()) {
77         query.InKeys(keys);
78     }
79 }
80 
FillStringQueryKeys(std::set<Key> & keys,const std::vector<Type> & pkList)81 void QueryUtils::FillStringQueryKeys(std::set<Key> &keys, const std::vector<Type> &pkList)
82 {
83     for (const auto &pk : pkList) {
84         const std::string *keyStrPtr = std::get_if<std::string>(&pk);
85         if (keyStrPtr == nullptr) {
86             continue;
87         }
88         keys.insert(Key((*keyStrPtr).begin(), (*keyStrPtr).end()));
89     }
90 }
91 
FillByteQueryKeys(std::set<Key> & keys,const std::vector<Type> & pkList)92 void QueryUtils::FillByteQueryKeys(std::set<Key> &keys, const std::vector<Type> &pkList)
93 {
94     for (const auto &pk : pkList) {
95         if (std::get_if<Bytes>(&pk) == nullptr) {
96             continue;
97         }
98         keys.insert(std::get<Bytes>(pk));
99     }
100 }
101 } // DistributedDB