This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
The following commit(s) were added to refs/heads/master by this push:
new 1ec8b20 [util] introduce comparison operators for Slice
1ec8b20 is described below
commit 1ec8b209e6b81768c56223f89931e4aed56b1f50
Author: Alexey Serbin <[email protected]>
AuthorDate: Thu Jul 29 18:26:37 2021 -0700
[util] introduce comparison operators for Slice
This patch introduces comparison operators for the Slice class.
In my opinion, that improves the readability of the code. Compare:
if (mid_key.compare(target) < 0) {
...
}
vs
if (mid_key < target) {
...
}
CHECK(prev_key.compare(enc_key) < 0);
vs
CHECK(prev_key < enc_key);
if (key_a.compare(key_b) == 0) {
...
}
vs
if (key_a == key_b) {
...
}
I also did other minor code modifications in the code around.
This patch doesn't contain any significant functional changes.
Change-Id: I995803246fb3c21d8fe043b882fc93a87d9c2619
Reviewed-on: http://gerrit.cloudera.org:8080/17740
Reviewed-by: Andrew Wong <[email protected]>
Tested-by: Alexey Serbin <[email protected]>
---
src/kudu/cfile/binary_plain_block.cc | 4 +-
src/kudu/cfile/binary_prefix_block.cc | 4 +-
src/kudu/cfile/cfile_util.cc | 2 +-
src/kudu/common/encoded_key.h | 3 +-
src/kudu/common/partition.cc | 6 +--
src/kudu/common/scan_spec.cc | 14 +++---
src/kudu/common/schema-test.cc | 2 +-
src/kudu/common/types.h | 6 +--
src/kudu/common/wire_protocol-test.cc | 5 +--
src/kudu/fs/fs_manager-test.cc | 2 +-
src/kudu/integration-tests/all_types-itest.cc | 2 +-
src/kudu/rpc/rpc-test-base.h | 4 +-
src/kudu/tablet/cbtree-test.cc | 4 +-
src/kudu/tablet/cfile_set.cc | 21 +++++----
src/kudu/tablet/deltamemstore-test.cc | 2 +-
src/kudu/tablet/diskrowset.cc | 7 +--
src/kudu/tablet/memrowset.h | 3 +-
src/kudu/tablet/rowset_info.cc | 18 ++++----
src/kudu/tablet/rowset_tree-test.cc | 2 +-
src/kudu/tablet/tablet.cc | 12 ++----
src/kudu/tserver/tablet_service.cc | 2 +-
src/kudu/util/memcmpable_varint-test.cc | 12 +++---
src/kudu/util/slice.h | 61 ++++++++++++++++++++++++---
23 files changed, 120 insertions(+), 78 deletions(-)
diff --git a/src/kudu/cfile/binary_plain_block.cc
b/src/kudu/cfile/binary_plain_block.cc
index 9782d24..ed7c641 100644
--- a/src/kudu/cfile/binary_plain_block.cc
+++ b/src/kudu/cfile/binary_plain_block.cc
@@ -271,8 +271,8 @@ Status BinaryPlainBlockDecoder::SeekAtOrAfterValue(const
void *value_void, bool
// Binary search in restart array to find the first restart point
// with a key >= target
- int32_t left = 0;
- int32_t right = num_elems_;
+ uint32_t left = 0;
+ uint32_t right = num_elems_;
while (left != right) {
uint32_t mid = (left + right) / 2;
Slice mid_key(string_at_index(mid));
diff --git a/src/kudu/cfile/binary_prefix_block.cc
b/src/kudu/cfile/binary_prefix_block.cc
index 21f405d..c79ed07 100644
--- a/src/kudu/cfile/binary_prefix_block.cc
+++ b/src/kudu/cfile/binary_prefix_block.cc
@@ -356,8 +356,8 @@ Status BinaryPrefixBlockDecoder::SeekAtOrAfterValue(const
void *value_void,
HexDump(Slice(entry, 16));
return Status::Corruption(err);
}
- Slice mid_key(key_ptr, non_shared);
- if (mid_key.compare(target) < 0) {
+ const Slice mid_key(key_ptr, non_shared);
+ if (mid_key < target) {
// Key at "mid" is smaller than "target". Therefore all
// blocks before "mid" are uninteresting.
left = mid;
diff --git a/src/kudu/cfile/cfile_util.cc b/src/kudu/cfile/cfile_util.cc
index c700e4e..087ea0a 100644
--- a/src/kudu/cfile/cfile_util.cc
+++ b/src/kudu/cfile/cfile_util.cc
@@ -141,7 +141,7 @@ size_t CommonPrefixLength(const Slice& slice_a, const
Slice& slice_b) {
}
void GetSeparatingKey(const Slice& left, Slice* right) {
- DCHECK_LE(left.compare(*right), 0);
+ DCHECK_LE(left, *right);
size_t cpl = CommonPrefixLength(left, *right);
right->truncate(cpl == right->size() ? cpl : cpl + 1);
}
diff --git a/src/kudu/common/encoded_key.h b/src/kudu/common/encoded_key.h
index 6e785a6..09d7299 100644
--- a/src/kudu/common/encoded_key.h
+++ b/src/kudu/common/encoded_key.h
@@ -72,8 +72,7 @@ class EncodedKey {
// The empty bound has special significance: it's both the lowest value
// (if in 'start') and the highest (if in 'end').
bool InRange(const Slice& start, const Slice& end) const {
- return (start.compare(encoded_key_) <= 0 &&
- (end.empty() || encoded_key_.compare(end) < 0));
+ return (start <= encoded_key_ && (end.empty() || encoded_key_ < end));
}
static std::string RangeToString(const EncodedKey* lower,
diff --git a/src/kudu/common/partition.cc b/src/kudu/common/partition.cc
index d97068a..57ba3ef 100644
--- a/src/kudu/common/partition.cc
+++ b/src/kudu/common/partition.cc
@@ -698,9 +698,9 @@ Status PartitionSchema::RangePartitionContainsRowImpl(const
Partition& partition
// If all of the hash buckets match, then the row is contained in the
// partition if the row is gte the lower bound; and if there is no upper
// bound, or the row is lt the upper bound.
- *contains = (Slice(range_partition_key).compare(partition.range_key_start())
>= 0)
- && (partition.range_key_end().empty()
- ||
Slice(range_partition_key).compare(partition.range_key_end()) < 0);
+ const auto key = Slice(range_partition_key);
+ *contains = (partition.range_key_start() <= key) &&
+ (partition.range_key_end().empty() || key < partition.range_key_end());
return Status::OK();
}
diff --git a/src/kudu/common/scan_spec.cc b/src/kudu/common/scan_spec.cc
index e82714f..f029f99 100644
--- a/src/kudu/common/scan_spec.cc
+++ b/src/kudu/common/scan_spec.cc
@@ -77,9 +77,8 @@ bool ScanSpec::CanShortCircuit() const {
return true;
}
- if (lower_bound_key_ &&
- exclusive_upper_bound_key_ &&
-
lower_bound_key_->encoded_key().compare(exclusive_upper_bound_key_->encoded_key())
>= 0) {
+ if (lower_bound_key_ && exclusive_upper_bound_key_ &&
+ lower_bound_key_->encoded_key() >=
exclusive_upper_bound_key_->encoded_key()) {
return true;
}
@@ -100,27 +99,28 @@ bool ScanSpec::ContainsBloomFilterPredicate() const {
void ScanSpec::SetLowerBoundKey(const EncodedKey* key) {
if (lower_bound_key_ == nullptr ||
- key->encoded_key().compare(lower_bound_key_->encoded_key()) > 0) {
+ lower_bound_key_->encoded_key() < key->encoded_key()) {
lower_bound_key_ = key;
}
}
void ScanSpec::SetExclusiveUpperBoundKey(const EncodedKey* key) {
if (exclusive_upper_bound_key_ == nullptr ||
- key->encoded_key().compare(exclusive_upper_bound_key_->encoded_key()) <
0) {
+ key->encoded_key() < exclusive_upper_bound_key_->encoded_key()) {
exclusive_upper_bound_key_ = key;
}
}
void ScanSpec::SetLowerBoundPartitionKey(const Slice& partition_key) {
- if (partition_key.compare(lower_bound_partition_key_) > 0) {
+ if (lower_bound_partition_key_ < partition_key) {
lower_bound_partition_key_ = partition_key.ToString();
}
}
void ScanSpec::SetExclusiveUpperBoundPartitionKey(const Slice& partition_key) {
if (exclusive_upper_bound_partition_key_.empty() ||
- (!partition_key.empty() &&
partition_key.compare(exclusive_upper_bound_partition_key_) < 0)) {
+ (!partition_key.empty() &&
+ partition_key < exclusive_upper_bound_partition_key_)) {
exclusive_upper_bound_partition_key_ = partition_key.ToString();
}
}
diff --git a/src/kudu/common/schema-test.cc b/src/kudu/common/schema-test.cc
index d71fbb6..79fad9b 100644
--- a/src/kudu/common/schema-test.cc
+++ b/src/kudu/common/schema-test.cc
@@ -555,7 +555,7 @@ TEST(TestKeyEncoder, TestKeyEncoder) {
encoder.Encode(&in[col], col == in.size() - 1, &fs);
}
- ASSERT_EQ(0, expected.compare(Slice(fs)))
+ ASSERT_EQ(expected, Slice(fs))
<< "Failed encoding example " << i << ".\n"
<< "Expected: " << HexDump(expected) << "\n"
<< "Got: " << HexDump(Slice(fs));
diff --git a/src/kudu/common/types.h b/src/kudu/common/types.h
index d13c0c8..81eaaec 100644
--- a/src/kudu/common/types.h
+++ b/src/kudu/common/types.h
@@ -458,9 +458,9 @@ struct DataTypeTraits<BINARY> {
// Strings are consecutive if the larger is equal to the lesser with an
// additional null byte.
- return a_size + 1 == b_size
- && (*b_slice)[a_size] == 0
- && a_slice->compare(Slice(b_slice->data(), a_size)) == 0;
+ return a_size + 1 == b_size &&
+ (*b_slice)[a_size] == 0 &&
+ *a_slice == Slice(b_slice->data(), a_size);
}
static const cpp_type* min_value() {
static Slice s("");
diff --git a/src/kudu/common/wire_protocol-test.cc
b/src/kudu/common/wire_protocol-test.cc
index 51e5bb3..8cff3d3 100644
--- a/src/kudu/common/wire_protocol-test.cc
+++ b/src/kudu/common/wire_protocol-test.cc
@@ -430,9 +430,8 @@ TEST_F(WireProtocolTest,
TestColumnarRowBlockToPBWithPadding) {
// 'col1' comes at 0 bytes offset in the projection schema.
const Slice* col1 = reinterpret_cast<const Slice*>(base_data);
- ASSERT_EQ(col1->compare(Slice("hello world col1")), 0) << "Unexpected val
for the "
- << i << "th row:"
- <<
col1->ToDebugString();
+ ASSERT_EQ(Slice("hello world col1"), *col1)
+ << "Unexpected val for the " << i << "th row:" <<
col1->ToDebugString();
// 'key' comes at 16 bytes offset.
const int64_t key = *reinterpret_cast<const int64_t*>(base_data + 16);
EXPECT_EQ(key, i);
diff --git a/src/kudu/fs/fs_manager-test.cc b/src/kudu/fs/fs_manager-test.cc
index 0c2d1c3..44ca395 100644
--- a/src/kudu/fs/fs_manager-test.cc
+++ b/src/kudu/fs/fs_manager-test.cc
@@ -128,7 +128,7 @@ class FsManagerTestBase : public KuduTest,
unique_ptr<ReadableBlock> reader;
ASSERT_OK(fs_manager()->OpenBlock(writer->id(), &reader));
ASSERT_OK(reader->Read(0, result));
- ASSERT_EQ(0, result.compare(data));
+ ASSERT_EQ(data, result);
}
FsManager *fs_manager() const { return fs_manager_.get(); }
diff --git a/src/kudu/integration-tests/all_types-itest.cc
b/src/kudu/integration-tests/all_types-itest.cc
index 193995e..f3f55a9 100644
--- a/src/kudu/integration-tests/all_types-itest.cc
+++ b/src/kudu/integration-tests/all_types-itest.cc
@@ -125,7 +125,7 @@ struct SliceKeysTestSetup {
int expected_row_key_num = (split_idx * increment_) + row_idx;
string expected_row_key = StringPrintf("%08x", expected_row_key_num);
Slice expected_row_key_slice(expected_row_key);
- if (expected_row_key_slice.compare(row_key_slice) != 0) {
+ if (expected_row_key_slice != row_key_slice) {
return Status::Corruption(strings::Substitute("Keys didn't match.
Expected: $0 Got: $1",
expected_row_key_slice.ToDebugString(),
row_key_slice.ToDebugString()));
diff --git a/src/kudu/rpc/rpc-test-base.h b/src/kudu/rpc/rpc-test-base.h
index 484f3f6..4a87b0f 100644
--- a/src/kudu/rpc/rpc-test-base.h
+++ b/src/kudu/rpc/rpc-test-base.h
@@ -503,11 +503,11 @@ class RpcTestBase : public KuduTest {
expected.resize(size1);
RandomString(expected.data(), size1, &rng);
- CHECK_EQ(0, first.compare(Slice(expected)));
+ CHECK_EQ(Slice(expected), first);
expected.resize(size2);
RandomString(expected.data(), size2, &rng);
- CHECK_EQ(0, second.compare(Slice(expected)));
+ CHECK_EQ(Slice(expected), second);
}
static Status DoTestOutgoingSidecar(const Proxy &p, int size1, int size2) {
diff --git a/src/kudu/tablet/cbtree-test.cc b/src/kudu/tablet/cbtree-test.cc
index 9352af6..7a8c3a4 100644
--- a/src/kudu/tablet/cbtree-test.cc
+++ b/src/kudu/tablet/cbtree-test.cc
@@ -234,7 +234,7 @@ void VerifyGet(const CBTree<T> &tree,
<< "Failed on key " << HexDump(key);
Slice got_val(vbuf, len);
- ASSERT_EQ(0, expected_val.compare(got_val))
+ ASSERT_EQ(expected_val, got_val)
<< "Failure!\n"
<< "Expected: " << HexDump(expected_val)
<< "Got: " << HexDump(got_val);
@@ -676,7 +676,7 @@ static void ScanThread(Barrier *go_barrier,
Slice k, v;
iter->GetCurrentEntry(&k, &v);
- if (k.compare(Slice(prev_key)) <= 0) {
+ if (k <= Slice(prev_key)) {
FAIL() << "prev key " << Slice(prev_key).ToString() <<
" wasn't less than cur key " << k.ToString();
}
diff --git a/src/kudu/tablet/cfile_set.cc b/src/kudu/tablet/cfile_set.cc
index 8c09ba9..f9899e4 100644
--- a/src/kudu/tablet/cfile_set.cc
+++ b/src/kudu/tablet/cfile_set.cc
@@ -171,7 +171,7 @@ Status CFileSet::DoOpen(const IOContext* io_context) {
RETURN_NOT_OK(LoadMinMaxKeys(io_context));
}
// Verify the loaded keys are valid.
- if (Slice(min_encoded_key_).compare(max_encoded_key_) > 0) {
+ if (Slice(min_encoded_key_) > max_encoded_key_) {
return Status::Corruption(Substitute("Min key $0 > max key $1",
KUDU_REDACT(Slice(min_encoded_key_).ToDebugString()),
KUDU_REDACT(Slice(max_encoded_key_).ToDebugString())),
@@ -426,10 +426,10 @@ Status
CFileSet::Iterator::PushdownRangeScanPredicate(ScanSpec *spec) {
key_schema_for_vlog = base_data_->tablet_schema().CreateKeyProjection();
}
- if (spec->lower_bound_key() &&
-
spec->lower_bound_key()->encoded_key().compare(base_data_->min_encoded_key_) >
0) {
+ const auto* lb_key = spec->lower_bound_key();
+ if (lb_key && lb_key->encoded_key() > base_data_->min_encoded_key_) {
bool exact;
- Status s = key_iter_->SeekAtOrAfter(*spec->lower_bound_key(), &exact);
+ Status s = key_iter_->SeekAtOrAfter(*lb_key, &exact);
if (s.IsNotFound()) {
// The lower bound is after the end of the key range.
// Thus, no rows will pass the predicate, so we set the lower bound
@@ -441,21 +441,20 @@ Status
CFileSet::Iterator::PushdownRangeScanPredicate(ScanSpec *spec) {
lower_bound_idx_ = std::max(lower_bound_idx_,
key_iter_->GetCurrentOrdinal());
VLOG(1) << "Pushed lower bound value "
- << spec->lower_bound_key()->Stringify(key_schema_for_vlog)
+ << lb_key->Stringify(key_schema_for_vlog)
<< " as row_idx >= " << lower_bound_idx_;
}
- if (spec->exclusive_upper_bound_key() &&
- spec->exclusive_upper_bound_key()->encoded_key().compare(
- base_data_->max_encoded_key_) <= 0) {
+ const auto* ub_key = spec->exclusive_upper_bound_key();
+ if (ub_key && ub_key->encoded_key() <= base_data_->max_encoded_key_) {
bool exact;
- Status s = key_iter_->SeekAtOrAfter(*spec->exclusive_upper_bound_key(),
&exact);
+ Status s = key_iter_->SeekAtOrAfter(*ub_key, &exact);
if (PREDICT_FALSE(s.IsNotFound())) {
LOG(DFATAL) << "CFileSet indicated upper bound was within range, but "
<< "key iterator could not seek. "
<< "CFileSet upper_bound = "
<<
KUDU_REDACT(Slice(base_data_->max_encoded_key_).ToDebugString())
<< ", enc_key = "
- <<
KUDU_REDACT(spec->exclusive_upper_bound_key()->encoded_key().ToDebugString());
+ << KUDU_REDACT(ub_key->encoded_key().ToDebugString());
} else {
RETURN_NOT_OK(s);
@@ -463,7 +462,7 @@ Status
CFileSet::Iterator::PushdownRangeScanPredicate(ScanSpec *spec) {
upper_bound_idx_ = std::min(upper_bound_idx_, cur);
VLOG(1) << "Pushed upper bound value "
- <<
spec->exclusive_upper_bound_key()->Stringify(key_schema_for_vlog)
+ << ub_key->Stringify(key_schema_for_vlog)
<< " as row_idx < " << upper_bound_idx_;
}
}
diff --git a/src/kudu/tablet/deltamemstore-test.cc
b/src/kudu/tablet/deltamemstore-test.cc
index fc3b9c1..7c4946e 100644
--- a/src/kudu/tablet/deltamemstore-test.cc
+++ b/src/kudu/tablet/deltamemstore-test.cc
@@ -510,7 +510,7 @@ TEST_F(TestDeltaMemStore, TestDMSBasic) {
ASSERT_EQ(i * 10, read_back[i]) << "failed at iteration " << i;
snprintf(buf2, sizeof(buf2), "hello %d", i);
Slice s(buf2);
- ASSERT_EQ(0, s.compare(read_back_slices[i]));
+ ASSERT_EQ(s, read_back_slices[i]);
}
diff --git a/src/kudu/tablet/diskrowset.cc b/src/kudu/tablet/diskrowset.cc
index b5959ff..da2ae7a 100644
--- a/src/kudu/tablet/diskrowset.cc
+++ b/src/kudu/tablet/diskrowset.cc
@@ -226,7 +226,7 @@ Status DiskRowSetWriter::AppendBlock(const RowBlock &block,
int live_row_count)
}
#ifndef NDEBUG
- CHECK(prev_key.size() == 0 || Slice(prev_key).compare(enc_key) < 0)
+ CHECK(prev_key.size() == 0 || Slice(prev_key) < enc_key)
<< KUDU_REDACT(enc_key.ToDebugString()) << " appended to file not >
previous key "
<< KUDU_REDACT(Slice(prev_key).ToDebugString());
#endif
@@ -260,8 +260,9 @@ Status
DiskRowSetWriter::FinishAndReleaseBlocks(BlockCreationTransaction* transa
key_index_writer()->GetMetaValueOrDie(DiskRowSet::kMinKeyMetaEntryName);
Slice first_enc_slice(first_encoded_key);
- CHECK_LE(first_enc_slice.compare(last_enc_slice), 0)
- << "First Key not <= Last key: first_key=" <<
KUDU_REDACT(first_enc_slice.ToDebugString())
+ CHECK(first_enc_slice <= last_enc_slice)
+ << "First Key not <= Last key: first_key="
+ << KUDU_REDACT(first_enc_slice.ToDebugString())
<< " last_key=" << KUDU_REDACT(last_enc_slice.ToDebugString());
key_index_writer()->AddMetadataPair(DiskRowSet::kMaxKeyMetaEntryName,
last_enc_slice);
if (FLAGS_rowset_metadata_store_keys) {
diff --git a/src/kudu/tablet/memrowset.h b/src/kudu/tablet/memrowset.h
index e5d2b30..06c9c25 100644
--- a/src/kudu/tablet/memrowset.h
+++ b/src/kudu/tablet/memrowset.h
@@ -535,8 +535,7 @@ class MemRowSet::Iterator : public RowwiseIterator {
bool out_of_bounds(const Slice &key) const {
DCHECK(has_upper_bound()) << "No upper bound set!";
-
- return key.compare(*exclusive_upper_bound_) >= 0;
+ return key >= *exclusive_upper_bound_;
}
size_t remaining_in_leaf() const {
diff --git a/src/kudu/tablet/rowset_info.cc b/src/kudu/tablet/rowset_info.cc
index 1337ce4..069dde5 100644
--- a/src/kudu/tablet/rowset_info.cc
+++ b/src/kudu/tablet/rowset_info.cc
@@ -113,10 +113,10 @@ bool LessCDFAndRSMax(const RowSetInfo& a, const
RowSetInfo& b) {
// Debug-checks that min <= imin <= imax <= max
void DCheckInside(const Slice& min, const Slice& max,
const Slice& imin, const Slice& imax) {
- DCHECK_LE(min.compare(max), 0);
- DCHECK_LE(imin.compare(imax), 0);
- DCHECK_LE(min.compare(imin), 0);
- DCHECK_LE(imax.compare(max), 0);
+ DCHECK_LE(min, max);
+ DCHECK_LE(imin, imax);
+ DCHECK_LE(min, imin);
+ DCHECK_LE(imax, max);
}
// Return the number of bytes of common prefix shared by 'min' and 'max'
@@ -370,7 +370,7 @@ void RowSetInfo::SplitKeyRange(const RowSetTree& tree,
uint64_t target_chunk_size,
vector<KeyRange>* ranges) {
// check start_key greater than stop_key
- CHECK(stop_key.empty() || start_key.compare(stop_key) <= 0);
+ CHECK(stop_key.empty() || start_key <= stop_key);
// The split process works as follows:
// For each sorted endpoint, first we identify whether it is a
@@ -398,9 +398,9 @@ void RowSetInfo::SplitKeyRange(const RowSetTree& tree,
RowSet* rs = rse.rowset_;
next = rse.slice_;
- if (prev.compare(next) < 0) {
+ if (prev < next) {
// reset next when next greater than stop_key
- if (!stop_key.empty() && next.compare(stop_key) > 0) {
+ if (!stop_key.empty() && next > stop_key) {
next = stop_key;
}
@@ -422,7 +422,7 @@ void RowSetInfo::SplitKeyRange(const RowSetTree& tree,
prev = next;
}
- if (!stop_key.empty() && prev.compare(stop_key) >= 0) {
+ if (!stop_key.empty() && prev >= stop_key) {
break;
}
@@ -443,7 +443,7 @@ void RowSetInfo::SplitKeyRange(const RowSetTree& tree,
<< "\t\tEndpointType=" << rse.endpoint_;
}
}
- if (last_bound.compare(stop_key) < 0 || stop_key.empty()) {
+ if (stop_key.empty() || last_bound < stop_key) {
ranges->emplace_back(last_bound.ToString(), stop_key.ToString(),
chunk_size);
}
}
diff --git a/src/kudu/tablet/rowset_tree-test.cc
b/src/kudu/tablet/rowset_tree-test.cc
index 7a39a2a..ce855f2 100644
--- a/src/kudu/tablet/rowset_tree-test.cc
+++ b/src/kudu/tablet/rowset_tree-test.cc
@@ -385,7 +385,7 @@ TEST_F(TestRowSetTree, TestEndpointsConsistency) {
ASSERT_TRUE(!slice.empty()) << "RowSetTree has an endpoint with no key";
if (!prev.empty()) {
- ASSERT_LE(prev.compare(slice), 0);
+ ASSERT_LE(prev, slice);
}
string min, max;
diff --git a/src/kudu/tablet/tablet.cc b/src/kudu/tablet/tablet.cc
index 86b8f7f..cd60461 100644
--- a/src/kudu/tablet/tablet.cc
+++ b/src/kudu/tablet/tablet.cc
@@ -44,11 +44,11 @@
#include "kudu/common/row.h"
#include "kudu/common/row_changelist.h"
#include "kudu/common/row_operations.h"
+#include "kudu/common/row_operations.pb.h"
#include "kudu/common/rowid.h"
#include "kudu/common/scan_spec.h"
#include "kudu/common/schema.h"
#include "kudu/common/timestamp.h"
-#include "kudu/common/wire_protocol.pb.h"
#include "kudu/consensus/log_anchor_registry.h"
#include "kudu/consensus/opid.pb.h"
#include "kudu/fs/block_manager.h"
@@ -1050,7 +1050,7 @@ Status Tablet::BulkCheckPresence(const IOContext*
io_context, WriteOpState* op_s
std::stable_sort(keys_and_indexes.begin(), keys_and_indexes.end(),
[](const pair<Slice, int>& a,
const pair<Slice, int>& b) {
- return a.first.compare(b.first) < 0;
+ return a.first < b.first;
});
// If the batch has more than one operation for the same row, then we can't
// use the up-front presence optimization on those operations, since the
@@ -1110,14 +1110,10 @@ Status Tablet::BulkCheckPresence(const IOContext*
io_context, WriteOpState* op_s
DCHECK(std::is_sorted(pending_group.begin(), pending_group.end(),
[&](const pair<RowSet*, int>& a,
const pair<RowSet*, int>& b) {
- auto s_a = keys[a.second];
- auto s_b = keys[b.second];
- return s_a.compare(s_b) < 0;
+ return keys[a.second] < keys[b.second];
}));
RowSet* rs = pending_group[0].first;
- for (auto it = pending_group.begin();
- it != pending_group.end();
- ++it) {
+ for (auto it = pending_group.begin(); it != pending_group.end(); ++it) {
DCHECK_EQ(it->first, rs) << "All results within a group should be for
the same RowSet";
int op_idx = keys_and_indexes[it->second].second;
RowOp* op = row_ops_base[op_idx];
diff --git a/src/kudu/tserver/tablet_service.cc
b/src/kudu/tserver/tablet_service.cc
index d2a1b94..f7a5c34 100644
--- a/src/kudu/tserver/tablet_service.cc
+++ b/src/kudu/tserver/tablet_service.cc
@@ -2361,7 +2361,7 @@ void TabletServiceImpl::SplitKeyRange(const
SplitKeyRangeRequestPB* req,
}
if (req->has_start_primary_key() && req->has_stop_primary_key()) {
// Validate the start key is less than the stop key, if they are both set
- if (start->encoded_key().compare(stop->encoded_key()) > 0) {
+ if (start->encoded_key() > stop->encoded_key()) {
SetupErrorAndRespond(resp->mutable_error(),
Status::InvalidArgument("Invalid primary key
range"),
TabletServerErrorPB::UNKNOWN_ERROR,
diff --git a/src/kudu/util/memcmpable_varint-test.cc
b/src/kudu/util/memcmpable_varint-test.cc
index fcbe25d..3762d41 100644
--- a/src/kudu/util/memcmpable_varint-test.cc
+++ b/src/kudu/util/memcmpable_varint-test.cc
@@ -117,11 +117,11 @@ TEST_F(TestMemcmpableVarint, TestCompositeKeys) {
SCOPED_TRACE(testing::Message() << p1 << "\n" << HexDump(Slice(buf1))
<< " vs\n" << p2 << "\n" << HexDump(Slice(buf2)));
if (p1 < p2) {
- ASSERT_LT(Slice(buf1).compare(Slice(buf2)), 0);
+ ASSERT_LT(Slice(buf1), Slice(buf2));
} else if (p1 > p2) {
- ASSERT_GT(Slice(buf1).compare(Slice(buf2)), 0);
+ ASSERT_GT(Slice(buf1), Slice(buf2));
} else {
- ASSERT_EQ(Slice(buf1).compare(Slice(buf2)), 0);
+ ASSERT_EQ(Slice(buf1), Slice(buf2));
}
}
}
@@ -158,11 +158,11 @@ TEST_F(TestMemcmpableVarint,
TestInterestingCompositeKeys) {
SCOPED_TRACE(testing::Message() << p1 << "\n" << HexDump(Slice(buf1))
<< " vs\n" << p2 << "\n" << HexDump(Slice(buf2)));
if (p1 < p2) {
- ASSERT_LT(Slice(buf1).compare(Slice(buf2)), 0);
+ ASSERT_LT(Slice(buf1), Slice(buf2));
} else if (p1 > p2) {
- ASSERT_GT(Slice(buf1).compare(Slice(buf2)), 0);
+ ASSERT_GT(Slice(buf1), Slice(buf2));
} else {
- ASSERT_EQ(Slice(buf1).compare(Slice(buf2)), 0);
+ ASSERT_EQ(Slice(buf1), Slice(buf2));
}
}
}
diff --git a/src/kudu/util/slice.h b/src/kudu/util/slice.h
index bc6f9da..5feb78b 100644
--- a/src/kudu/util/slice.h
+++ b/src/kudu/util/slice.h
@@ -278,6 +278,50 @@ inline bool operator!=(const Slice& x, const Slice& y) {
return !(x == y);
}
+/// Check whether x < y.
+///
+/// @param [in] x
+/// One slice.
+/// @param [in] y
+/// Another slice.
+/// @return @c true iff x less than y
+inline bool operator<(const Slice& x, const Slice& y) {
+ return x.compare(y) < 0;
+}
+
+/// Check whether x > y.
+///
+/// @param [in] x
+/// One slice.
+/// @param [in] y
+/// Another slice.
+/// @return @c true iff x greater than y
+inline bool operator>(const Slice& x, const Slice& y) {
+ return x.compare(y) > 0;
+}
+
+/// Check whether x >= y.
+///
+/// @param [in] x
+/// One slice.
+/// @param [in] y
+/// Another slice.
+/// @return @c true iff x is greater than or equal to y
+inline bool operator>=(const Slice& x, const Slice& y) {
+ return x.compare(y) >= 0;
+}
+
+/// Check whether x <= y.
+///
+/// @param [in] x
+/// One slice.
+/// @param [in] y
+/// Another slice.
+/// @return @c true iff x is less than or equal to y
+inline bool operator<=(const Slice& x, const Slice& y) {
+ return x.compare(y) <= 0;
+}
+
/// Output printable representation of the slice into the given output stream.
///
/// @param [out] o
@@ -290,13 +334,18 @@ inline std::ostream& operator<<(std::ostream& o, const
Slice& s) {
}
inline int Slice::compare(const Slice& b) const {
- const int min_len = (size_ < b.size_) ? size_ : b.size_;
- int r = MemCompare(data_, b.data_, min_len);
- if (r == 0) {
- if (size_ < b.size_) r = -1;
- else if (size_ > b.size_) r = +1;
+ const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
+ const int r = MemCompare(data_, b.data_, min_len);
+ if (r != 0) {
+ return r;
+ }
+ if (size_ < b.size_) {
+ return -1;
+ }
+ if (size_ > b.size_) {
+ return 1;
}
- return r;
+ return 0;
}
// We don't run TSAN on this function because it makes it really slow and
causes some