]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
osd/osd_types: add coll_t::parse() method
authorSage Weil <sage@redhat.com>
Mon, 5 Jan 2015 23:24:35 +0000 (15:24 -0800)
committerSage Weil <sage@redhat.com>
Fri, 19 Jun 2015 00:02:47 +0000 (17:02 -0700)
This will explicitly validate the form of the input string to make sure it
is a recognized collection.  (Eventually we can then store things
internally as something other than a string.)

Signed-off-by: Sage Weil <sage@redhat.com>
src/osd/osd_types.h
src/test/osd/types.cc

index ab349dfe1d0cf00c74538c410abbf2398de1add3..591aadc72ab1a3067cbadb40ad87494b338a306e 100644 (file)
@@ -414,6 +414,9 @@ struct spg_t {
     return pgid.preferred();
   }
   bool parse(const char *s);
+  bool parse(const std::string& s) {
+    return parse(s.c_str());
+  }
   bool is_split(unsigned old_pg_num, unsigned new_pg_num,
                set<spg_t> *pchildren) const {
     set<pg_t> _children;
@@ -497,6 +500,21 @@ public:
   const std::string& to_str() const {
     return str;
   }
+  bool parse(const std::string& s) {
+    if (s == "meta") {
+      str = s;
+      return true;
+    }
+    if (s.find("_head") == s.length() - 5 ||
+       s.find("_TEMP") == s.length() - 5) {
+      spg_t pgid;
+      if (pgid.parse(s.substr(0, s.length() - 5))) {
+       str = s;
+       return true;
+      }
+    }
+    return false;
+  }
 
   const char* c_str() const {
     return str.c_str();
index e8b8857dd4a8dc922a8a31d3cfdb7c062fbd875e..09fdff622cf1fc6146f1e55f1ce0faf45ace1ae3 100644 (file)
@@ -1310,6 +1310,20 @@ TEST(spg_t, parse) {
   ASSERT_EQ(b, bb);
 }
 
+TEST(coll_t, parse) {
+  coll_t a;
+  ASSERT_TRUE(a.parse("meta"));
+  ASSERT_TRUE(a.parse("1.2_head"));
+  ASSERT_TRUE(a.parse("1.2_TEMP"));
+  ASSERT_TRUE(a.parse("1.2s3_head"));
+  ASSERT_TRUE(a.parse("1.2s3_TEMP"));
+  ASSERT_TRUE(a.parse("1.2s0_head"));
+  ASSERT_FALSE(a.parse("foo"));
+  ASSERT_FALSE(a.parse("1.2_food"));
+  ASSERT_FALSE(a.parse("1.2_temp"));
+  ASSERT_FALSE(a.parse(""));
+}
+
 TEST(coll_t, temp) {
   spg_t pgid;
   coll_t foo(pgid);