/Users/deen/code/yugabyte-db/src/yb/tools/data_gen_util.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #include "yb/tools/data_gen_util.h" |
33 | | |
34 | | #include "yb/client/schema.h" |
35 | | #include "yb/common/ql_protocol.pb.h" |
36 | | #include "yb/common/ql_type.h" |
37 | | |
38 | | #include "yb/gutil/casts.h" |
39 | | |
40 | | #include "yb/util/random.h" |
41 | | #include "yb/util/status.h" |
42 | | |
43 | | namespace yb { |
44 | | namespace tools { |
45 | | |
46 | | void WriteValueToColumn(const client::YBSchema& schema, |
47 | | size_t col_idx, |
48 | | uint64_t value, |
49 | 0 | QLValuePB* out) { |
50 | 0 | DataType type = schema.Column(col_idx).type()->main(); |
51 | 0 | char buf[kFastToBufferSize]; |
52 | 0 | switch (type) { |
53 | 0 | case INT8: |
54 | 0 | out->set_int8_value(static_cast<int32_t>(value)); |
55 | 0 | return; |
56 | 0 | case INT16: |
57 | 0 | out->set_int16_value(static_cast<int32_t>(value)); |
58 | 0 | return; |
59 | 0 | case INT32: |
60 | 0 | out->set_int32_value(static_cast<int32_t>(value)); |
61 | 0 | return; |
62 | 0 | case INT64: |
63 | 0 | out->set_int64_value(value); |
64 | 0 | return; |
65 | 0 | case FLOAT: |
66 | 0 | out->set_float_value(value / 123.0); |
67 | 0 | return; |
68 | 0 | case DOUBLE: |
69 | 0 | out->set_double_value(value / 123.0); |
70 | 0 | return; |
71 | 0 | case STRING: |
72 | 0 | out->set_string_value(FastHex64ToBuffer(value, buf)); |
73 | 0 | return; |
74 | 0 | case BOOL: |
75 | 0 | out->set_bool_value(value); |
76 | 0 | return; |
77 | 0 | default: |
78 | 0 | LOG(FATAL) << "Unexpected data type: " << type; |
79 | 0 | } |
80 | 0 | FATAL_INVALID_ENUM_VALUE(DataType, type); |
81 | 0 | } |
82 | | |
83 | | void GenerateDataForRow(const client::YBSchema& schema, uint64_t record_id, |
84 | 0 | Random* random, QLWriteRequestPB* req) { |
85 | 0 | for (size_t col_idx = 0; col_idx < schema.num_columns(); col_idx++) { |
86 | | // We randomly generate the inserted data, except for the first column, |
87 | | // which is always based on a monotonic "record id". |
88 | 0 | uint64_t value; |
89 | 0 | if (col_idx == 0) { |
90 | 0 | value = record_id; |
91 | 0 | } else { |
92 | 0 | value = random->Next64(); |
93 | 0 | } |
94 | 0 | QLValuePB* out; |
95 | 0 | if (col_idx < schema.num_hash_key_columns()) { |
96 | 0 | out = req->add_hashed_column_values()->mutable_value(); |
97 | 0 | } else { |
98 | 0 | auto column_value = req->add_column_values(); |
99 | 0 | column_value->set_column_id(schema.ColumnId(col_idx)); |
100 | 0 | out = column_value->mutable_expr()->mutable_value(); |
101 | 0 | } |
102 | 0 | WriteValueToColumn(schema, col_idx, value, out); |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | } // namespace tools |
107 | | } // namespace yb |