]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crush: add CrushWrapper::rename_item and can_rename_item
authorLoic Dachary <loic-201408@dachary.org>
Thu, 16 Oct 2014 00:06:12 +0000 (17:06 -0700)
committerLoic Dachary <loic-201408@dachary.org>
Thu, 16 Oct 2014 00:12:36 +0000 (17:12 -0700)
The can_rename_item is a const method checking if renaming an item could
succeed. If not it returns a unique -errno code and a human readable
message message.

Trying to rename a non existent item into an existent item returns
-EALREADY which can be treated as success if renaming is to be
idempotent.

Signed-off-by: Loic Dachary <loic-201408@dachary.org>
src/crush/CrushWrapper.cc
src/crush/CrushWrapper.h

index e605aa32d79f4060c10f62319a2796e00bd62f6e..dbd50d165d5ee8fccef6bb09e7fe4f935cb0d02c 100644 (file)
@@ -63,6 +63,43 @@ bool CrushWrapper::is_v3_rule(unsigned ruleid) const
   return false;
 }
 
+int CrushWrapper::can_rename_item(const string& srcname,
+                                  const string& dstname,
+                                  ostream *ss) const
+{
+  if (name_exists(srcname)) {
+    if (name_exists(dstname)) {
+      *ss << "dstname = '" << dstname << "' already exists";
+      return -EEXIST;
+    }
+    if (is_valid_crush_name(dstname)) {
+      return 0;
+    } else {
+      *ss << "srcname = '" << srcname << "' does not match [-_.0-9a-zA-Z]+";
+      return -EINVAL;
+    }
+  } else {
+    if (name_exists(dstname)) {
+      *ss << "srcname = '" << srcname << "' does not exist "
+          << "and dstname = '" << dstname << "' already exists";
+      return -EALREADY;
+    } else {
+      *ss << "srcname = '" << srcname << "' does not exist";
+      return -ENOENT;
+    }
+  }
+}
+
+int CrushWrapper::rename_item(const string& srcname,
+                              const string& dstname,
+                              ostream *ss)
+{
+  int ret = can_rename_item(srcname, dstname, ss);
+  if (ret < 0)
+    return ret;
+  int oldid = get_item_id(srcname);
+  return set_item_name(oldid, dstname);
+}
 
 void CrushWrapper::find_takes(set<int>& roots) const
 {
index 1e01d74c7af5aa30841f2e91cd4e3dde0388c9c2..459d761071d65b1e72fb2421734b680585abeaf8 100644 (file)
@@ -269,6 +269,12 @@ public:
     return 0;
   }
 
+  int can_rename_item(const string& srcname,
+                     const string& dstname,
+                     ostream *ss) const;
+  int rename_item(const string& srcname,
+                 const string& dstname,
+                 ostream *ss);
   // rule names
   bool rule_exists(string name) const {
     build_rmaps();