C++ CEPH&x2B;Librados++;没有返回一致的结果
嘿,我正在尝试使用CEPH来存储一些数据,但是有一个问题,我通过librados++向CEPH写入数据和从CEPH读取数据是不同的 我有一个简单的例子:C++ CEPH&x2B;Librados++;没有返回一致的结果,c++,C++,嘿,我正在尝试使用CEPH来存储一些数据,但是有一个问题,我通过librados++向CEPH写入数据和从CEPH读取数据是不同的 我有一个简单的例子: #include <iostream> #include <string> #include <rados/librados.hpp> int main(int argc, const char **argv) { int ret = 0; /* Declare the cluster
#include <iostream>
#include <string>
#include <rados/librados.hpp>
int main(int argc, const char **argv)
{
int ret = 0;
/* Declare the cluster handle and required variables. */
librados::Rados cluster;
char cluster_name[] = "ceph";
char user_name[] = "client.admin";
uint64_t flags = 0;
/* Initialize the cluster handle with the "ceph" cluster name and "client.admin" user */
{
ret = cluster.init2(user_name, cluster_name, flags);
if (ret < 0)
{
std::cerr << "Couldn't initialize the cluster handle! error " << ret << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "Created a cluster handle." << std::endl;
}
}
/* Read a Ceph configuration file to configure the cluster handle. */
{
ret = cluster.conf_read_file("/etc/ceph/ceph.conf");
if (ret < 0)
{
std::cerr << "Couldn't read the Ceph configuration file! error " << ret << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "Read the Ceph configuration file." << std::endl;
}
}
/* Read command line arguments */
{
ret = cluster.conf_parse_argv(argc, argv);
if (ret < 0)
{
std::cerr << "Couldn't parse command line options! error " << ret << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "Parsed command line options." << std::endl;
}
}
/* Connect to the cluster */
{
ret = cluster.connect();
if (ret < 0)
{
std::cerr << "Couldn't connect to cluster! error " << ret
<< std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "Connected to the cluster." << std::endl;
}
}
/* Continued from previous C++ example, where cluster handle and
* connection are established. First declare an I/O Context.
*/
librados::IoCtx io_ctx;
const char *pool_name = "data";
{
ret = cluster.ioctx_create(pool_name, io_ctx);
if (ret < 0)
{
std::cerr << "Couldn't set up ioctx! error " << ret << std::endl;
exit(EXIT_FAILURE);
}
else
{
std::cout << "Created an ioctx for the pool." << std::endl;
}
librados::bufferlist bl;
std::string hw = "hello world!";
bl.append(hw.c_str());
ret = io_ctx.write_full("hw", bl);
librados::bufferlist rl;
int read_len = hw.size();
librados::AioCompletion* rc = librados::Rados::aio_create_completion();
ret = io_ctx.aio_read("hw", rc, &rl, read_len, 0);
rc->wait_for_complete();
ret = rc->get_return_value();
if (!(ret < 0))
{
auto out = std::string(rl.c_str());
std::cout << out << std::endl;
}
else
{
std::cout << "da <expletive deleted>?" << std::endl;
}
}
return 0;
}
#包括
#包括
#包括
int main(int argc,常量字符**argv)
{
int-ret=0;
/*声明集群句柄和必需的变量*/
librados::Rados集群;
char cluster_name[]=“ceph”;
char user_name[]=“client.admin”;
uint64_t标志=0;
/*使用“ceph”集群名称和“client.admin”用户初始化集群句柄*/
{
ret=cluster.init2(用户名、集群名称、标志);
如果(ret<0)
{
即使调用c_str()函数,Ceph bufferlist结构实际上也不是以NULL结尾的。(很抱歉,设计不好!)
因此,当您调用bufferlist::c_str()并将其交给字符串构造函数时,它的长度将是任意的,直到它后面的第一个随机空字节为止。您可以通过获取长度并在构造字符串时显式设置来解决此问题。@user4581301感谢您的编辑,并对这个顽皮的单词表示抱歉