]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: make C++ api nicer
authorJosh Durgin <josh.durgin@dreamhost.com>
Thu, 24 Feb 2011 21:41:55 +0000 (13:41 -0800)
committerJosh Durgin <josh.durgin@dreamhost.com>
Thu, 24 Feb 2011 21:41:55 +0000 (13:41 -0800)
Adds Image class and replaces aio_create_completion with a constructor.

Signed-off-by: Josh Durgin <josh.durgin@dreamhost.com>
src/include/rbd/librbd.hpp
src/librbd.cc

index 2d8dcafb3a3193aa1c058006e2124c3a957239c6..a396b5c94a46905b32bf294d585e244b22b01607 100644 (file)
@@ -28,8 +28,8 @@ namespace librbd {
 
   using librados::pool_t;
 
-  class RBDClient;
-  typedef void *image_t;
+  class Image;
+  typedef void *image_ctx_t;
   typedef void *completion_t;
   typedef void (*callback_t)(completion_t cb, void *arg);
 
@@ -49,46 +49,60 @@ public:
 
   struct AioCompletion {
     void *pc;
-    AioCompletion(void *_pc) : pc(_pc) {}
+    AioCompletion(void *cb_arg, callback_t complete_cb);
     int wait_for_complete();
     int get_return_value();
     void release();
   };
 
-  /* We don't allow assignment or copying */
-  RBD(const RBD& rhs);
-  const RBD& operator=(const RBD& rhs);
-
   void version(int *major, int *minor, int *extra);
 
+  Image *image_open(pool_t pool, const char *name);
+  Image *image_open(pool_t pool, const char *name, const char *snapname);
   int list(pool_t pool, std::vector<std::string>& names);
   int create(pool_t pool, const char *name, size_t size, int *order);
   int remove(pool_t pool, const char *name);
   int copy(pool_t src_pool, const char *srcname, pool_t dest_pool, const char *destname);
   int rename(pool_t src_pool, const char *srcname, const char *destname);
 
-  int open(pool_t pool, const char *name, image_t *image, const char *snap_name = NULL);
-  int close(image_t image);
-  int resize(image_t image, size_t size);
-  int stat(image_t image, image_info_t& info, size_t infosize);
+private:
+  /* We don't allow assignment or copying */
+  RBD(const RBD& rhs);
+  const RBD& operator=(const RBD& rhs);
+};
+
+class Image
+{
+public:
+  Image();
+  Image(image_ctx_t ctx_);
+  ~Image();
+
+  void close();
+  int resize(size_t size);
+  int stat(image_info_t &info, size_t infosize);
 
   /* snapshots */
-  int snap_list(image_t image, std::vector<snap_info_t>& snaps);
-  int snap_create(image_t image, const char *snap_name);
-  int snap_remove(image_t image, const char *snap_name);
-  int snap_rollback(image_t image, const char *snap_name);
-  int snap_set(image_t image, const char *snap_name);
+  int snap_list(std::vector<snap_info_t>& snaps);
+  int snap_create(const char *snapname);
+  int snap_remove(const char *snapname);
+  int snap_rollback(const char *snap_name);
+  int snap_set(const char *snap_name);
 
   /* I/O */
-  int read(image_t image, off_t ofs, size_t len, ceph::bufferlist& bl);
-  int read_iterate(image_t image, off_t ofs, size_t len,
+  int read(off_t ofs, size_t len, ceph::bufferlist& bl);
+  int read_iterate(off_t ofs, size_t len,
                    int (*cb)(off_t, size_t, const char *, void *), void *arg);
-  int write(image_t image, off_t ofs, size_t len, ceph::bufferlist& bl);
+  int write(off_t ofs, size_t len, ceph::bufferlist& bl);
+
+  int aio_write(off_t off, size_t len, ceph::bufferlist& bl, RBD::AioCompletion *c);
+  int aio_read(off_t off, size_t len, ceph::bufferlist& bl, RBD::AioCompletion *c);
+
+private:
+  Image(const Image& rhs);
+  const Image& operator=(const Image& rhs);
 
-  AioCompletion *aio_create_completion(void *cb_arg, callback_t complete_cb);
-  int aio_write(image_t image, off_t off, size_t len, ceph::bufferlist& bl,
-                AioCompletion *c);
-  int aio_read(image_t image, off_t off, size_t len, ceph::bufferlist& bl, AioCompletion *c);
+  image_ctx_t ctx;
 };
 
 }
index cf90ad54cb8b2e4cdff32eb490935791a6594b4e..43717718e17a4055c3edf1bac9e819017ab558da 100644 (file)
@@ -1341,29 +1341,30 @@ RBD::~RBD()
 {
 }
 
-void librbd::RBD::version(int *major, int *minor, int *extra)
+void RBD::version(int *major, int *minor, int *extra)
 {
   rbd_version(major, minor, extra);
 }
 
