bool select_task(const std::string& query, CassConsistency consistency, int num_iterations) { bool is_successful = true; test_utils::CassStatementPtr statement(cass_statement_new(query.c_str(), 0)); cass_statement_set_consistency(statement.get(), consistency); for (int i = 0; i < num_iterations; ++i) { test_utils::CassFuturePtr future(cass_session_execute(session, statement.get())); cass_future_wait(future.get()); CassError code = cass_future_error_code(future.get()); if (code != CASS_OK && code != CASS_ERROR_LIB_REQUEST_TIMED_OUT && code != CASS_ERROR_SERVER_READ_TIMEOUT) { // Timeout is okay CassString message; cass_future_error_message(future.get(), &message.data, &message.length); fprintf(stderr, "Error occurred during select '%.*s'\n", static_cast<int>(message.length), message.data); is_successful = false; } if (code == CASS_OK) { test_utils::CassResultPtr result(cass_future_get_result(future.get())); if (cass_result_row_count(result.get()) == 0) { fprintf(stderr, "No rows returned from query\n"); is_successful = false; } } } return is_successful; }
bool execute_insert(CassSession* session, const std::string& table_name) { std::string query = str(boost::format("INSERT INTO %s (id, event_time, text_sample) VALUES (?, ?, ?)") % table_name); test_utils::CassStatementPtr statement(cass_statement_new(query.c_str(), 3)); // Determine if bound parameters can be used based on C* version if (version.major == 1) { test_utils::CassPreparedPtr prepared = test_utils::prepare(session, query.c_str()); statement = test_utils::CassStatementPtr(cass_prepared_bind(prepared.get())); } boost::chrono::system_clock::time_point now(boost::chrono::system_clock::now()); boost::chrono::milliseconds event_time(boost::chrono::duration_cast<boost::chrono::milliseconds>(now.time_since_epoch())); std::string text_sample(test_utils::string_from_time_point(now)); cass_statement_bind_uuid(statement.get(), 0, test_utils::generate_time_uuid(uuid_gen)); cass_statement_bind_int64(statement.get(), 1, event_time.count()); cass_statement_bind_string(statement.get(), 2, text_sample.c_str()); test_utils::CassFuturePtr future(cass_session_execute(session, statement.get())); cass_future_wait(future.get()); CassError code = cass_future_error_code(future.get()); if (code != CASS_OK && code != CASS_ERROR_LIB_REQUEST_TIMED_OUT) { // Timeout is okay CassString message; cass_future_error_message(future.get(), &message.data, &message.length); fprintf(stderr, "Error occurred during insert '%.*s'\n", static_cast<int>(message.length), message.data); return false; } return true; }
const CassResult* Connection::execute(CassStatement* statement) { this->log("debug", "Connection::execute"); CassFuture* result = cass_session_execute(this->_session, statement); CassError error = cass_future_error_code(result); if (error != CASS_OK) { CassString message = cass_future_error_message(result); cass_future_free(result); std::ostringstream out; out << cass_error_desc(error) << ": " << message.data; this->log("error", out.str()); throw Php::Exception(out.str()); return nullptr; } const CassResult* res = cass_future_get_result(result); if (res == nullptr) { throw Php::Exception("Error executing query, result is null"); } cass_future_free(result); return res; }
void wait_and_check_error(CassFuture* future, cass_duration_t timeout) { CassError code = wait_and_return_error(future, timeout); if (code != CASS_OK) { CassString message; cass_future_error_message(future, &message.data, &message.length); BOOST_FAIL("Error occurred during query '" << std::string(message.data, message.length) << "' (" << boost::format("0x%08X") % code << ")"); } }
/** * librdf_storage_cassandra_context_add_statement: * @storage: #librdf_storage object * @context_node: #librdf_node object * @statement: #librdf_statement statement to add * * Add a statement to a storage context. * * Return value: non 0 on failure **/ static int librdf_storage_cassandra_context_add_statement(librdf_storage* storage, librdf_node* context_node, librdf_statement* statement) { char* s; char* p; char* o; char* c; statement_helper(storage, statement, context_node, &s, &p, &o, &c); librdf_storage_cassandra_instance* context; context = (librdf_storage_cassandra_instance*)storage->instance; char* query = "BEGIN BATCH " " INSERT INTO rdf.spo (s, p, o) VALUES (?, ?, ?);" " INSERT INTO rdf.pos (s, p, o) VALUES (?, ?, ?);" " INSERT INTO rdf.osp (s, p, o) VALUES (?, ?, ?);" "APPLY BATCH;"; CassStatement* stmt = cass_statement_new(query, 9); cass_statement_bind_string(stmt, 0, s); cass_statement_bind_string(stmt, 1, p); cass_statement_bind_string(stmt, 2, o); cass_statement_bind_string(stmt, 3, s); cass_statement_bind_string(stmt, 4, p); cass_statement_bind_string(stmt, 5, o); cass_statement_bind_string(stmt, 6, s); cass_statement_bind_string(stmt, 7, p); cass_statement_bind_string(stmt, 8, o); if (s) free(s); if (p) free(p); if (o) free(o); CassFuture* future = cass_session_execute(context->session, stmt); cass_statement_free(stmt); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); const char* msg; size_t msg_len; cass_future_error_message(future, &msg, &msg_len); fprintf(stderr, "Cassandra: %*s\n", msg_len, msg); cass_future_free(future); return -1; } cass_future_free(future); return 0; }
static int cassandra_results_stream_end_of_stream(void* context) { cassandra_results_stream* scontext; scontext = (cassandra_results_stream*)context; if (scontext->at_end) { if (scontext->more_pages) { CassError rc; rc = cass_statement_set_paging_state(scontext->stmt, scontext->result); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); return -1; } cass_result_free(scontext->result); scontext->result = 0; CassFuture* future = cass_session_execute(scontext->cassandra_context->session, scontext->stmt); rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); const char* msg; size_t msg_len; cass_future_error_message(future, &msg, &msg_len); fprintf(stderr, "Cassandra: %*s\n", msg_len, msg); cass_future_free(future); return -1; } scontext->result = cass_future_get_result(future); cass_future_free(future); scontext->iter = cass_iterator_from_result(scontext->result); scontext->more_pages = cass_result_has_more_pages(scontext->result); scontext->at_end = !cass_iterator_next(scontext->iter); } } return (scontext->at_end); }
bool execute_insert(CassStatement* statement) { test_utils::CassFuturePtr future(cass_session_execute(session, statement)); cass_future_wait(future.get()); CassError code = cass_future_error_code(future.get()); if (code != CASS_OK && code != CASS_ERROR_LIB_REQUEST_TIMED_OUT) { // Timeout is okay CassString message; cass_future_error_message(future.get(), &message.data, &message.length); fprintf(stderr, "Error occurred during insert '%.*s'\n", static_cast<int>(message.length), message.data); return false; } return true; }
std::string get_executing_host(test_utils::CassSessionPtr session) { std::stringstream query; query << "SELECT * FROM " << (version >= "3.0.0" ? "system_schema.keyspaces" : "system.schema_keyspaces"); test_utils::CassStatementPtr statement(cass_statement_new(query.str().c_str(), 0)); test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get())); if (cass_future_error_code(future.get()) == CASS_OK) { return cass::get_host_from_future(future.get()); } else { CassString message; cass_future_error_message(future.get(), &message.data, &message.length); std::cerr << "Failed to query host: " << std::string(message.data, message.length) << std::endl; } return ""; }
/** * Create the session * * @param is_timeout True if for timeout tests; false otherwise */ void create_session(bool is_timeout = false) { close_session(); test_utils::CassSessionPtr session(cass_session_new()); test_utils::CassFuturePtr connect_future(cass_session_connect(session.get(), cluster_.get())); CassError error_code = test_utils::wait_and_return_error(connect_future.get()); session_ = test_utils::create_session(cluster_.get(), &error_code); if (error_code != CASS_OK) { if (is_timeout) { if (error_code == CASS_ERROR_LIB_NO_HOSTS_AVAILABLE) { return; } } CassString message; cass_future_error_message(connect_future.get(), &message.data, &message.length); BOOST_FAIL(std::string(message.data, message.length) << "' (" << cass_error_desc(error_code) << ")"); } }
static int librdf_storage_cassandra_size(librdf_storage* storage) { librdf_storage_cassandra_instance* context; context = (librdf_storage_cassandra_instance*)storage->instance; char* query = "SELECT count(s) FROM rdf.spo"; CassStatement* stmt = cass_statement_new(query, 0); CassFuture* future = cass_session_execute(context->session, stmt); cass_statement_free(stmt); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); const char* msg; size_t msg_len; cass_future_error_message(future, &msg, &msg_len); fprintf(stderr, "Cassandra: %*s\n", msg_len, msg); cass_future_free(future); return 0; } const CassResult* result = cass_future_get_result(future); cass_future_free(future); const CassRow* row = cass_result_first_row(result); int64_t count; cass_value_get_int64(cass_row_get_column(row, 0), &count); cass_result_free(result); return count; }
bool client_thread(CassSession* session, const std::string table_name) { std::string query = str(boost::format("SELECT * FROM %s LIMIT 10000") % table_name); for (int i = 0 ; i < 10; ++i) execute_insert(session, table_name); boost::posix_time::ptime start = boost::posix_time::second_clock::universal_time(); test_utils::CassStatementPtr statement(cass_statement_new(query.c_str(), 0)); cass_statement_set_consistency(statement.get(), CASS_CONSISTENCY_ONE); while ((boost::posix_time::second_clock::universal_time() - start).total_seconds() < TEST_DURATION_SECS) { test_utils::CassFuturePtr future(cass_session_execute(session, statement.get())); cass_future_wait(future.get()); CassError code = cass_future_error_code(future.get()); if (code != CASS_OK && code != CASS_ERROR_LIB_REQUEST_TIMED_OUT && code != CASS_ERROR_SERVER_READ_TIMEOUT) { // Timeout is okay CassString message; cass_future_error_message(future.get(), &message.data, &message.length); fprintf(stderr, "Error occurred during select '%.*s'\n", static_cast<int>(message.length), message.data); is_done = true; return false; } if (code == CASS_OK) { test_utils::CassResultPtr result(cass_future_get_result(future.get())); if (cass_result_row_count(result.get()) == 0) { fprintf(stderr, "No rows returned from query\n"); is_done = true; return false; } } } is_done = true; return true; }
void check_for_live_hosts(test_utils::CassSessionPtr session, const std::set<std::string>& should_be_present) { std::set<std::string> hosts; for (size_t i = 0; i < should_be_present.size() + 1; ++i) { const char* query = "SELECT * FROM system.schema_keyspaces"; test_utils::CassStatementPtr statement(cass_statement_new(query, 0)); test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get())); if (cass_future_error_code(future.get()) == CASS_OK) { hosts.insert(cass::get_host_from_future(future.get())); } else { CassString message; cass_future_error_message(future.get(), &message.data, &message.length); BOOST_MESSAGE("Failed to query host: " << std::string(message.data, message.length)); } } BOOST_CHECK(hosts.size() == should_be_present.size()); for (std::set<std::string>::const_iterator it = should_be_present.begin(); it != should_be_present.end(); ++it) { BOOST_CHECK(hosts.count(*it) > 0); } }
void check_for_live_hosts(test_utils::CassSessionPtr session, const std::set<std::string>& should_be_present) { std::set<std::string> hosts; std::stringstream query; query << "SELECT * FROM " << (version >= "3.0.0" ? "system_schema.keyspaces" : "system.schema_keyspaces"); for (size_t i = 0; i < should_be_present.size() + 2; ++i) { test_utils::CassStatementPtr statement(cass_statement_new(query.str().c_str(), 0)); test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get())); if (cass_future_error_code(future.get()) == CASS_OK) { hosts.insert(cass::get_host_from_future(future.get())); } else { CassString message; cass_future_error_message(future.get(), &message.data, &message.length); std::cerr << "Failed to query host: " << std::string(message.data, message.length) << std::endl; } } BOOST_CHECK(hosts.size() == should_be_present.size()); for (std::set<std::string>::const_iterator it = should_be_present.begin(); it != should_be_present.end(); ++it) { BOOST_CHECK(hosts.count(*it) > 0); } }
void print_error(CassFuture* future) { const char* message; size_t message_length; cass_future_error_message(future, &message, &message_length); fprintf(stderr, "Error: %.*s\n", (int)message_length, message); }
int main(int argc, char* argv[]) { /* Setup and connect to cluster */ CassFuture* connect_future = NULL; CassCluster* cluster = cass_cluster_new(); CassSession* session = cass_session_new(); CassSsl* ssl = cass_ssl_new(); char* hosts = "127.0.0.1"; if (argc > 1) { hosts = argv[1]; } cass_cluster_set_contact_points(cluster, hosts); /* Only verify the certification and not the identity */ cass_ssl_set_verify_flags(ssl, CASS_SSL_VERIFY_PEER_CERT); if (!load_trusted_cert_file("cert.pem", ssl)) { fprintf(stderr, "Failed to load certificate disabling peer verification\n"); cass_ssl_set_verify_flags(ssl, CASS_SSL_VERIFY_NONE); } cass_cluster_set_ssl(cluster, ssl); connect_future = cass_session_connect(session, cluster); if (cass_future_error_code(connect_future) == CASS_OK) { CassFuture* close_future = NULL; /* Build statement and execute query */ const char* query = "SELECT release_version FROM system.local"; CassStatement* statement = cass_statement_new(query, 0); CassFuture* result_future = cass_session_execute(session, statement); if (cass_future_error_code(result_future) == CASS_OK) { /* Retrieve result set and get the first row */ const CassResult* result = cass_future_get_result(result_future); const CassRow* row = cass_result_first_row(result); if (row) { const CassValue* value = cass_row_get_column_by_name(row, "release_version"); const char* release_version; size_t release_version_length; cass_value_get_string(value, &release_version, &release_version_length); printf("release_version: '%.*s'\n", (int)release_version_length, release_version); } cass_result_free(result); } else { /* Handle error */ const char* message; size_t message_length; cass_future_error_message(result_future, &message, &message_length); fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, message); } cass_statement_free(statement); cass_future_free(result_future); /* Close the session */ close_future = cass_session_close(session); cass_future_wait(close_future); cass_future_free(close_future); } else { /* Handle error */ const char* message; size_t message_length; cass_future_error_message(connect_future, &message, &message_length); fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message); } cass_future_free(connect_future); cass_cluster_free(cluster); cass_session_free(session); cass_ssl_free(ssl); return 0; }
void CassDriver::PrintError(CassFuture* future) { CassString message = cass_future_error_message(future); fprintf(stderr, "Error: %.*s\n", (int)message.length, message.data); }
/** * Get the error message of the future if an error occurred * * @return Error message */ const std::string error_message() { const char* message; size_t message_length; cass_future_error_message(get(), &message, &message_length); return std::string(message, message_length); }
int main(int argc, char* argv[]) { /* Setup and connect to cluster */ CassFuture* connect_future = NULL; CassCluster* cluster = cass_cluster_new(); CassSession* session = cass_session_new(); char* hosts = "127.0.0.1"; if (argc > 1) { hosts = argv[1]; } /* Add contact points */ cass_cluster_set_contact_points(cluster, hosts); /* Provide the cluster object as configuration to connect the session */ connect_future = cass_session_connect(session, cluster); if (cass_future_error_code(connect_future) == CASS_OK) { CassFuture* close_future = NULL; /* Build statement and execute query */ const char* query = "SELECT keyspace_name " "FROM system.schema_keyspaces;"; CassStatement* statement = cass_statement_new(query, 0); CassFuture* result_future = cass_session_execute(session, statement); if (cass_future_error_code(result_future) == CASS_OK) { /* Retrieve result set and iterate over the rows */ const CassResult* result = cass_future_get_result(result_future); CassIterator* rows = cass_iterator_from_result(result); while (cass_iterator_next(rows)) { const CassRow* row = cass_iterator_get_row(rows); const CassValue* value = cass_row_get_column_by_name(row, "keyspace_name"); const char* keyspace_name; size_t keyspace_name_length; cass_value_get_string(value, &keyspace_name, &keyspace_name_length); printf("keyspace_name: '%.*s'\n", (int)keyspace_name_length, keyspace_name); } cass_result_free(result); cass_iterator_free(rows); } else { /* Handle error */ const char* message; size_t message_length; cass_future_error_message(result_future, &message, &message_length); fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, message); } cass_statement_free(statement); cass_future_free(result_future); /* Close the session */ close_future = cass_session_close(session); cass_future_wait(close_future); cass_future_free(close_future); } else { /* Handle error */ const char* message; size_t message_length; cass_future_error_message(connect_future, &message, &message_length); fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message); } cass_future_free(connect_future); cass_cluster_free(cluster); cass_session_free(session); return 0; }
static int librdf_storage_cassandra_add_statements(librdf_storage* storage, librdf_stream* statement_stream) { librdf_storage_cassandra_instance* context; context = (librdf_storage_cassandra_instance*)storage->instance; CassBatch* batch = 0; const int batch_size = 10; int rows = 0; for(; !librdf_stream_end(statement_stream); librdf_stream_next(statement_stream)) { librdf_statement* statement; librdf_node* context_node; statement = librdf_stream_get_object(statement_stream); context_node = librdf_stream_get_context2(statement_stream); if(!statement) { break; } char* s; char* p; char* o; char* c; statement_helper(storage, statement, context_node, &s, &p, &o, &c); if (batch == 0) batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED); char* query = "INSERT INTO rdf.spo (s, p, o) VALUES (?, ?, ?);"; CassStatement* stmt = cass_statement_new(query, 3); cass_statement_bind_string(stmt, 0, s); cass_statement_bind_string(stmt, 1, p); cass_statement_bind_string(stmt, 2, o); cass_batch_add_statement(batch, stmt); cass_statement_free(stmt); query = "INSERT INTO rdf.pos (s, p, o) VALUES (?, ?, ?);"; stmt = cass_statement_new(query, 3); cass_statement_bind_string(stmt, 0, s); cass_statement_bind_string(stmt, 1, p); cass_statement_bind_string(stmt, 2, o); cass_batch_add_statement(batch, stmt); cass_statement_free(stmt); query = "INSERT INTO rdf.osp (s, p, o) VALUES (?, ?, ?);"; stmt = cass_statement_new(query, 3); cass_statement_bind_string(stmt, 0, s); cass_statement_bind_string(stmt, 1, p); cass_statement_bind_string(stmt, 2, o); cass_batch_add_statement(batch, stmt); cass_statement_free(stmt); free(s); free(p); free(o); if (++rows > batch_size) { CassFuture* future = cass_session_execute_batch(context->session, batch); cass_batch_free(batch); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); const char* msg; size_t msg_len; cass_future_error_message(future, &msg, &msg_len); fprintf(stderr, "Cassandra: %*s\n", msg_len, msg); cass_future_free(future); return -1; } cass_future_free(future); batch = 0; } } if (batch) { CassFuture* future = cass_session_execute_batch(context->session, batch); cass_batch_free(batch); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); const char* msg; size_t msg_len; cass_future_error_message(future, &msg, &msg_len); fprintf(stderr, "Cassandra: %*s\n", msg_len, msg); cass_future_free(future); return -1; } cass_future_free(future); } return 0; }
static std::string getFutureError(CassFuture *future) { const char *message; size_t message_length; cass_future_error_message(future, &message, &message_length); return std::string(message, message_length); }
/** * librdf_storage_cassandra_find_statements: * @storage: the storage * @statement: the statement to match * * . * * Return a stream of statements matching the given statement (or * all statements if NULL). Parts (subject, predicate, object) of the * statement can be empty in which case any statement part will match that. * Uses #librdf_statement_match to do the matching. * * Return value: a #librdf_stream or NULL on failure **/ static librdf_stream* librdf_storage_cassandra_find_statements(librdf_storage* storage, librdf_statement* statement) { librdf_storage_cassandra_instance* context; cassandra_results_stream* scontext; librdf_stream* stream; char* s; char* p; char* o; char* c; context = (librdf_storage_cassandra_instance*)storage->instance; scontext = LIBRDF_CALLOC(cassandra_results_stream*, 1, sizeof(*scontext)); if(!scontext) return NULL; scontext->storage = storage; librdf_storage_add_reference(scontext->storage); scontext->cassandra_context = context; statement_helper(storage, statement, 0, &s, &p, &o, &c); #ifdef DEBUG fprintf(stderr, "Query: "); if (s) fprintf(stderr, "s=%s ", s); if (p) fprintf(stderr, "p=%s ", p); if (o) fprintf(stderr, "o=%s ", o); fprintf(stderr, "\n"); #endif typedef CassStatement* (*query_function)(const char* s, const char* p, const char* o); query_function functions[8] = { &cassandra_query_, /* ??? */ &cassandra_query_s, /* S?? */ &cassandra_query_p, /* ?P? */ &cassandra_query_sp, /* SP? */ &cassandra_query_o, /* ??O */ &cassandra_query_so, /* S?O */ &cassandra_query_po, /* ?PO */ &cassandra_query_spo /* SPO */ }; /* This creates an index into the function table, depending on input terms. */ int num = 0; if (o) num += 4; if (p) num += 2; if (s) num++; index_type tp; query_function fn = functions[num]; CassStatement* stmt = (*fn)(s, p, o); cass_statement_set_paging_size(stmt, 1000); if (s) free(s); if (p) free(p); if (o) free(o); CassFuture* future = cass_session_execute(context->session, stmt); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); const char* msg; size_t msg_len; cass_future_error_message(future, &msg, &msg_len); fprintf(stderr, "Cassandra: %*s\n", msg_len, msg); cass_future_free(future); return 0; } const CassResult* result = cass_future_get_result(future); cass_future_free(future); CassIterator* iter = cass_iterator_from_result(result); scontext->stmt = stmt; scontext->result = result; scontext->iter = iter; scontext->more_pages = cass_result_has_more_pages(result); scontext->at_end = !cass_iterator_next(scontext->iter); stream = librdf_new_stream(storage->world, (void*)scontext, &cassandra_results_stream_end_of_stream, &cassandra_results_stream_next_statement, &cassandra_results_stream_get_statement, &cassandra_results_stream_finished); if(!stream) { cassandra_results_stream_finished((void*)scontext); return NULL; } return stream; }