|
#include <iostream>
|
|
#include <string>
|
|
#include <rados/librados.hpp>
|
|
|
|
int main(int argc, const char **argv)
|
|
{
|
|
|
|
/* Continued from previous C++ example, where cluster handle and
|
|
* * connection are established. First declare an I/O Context.
|
|
* */
|
|
|
|
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;
|
|
|
|
/* 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;
|
|
ret = EXIT_FAILURE;
|
|
return 1;
|
|
} 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;
|
|
ret = EXIT_FAILURE;
|
|
return 1;
|
|
} else {
|
|
std::cout << "Read the Ceph configuration file." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Connect to the cluster */
|
|
{
|
|
ret = cluster.connect();
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't connect to cluster! error " << ret << std::endl;
|
|
ret = EXIT_FAILURE;
|
|
return 1;
|
|
} else {
|
|
std::cout << "Connected to the cluster." << std::endl;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
librados::IoCtx io_ctx;
|
|
const char *pool_name = "cephlrc";
|
|
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
/* Write an object synchronously. */
|
|
{
|
|
char hello[4096] = {0,};
|
|
(void) memcpy (hello, "hello world", 11);
|
|
librados::bufferlist bl;
|
|
bl.append(hello, 4096);
|
|
|
|
int i=5000;
|
|
while (i > 0) {
|
|
ret = io_ctx.append("another2", bl, 4096);
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't write object! error " << ret << std::endl;
|
|
// exit(EXIT_FAILURE);
|
|
} else {
|
|
std::cout << "Wrote new object 'newobj' " << std::endl;
|
|
}
|
|
i--;
|
|
}
|
|
}
|
|
|
|
{
|
|
std::cout <<io_ctx.pool_required_alignment();
|
|
}
|
|
|
|
}
|