]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librados: add assert_exists guard operation
authorSage Weil <sage@inktank.com>
Tue, 23 Oct 2012 00:51:11 +0000 (17:51 -0700)
committerSage Weil <sage@inktank.com>
Tue, 23 Oct 2012 04:04:30 +0000 (21:04 -0700)
Add a guard operation for writes that asserts that the object already
exists.  To avoid requiring new functionality on the OSD side, implement
this by including a STAT operation, and discard the results on the
client side.

Signed-off-by: Sage Weil <sage@inktank.com>
src/include/rados/librados.hpp
src/librados/librados.cc
src/test/rados-api/misc.cc

index e50acdb2184441dfee6bcfd2c665f038400285d8..3df4c86e576cafc8c51db2650316288fd27ce58c 100644 (file)
@@ -143,6 +143,11 @@ namespace librados
      */
     void assert_version(uint64_t ver);
 
+    /**
+     * Guard operatation with a check that the object already exists
+     */
+    void assert_exists();
+
     /**
      * get key/value paris for specified keys
      *
index 274119c1b2807613989b41717b75d55e7d707bba..c31b82ae34b6d4f3cb6ae96d5ccf8cc474ab8aef 100644 (file)
@@ -109,6 +109,12 @@ void librados::ObjectOperation::assert_version(uint64_t ver)
   o->assert_version(ver);
 }
 
+void librados::ObjectOperation::assert_exists()
+{
+  ::ObjectOperation *o = (::ObjectOperation *)impl;
+  o->stat(NULL, (utime_t*)NULL, NULL);
+}
+
 void librados::ObjectOperation::exec(const char *cls, const char *method, bufferlist& inbl)
 {
   ::ObjectOperation *o = (::ObjectOperation *)impl;
index 0821ec0778482f3ec55882a685659c8af9248766..5f0545adb60db542eebd82cba915b6db2fc4a722 100644 (file)
@@ -338,3 +338,27 @@ TEST(LibRadosMisc, CloneRange) {
   rados_ioctx_destroy(ioctx);
   ASSERT_EQ(0, destroy_one_pool(pool_name, &cluster));
 }
+
+TEST(LibRadosMisc, AssertExistsPP) {
+  Rados cluster;
+  std::string pool_name = get_temp_pool_name();
+  ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
+  IoCtx ioctx;
+  ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
+
+  char buf[64];
+  memset(buf, 0xcc, sizeof(buf));
+  bufferlist bl;
+  bl.append(buf, sizeof(buf));
+
+  ObjectWriteOperation op;
+  op.assert_exists();
+  op.write(0, bl);
+  ASSERT_EQ(-ENOENT, ioctx.operate("asdffoo", &op));
+  ASSERT_EQ(0, ioctx.create("asdffoo", true));
+  ASSERT_EQ(0, ioctx.operate("asdffoo", &op));
+  ASSERT_EQ(-EEXIST, ioctx.create("asdffoo", true));
+
+  ioctx.close();
+  ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
+}