设为首页 加入收藏

TOP

【分析】Ceph编程实例接口Librbd(C++)--映像创建与数据读写(四)
2018-03-02 06:56:54 】 浏览:405
Tags:【分析 Ceph 编程 实例 接口 Librbd 映像 创建 数据 读写
uot;image stripe count is " << image.get_stripe_count() << std::endl;

RBD映像数据读写

数据读写 – synchronous

uint64_t ofs_w = (uint64_t) 0; //读写偏移量 uint64_t ofs_r = (uint64_t) 0; size_t len_w = 100; //读写长度 size_t len_r = 100; ceph::bufferlist bl_w; //读写bufferlist ceph::bufferlist bl_r; const char *fn_i = "input"; //读写文件名 const char *fn_o = "output"; std::string error; ret = bl_r.read_file(fn_i, &error); std::cout << "read file ret = " << ret << std::endl; if (ret < 0) { std::cerr << "couldn't read file! err " << ret << std::endl; ret = EXIT_FAILURE; image.close(); //关闭rbd映像 io_ctx.close(); //关闭I/O上下文 rados.shutdown(); //断开集群连接 return EXIT_FAILURE; } else { std::cout << "We just read a file" << std::endl; } ssize_t ret_w = image.write2(ofs_w, len_w, bl_r, 0); ssize_t ret_r = image.read2(ofs_r, len_r, bl_w, 0); ret = bl_w.write_file(fn_o, 0644); std::cout << "write file ret = " << ret << std::endl; if (ret < 0) { std::cerr << "couldn't write file! err " << ret << std::endl; ret = EXIT_FAILURE; image.close(); //关闭rbd映像 io_ctx.close(); //关闭I/O上下文 rados.shutdown(); //断开集群连接 return EXIT_FAILURE; } else { std::cout << "We just wrote a file" << std::endl; }

数据读写 – asynchronous

std::string data = "foo"; uint64_t ofs_aiow = (uint64_t) 100; //读写偏移量 uint64_t ofs_aior = (uint64_t) 100; size_t len_aiow = 600; //读写长度 size_t len_aior = 600; ceph::bufferlist bl_aiow; //读写bufferlist ceph::bufferlist bl_aior; librbd::RBD::AioCompletion *write_completion = new librbd::RBD::AioCompletion( NULL, (librbd::callback_t) simple_write_cb); //读写AioCompletion librbd::RBD::AioCompletion *read_completion = new librbd::RBD::AioCompletion( NULL, (librbd::callback_t) simple_read_cb); for (int i = 0; i < 200; ++i) { bl_aior.append(data); } std::cout << bl_aior.to_str() << std::endl; ret = image.aio_write2(ofs_aiow, len_aiow, bl_aior, write_completion, 0); if (ret < 0) { std::cerr << "couldn't start write! error " << ret << std::endl; ret = EXIT_FAILURE; image.close(); //关闭rbd映像 io_ctx.close(); //关闭I/O上下文 rados.shutdown(); //断开集群连接 return EXIT_FAILURE; } write_completion->wait_for_complete(); //等待写完成 ret_w = write_completion->get_return_value(); if (ret_w < 0) { std::cerr << "couldn't write! error " << ret << std::endl; ret_w = EXIT_FAILURE; image.close(); //关闭rbd映像 io_ctx.close(); //关闭I/O上下文 rados.shutdown(); //断开集群连接 return EXIT_FAILURE; } else { std::cout << "we just write data successfully, return value is " << ret_w << std::endl; } ret = image.aio_read2(ofs_aior, len_aior, bl_aiow, read_completion, 0); if (ret < 0) { std::cerr << "couldn't start read! error " << ret << std::endl; ret = EXIT_FAILURE; image.close(); //关闭rbd映像 io_ctx.close(); //关闭I/O上下文 rados.shutdown(); //断开集群连接 return EXIT_FAILURE; } read_completion->wait_for_complete(); //等待读完成 ret_r = read_completion->get_return_value(); if (ret_r < 0) { std::cerr << "couldn't read! error " << ret << std::endl; ret_r = EXIT_FAILURE; image.close(); //关闭rbd映像 io_ctx.close(); //关闭I/O上下文 rados.shutdown(); //断开集群连接 return EXIT_FAILURE; }
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++中struct和class的区别介绍 下一篇C++中的类和对象实例讲解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目