]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librados: fix copy ctor of ObjectIterator
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 28 Feb 2011 13:19:04 +0000 (05:19 -0800)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 28 Feb 2011 13:32:31 +0000 (05:32 -0800)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/include/rados/librados.hpp
src/librados.cc
src/testradospp.cc

index af9a8523d3e07f8a8e68ebe41e3e2ed9799fa785..99ed0c9d24a1ec627fcb24674645c189b84629ed 100644 (file)
@@ -5,6 +5,7 @@
 #include <string>
 #include <list>
 #include <map>
+#include <tr1/memory>
 #include <vector>
 #include "buffer.h"
 
@@ -14,6 +15,7 @@ class RadosClient;
 class Context;
 class AioCompletionImpl;
 class IoCtxImpl;
+class ObjListCtx;
 
 namespace librados
 {
@@ -52,17 +54,17 @@ namespace librados
   class ObjectIterator : public std::iterator <std::forward_iterator_tag, std::string> {
   public:
     static const ObjectIterator __EndObjectIterator;
-    ObjectIterator(rados_list_ctx_t ctx_);
+    ObjectIterator(ObjListCtx *ctx_);
     ~ObjectIterator();
     bool operator==(const ObjectIterator& rhs) const;
     bool operator!=(const ObjectIterator& rhs) const;
     const std::string& operator*() const;
     ObjectIterator &operator++(); // Preincrement
     ObjectIterator operator++(int); // Postincrement
+    friend class IoCtx;
   private:
     void get_next();
-    rados_list_ctx_t ctx;
-    bufferlist *bl;
+    std::tr1::shared_ptr < ObjListCtx > ctx;
     std::string cur_obj;
   };
 
index 8a9bcdfb24398c4ea5b0f199400542d36984c94c..a8d6d4b272daa2673b7b0843ec1118fc18de515a 100644 (file)
@@ -202,6 +202,16 @@ struct AioCompletionImpl {
   }
 };
 
