YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/block_internal.h
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
#ifndef YB_ROCKSDB_TABLE_BLOCK_INTERNAL_H
17
#define YB_ROCKSDB_TABLE_BLOCK_INTERNAL_H
18
19
#include "yb/util/logging.h"
20
#include "yb/util/std_util.h"
21
22
namespace rocksdb {
23
24
inline const char* DecodeVarint32Ptr(
25
1.17M
    const char* p, const char* limit, uint32_t* value, const bool is_non_zero) {
26
1.17M
  if (is_non_zero) {
27
217k
    return GetVarint32Ptr(p, limit, value);
28
217k
  }
29
954k
  *value = 0;
30
954k
  return p;
31
1.17M
}
32
33
inline const char* DecodeSignedVarint64Ptr(
34
    const char* p, const char* limit, const char* read_allowed_from, int64_t* value,
35
2.34M
    const bool is_non_zero) {
36
2.34M
  if (is_non_zero) {
37
348k
    return GetSignedVarint64Ptr(p, limit, read_allowed_from, value);
38
348k
  }
39
1.99M
  *value = 0;
40
1.99M
  return p;
41
2.34M
}
42
43
// This function decodes key components sizes, flags and value size starting at p.
44
// limit specifies exclusive upper bound on where we allowed to decode from.
45
// read_allowed_from specifies exclusive upper bound on where we allowed to read data from (used
46
// for performance optimization in some cases to read by multi-bytes chunks), but
47
// still only data before limit will be used for decoding.
48
//
49
// Parameters after read_allowed_from are the output parameters filled by decoded values.
50
// We don't use struct here because of performance reasons, since this function is in hot path.
51
//
52
// See EncodeThreeSharedPartsSizes inside block_builder.cc for the sizes & flags encoding
53
// format description.
54
//
55
// Returns either pointer to the next byte to read (containing key/value data) or nullptr in case
56
// of decoding failure or corruption.
57
inline const char* DecodeEntryThreeSharedParts(
58
    const char* p, const char* limit, const char* read_allowed_from, uint32_t* shared_prefix_size,
59
    uint32_t* non_shared_1_size, int64_t* non_shared_1_size_delta, bool* is_something_shared,
60
    uint32_t* non_shared_2_size, int64_t* non_shared_2_size_delta,
61
    uint32_t* shared_last_component_size, uint64_t* shared_last_component_increase,
62
23.9M
    uint32_t* value_size) {
63
23.9M
  if (limit - p < 2) {
64
0
    return nullptr;
65
0
  }
66
67
23.9M
  uint64_t encoded_1;
68
23.9M
  if ((p = GetVarint64Ptr(p, limit, &encoded_1)) == nullptr) {
69
0
    return nullptr;
70
0
  }
71
23.9M
  *value_size = static_cast<uint32_t>(encoded_1 >> 2);
72
23.9M
  *shared_last_component_increase = (encoded_1 & 2) << 7;
73
23.9M
  const bool is_frequent_case_1 = encoded_1 & 1;
74
75
23.9M
  if (PREDICT_TRUE(is_frequent_case_1)) {
76
0
    p = GetVarint32Ptr(p, limit, shared_prefix_size);
77
0
    if (PREDICT_FALSE(!p)) {
78
0
      return nullptr;
79
0
    }
80
0
    *shared_last_component_size = kLastInternalComponentSize;
81
0
    *is_something_shared = true;
82
0
    *non_shared_1_size = 1;
83
0
    *non_shared_1_size_delta = 0;
84
0
    *non_shared_2_size = 1;
85
0
    *non_shared_2_size_delta = 0;
86
23.9M
  } else {
87
23.9M
    const uint8_t encoded_2 = *pointer_cast<const uint8_t*>(p++);
88
23.9M
    if ((encoded_2 & 1) == 0) {
89
22.7M
      
DVLOG_WITH_FUNC0
(4) << "encoded_2: " << static_cast<uint16_t>(encoded_2)0
;
90
22.7M
      *is_something_shared = false;
91
22.7M
      if (encoded_2 == 0) {
92
668k
        p = GetVarint32Ptr(p, limit, non_shared_1_size);
93
668k
        if (PREDICT_FALSE(!p)) {
94
0
          return nullptr;
95
0
        }
96
22.1M
      } else {
97
22.1M
        *non_shared_1_size = encoded_2 >> 1;
98
22.1M
      }
99
22.7M
      *non_shared_1_size_delta = 0;
100
22.7M
      *non_shared_2_size = 0;
101
22.7M
      *non_shared_2_size_delta = 0;
102
22.7M
      *shared_last_component_size = 0;
103
22.7M
      *shared_prefix_size = 0;
104
22.7M
    } else {
105
1.18M
      *is_something_shared = true;
106
1.18M
      if ((encoded_2 & 2) == 0) {
107
11.3k
        
DVLOG_WITH_FUNC0
(4) << "encoded_2: " << static_cast<uint16_t>(encoded_2)0
;
108
11.3k
        *shared_last_component_size = kLastInternalComponentSize;
109
11.3k
        *non_shared_1_size_delta = 0;
110
11.3k
        *non_shared_2_size_delta = (encoded_2 >> 2) & 1;
111
11.3k
        *non_shared_1_size = (encoded_2 >> 3) & 7;
112
11.3k
        *non_shared_2_size = (encoded_2 >> 6) & 3;
113
1.17M
      } else {
114
1.17M
        
DVLOG_WITH_FUNC0
(4) << "encoded_2: " << static_cast<uint16_t>(encoded_2)0
;
115
1.17M
        *shared_last_component_size = (encoded_2 & 4) ? 
kLastInternalComponentSize101k
:
01.07M
;
116
1.17M
        p = GetVarint32Ptr(p, limit, non_shared_1_size);
117
1.17M
        if (PREDICT_FALSE(!p)) {
118
0
          return nullptr;
119
0
        }
120
1.17M
        p = DecodeSignedVarint64Ptr(
121
1.17M
            p, limit, read_allowed_from, non_shared_1_size_delta, (encoded_2 & 8) != 0);
122
1.17M
        if (PREDICT_FALSE(!p)) {
123
0
          return nullptr;
124
0
        }
125
1.17M
        p = DecodeVarint32Ptr(p, limit, non_shared_2_size, (encoded_2 & 16) != 0);
126
1.17M
        if (PREDICT_FALSE(!p)) {
127
0
          return nullptr;
128
0
        }
129
1.17M
        p = DecodeSignedVarint64Ptr(
130
1.17M
            p, limit, read_allowed_from, non_shared_2_size_delta, (encoded_2 & 32) != 0);
131
1.17M
        if (PREDICT_FALSE(!p)) {
132
0
          return nullptr;
133
0
        }
134
1.17M
      }
135
1.18M
      p = GetVarint32Ptr(p, limit, shared_prefix_size);
136
1.18M
      if (PREDICT_FALSE(!p)) {
137
0
        return nullptr;
138
0
      }
139
1.18M
    }
140
23.9M
  }
141
142
23.9M
  if (PREDICT_FALSE(yb::std_util::cmp_less(
143
23.9M
          limit - p, *non_shared_1_size + *non_shared_2_size + *value_size))) {
144
0
    return nullptr;
145
0
  }
146
23.9M
  return p;
147
23.9M
}
148
149
}  // namespace rocksdb
150
151
#endif // YB_ROCKSDB_TABLE_BLOCK_INTERNAL_H