From: Kefu Chai Date: Tue, 3 Apr 2018 14:32:25 +0000 (+0800) Subject: common/hobject: fix the sort order of hobject X-Git-Tag: v13.1.0~376^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=35dcbcab6ef81f467e7e3b31a53ab5ad6852f715;p=ceph.git common/hobject: fix the sort order of hobject we *cannot* change the sort order of hobject. and to avoid duplicated comparisions, we can check for empty keys, and do not compare the names again if both hobjects being compared have empty keys. Signed-off-by: Kefu Chai --- diff --git a/src/common/hobject.cc b/src/common/hobject.cc index 2c80727cf8a..6a4cebf520a 100644 --- a/src/common/hobject.cc +++ b/src/common/hobject.cc @@ -336,10 +336,14 @@ int cmp(const hobject_t& l, const hobject_t& r) return -1; if (l.nspace > r.nspace) return 1; - if (l.get_key() < r.get_key()) - return -1; - if (l.get_key() > r.get_key()) - return 1; + if (!(l.get_key().empty() && r.get_key().empty())) { + if (l.get_effective_key() < r.get_effective_key()) { + return -1; + } + if (l.get_effective_key() > r.get_effective_key()) { + return 1; + } + } if (l.oid < r.oid) return -1; if (l.oid > r.oid) diff --git a/src/test/common/CMakeLists.txt b/src/test/common/CMakeLists.txt index b934cfd9c2a..5272fbef467 100644 --- a/src/test/common/CMakeLists.txt +++ b/src/test/common/CMakeLists.txt @@ -277,3 +277,8 @@ add_ceph_unittest(unittest_bounded_key_counter) add_executable(unittest_static_ptr test_static_ptr.cc) add_ceph_unittest(unittest_static_ptr) + +add_executable(unittest_hobject test_hobject.cc + $) +target_link_libraries(unittest_hobject global ceph-common) +add_ceph_unittest(unittest_hobject) diff --git a/src/test/common/test_hobject.cc b/src/test/common/test_hobject.cc new file mode 100644 index 00000000000..0bb4aef9ee1 --- /dev/null +++ b/src/test/common/test_hobject.cc @@ -0,0 +1,11 @@ +#include "common/hobject.h" +#include "gtest/gtest.h" + +TEST(HObject, cmp) +{ + hobject_t c{object_t{"fooc"}, "food", CEPH_NOSNAP, 42, 0, "nspace"}; + hobject_t d{object_t{"food"}, "", CEPH_NOSNAP, 42, 0, "nspace"}; + hobject_t e{object_t{"fooe"}, "food", CEPH_NOSNAP, 42, 0, "nspace"}; + ASSERT_EQ(-1, cmp(c, d)); + ASSERT_EQ(-1, cmp(d, e)); +}