/Users/deen/code/yugabyte-db/src/yb/yql/pggate/util/pg_wire.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. 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 distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | //-------------------------------------------------------------------------------------------------- |
15 | | |
16 | | #include "yb/yql/pggate/util/pg_wire.h" |
17 | | |
18 | | #include "yb/gutil/endian.h" |
19 | | |
20 | | namespace yb { |
21 | | namespace pggate { |
22 | | |
23 | 36.6M | void PgWire::WriteBool(bool value, faststring *buffer) { |
24 | 36.6M | buffer->append(&value, sizeof(bool)); |
25 | 36.6M | } |
26 | | |
27 | 21.1M | void PgWire::WriteInt8(int8_t value, faststring *buffer) { |
28 | 21.1M | buffer->append(&value, sizeof(int8_t)); |
29 | 21.1M | } |
30 | | |
31 | 228M | void PgWire::WriteUint8(uint8_t value, faststring *buffer) { |
32 | 228M | buffer->append(&value, sizeof(uint8_t)); |
33 | 228M | } |
34 | | |
35 | 0 | void PgWire::WriteUint16(uint16_t value, faststring *buffer) { |
36 | 0 | WriteInt(NetworkByteOrder::Store16, value, buffer); |
37 | 0 | } |
38 | | |
39 | 12.2M | void PgWire::WriteInt16(int16_t value, faststring *buffer) { |
40 | 12.2M | WriteInt(NetworkByteOrder::Store16, static_cast<uint16>(value), buffer); |
41 | 12.2M | } |
42 | | |
43 | 49.8M | void PgWire::WriteUint32(uint32_t value, faststring *buffer) { |
44 | 49.8M | WriteInt(NetworkByteOrder::Store32, value, buffer); |
45 | 49.8M | } |
46 | | |
47 | 53.6M | void PgWire::WriteInt32(int32_t value, faststring *buffer) { |
48 | 53.6M | WriteInt(NetworkByteOrder::Store32, static_cast<uint32>(value), buffer); |
49 | 53.6M | } |
50 | | |
51 | 0 | void PgWire::WriteUint64(uint64_t value, faststring *buffer) { |
52 | 0 | WriteInt(NetworkByteOrder::Store64, value, buffer); |
53 | 0 | } |
54 | | |
55 | 5.12M | void PgWire::WriteInt64(int64_t value, faststring *buffer) { |
56 | 5.12M | WriteInt(NetworkByteOrder::Store64, static_cast<uint64>(value), buffer); |
57 | 5.12M | } |
58 | | |
59 | 694k | void PgWire::WriteFloat(float value, faststring *buffer) { |
60 | 694k | const uint32 int_value = *reinterpret_cast<const uint32*>(&value); |
61 | 694k | WriteInt(NetworkByteOrder::Store32, int_value, buffer); |
62 | 694k | } |
63 | | |
64 | 60.8k | void PgWire::WriteDouble(double value, faststring *buffer) { |
65 | 60.8k | const uint64 int_value = *reinterpret_cast<const uint64*>(&value); |
66 | 60.8k | WriteInt(NetworkByteOrder::Store64, int_value, buffer); |
67 | 60.8k | } |
68 | | |
69 | 13.9M | void PgWire::WriteText(const std::string& value, faststring *buffer) { |
70 | | // Postgres expected text string to be null-terminated, so we have to add '\0' here. |
71 | | // Postgres will call strlen() without using the returning byte count. |
72 | 13.9M | const uint64 length = value.size() + 1; |
73 | 13.9M | WriteInt(NetworkByteOrder::Store64, length, buffer); |
74 | 13.9M | buffer->append(static_cast<const void *>(value.c_str()), length); |
75 | 13.9M | } |
76 | | |
77 | 19.1M | void PgWire::WriteBinary(const std::string& value, faststring *buffer) { |
78 | 19.1M | const uint64 length = value.size(); |
79 | 19.1M | WriteInt(NetworkByteOrder::Store64, length, buffer); |
80 | 19.1M | buffer->append(value); |
81 | 19.1M | } |
82 | | |
83 | | //-------------------------------------------------------------------------------------------------- |
84 | | // Read numbers. |
85 | | |
86 | | // This is not called ReadBool but ReadNumber because it is invoked from the TranslateNumber |
87 | | // template function similarly to the rest of numeric types. |
88 | 36.6M | size_t PgWire::ReadNumber(Slice *cursor, bool *value) { |
89 | 36.6M | *value = !!*reinterpret_cast<const bool*>(cursor->data()); |
90 | 36.6M | return sizeof(bool); |
91 | 36.6M | } |
92 | | |
93 | 21.1M | size_t PgWire::ReadNumber(Slice *cursor, int8_t *value) { |
94 | 21.1M | *value = *reinterpret_cast<const int8_t*>(cursor->data()); |
95 | 21.1M | return sizeof(int8_t); |
96 | 21.1M | } |
97 | | |
98 | 228M | size_t PgWire::ReadNumber(Slice *cursor, uint8_t *value) { |
99 | 228M | *value = *reinterpret_cast<const uint8*>(cursor->data()); |
100 | 228M | return sizeof(uint8_t); |
101 | 228M | } |
102 | | |
103 | 0 | size_t PgWire::ReadNumber(Slice *cursor, uint16 *value) { |
104 | 0 | return ReadNumericValue(NetworkByteOrder::Load16, cursor, value); |
105 | 0 | } |
106 | | |
107 | 12.2M | size_t PgWire::ReadNumber(Slice *cursor, int16 *value) { |
108 | 12.2M | return ReadNumericValue(NetworkByteOrder::Load16, cursor, reinterpret_cast<uint16*>(value)); |
109 | 12.2M | } |
110 | | |
111 | 49.8M | size_t PgWire::ReadNumber(Slice *cursor, uint32 *value) { |
112 | 49.8M | return ReadNumericValue(NetworkByteOrder::Load32, cursor, value); |
113 | 49.8M | } |
114 | | |
115 | 53.3M | size_t PgWire::ReadNumber(Slice *cursor, int32 *value) { |
116 | 53.3M | return ReadNumericValue(NetworkByteOrder::Load32, cursor, reinterpret_cast<uint32*>(value)); |
117 | 53.3M | } |
118 | | |
119 | 0 | size_t PgWire::ReadNumber(Slice *cursor, uint64 *value) { |
120 | 0 | return ReadNumericValue(NetworkByteOrder::Load64, cursor, value); |
121 | 0 | } |
122 | | |
123 | 34.8M | size_t PgWire::ReadNumber(Slice *cursor, int64 *value) { |
124 | 34.8M | return ReadNumericValue(NetworkByteOrder::Load64, cursor, reinterpret_cast<uint64*>(value)); |
125 | 34.8M | } |
126 | | |
127 | 694k | size_t PgWire::ReadNumber(Slice *cursor, float *value) { |
128 | 694k | uint32 int_value; |
129 | 694k | size_t read_size = ReadNumericValue(NetworkByteOrder::Load32, cursor, &int_value); |
130 | 694k | *value = *reinterpret_cast<float*>(&int_value); |
131 | 694k | return read_size; |
132 | 694k | } |
133 | | |
134 | 60.8k | size_t PgWire::ReadNumber(Slice *cursor, double *value) { |
135 | 60.8k | uint64 int_value; |
136 | 60.8k | size_t read_size = ReadNumericValue(NetworkByteOrder::Load64, cursor, &int_value); |
137 | 60.8k | *value = *reinterpret_cast<double*>(&int_value); |
138 | 60.8k | return read_size; |
139 | 60.8k | } |
140 | | |
141 | | // Read Text Data |
142 | 0 | size_t PgWire::ReadBytes(Slice *cursor, char *value, int64_t bytes) { |
143 | 0 | memcpy(value, cursor->data(), bytes); |
144 | 0 | return bytes; |
145 | 0 | } |
146 | | |
147 | | // Read Text data into string |
148 | 8.15k | size_t PgWire::ReadString(Slice *cursor, std::string *value, int64_t bytes) { |
149 | 8.15k | value->assign(cursor->cdata(), bytes); |
150 | 8.15k | return bytes; |
151 | 8.15k | } |
152 | | |
153 | | } // namespace pggate |
154 | | } // namespace yb |