Advertisement
Guest User

Librados example

a guest
May 2nd, 2014
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <rados/librados.hpp>
  2. #include <string>
  3. #include <list>
  4.  
  5. int main(int argc, const char **argv)
  6. {
  7.   int ret = 0;
  8.  
  9.   /*
  10.    * Errors are not checked to avoid pollution.
  11.    * After each Ceph operation:
  12.    * if (ret < 0) error_condition
  13.    * else success
  14.    */
  15.  
  16.   // Get cluster handle and connect to cluster
  17.   std::string cluster_name("ceph");
  18.   std::string user_name("client.admin");
  19.   librados::Rados cluster;
  20.   cluster.init2(user_name.c_str(), cluster_name.c_str(), 0);
  21.   cluster.conf_read_file("ceph.conf");
  22.   cluster.connect();
  23.  
  24.   // IO context
  25.   librados::IoCtx io_ctx;
  26.   std::string pool_name("data");
  27.   cluster.ioctx_create(pool_name.c_str(), io_ctx);
  28.  
  29.   // Write an object synchronously
  30.   librados::bufferlist bl;
  31.   std::string objectId("hw");
  32.   std::string objectContent("Hello World!");
  33.   bl.append(objectContent);
  34.   io_ctx.write(objectId, bl, objectContent.size(), 0);
  35.  
  36.   // Add an xattr to the object.
  37.   librados::bufferlist lang_bl;
  38.   lang_bl.append("en_US");
  39.   io_ctx.setxattr(objectId, "lang", lang_bl);
  40.  
  41.   // Read the object back asynchronously
  42.   librados::bufferlist read_buf;
  43.   int read_len = 4194304;
  44.   //Create I/O Completion.
  45.   librados::AioCompletion *read_completion = librados::Rados::aio_create_completion();
  46.   //Send read request.
  47.   io_ctx.aio_read(objectId, read_completion, &read_buf, read_len, 0);
  48.  
  49.   // Wait for the request to complete, and print content
  50.   read_completion->wait_for_complete();
  51.   read_completion->get_return_value();
  52.   std::cout << "Object name: " << objectId << "\n"
  53.             << "Content: " << read_buf.c_str() << std::endl;
  54.  
  55.   // Read the xattr.
  56.   librados::bufferlist lang_res;
  57.   io_ctx.getxattr(objectId, "lang", lang_res);
  58.   std::cout << "Object xattr: " << lang_res.c_str() << std::endl;
  59.  
  60.  
  61.   // Print the list of pools
  62.   std::list<std::string> pools;
  63.   cluster.pool_list(pools);
  64.   std::cout << "List of pools from this cluster handle" << std::endl;
  65.   for (auto pool_id : pools) {
  66.     std::cout << "\t" << pool_id << std::endl;
  67.   }
  68.  
  69.   // Print the list of objects
  70.   librados::ObjectIterator oit = io_ctx.objects_begin();
  71.   librados::ObjectIterator oet = io_ctx.objects_end();
  72.   std::cout << "List of objects from this pool" << std::endl;
  73.   for (; oit != oet; oit++) {
  74.     std::cout << "\t" << oit->first << std::endl;
  75.   }
  76.  
  77.   // Remove the xattr
  78.   io_ctx.rmxattr(objectId, "lang");
  79.  
  80.   // Remove the object.
  81.   io_ctx.remove(objectId);
  82.  
  83.   // Cleanup
  84.   io_ctx.close();
  85.   cluster.shutdown();
  86.  
  87.   return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement