Adding Slice::difference_offset() function

Summary:
There are some use cases in MyRocks to compare two slices
and to return the first byte where they differ. It may be
useful to add it as a RocksDB Slice function.

Test Plan: db_test

Reviewers: sdong, rven, igor

Reviewed By: igor

Subscribers: jkedgar, dhruba

Differential Revision: https://reviews.facebook.net/D46935
main
Yoshinori Matsunobu 9 years ago
parent 5ce63e30ea
commit 4886073174
  1. 2
      db/db_test.cc
  2. 12
      include/rocksdb/slice.h

@ -5937,6 +5937,8 @@ TEST_F(DBTest, PrefixScan) {
snprintf(buf, sizeof(buf), "03______:"); snprintf(buf, sizeof(buf), "03______:");
prefix = Slice(buf, 8); prefix = Slice(buf, 8);
key = Slice(buf, 9); key = Slice(buf, 9);
ASSERT_EQ(key.difference_offset(prefix), 8);
ASSERT_EQ(prefix.difference_offset(key), 8);
// db configs // db configs
env_->count_random_reads_ = true; env_->count_random_reads_ = true;
Options options = CurrentOptions(); Options options = CurrentOptions();

@ -88,6 +88,9 @@ class Slice {
(memcmp(data_, x.data_, x.size_) == 0)); (memcmp(data_, x.data_, x.size_) == 0));
} }
// Compare two slices and returns the first byte where they differ
size_t difference_offset(const Slice& b) const;
// private: make these public for rocksdbjni access // private: make these public for rocksdbjni access
const char* data_; const char* data_;
size_t size_; size_t size_;
@ -125,6 +128,15 @@ inline int Slice::compare(const Slice& b) const {
return r; return r;
} }
inline size_t Slice::difference_offset(const Slice& b) const {
size_t off = 0;
const size_t len = (size_ < b.size_) ? size_ : b.size_;
for (; off < len; off++) {
if (data_[off] != b.data_[off]) break;
}
return off;
}
} // namespace rocksdb } // namespace rocksdb
#endif // STORAGE_ROCKSDB_INCLUDE_SLICE_H_ #endif // STORAGE_ROCKSDB_INCLUDE_SLICE_H_

Loading…
Cancel
Save