YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/util/pg_wire.h
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_YQL_PGGATE_UTIL_PG_WIRE_H_
15
#define YB_YQL_PGGATE_UTIL_PG_WIRE_H_
16
17
#include <bitset>
18
#include "yb/util/slice.h"
19
20
namespace yb {
21
namespace pggate {
22
23
// This class represent how YugaByte sends data over the wire. See also file
24
// "yb/common/wire_protocol.proto".
25
//
26
// TODO(neil) Consider moving this file to "yb/common" directory and merging and organizing with
27
// the existing functions in "wire_protocol.*" accordingly.
28
class PgWire {
29
 public:
30
  //------------------------------------------------------------------------------------------------
31
  // Read Numeric Data
32
  template<typename num_type>
33
151M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
151M
    *value = reader(cursor->data());
35
151M
    return sizeof(num_type);
36
151M
  }
_ZN2yb6pggate6PgWire16ReadNumericValueItEEmPFT_PKvEPNS_5SliceEPS3_
Line
Count
Source
33
12.2M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
12.2M
    *value = reader(cursor->data());
35
12.2M
    return sizeof(num_type);
36
12.2M
  }
_ZN2yb6pggate6PgWire16ReadNumericValueIjEEmPFT_PKvEPNS_5SliceEPS3_
Line
Count
Source
33
103M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
103M
    *value = reader(cursor->data());
35
103M
    return sizeof(num_type);
36
103M
  }
_ZN2yb6pggate6PgWire16ReadNumericValueIyEEmPFT_PKvEPNS_5SliceEPS3_
Line
Count
Source
33
34.9M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
34.9M
    *value = reader(cursor->data());
35
34.9M
    return sizeof(num_type);
36
34.9M
  }
37
38
  static size_t ReadNumber(Slice *cursor, bool *value);
39
  static size_t ReadNumber(Slice *cursor, uint8 *value);
40
  static size_t ReadNumber(Slice *cursor, int8 *value);
41
  static size_t ReadNumber(Slice *cursor, uint16 *value);
42
  static size_t ReadNumber(Slice *cursor, int16 *value);
43
  static size_t ReadNumber(Slice *cursor, uint32 *value);
44
  static size_t ReadNumber(Slice *cursor, int32 *value);
45
  static size_t ReadNumber(Slice *cursor, uint64 *value);
46
  static size_t ReadNumber(Slice *cursor, int64 *value);
47
  static size_t ReadNumber(Slice *cursor, float *value);
48
  static size_t ReadNumber(Slice *cursor, double *value);
49
50
  // Read Text Data
51
  static size_t ReadBytes(Slice *cursor, char *value, int64_t bytes);
52
  static size_t ReadString(Slice *cursor, std::string *value, int64_t bytes);
53
54
  //------------------------------------------------------------------------------------------------
55
  // Write Numeric Data
56
  template<typename num_type>
57
154M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
154M
    num_type bytes;
59
154M
    writer(&bytes, value);
60
154M
    buffer->append(&bytes, sizeof(num_type));
61
154M
  }
_ZN2yb6pggate6PgWire8WriteIntItEEvPFvPvT_ES4_PNS_10faststringE
Line
Count
Source
57
12.2M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
12.2M
    num_type bytes;
59
12.2M
    writer(&bytes, value);
60
12.2M
    buffer->append(&bytes, sizeof(num_type));
61
12.2M
  }
_ZN2yb6pggate6PgWire8WriteIntIjEEvPFvPvT_ES4_PNS_10faststringE
Line
Count
Source
57
104M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
104M
    num_type bytes;
59
104M
    writer(&bytes, value);
60
104M
    buffer->append(&bytes, sizeof(num_type));
61
104M
  }
_ZN2yb6pggate6PgWire8WriteIntIyEEvPFvPvT_ES4_PNS_10faststringE
Line
Count
Source
57
38.2M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
38.2M
    num_type bytes;
59
38.2M
    writer(&bytes, value);
60
38.2M
    buffer->append(&bytes, sizeof(num_type));
61
38.2M
  }
62
63
  static void WriteBool(bool value, faststring *buffer);
64
  static void WriteInt8(int8_t value, faststring *buffer);
65
  static void WriteUint8(uint8_t value, faststring *buffer);
66
  static void WriteUint16(uint16_t value, faststring *buffer);
67
  static void WriteInt16(int16_t value, faststring *buffer);
68
  static void WriteUint32(uint32_t value, faststring *buffer);
69
  static void WriteInt32(int32_t value, faststring *buffer);
70
  static void WriteUint64(uint64_t value, faststring *buffer);
71
  static void WriteInt64(int64_t value, faststring *buffer);
72
  static void WriteFloat(float value, faststring *buffer);
73
  static void WriteDouble(double value, faststring *buffer);
74
75
  // Write Text Data
76
  static void WriteText(const std::string& value, faststring *buffer);
77
78
  // Write Text Data
79
  static void WriteBinary(const std::string& value, faststring *buffer);
80
};
81
82
// Just in case we change the serialization format. Different versions of DocDB and Postgres
83
// support can work with multiple data formats.
84
// - Rolling upgrade will need this.
85
// - TODO(neil) yb_client should have information on what pg_format_version should be used.
86
// GFLAG for CurrentDataFormatVersion;
87
88
// Data Header on the wire.
89
// We'll use one byte to represent column metadata.
90
//   Bit 0x01 - NULL indicator
91
//   Bit 0x02 - unused
92
//   Bit 0x04 - unused
93
//   Bit 0x08 - unused
94
//   ....
95
class PgWireDataHeader {
96
 public:
97
228M
  PgWireDataHeader() {
98
228M
  }
99
100
228M
  explicit PgWireDataHeader(uint8_t val) : data_(val) {
101
228M
  }
102
103
20.8M
  void set_null() {
104
20.8M
    data_[0] = 1;
105
20.8M
  }
106
228M
  bool is_null() const {
107
228M
    return data_[0] == 1;
108
228M
  }
109
110
228M
  uint8_t ToUint8() const {
111
228M
    return static_cast<uint8_t>(data_.to_ulong());
112
228M
  }
113
114
 private:
115
  std::bitset<8> data_;
116
};
117
118
}  // namespace pggate
119
}  // namespace yb
120
121
#endif // YB_YQL_PGGATE_UTIL_PG_WIRE_H_