-int librbd::RBD::open(pool_t pool, const char *name, image_t *image, const char *snap_name)
+Image *RBD::image_open(pool_t pool, const char *name)
 {
-  ImageCtx *ictx = new ImageCtx(name, pool);
-  if (!ictx)
-    return -ENOMEM;
-  int r = librbd::open_image(pool, ictx, name, snap_name);
-  *image = (image_t)ictx;
-  return r;
+  return image_open(pool, name, NULL);
 }
 
-int librbd::RBD::close(image_t image)
+Image *RBD::image_open(pool_t pool, const char *name, const char *snapname)
 {
-  ImageCtx *ctx = (ImageCtx *)image;
-  librbd::close_image(ctx);
-  return 0; 
+  ImageCtx *ictx = new ImageCtx(name, pool);
+  if (!ictx)
+    return NULL;
+  int r = librbd::open_image(pool, ictx, name, snapname);
+  if (r < 0)
+    return NULL;
+
+  Image *image = new Image((image_ctx_t)ictx);
+  return image;
 }
 
-int librbd::RBD::create(pool_t pool, const char *name, size_t size, int *order)
+int RBD::create(pool_t pool, const char *name, size_t size, int *order)
 {
   string md_oid = name;
   md_oid += RBD_SUFFIX;
@@ -1371,146 +1372,165 @@ int librbd::RBD::create(pool_t pool, const char *name, size_t size, int *order)
   return r;
 }
 
-int librbd::RBD::remove(pool_t pool, const char *name)
+int RBD::remove(pool_t pool, const char *name)
 {
   int r = librbd::remove(pool, name);
   return r;
 }
 
-int librbd::RBD::resize(image_t image, size_t size)
+int RBD::list(pool_t pool, std::vector<std::string>& names)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
-  int r = librbd::resize(ictx, size);
+  int r = librbd::list(pool, names);
   return r;
 }
 
-int librbd::RBD::stat(image_t image, image_info_t& info, size_t infosize)
+int RBD::copy(pool_t src_pool, const char *srcname, pool_t dest_pool, const char *destname)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
-  int r = librbd::info(ictx, info, infosize);
+  int r = librbd::copy(src_pool, srcname, dest_pool, destname);
   return r;
 }
 
-int librbd::RBD::list(pool_t pool, std::vector<std::string>& names)
+int RBD::rename(pool_t src_pool, const char *srcname, const char *destname)
 {
-  int r = librbd::list(pool, names);
+  int r = librbd::rename(src_pool, srcname, destname);
   return r;
 }
 
-int librbd::RBD::copy(pool_t src_pool, const char *srcname, pool_t dest_pool, const char *destname)
+RBD::AioCompletion::AioCompletion(void *cb_arg, callback_t complete_cb)
 {
-  int r = librbd::copy(src_pool, srcname, dest_pool, destname);
+  librbd::AioCompletion *c = librbd::aio_create_completion(cb_arg, complete_cb);
+  pc = (void *)c;
+  c->rbd_comp = this;
+}
+
+int RBD::AioCompletion::wait_for_complete()
+{
+  librbd::AioCompletion *c = (librbd::AioCompletion *)pc;
+  return c->wait_for_complete();
+}
+
+int RBD::AioCompletion::get_return_value()
+{
+  librbd::AioCompletion *c = (librbd::AioCompletion *)pc;
+  return c->get_return_value();
+}
+
+void RBD::AioCompletion::release()
+{
+  librbd::AioCompletion *c = (librbd::AioCompletion *)pc;
+  c->release();
+}
+
+/*
+  Image
+*/
+
+Image::Image()
+{
+}
+
+Image::Image(image_ctx_t ctx_) : ctx(ctx_)
+{
+}
+
+Image::~Image()
+{
+}
+
+void Image::close()
+{
+  ImageCtx *ictx = (ImageCtx *)ctx;
+  close_image(ictx);
+}
+
+int Image::resize(size_t size)
+{
+  ImageCtx *ictx = (ImageCtx *)ctx;
+  int r = librbd::resize(ictx, size);
   return r;
 }
 
-int librbd::RBD::rename(pool_t src_pool, const char *srcname, const char *destname)
+int Image::stat(image_info_t& info, size_t infosize)
 {
-  int r = librbd::rename(src_pool, srcname, destname);
+  ImageCtx *ictx = (ImageCtx *)ctx;
+  int r = librbd::info(ictx, info, infosize);
   return r;
 }
 
-int librbd::RBD::snap_create(image_t image, const char *snap_name)
+
+int Image::snap_create(const char *snap_name)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   int r = librbd::snap_create(ictx, snap_name);
   return r;
 }
 
-int librbd::RBD::snap_remove(image_t image, const char *snap_name)
+int Image::snap_remove(const char *snap_name)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   int r = librbd::snap_remove(ictx, snap_name);
   return r;
 }
 
