1 /*
2  * Copyright (c) 2023 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 #include "cloud_db_data_utils.h"
16 
17 #include "cloud/cloud_db_constant.h"
18 #include "cloud/cloud_db_types.h"
19 namespace DistributedDB {
GenerateRecords(int recordCounts,const TableSchema & schema)20 std::vector<VBucket> CloudDBDataUtils::GenerateRecords(int recordCounts, const TableSchema &schema)
21 {
22     std::vector<VBucket> records;
23     records.resize(recordCounts);
24     for (int i = 0; i < recordCounts; ++i) {
25         records.push_back(GenerateRecord(schema, i));
26     }
27     return records;
28 }
29 
GenerateRecord(const DistributedDB::TableSchema & schema,int32_t start)30 VBucket CloudDBDataUtils::GenerateRecord(const DistributedDB::TableSchema &schema, int32_t start)
31 {
32     VBucket bucket;
33     Type value = Nil();
34     for (const auto &field: schema.fields) {
35         switch (field.type) {
36             case TYPE_INDEX<int64_t>:
37                 value = static_cast<int64_t>(start);
38                 break;
39             case TYPE_INDEX<double>:
40                 value = start * 1.0;
41                 break;
42             case TYPE_INDEX<std::string>:
43                 value = std::to_string(start);
44                 break;
45             case TYPE_INDEX<bool>:
46                 value = (start != 0);
47                 break;
48             case TYPE_INDEX<Bytes>:
49             case TYPE_INDEX<Asset>:
50             case TYPE_INDEX<Assets>: {
51                 std::string blob = std::to_string(start);
52                 value = Bytes(blob.begin(), blob.end());
53                 break;
54             }
55             default:
56                 value = Nil();
57         }
58         bucket[field.colName] = std::move(value);
59     }
60     return bucket;
61 }
62 
GenerateExtends(int extendCounts)63 std::vector<VBucket> CloudDBDataUtils::GenerateExtends(int extendCounts)
64 {
65     std::vector<VBucket> extends;
66     extends.resize(extendCounts);
67     for (int i = 0; i < extendCounts; ++i) {
68         VBucket info = {
69             { CloudDbConstant::DELETE_FIELD, false }
70         };
71         extends.push_back(info);
72     }
73     return extends;
74 }
75 }