YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/row_key-util-test.cc
Line
Count
Source
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
33
#include <gtest/gtest.h>
34
35
#include "yb/common/partial_row.h"
36
#include "yb/common/row.h"
37
#include "yb/common/row_key-util.h"
38
#include "yb/common/schema.h"
39
40
#include "yb/gutil/mathlimits.h"
41
42
#include "yb/util/memory/arena.h"
43
#include "yb/util/test_util.h"
44
45
namespace yb {
46
47
class RowKeyUtilTest : public YBTest {
48
 public:
49
  RowKeyUtilTest()
50
4
    : arena_(1024, 4096) {}
51
52
 protected:
53
4
  uint8_t* row_data(YBPartialRow* row) {
54
4
    return row->row_data_;
55
4
  }
56
57
  Arena arena_;
58
};
59
60
1
TEST_F(RowKeyUtilTest, TestIncrementNonCompositeKey) {
61
1
  Schema schema({ ColumnSchema("key", INT32),
62
1
                  ColumnSchema("other_col", INT32),
63
1
                  ColumnSchema("other_col2", STRING, true) },
64
1
                1);
65
1
  YBPartialRow p_row(&schema);
66
1
  ContiguousRow row(&schema, row_data(&p_row));
67
68
  // Normal increment.
69
1
  EXPECT_OK(p_row.SetInt32(0, 1000));
70
1
  EXPECT_TRUE(row_key_util::IncrementKey(&row, &arena_));
71
1
  EXPECT_EQ("int32 key=1001", p_row.ToString());
72
73
  // Overflow increment.
74
1
  EXPECT_OK(p_row.SetInt32(0, MathLimits<int32_t>::kMax));
75
1
  EXPECT_FALSE(row_key_util::IncrementKey(&row, &arena_));
76
1
  EXPECT_EQ("int32 key=-2147483648", p_row.ToString());
77
1
}
78
79
1
TEST_F(RowKeyUtilTest, TestIncrementCompositeKey) {
80
1
  Schema schema({ ColumnSchema("k1", INT32),
81
1
                  ColumnSchema("k2", INT32),
82
1
                  ColumnSchema("other_col", STRING, true) },
83
1
                2);
84
85
1
  YBPartialRow p_row(&schema);
86
1
  ContiguousRow row(&schema, row_data(&p_row));
87
88
  // Normal increment.
89
1
  EXPECT_OK(p_row.SetInt32(0, 1000));
90
1
  EXPECT_OK(p_row.SetInt32(1, 1000));
91
1
  EXPECT_TRUE(row_key_util::IncrementKey(&row, &arena_));
92
1
  EXPECT_EQ("int32 k1=1000, int32 k2=1001", p_row.ToString());
93
94
  // Overflow a later part of the key, carrying into the earlier
95
  // part..
96
1
  EXPECT_OK(p_row.SetInt32(1, MathLimits<int32_t>::kMax));
97
1
  EXPECT_TRUE(row_key_util::IncrementKey(&row, &arena_));
98
1
  EXPECT_EQ("int32 k1=1001, int32 k2=-2147483648", p_row.ToString());
99
100
  // Overflow the whole key.
101
1
  EXPECT_OK(p_row.SetInt32(0, MathLimits<int32_t>::kMax));
102
1
  EXPECT_OK(p_row.SetInt32(1, MathLimits<int32_t>::kMax));
103
1
  EXPECT_FALSE(row_key_util::IncrementKey(&row, &arena_));
104
1
  EXPECT_EQ("int32 k1=-2147483648, int32 k2=-2147483648", p_row.ToString());
105
1
}
106
107
1
TEST_F(RowKeyUtilTest, TestIncrementCompositeIntStringKey) {
108
1
  Schema schema({ ColumnSchema("k1", INT32),
109
1
                  ColumnSchema("k2", STRING),
110
1
                  ColumnSchema("other_col", STRING, true) },
111
1
                2);
112
113
1
  YBPartialRow p_row(&schema);
114
1
  ContiguousRow row(&schema, row_data(&p_row));
115
116
  // Normal increment.
117
1
  EXPECT_OK(p_row.SetInt32(0, 1000));
118
1
  EXPECT_OK(p_row.SetString(1, "hello"));
119
1
  EXPECT_TRUE(row_key_util::IncrementKey(&row, &arena_));
120
1
  EXPECT_EQ("int32 k1=1000, string k2=hello\\000", p_row.ToString());
121
122
  // There's no way to overflow a string key - you can always make it higher
123
  // by tacking on more \x00.
124
1
  EXPECT_TRUE(row_key_util::IncrementKey(&row, &arena_));
125
1
  EXPECT_EQ("int32 k1=1000, string k2=hello\\000\\000", p_row.ToString());
126
1
}
127
128
1
TEST_F(RowKeyUtilTest, TestIncrementCompositeStringIntKey) {
129
1
  Schema schema({ ColumnSchema("k1", STRING),
130
1
                  ColumnSchema("k2", INT32),
131
1
                  ColumnSchema("other_col", STRING, true) },
132
1
                2);
133
134
1
  YBPartialRow p_row(&schema);
135
1
  ContiguousRow row(&schema, row_data(&p_row));
136
137
  // Normal increment.
138
1
  EXPECT_OK(p_row.SetString(0, "hello"));
139
1
  EXPECT_OK(p_row.SetInt32(1, 1000));
140
1
  EXPECT_TRUE(row_key_util::IncrementKey(&row, &arena_));
141
1
  EXPECT_EQ("string k1=hello, int32 k2=1001", p_row.ToString());
142
143
  // Overflowing the int32 portion should tack \x00 onto the
144
  // string portion.
145
1
  EXPECT_OK(p_row.SetInt32(1, MathLimits<int32_t>::kMax));
146
1
  EXPECT_TRUE(row_key_util::IncrementKey(&row, &arena_));
147
1
  EXPECT_EQ("string k1=hello\\000, int32 k2=-2147483648", p_row.ToString());
148
1
}
149
150
151
152
153
} // namespace yb