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
16 #include "distributeddb_data_generate_unit_test.h"
17 #include "store_types.h"
18
19 using namespace DistributedDB;
20
21 namespace DistributedDBUnitTest {
GenerateKey(int keyCount,int startPosition,Key & keyTest)22 void GenerateKey(int keyCount, int startPosition, Key &keyTest)
23 {
24 if (keyCount <= 0) {
25 return;
26 }
27 int i;
28 for (i = 0; i < keyCount; i++) {
29 keyTest.push_back(KEY_NUM[(i + startPosition) % NUM_LENGTH]);
30 }
31 }
32
GenerateValue(int valueCount,int startPosition,Value & valueTest)33 void GenerateValue(int valueCount, int startPosition, Value &valueTest)
34 {
35 if (valueCount <= 0) {
36 return;
37 }
38 int i;
39 for (i = 0; i < valueCount; i++) {
40 valueTest.push_back(VALUE_LETTER[(i + startPosition) % LETTER_LENGTH]);
41 }
42 }
43
GenerateEntry(int entryCount,int startPosition,Entry & entryTest)44 void GenerateEntry(int entryCount, int startPosition, Entry &entryTest)
45 {
46 if (entryCount <= 0) {
47 return;
48 }
49 GenerateKey(entryCount, startPosition, entryTest.key);
50 GenerateValue(entryCount, startPosition, entryTest.value);
51 }
52
GenerateEntryVector(int entryVectorCount,int entryCount,std::vector<Entry> & entrysTest)53 void GenerateEntryVector(int entryVectorCount, int entryCount, std::vector<Entry> &entrysTest)
54 {
55 if (entryVectorCount <= 0 || entryCount <= 0) {
56 return;
57 }
58 int i;
59 for (i = 0; i < entryVectorCount; i++) {
60 Entry entry;
61 GenerateEntry(entryCount, i, entry);
62 entrysTest.push_back(entry);
63 }
64 }
65
GenerateRecords(int recordNum,std::vector<Entry> & entries,std::vector<Key> & keys,int keySize,int valSize)66 void GenerateRecords(int recordNum, std::vector<Entry> &entries, std::vector<Key> &keys, int keySize, int valSize)
67 {
68 Entry entry;
69 // start from index 1
70 for (int recordIndex = 1; recordIndex <= recordNum; ++recordIndex) {
71 std::string cntStr = std::to_string(recordIndex);
72 int len = static_cast<int>(cntStr.length());
73 if (keySize <= len) {
74 break;
75 }
76 if (valSize <= len) {
77 break;
78 }
79
80 entry.key.assign((keySize - len), '0');
81 entry.value.assign((valSize - len), 'v');
82 for (auto item = cntStr.begin(); item != cntStr.end(); ++item) {
83 entry.key.push_back(*item);
84 entry.value.push_back(*item);
85 }
86 entries.push_back(entry);
87 keys.push_back(entry.key);
88
89 entry.key.clear();
90 entry.value.clear();
91 }
92 }
93 } // namespace DistributedDBUnitTest