]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/hobject: fix the sort order of hobject 21217/head
authorKefu Chai <kchai@redhat.com>
Tue, 3 Apr 2018 14:32:25 +0000 (22:32 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 3 Apr 2018 14:46:57 +0000 (22:46 +0800)
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 <kchai@redhat.com>
src/common/hobject.cc
src/test/common/CMakeLists.txt
src/test/common/test_hobject.cc [new file with mode: 0644]

index 2c80727cf8a5748673bba1535ed99037f2513566..6a4cebf520a7df9a0dc248f9bada448d631fe046 100644 (file)
@@ -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)
index b934cfd9c2ab7ea4da2eda817005f964a30648f2..5272fbef46785dbeae8888288d9dbb554ea1b35b 100644 (file)
@@ -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_OBJECTS:unit-main>)
+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 (file)
index 0000000..0bb4aef
--- /dev/null
@@ -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));
+}