]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
osd: extract do_cmp_xattr()
authorKefu Chai <kchai@redhat.com>
Tue, 29 Jun 2021 06:15:57 +0000 (14:15 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 29 Jun 2021 06:54:08 +0000 (14:54 +0800)
to consolidate the logic to dispatch by op.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/osd/PrimaryLogPG.cc

index 7663170f043b4727c77a6eb8bd420db939fdcf1f..8a09683504c01c88e039946831f412895c2c0a05 100644 (file)
@@ -4900,58 +4900,50 @@ void PrimaryLogPG::snap_trimmer(epoch_t queued)
   return;
 }
 
-int PrimaryLogPG::do_xattr_cmp_u64(int op, __u64 v1, bufferlist& xattr)
-{
-  __u64 v2;
-
-  string v2s(xattr.c_str(), xattr.length());
-  if (v2s.length())
-    v2 = strtoull(v2s.c_str(), NULL, 10);
-  else
-    v2 = 0;
-
-  dout(20) << "do_xattr_cmp_u64 '" << v1 << "' vs '" << v2 << "' op " << op << dendl;
+namespace {
 
+template<typename U, typename V>
+int do_cmp_xattr(int op, const U& lhs, const V& rhs)
+{
   switch (op) {
   case CEPH_OSD_CMPXATTR_OP_EQ:
-    return (v1 == v2);
+    return lhs == rhs;
   case CEPH_OSD_CMPXATTR_OP_NE:
-    return (v1 != v2);
+    return lhs != rhs;
   case CEPH_OSD_CMPXATTR_OP_GT:
-    return (v1 > v2);
+    return lhs > rhs;
   case CEPH_OSD_CMPXATTR_OP_GTE:
-    return (v1 >= v2);
+    return lhs >= rhs;
   case CEPH_OSD_CMPXATTR_OP_LT:
-    return (v1 < v2);
+    return lhs < rhs;
   case CEPH_OSD_CMPXATTR_OP_LTE:
-    return (v1 <= v2);
+    return lhs <= rhs;
   default:
     return -EINVAL;
   }
 }
 
-int PrimaryLogPG::do_xattr_cmp_str(int op, string& v1s, bufferlist& xattr)
+} // anonymous namespace
+
+int PrimaryLogPG::do_xattr_cmp_u64(int op, __u64 v1, bufferlist& xattr)
 {
+  __u64 v2;
+
   string v2s(xattr.c_str(), xattr.length());
+  if (v2s.length())
+    v2 = strtoull(v2s.c_str(), NULL, 10);
+  else
+    v2 = 0;
 
-  dout(20) << "do_xattr_cmp_str '" << v1s << "' vs '" << v2s << "' op " << op << dendl;
+  dout(20) << "do_xattr_cmp_u64 '" << v1 << "' vs '" << v2 << "' op " << op << dendl;
+  return do_cmp_xattr(op, v1, v2);
+}
 
-  switch (op) {
-  case CEPH_OSD_CMPXATTR_OP_EQ:
-    return (v1s.compare(v2s) == 0);
-  case CEPH_OSD_CMPXATTR_OP_NE:
-    return (v1s.compare(v2s) != 0);
-  case CEPH_OSD_CMPXATTR_OP_GT:
-    return (v1s.compare(v2s) > 0);
-  case CEPH_OSD_CMPXATTR_OP_GTE:
-    return (v1s.compare(v2s) >= 0);
-  case CEPH_OSD_CMPXATTR_OP_LT:
-    return (v1s.compare(v2s) < 0);
-  case CEPH_OSD_CMPXATTR_OP_LTE:
-    return (v1s.compare(v2s) <= 0);
-  default:
-    return -EINVAL;
-  }
+int PrimaryLogPG::do_xattr_cmp_str(int op, string& v1s, bufferlist& xattr)
+{
+  string_view v2s(xattr.c_str(), xattr.length());
+  dout(20) << "do_xattr_cmp_str '" << v1s << "' vs '" << v2s << "' op " << op << dendl;
+  return do_cmp_xattr(op, v1s, v2s);
 }
 
 int PrimaryLogPG::do_writesame(OpContext *ctx, OSDOp& osd_op)