+struct ObjListCtx {
+  IoCtxImpl *ctx;
+  Objecter::ListContext *lc;
+
+  ObjListCtx(IoCtxImpl *c, Objecter::ListContext *l) : ctx(c), lc(l) {}
+  ~ObjListCtx() {
+    delete lc;
+  }
+};
+
 class RadosClient : public Dispatcher
 {
   OSDMap osdmap;
@@ -240,17 +250,6 @@ public:
   int connect();
   void shutdown();
 
-  struct ListCtx {
-    IoCtxImpl *ctx;
-    Objecter::ListContext *lc;
-
-    ListCtx(IoCtxImpl *c, Objecter::ListContext *l) : ctx(c), lc(l) {}
-    ~ListCtx() {
-      delete lc;
-    }
-  };
-
-
   int lookup_pool(const char *name) {
     int ret = osdmap.lookup_pg_pool_name(name);
     if (ret < 0)
@@ -1825,7 +1824,7 @@ int RadosClient::notify(IoCtxImpl& io, const object_t& oid, uint64_t ver)
 
 ///////////////////////////// ObjectIterator /////////////////////////////
 librados::ObjectIterator::
-ObjectIterator(rados_list_ctx_t ctx_)
+ObjectIterator(ObjListCtx *ctx_)
 : ctx(ctx_)
 {
 }
@@ -1833,20 +1832,17 @@ ObjectIterator(rados_list_ctx_t ctx_)
 librados::ObjectIterator::
 ~ObjectIterator()
 {
-  if (ctx) {
-    rados_objects_list_close(ctx);
-    ctx = NULL;
-  }
+  ctx.reset();
 }
 
 bool librados::ObjectIterator::
 operator==(const librados::ObjectIterator& rhs) const {
-  return (ctx == rhs.ctx);
+  return (ctx.get() == rhs.ctx.get());
 }
 
 bool librados::ObjectIterator::
 operator!=(const librados::ObjectIterator& rhs) const {
-  return (ctx != rhs.ctx);
+  return (ctx.get() != rhs.ctx.get());
 }
 
 const std::string& librados::ObjectIterator::
@@ -1873,10 +1869,9 @@ void librados::ObjectIterator::
 get_next()
 {
   const char *entry;
-  int ret = rados_objects_list_next(ctx, &entry);
+  int ret = rados_objects_list_next(ctx.get(), &entry);
   if (ret == -ENOENT) {
-    rados_objects_list_close(ctx);
-    ctx = NULL;
+    ctx.reset();
     *this = __EndObjectIterator;
     return;
   }
@@ -2171,7 +2166,9 @@ objects_begin()
 {
   rados_list_ctx_t listh;
   rados_objects_list_open(io_ctx_impl, &listh);
-  return ObjectIterator(listh);
+  ObjectIterator iter((ObjListCtx*)listh);
+  iter.get_next();
+  return iter;
 }
 
 const librados::ObjectIterator& librados::IoCtx::
@@ -2901,19 +2898,19 @@ extern "C" int rados_objects_list_open(rados_ioctx_t io, rados_list_ctx_t *listh
   Objecter::ListContext *h = new Objecter::ListContext;
   h->pool_id = ctx->poolid;
   h->pool_snap_seq = ctx->snap_seq;
-  *listh = (void *)new RadosClient::ListCtx(ctx, h);
+  *listh = (void *)new ObjListCtx(ctx, h);
   return 0;
 }
 
 extern "C" void rados_objects_list_close(rados_list_ctx_t h)
 {
-  RadosClient::ListCtx *lh = (RadosClient::ListCtx *)h;
+  ObjListCtx *lh = (ObjListCtx *)h;
   delete lh;
 }
 
 extern "C" int rados_objects_list_next(rados_list_ctx_t listctx, const char **entry)
 {
-  RadosClient::ListCtx *lh = (RadosClient::ListCtx *)listctx;
+  ObjListCtx *lh = (ObjListCtx *)listctx;
   Objecter::ListContext *h = lh->lc;
   int ret;
 
index 834831f1d0b9485f450d789b441d9e3c2d49ab3a..de60c3da520ae1f0ed4e1ad065efb17bab10a3e8 100644 (file)
@@ -38,6 +38,12 @@ public:
   }
 };
 
+void testradospp_milestone(void)
+{
+  cout << "*** press enter to continue ***" << std::endl;
+  getchar();
+}
+
 int main(int argc, const char **argv) 
 {
   Rados rados;
@@ -77,8 +83,7 @@ int main(int argc, const char **argv)
   }
 
   cout << "rados_initialize completed" << std::endl;
-  cout << "*** press enter to continue ***" << std::endl;
-  getchar();
+  testradospp_milestone();
 
   time_t tm;
   bufferlist bl, bl2, blf;
@@ -104,34 +109,30 @@ int main(int argc, const char **argv)
   r = io_ctx.watch(oid, objver, &handle, &wc);
   cout << "io_ctx.watch returned " << r << std::endl;
 
-  cout << "*** press enter to continue ***" << std::endl;
-  getchar();
+  testradospp_milestone();
   io_ctx.set_notify_timeout(7);
   r = io_ctx.notify(oid, objver);
   cout << "io_ctx.notify returned " << r << std::endl;
-  cout << "*** press enter to continue ***" << std::endl;
-  getchar();
+  testradospp_milestone();
 
   r = io_ctx.notify(oid, objver);
   cout << "io_ctx.notify returned " << r << std::endl;
-  cout << "*** press enter to continue ***" << std::endl;
-  getchar();
+  testradospp_milestone();
 
   r = io_ctx.unwatch(oid, handle);
   cout << "io_ctx.unwatch returned " << r << std::endl;
   cout << "*** press enter to continue ***" << std::endl;
-  getchar();
+  testradospp_milestone();
 
   r = io_ctx.notify(oid, objver);
   cout << "io_ctx.notify returned " << r << std::endl;
   cout << "*** press enter to continue ***" << std::endl;
-  getchar();
+  testradospp_milestone();
   io_ctx.set_assert_version(objver);
 
   r = io_ctx.write(oid, bl, bl.length() - 1, 0);
   cout << "io_ctx.write returned " << r << std::endl;
 
-  exit(0);
   r = io_ctx.write(oid, bl, bl.length() - 2, 0);
   cout << "io_ctx.write returned " << r << std::endl;
   r = io_ctx.write(oid, bl, bl.length() - 3, 0);
@@ -181,10 +182,14 @@ int main(int argc, const char **argv)
       cout << s << std::endl;
   }
 
+  cout << "iterating over objects..." << std::endl;
+  int num_objs = 0;
   for (ObjectIterator iter = io_ctx.objects_begin();
-       iter != io_ctx.objects_end(); iter++) {
-    cout << *iter << std::endl;
+       iter != io_ctx.objects_end(); ++iter) {
+    num_objs++;
+    cout << "'" << *iter << "'" << std::endl;
   }
+  cout << "iterated over " << num_objs << " objects." << std::endl;
   map<string, bufferlist> attrset;
   io_ctx.getxattrs(oid, attrset);