]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson/osd: extract do_cmp_xattr()
authorKefu Chai <kchai@redhat.com>
Tue, 29 Jun 2021 06:25:21 +0000 (14:25 +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/crimson/osd/pg_backend.cc

index f9f869478b08d6b9e5f898e1ab3592f686abbda4..8b1a1eeac5b2391800be7a6006aded676b7e3d2c 100644 (file)
@@ -830,30 +830,32 @@ PGBackend::get_attr_ierrorator::future<> PGBackend::get_xattrs(
     return get_attr_errorator::now();
   });
 }
-static int do_xattr_cmp_str(int op, const string& lhs, bufferlist& rhs_xattr)
-{
-  string rhs(rhs_xattr.c_str(), rhs_xattr.length());
 
-  logger().debug("do_xattr_cmp_str '{}' vs '{}' op {}", lhs, rhs, op);
+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 (lhs.compare(rhs) == 0);
+    return lhs == rhs;
   case CEPH_OSD_CMPXATTR_OP_NE:
-    return (lhs.compare(rhs) != 0);
+    return lhs != rhs;
   case CEPH_OSD_CMPXATTR_OP_GT:
-    return (lhs.compare(rhs) > 0);
+    return lhs > rhs;
   case CEPH_OSD_CMPXATTR_OP_GTE:
-    return (lhs.compare(rhs) >= 0);
+    return lhs >= rhs;
   case CEPH_OSD_CMPXATTR_OP_LT:
-    return (lhs.compare(rhs) < 0);
+    return lhs < rhs;
   case CEPH_OSD_CMPXATTR_OP_LTE:
-    return (lhs.compare(rhs) <= 0);
+    return lhs <= rhs;
   default:
     return -EINVAL;
   }
 }
 
+} // anonymous namespace
+
 static int do_xattr_cmp_u64(int op, uint64_t lhs, bufferlist& rhs_xattr)
 {
   uint64_t rhs;
@@ -864,24 +866,9 @@ static int do_xattr_cmp_u64(int op, uint64_t lhs, bufferlist& rhs_xattr)
     rhs = 0;
   }
   logger().debug("do_xattr_cmp_u64 '{}' vs '{}' op {}", lhs, rhs, op);
-
-  switch (op) {
-  case CEPH_OSD_CMPXATTR_OP_EQ:
-    return (lhs == rhs);
-  case CEPH_OSD_CMPXATTR_OP_NE:
-    return (lhs != rhs);
-  case CEPH_OSD_CMPXATTR_OP_GT:
-    return (lhs > rhs);
-  case CEPH_OSD_CMPXATTR_OP_GTE:
-    return (lhs >= rhs);
-  case CEPH_OSD_CMPXATTR_OP_LT:
-    return (lhs < rhs);
-  case CEPH_OSD_CMPXATTR_OP_LTE:
-    return (lhs <= rhs);
-  default:
-    return -EINVAL;
-  }
+  return do_cmp_xattr(op, lhs, rhs);
 }
+
 PGBackend::cmp_xattr_ierrorator::future<> PGBackend::cmp_xattr(
   const ObjectState& os,
   OSDOp& osd_op) const
@@ -900,10 +887,11 @@ PGBackend::cmp_xattr_ierrorator::future<> PGBackend::cmp_xattr(
     switch (osd_op.op.xattr.cmp_mode) {
     case CEPH_OSD_CMPXATTR_MODE_STRING:
       {
-        string val;
-        bp.copy(osd_op.op.xattr.value_len, val);
-        result = do_xattr_cmp_str(osd_op.op.xattr.cmp_op, val, xattr);
-        logger().debug("cmpxattr val={}, xattr= {}", val, xattr);
+        string lhs;
+        bp.copy(osd_op.op.xattr.value_len, lhs);
+        string_view rhs(xattr.c_str(), xattr.length());
+        result = do_cmp_xattr(osd_op.op.xattr.cmp_op, lhs, rhs);
+        logger().debug("cmpxattr lhs={}, rhs={}", lhs, rhs);
       }
     break;
     case CEPH_OSD_CMPXATTR_MODE_U64: