CassFuture* CassandraFS::create_dir_entry(const char* path, mode_t mode) { CassStatement* statement = cass_statement_new("INSERT INTO entries(path, mode, created_at, modified_at) VALUES(?,?,?,?)", 4); cass_statement_bind_string(statement, 0, path); cass_statement_bind_int32(statement, 1, mode); cass_statement_bind_int64(statement, 2, time(NULL)*1000); cass_statement_bind_int64(statement, 3, time(NULL)*1000); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); return result_future; }
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); }
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(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; }
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; }
bool bind_and_execute_insert(CassStatement* statement) { 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, 0, test_utils::generate_time_uuid(uuid_gen)); cass_statement_bind_int64(statement, 1, event_time.count()); cass_statement_bind_string_n(statement, 2, text_sample.data(), text_sample.size()); return execute_insert(statement); }
int CassandraFS::update_timestamps(const char* path, const struct timespec last_access_stamp, const struct timespec last_modification_stamp) { CassStatement* statement = cass_statement_new("UPDATE entries SET modified_at = ? WHERE path = ?", 2); cass_statement_bind_int64(statement, 0, last_modification_stamp.tv_sec * 1000 + (last_modification_stamp.tv_nsec / 1000000)); cass_statement_bind_string(statement, 1, path); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); int error = 0; if (cass_future_error_code(result_future) == CASS_OK) { // Nada } else { /* Handle error */ error = -EIO; cassandra_log_error(result_future); } cass_future_free(result_future); return error; }
/// ATM only supports (key, value) inserts void asyncInsert(const std::string &table, const int64_t &key, const std::string &value) { if(!connected_) { LOG(FATAL) << "asyncInsert failed, establish connection to cassandra first"; } else if(futures_.size() > kMaxNumQueuedFutures) { VLOG(1) << "asyncInsert, queue has reached capacity, draining..."; wait(); } const auto tableName = keyspace_ + "." + table; const auto query("INSERT INTO " + tableName + " (key, value) " + "VALUES (?, ?);"); auto statement = createStatement(cass_statement_new(query.c_str(), 2)); cass_statement_bind_int64(statement.get(), 0, (cass_int64_t)key); cass_statement_bind_string(statement.get(), 1, value.data()); futures_.emplace(cass_session_execute(session_.get(), statement.get()), cass_future_free); }
int CassandraFS::truncate(const char* path, off_t size) { CassStatement* statement = NULL; struct stat stat; struct cfs_attrs cfs_attrs; CassandraFutureSpool* spool = new CassandraFutureSpool(4); int err = getattr(path, &stat, &cfs_attrs); if (err != 0) { return -EIO; } 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, cfs_attrs.physical_file_id); spool->append(cass_session_execute(ctxt->session, statement)); cass_statement_free(statement); off_t current_size = stat.st_size; int current_blocks = blocks_needed(current_size, cfs_attrs.block_size); int needed_blocks = blocks_needed(size, cfs_attrs.block_size); for (int a = needed_blocks; a<current_blocks; a++) { spool->append(delete_file_block(&stat, &cfs_attrs, a)); } if (size % cfs_attrs.block_size > 0) { spool->append(truncate_block(&stat, &cfs_attrs, needed_blocks-1, size % cfs_attrs.block_size)); } spool->wait_all(); int errors = spool->get_errors(); delete spool; if (errors>0) { return -EIO; } return 0; }
void insert_into_async(CassSession* session, const char* key) { CassError rc = CASS_OK; CassStatement* statement = NULL; const char* query = "INSERT INTO async (key, bln, flt, dbl, i32, i64) VALUES (?, ?, ?, ?, ?, ?);"; CassFuture* futures[NUM_CONCURRENT_REQUESTS]; size_t i; for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) { char key_buffer[64]; statement = cass_statement_new(query, 6); sprintf(key_buffer, "%s%u", key, (unsigned int)i); cass_statement_bind_string(statement, 0, key_buffer); cass_statement_bind_bool(statement, 1, i % 2 == 0 ? cass_true : cass_false); cass_statement_bind_float(statement, 2, i / 2.0f); cass_statement_bind_double(statement, 3, i / 200.0); cass_statement_bind_int32(statement, 4, (cass_int32_t)(i * 10)); cass_statement_bind_int64(statement, 5, (cass_int64_t)(i * 100)); futures[i] = cass_session_execute(session, statement); cass_statement_free(statement); } for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) { CassFuture* future = futures[i]; cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } cass_future_free(future); } }
void bindInt64(unsigned idx, __int64 value) { if (query.length()) traceBind(idx, "%" I64F "d", value); check(cass_statement_bind_int64(statement, idx, value)); }
void InsertThread::run() { // CONNECT TO CASSANDRA CassCluster* cluster; CassSession* session; if (!CassConnectToKeyspace("127.0.0.1", "chaos", "'class': 'SimpleStrategy', 'replication_factor': 1", &cluster, &session)) { // free resources CassFreeClusterAndSession(cluster, session); return; }// if error ... // INSERTING NUMBERS const CassPrepared *preparedInsert = NULL; CassFuture* prefuture = cass_session_prepare(session, "INSERT INTO test (group, num, minimize) VALUES (?, ?, ?) IF NOT EXISTS"); cass_future_wait(prefuture); CassError prerc = cass_future_error_code(prefuture); if (prerc != CASS_OK) { CassPrintError(prefuture); // free resources cass_future_free(prefuture); CassFreeClusterAndSession(cluster, session); return; } else preparedInsert = cass_future_get_prepared(prefuture); cass_future_free(prefuture); int group; unsigned long minimize; CassStatement* statement; CassFuture* future; CassError rcI; while (GetNextNum()) { group = num % 1000000; minimize = num; statement = cass_prepared_bind(preparedInsert); cass_statement_bind_int32(statement, 0, group); cass_statement_bind_int64(statement, 1, num); cass_statement_bind_int64(statement, 2, minimize); future = cass_session_execute(session, statement); cass_future_wait(future); rcI = cass_future_error_code(future); if (rcI != CASS_OK) { CassPrintError(future); }// if error ... cass_future_free(future); cass_statement_free(statement); // stop loop on error if (rcI != CASS_OK) break; // show progress on console if ((num % 3751) == 0) printf("%lu\n", num); }// foreach num ... // free resources cass_prepared_free(preparedInsert); CassFreeClusterAndSession(cluster, session); }
static CassError bind(CassStatement* statement, size_t index, cass_int64_t value) { return cass_statement_bind_int64(statement, index, value); }
int CassandraFS::hardlink(const char* from, const char* to) { int operation_done = 0; CassStatement* statement = cass_statement_new("SELECT mode, created_at, modified_at, physical_file_id FROM entries WHERE path = ?", 1); cass_statement_bind_string(statement, 0, from); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); int error = 0; 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); if (cass_iterator_next(rows)) { const CassRow* row = cass_iterator_get_row(rows); const CassValue* mode_value = cass_row_get_column_by_name(row, "mode"); const CassValue* modified_at_value = cass_row_get_column_by_name(row, "modified_at"); const CassValue* created_at_value = cass_row_get_column_by_name(row, "created_at"); const CassValue* physical_file_id_value = cass_row_get_column_by_name(row, "physical_file_id"); int mode; cass_value_get_int32(mode_value, &mode); cass_int64_t modified_at; cass_value_get_int64(modified_at_value, &modified_at); cass_int64_t created_at; cass_value_get_int64(created_at_value, &created_at); CassUuid physical_file_id; cass_value_get_uuid(physical_file_id_value, &physical_file_id); CassStatement* insert_statement = cass_statement_new("INSERT INTO entries(path, mode, created_at, modified_at, physical_file_id) VALUES(?,?,?,?,?)", 5); cass_statement_bind_string(insert_statement, 0, to); cass_statement_bind_int32(insert_statement, 1, mode); cass_statement_bind_int64(insert_statement, 2, created_at); cass_statement_bind_int64(insert_statement, 3, modified_at); cass_statement_bind_uuid(insert_statement, 4, physical_file_id); CassFuture* insert_future2 = create_sub_entry(to); CassFuture* insert_future = cass_session_execute(ctxt->session, insert_statement); cass_statement_free(insert_statement); if (cass_future_error_code(insert_future) == CASS_OK) { operation_done = 1; } else { operation_done = 0; cassandra_log_error(insert_future); } if (cass_future_error_code(insert_future2) == CASS_OK) { operation_done = 1; } else { operation_done = 0; cassandra_log_error(insert_future2); } cass_future_free(insert_future); cass_future_free(insert_future2); } cass_result_free(result); cass_iterator_free(rows); } else { /* Handle error */ error = -EIO; cassandra_log_error(result_future); } cass_future_free(result_future); if (!operation_done) { return -ENOENT; } return error; }