-int librbd::RBD::snap_rollback(image_t image, const char *snap_name)
+int Image::snap_rollback(const char *snap_name)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   int r = librbd::snap_rollback(ictx, snap_name);
   return r;
 }
 
-int librbd::RBD::snap_list(image_t image, std::vector<librbd::snap_info_t>& snaps)
+int Image::snap_list(std::vector<librbd::snap_info_t>& snaps)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   return librbd::snap_list(ictx, snaps);
 }
 
-int librbd::RBD::snap_set(image_t image, const char *snap_name)
+int Image::snap_set(const char *snap_name)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   return librbd::snap_set(ictx, snap_name);
 }
 
-int librbd::RBD::read(image_t image, off_t ofs, size_t len, bufferlist& bl)
+int Image::read(off_t ofs, size_t len, bufferlist& bl)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   bufferptr ptr(len);
   bl.push_back(ptr);
   return librbd::read(ictx, ofs, len, bl.c_str());
 }
 
-int librbd::RBD::read_iterate(image_t image, off_t ofs, size_t len,
+int Image::read_iterate(off_t ofs, size_t len,
                    int (*cb)(off_t, size_t, const char *, void *), void *arg)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   return librbd::read_iterate(ictx, ofs, len, cb, arg);
 }
 
-int librbd::RBD::write(image_t image, off_t ofs, size_t len, bufferlist& bl)
+int Image::write(off_t ofs, size_t len, bufferlist& bl)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   if (bl.length() < len)
     return -EINVAL;
   return librbd::write(ictx, ofs, len, bl.c_str());
 }
 
-
-librbd::RBD::AioCompletion *librbd::RBD::aio_create_completion(void *cb_arg, callback_t complete_cb)
-{
-  librbd::AioCompletion *c = librbd::aio_create_completion(cb_arg, complete_cb);
-  RBD::AioCompletion *rbd_completion = new AioCompletion(c);
-  c->rbd_comp = rbd_completion;
-  return rbd_completion;
-}
-
-int librbd::RBD::aio_write(image_t image, off_t off, size_t len, bufferlist& bl,
-                          AioCompletion *c)
+int Image::aio_write(off_t off, size_t len, bufferlist& bl, RBD::AioCompletion *c)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   if (bl.length() < len)
     return -EINVAL;
   return librbd::aio_write(ictx, off, len, bl.c_str(), (librbd::AioCompletion *)c->pc);
 }
 
-int librbd::RBD::aio_read(image_t image, off_t off, size_t len, bufferlist& bl,
-                         AioCompletion *c)
+int Image::aio_read(off_t off, size_t len, bufferlist& bl, RBD::AioCompletion *c)
 {
-  ImageCtx *ictx = (ImageCtx *)image;
+  ImageCtx *ictx = (ImageCtx *)ctx;
   bufferptr ptr(len);
   bl.push_back(ptr);
-  dout(10) << "librbd::RBD::aio_read() buf=" << (void *)bl.c_str() << "~" << (void *)(bl.c_str() + len - 1) << dendl;
+  dout(10) << "Image::aio_read() buf=" << (void *)bl.c_str() << "~" << (void *)(bl.c_str() + len - 1) << dendl;
   return librbd::aio_read(ictx, off, len, bl.c_str(), (librbd::AioCompletion *)c->pc);
 }
 
-int librbd::RBD::AioCompletion::wait_for_complete()
-{
-  librbd::AioCompletion *c = (librbd::AioCompletion *)pc;
-  return c->wait_for_complete();
-}
-
-int librbd::RBD::AioCompletion::get_return_value()
-{
-  librbd::AioCompletion *c = (librbd::AioCompletion *)pc;
-  return c->get_return_value();
-}
-
-void librbd::RBD::AioCompletion::release()
-{
-  librbd::AioCompletion *c = (librbd::AioCompletion *)pc;
-  c->release();
-}
-
 } // namespace librbd
 
 extern "C" void rbd_version(int *major, int *minor, int *extra)
@@ -1692,9 +1712,7 @@ extern "C" int rbd_write(rbd_image_t image, off_t ofs, size_t len, const char *b
 
 extern "C" int rbd_aio_create_completion(void *cb_arg, rbd_callback_t complete_cb, rbd_completion_t *c)
 {
-  librbd::AioCompletion *comp = librbd::aio_create_completion(cb_arg, complete_cb);
-  librbd::RBD::AioCompletion *rbd_comp = new librbd::RBD::AioCompletion(comp);
-  comp->rbd_comp = rbd_comp;
+  librbd::RBD::AioCompletion *rbd_comp = new librbd::RBD::AioCompletion(cb_arg, complete_cb);
   *c = (rbd_completion_t) rbd_comp;
   return 0;
 }