PHP_METHOD(Rows, nextPageAsync) { cassandra_rows* self = NULL; cassandra_session* session = NULL; CassFuture* future = NULL; cassandra_future_rows* future_rows = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = (cassandra_rows*) zend_object_store_get_object(getThis() TSRMLS_CC); if (!(self->result && cass_result_has_more_pages(self->result))) { object_init_ex(return_value, cassandra_future_value_ce); return; } ASSERT_SUCCESS(cass_statement_set_paging_state(self->statement->statement, self->result)); session = (cassandra_session*) zend_object_store_get_object(self->session TSRMLS_CC); future = cass_session_execute(session->session, self->statement->statement); object_init_ex(return_value, cassandra_future_rows_ce); future_rows = (cassandra_future_rows*) zend_object_store_get_object(return_value TSRMLS_CC); Z_ADDREF_P(self->session); future_rows->session = self->session; future_rows->statement = php_cassandra_add_ref(self->statement); future_rows->future = future; }
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; }
CassError insert_into_basic(CassSession* session, const char* key, const Basic* basic) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; const char* query = "INSERT INTO examples.basic (key, bln, flt, dbl, i32, i64) VALUES (?, ?, ?, ?, ?, ?);"; statement = cass_statement_new(query, 6); cass_statement_bind_string(statement, 0, key); cass_statement_bind_bool(statement, 1, basic->bln); cass_statement_bind_float(statement, 2, basic->flt); cass_statement_bind_double(statement, 3, basic->dbl); cass_statement_bind_int32(statement, 4, basic->i32); cass_statement_bind_int64(statement, 5, basic->i64); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } cass_future_free(future); cass_statement_free(statement); return rc; }
void on_create_table(CassFuture* future, void* data) { const char* insert_query = "INSERT INTO callbacks (key, value) " "VALUES (?, ?)"; CassUuid key; CassStatement* statement = NULL; CassFuture* insert_future = NULL; CassError code = cass_future_error_code(future); if (code != CASS_OK) { print_error(future); } statement = cass_statement_new(insert_query, 2); cass_uuid_gen_time(uuid_gen, &key); cass_statement_bind_uuid(statement, 0, key); cass_statement_bind_int64(statement, 1, cass_uuid_timestamp(key)); insert_future = cass_session_execute((CassSession*)data, statement); cass_future_set_callback(insert_future, on_insert, data); cass_statement_free(statement); cass_future_free(insert_future); }
static std::vector<CassUuid> insert_async(CassSession* session, CassUuidGen* uuid_gen, const std::string& table_name, size_t num_concurrent_requests, std::vector<test_utils::CassFuturePtr>* futures) { std::string create_table_query = str(boost::format("CREATE TABLE %s (id timeuuid PRIMARY KEY, num int, str text);") % table_name); test_utils::execute_query(session, create_table_query); std::string insert_query = str(boost::format("INSERT INTO %s (id, num, str) VALUES(?, ?, ?)") % table_name); std::vector<CassUuid> ids; for (size_t i = 0; i < num_concurrent_requests; ++i) { CassUuid id = test_utils::generate_time_uuid(uuid_gen); test_utils::CassStatementPtr statement(cass_statement_new(insert_query.c_str(), 3)); // Determine if bound parameters can be used based on C* version if (version.major == 1) { insert_query = str(boost::format("INSERT INTO %s (id, num, str) VALUES(%s, %s, 'row%s')") % table_name % test_utils::Value<CassUuid>::to_string(id) % i % i); statement = test_utils::CassStatementPtr(cass_statement_new(insert_query.c_str(), 0)); } else { BOOST_REQUIRE(cass_statement_bind_uuid(statement.get(), 0, id) == CASS_OK); BOOST_REQUIRE(cass_statement_bind_int32(statement.get(), 1, i) == CASS_OK); std::string str_value = str(boost::format("row%d") % i); BOOST_REQUIRE(cass_statement_bind_string(statement.get(), 2, str_value.c_str()) == CASS_OK); } cass_statement_set_consistency(statement.get(), CASS_CONSISTENCY_QUORUM); futures->push_back(test_utils::CassFuturePtr(cass_session_execute(session, statement.get()))); ids.push_back(id); } return ids; }
static void ClearDatabase(Upload& upload) { DatabaseSession session; std::string str = "delete from uploadchunks where model_id = ? and upload_id = ?"; CassStatement* statement = cass_statement_new(str.c_str(), 2); cass_statement_bind_uuid(statement, 0, upload.ModelId()); cass_statement_bind_uuid(statement, 1, upload.UploadId()); CassFuture* future = cass_session_execute(session.Session(), statement); cass_future_wait(future); cass_statement_free(statement); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { std::string err = std::string("Failed to execute query: ") + CassandraUtils::GetCassandraError(future); cass_future_free(future); throw err; } cass_future_free(future); }
CassError insert_into_collections(CassSession* session, const char* key, const char* items[]) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; CassCollection* collection = NULL; const char** item = NULL; const char* query = "INSERT INTO examples.collections (key, items) VALUES (?, ?);"; statement = cass_statement_new(query, 2); cass_statement_bind_string(statement, 0, key); collection = cass_collection_new(CASS_COLLECTION_TYPE_SET, 2); for (item = items; *item; item++) { cass_collection_append_string(collection, *item); } cass_statement_bind_collection(statement, 1, collection); cass_collection_free(collection); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } cass_future_free(future); cass_statement_free(statement); return rc; }
void select_from_perf(CassSession* session, const char* query, const CassPrepared* prepared) { int i; CassFuture* futures[NUM_CONCURRENT_REQUESTS]; for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) { CassStatement* statement; if (prepared != NULL) { statement = cass_prepared_bind(prepared); } else { statement = cass_statement_new(query, 0); } futures[i] = cass_session_execute(session, statement); cass_statement_free(statement); } for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) { CassFuture* future = futures[i]; CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } else { const CassResult* result = cass_future_get_result(future); assert(cass_result_column_count(result) == 6); cass_result_free(result); } cass_future_free(future); } }
CassError insert_into(CassSession* session, const char* key) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; cass_uint32_t d; cass_int64_t t; const char* query = "INSERT INTO examples.date_time (key, d, t) VALUES (?, ?, ?);"; time_t now = time(NULL); d = cass_date_from_epoch(now); t = cass_time_from_epoch(now); statement = cass_statement_new(query, 3); cass_statement_bind_string(statement, 0, key); cass_statement_bind_uint32(statement, 1, d); cass_statement_bind_int64(statement, 2, t); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } cass_future_free(future); cass_statement_free(statement); return rc; }
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; }
void build(const std::string& ip_prefix, int num_nodes) { test_utils::CassClusterPtr cluster(cass_cluster_new()); test_utils::initialize_contact_points(cluster.get(), ip_prefix, num_nodes, 0); cass_cluster_set_load_balance_round_robin(cluster.get()); cass_cluster_set_token_aware_routing(cluster.get(), cass_false); test_utils::CassSessionPtr session(test_utils::create_session(cluster.get())); for (int i = 0; i < num_nodes; ++i) { test_utils::CassStatementPtr statement( cass_statement_new("SELECT tokens, data_center FROM system.local", 0)); test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get())); test_utils::wait_and_check_error(future.get()); test_utils::CassResultPtr result(cass_future_get_result(future.get())); const CassRow* row = cass_result_first_row(result.get()); const CassValue* data_center = cass_row_get_column_by_name(row, "data_center"); const CassValue* token_set = cass_row_get_column_by_name(row, "tokens"); CassString str; cass_value_get_string(data_center, &str.data, &str.length); std::string dc(str.data, str.length); std::string ip = cass::get_host_from_future(future.get()); test_utils::CassIteratorPtr iterator(cass_iterator_from_collection(token_set)); while (cass_iterator_next(iterator.get())) { cass_value_get_string(cass_iterator_get_value(iterator.get()), &str.data, &str.length); std::string token(str.data, str.length); tokens[boost::lexical_cast<int64_t>(token)] = Host(ip, dc); } } }
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; }
CassError insert_into_basic(CassSession* session, const char* key, const Basic* basic) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; const char* query = "INSERT INTO examples.basic (key, bln, flt, dbl, i32, i64) VALUES (:k, :b, :f, :d, :i32, :i64);"; statement = cass_statement_new(query, 6); cass_statement_bind_string_by_name(statement, "k", key); cass_statement_bind_bool_by_name(statement, "b", basic->bln); cass_statement_bind_float_by_name(statement, "f", basic->flt); cass_statement_bind_double_by_name(statement, "d", basic->dbl); cass_statement_bind_int32_by_name(statement, "i32", basic->i32); cass_statement_bind_int64_by_name(statement, "i64", basic->i64); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } cass_future_free(future); cass_statement_free(statement); return rc; }
static int execute(CassSession* session, char* query, int ignore_error) { CassStatement* stmt = cass_statement_new(query, 0); CassFuture* future = cass_session_execute(session, stmt); cass_statement_free(stmt); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { if (ignore_error) { cass_future_free(future); return 0; } fprintf(stderr, "Cassandra error: %s\n", cass_error_desc(rc)); cass_future_free(future); return -1; } cass_future_free(future); return 0; }
/** * Execute a query on the system table * * @param is_async True if async query; false otherwise */ void execute_query(bool is_async = false) { std::string query = "SELECT * FROM system.local"; test_utils::CassStatementPtr statement(cass_statement_new_n(query.data(), query.size(), 0)); test_utils::CassFuturePtr future(cass_session_execute(session_.get(), statement.get())); if (!is_async) { test_utils::wait_and_check_error(future.get()); } }
void execute_query(CassSession* session, const char* query, CassFutureCallback callback) { CassStatement* statement = cass_statement_new(query, 0); CassFuture* future = cass_session_execute(session, statement); cass_future_set_callback(future, callback, session); cass_future_free(future); cass_statement_free(statement); }
CassFuture* CassandraFS::remove_entry(const char* path) { CassStatement* statement = cass_statement_new("DELETE FROM entries WHERE path = ?", 1); cass_statement_bind_string(statement, 0, path); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); return result_future; }
/** * 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; }
CassFuture* CassandraFS::delete_file_block(struct stat* stat, struct cfs_attrs* cfs_attrs, int block_number) { CassStatement* statement = cass_statement_new("DELETE FROM file_blocks WHERE block_number = ? AND physical_file_id = ?", 2); cass_statement_bind_int32(statement, 0, block_number); cass_statement_bind_uuid(statement, 1, cfs_attrs->physical_file_id); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); return result_future; }
/** * Execute a select statement against the system tables for a specified amount * of time. * * NOTE: Results and errors are ignored * * @param duration Duration in seconds to execute queries * @param session Session instance */ void execute_system_query(int duration, test_utils::CassSessionPtr session) { boost::posix_time::ptime start = boost::posix_time::second_clock::universal_time(); while ((boost::posix_time::second_clock::universal_time() - start).total_seconds() < duration) { test_utils::CassStatementPtr statement(cass_statement_new("SELECT * FROM system.local", 0)); cass_statement_set_consistency(statement.get(), CASS_CONSISTENCY_ONE); test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get())); cass_future_wait_timed(future.get(), test_utils::ONE_SECOND_IN_MICROS); } }
CassFuture* CassandraFS::update_file_length(CassUuid* physical_file_id, long size) { CassStatement* statement = cass_statement_new("UPDATE physical_files SET size = ? WHERE id = ?", 2); cass_statement_bind_int64(statement, 0, size); cass_statement_bind_uuid(statement, 1, *physical_file_id); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); return result_future; }
CassFuture* CassandraFS::create_physical_file(CassUuid* uuid) { CassStatement* statement = cass_statement_new("INSERT INTO physical_files(id, size, block_size) VALUES(?, 0, " DEFAULT_BLOCK_SIZE ")", 1); cass_statement_bind_uuid(statement, 0, *uuid); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); return result_future; }
CassFuture* CassandraFS::truncate_block(struct stat* stat, struct cfs_attrs* cfs_attrs, int block_number, int size) { CassStatement* statement = cass_statement_new("UPDATE file_blocks SET size = ? WHERE physical_file_id = ? AND block_number = ?", 3); cass_statement_bind_int32(statement, 0, size); cass_statement_bind_uuid(statement, 1, cfs_attrs->physical_file_id); cass_statement_bind_int32(statement, 2, block_number); CassFuture* future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); return future; }
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); }
void PolicyTool::query(CassSession* session, int n, CassConsistency cl) { std::string select_query = str(boost::format("SELECT * FROM %s WHERE k = 0") % test_utils::SIMPLE_TABLE); for (int i = 0; i < n; ++i) { test_utils::CassStatementPtr statement(cass_statement_new_n(select_query.data(), select_query.size(), 0)); cass_statement_set_consistency(statement.get(), cl); test_utils::CassFuturePtr future(cass_session_execute(session, statement.get())); test_utils::wait_and_check_error(future.get()); add_coordinator(cass::get_host_from_future(future.get())); } }
void CassandraFS::write_block(CassUuid* physical_file_id, int block, const unsigned char* data, int length, CassandraFutureSpool* spool) { CassStatement* statement = cass_statement_new("INSERT INTO file_blocks(physical_file_id, block_number, data, size) VALUES(?,?,?,?)", 4); cass_statement_bind_uuid(statement, 0, *physical_file_id); cass_statement_bind_int32(statement, 1, block); cass_statement_bind_bytes(statement, 2, data, length); cass_statement_bind_int32(statement, 3, length); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); spool->append(result_future); }
bool CCassandraLogAgent::executeSimpleSelectStatement(const char* st, unsigned& resultValue) { CassandraStatement statement(cassSession->prepareStatement(st, getEspLogLevel()>LogNormal)); CassandraFuture future(cass_session_execute(cassSession->querySession(), statement)); future.wait("execute"); CassandraResult result(cass_future_get_result(future)); if (cass_result_row_count(result) == 0) return false; resultValue = getUnsignedResult(NULL, getSingleResult(result)); return true; }
void CCassandraLogAgent::executeUpdateLogStatement(StringBuffer& st) { cassSession->connect(); CassandraFuture futurePrep(cass_session_prepare_n(cassSession->querySession(), st.str(), st.length())); futurePrep.wait("prepare statement"); Owned<CassandraPrepared> prepared = new CassandraPrepared(cass_future_get_prepared(futurePrep), NULL); CassandraStatement statement(prepared.getClear()); CassandraFuture future(cass_session_execute(cassSession->querySession(), statement)); future.wait("execute"); cassSession->disconnect(); }
PHP_METHOD(Rows, nextPage) { zval* timeout = NULL; cassandra_session* session = NULL; CassFuture* future = NULL; const CassResult* result = NULL; cassandra_rows* rows = NULL; cassandra_rows* self = (cassandra_rows*) zend_object_store_get_object(getThis() TSRMLS_CC); if (!(self->result && cass_result_has_more_pages(self->result))) return; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) { return; } ASSERT_SUCCESS(cass_statement_set_paging_state(self->statement->statement, self->result)); session = (cassandra_session*) zend_object_store_get_object(self->session TSRMLS_CC); future = cass_session_execute(session->session, self->statement->statement); if (php_cassandra_future_wait_timed(future, timeout TSRMLS_CC) == FAILURE) { return; } if (php_cassandra_future_is_error(future TSRMLS_CC) == FAILURE) { return; } result = cass_future_get_result(future); if (!result) { zend_throw_exception_ex(cassandra_runtime_exception_ce, 0 TSRMLS_CC, "Future doesn't contain a result."); return; } object_init_ex(return_value, cassandra_rows_ce); rows = (cassandra_rows*) zend_object_store_get_object(return_value TSRMLS_CC); if (php_cassandra_get_result(result, &rows->rows TSRMLS_CC) == FAILURE) { cass_result_free(result); zval_dtor(return_value); return; } Z_ADDREF_P(self->session); rows->statement = php_cassandra_add_ref(self->statement); rows->session = self->session; rows->result = result; }
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; }