YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
417M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
417M
    *value = reader(cursor->data());
35
417M
    return sizeof(num_type);
36
417M
  }
unsigned long yb::pggate::PgWire::ReadNumericValue<unsigned short>(unsigned short (*)(void const*), yb::Slice*, unsigned short*)
Line
Count
Source
33
37.6M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
37.6M
    *value = reader(cursor->data());
35
37.6M
    return sizeof(num_type);
36
37.6M
  }
unsigned long yb::pggate::PgWire::ReadNumericValue<unsigned int>(unsigned int (*)(void const*), yb::Slice*, unsigned int*)
Line
Count
Source
33
286M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
286M
    *value = reader(cursor->data());
35
286M
    return sizeof(num_type);
36
286M
  }
unsigned long yb::pggate::PgWire::ReadNumericValue<unsigned long long>(unsigned long long (*)(void const*), yb::Slice*, unsigned long long*)
Line
Count
Source
33
93.0M
  static size_t ReadNumericValue(num_type (*reader)(const void*), Slice *cursor, num_type *value) {
34
93.0M
    *value = reader(cursor->data());
35
93.0M
    return sizeof(num_type);
36
93.0M
  }
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
436M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
436M
    num_type bytes;
59
436M
    writer(&bytes, value);
60
436M
    buffer->append(&bytes, sizeof(num_type));
61
436M
  }
void yb::pggate::PgWire::WriteInt<unsigned short>(void (*)(void*, unsigned short), unsigned short, yb::faststring*)
Line
Count
Source
57
37.6M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
37.6M
    num_type bytes;
59
37.6M
    writer(&bytes, value);
60
37.6M
    buffer->append(&bytes, sizeof(num_type));
61
37.6M
  }
void yb::pggate::PgWire::WriteInt<unsigned int>(void (*)(void*, unsigned int), unsigned int, yb::faststring*)
Line
Count
Source
57
291M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
291M
    num_type bytes;
59
291M
    writer(&bytes, value);
60
291M
    buffer->append(&bytes, sizeof(num_type));
61
291M
  }
void yb::pggate::PgWire::WriteInt<unsigned long long>(void (*)(void*, unsigned long long), unsigned long long, yb::faststring*)
Line
Count
Source
57
107M
  static void WriteInt(void (*writer)(void *, num_type), num_type value, faststring *buffer) {
58
107M
    num_type bytes;
59
107M
    writer(&bytes, value);
60
107M
    buffer->append(&bytes, sizeof(num_type));
61
107M
  }
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
656M
  PgWireDataHeader() {
98
656M
  }
99
100
650M
  explicit PgWireDataHeader(uint8_t val) : data_(val) {
101
650M
  }
102
103
63.7M
  void set_null() {
104
63.7M
    data_[0] = 1;
105
63.7M
  }
106
650M
  bool is_null() const {
107
650M
    return data_[0] == 1;
108
650M
  }
109
110
657M
  uint8_t ToUint8() const {
111
657M
    return static_cast<uint8_t>(data_.to_ulong());
112
657M
  }
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_