unsigned char* CassandraFS::read_block(CassUuid* physical_file_id, int block, int* bytes_read) { CassStatement* statement = cass_statement_new("SELECT data, size FROM file_blocks WHERE physical_file_id = ? AND block_number = ?", 2); cass_statement_bind_uuid(statement, 0, *physical_file_id); cass_statement_bind_int32(statement, 1, block); unsigned char* return_data = NULL; CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); if (cass_future_error_code(result_future) == CASS_OK) { 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* data_value = cass_row_get_column_by_name(row, "data"); const CassValue* size_value = cass_row_get_column_by_name(row, "size"); const cass_byte_t* cass_data; size_t size; int size2; cass_value_get_bytes(data_value, &cass_data, &size); cass_value_get_int32(size_value, &size2); // Let's use the value from "size" field unless bigger than the actual data. if (size2>size) { warning("Problem: size field is showing value %d which is bigger than the actual block data length %zu", size2, size); size2 = size; } return_data = (unsigned char*)malloc(size2); memcpy(return_data, cass_data, size2); (*bytes_read) = size2; } cass_result_free(result); cass_iterator_free(rows); } else { /* Handle error */ cassandra_log_error(result_future); } cass_future_free(result_future); return return_data; }
/** * Test established connection with a normal load query */ void test_normal_load() { //Create and use the simple keyspace test_utils::execute_query(session_, str(boost::format(test_utils::CREATE_KEYSPACE_SIMPLE_FORMAT) % test_utils::SIMPLE_KEYSPACE % "1")); test_utils::execute_query(session_, str(boost::format("USE %s") % test_utils::SIMPLE_KEYSPACE)); //Create a table to fill with numbers and characters test_utils::execute_query(session_, "CREATE TABLE normal_load (key int PRIMARY KEY, a int, b float, c text)"); //Perform queries and validate inserted data for (int n = 0; n < NUMBER_OF_ITERATIONS; ++n) { std::string text = test_utils::generate_random_string(16); test_utils::execute_query(session_, str(boost::format("INSERT INTO normal_load (key, a, b, c) VALUES (%d, %d, %f, '%s')") % n % (n * 100) % (n * .001f) % text)); test_utils::CassResultPtr result; test_utils::execute_query(session_, str(boost::format("SELECT * FROM normal_load WHERE key = %d") % n), &result); BOOST_REQUIRE_EQUAL(cass_result_column_count(result.get()), 4u); BOOST_REQUIRE_EQUAL(cass_result_row_count(result.get()), 1u); const CassRow* row = cass_result_first_row(result.get()); const CassValue* value; value = cass_row_get_column_by_name(row, "key"); BOOST_REQUIRE(value != NULL); cass_int32_t key; BOOST_REQUIRE_EQUAL(cass_value_get_int32(value, &key), CASS_OK); BOOST_CHECK_EQUAL(key, n); value = cass_row_get_column_by_name(row, "a"); BOOST_REQUIRE(value != NULL); cass_int32_t a; BOOST_REQUIRE_EQUAL(cass_value_get_int32(value, &a), CASS_OK); BOOST_CHECK_EQUAL(a, (n * 100)); value = cass_row_get_column_by_name(row, "b"); BOOST_REQUIRE(value != NULL); cass_float_t b; BOOST_REQUIRE_EQUAL(cass_value_get_float(value, &b), CASS_OK); BOOST_CHECK_EQUAL(b, (n * .001f)); value = cass_row_get_column_by_name(row, "c"); BOOST_REQUIRE(value != NULL); CassString c; BOOST_REQUIRE_EQUAL(cass_value_get_string(value, &c.data, &c.length), CASS_OK); BOOST_CHECK(test_utils::Value<CassString>::equal(c, CassString(text.c_str()))); } //Drop the table and keyspace test_utils::execute_query(session_, "DROP TABLE normal_load"); test_utils::execute_query(session_, str(boost::format("DROP KEYSPACE %s") % test_utils::SIMPLE_KEYSPACE)); }
void check_result(CassSession* session) { test_utils::CassResultPtr result; test_utils::execute_query(session, "SELECT * FROM test", &result); BOOST_REQUIRE(cass_result_column_count(result.get()) == 5); BOOST_REQUIRE(cass_result_row_count(result.get()) > 0); const CassRow* row = cass_result_first_row(result.get()); cass_int32_t key; BOOST_REQUIRE(cass_value_get_int32(cass_row_get_column(row, 0), &key) == CASS_OK); cass_int32_t v1; BOOST_REQUIRE(cass_value_get_int32(cass_row_get_column(row, 1), &v1) == CASS_OK); CassString v2; BOOST_REQUIRE(cass_value_get_string(cass_row_get_column(row, 2), &v2.data, &v2.length) == CASS_OK); test_utils::CassIteratorPtr v3(cass_iterator_from_collection(cass_row_get_column(row, 3))); cass_int32_t i = 0; while (cass_iterator_next(v3.get())) { const CassValue* value = cass_iterator_get_value(v3.get()); BOOST_REQUIRE(cass_value_type(value) == CASS_VALUE_TYPE_INT); cass_int32_t output; cass_value_get_int32(value, &output); BOOST_REQUIRE(i == output); i++; } test_utils::CassIteratorPtr v4(cass_iterator_from_collection(cass_row_get_column(row, 4))); cass_int32_t j = 0; while (cass_iterator_next(v4.get())) { const CassValue* value = cass_iterator_get_value(v4.get()); BOOST_REQUIRE(cass_value_type(value) == CASS_VALUE_TYPE_VARCHAR); CassString output; cass_value_get_string(value, &output.data, &output.length); BOOST_REQUIRE(output.length == 1 && output.data[0] == 'd' + j); j++; } }
/** * Test established connection with a high load query */ void test_high_load() { //Create and use the simple keyspace test_utils::execute_query(session_, str(boost::format(test_utils::CREATE_KEYSPACE_SIMPLE_FORMAT) % test_utils::SIMPLE_KEYSPACE % "1")); test_utils::execute_query(session_, str(boost::format("USE %s") % test_utils::SIMPLE_KEYSPACE)); //Create a table to fill with large text fields test_utils::execute_query(session_, "CREATE TABLE high_load (key int PRIMARY KEY, a text, b text, c text)"); //Perform queries and validate inserted data for (int n = 0; n < NUMBER_OF_ITERATIONS; ++n) { std::string text_a = test_utils::generate_random_string(10240); std::string text_b = test_utils::generate_random_string(20480); std::string text_c = test_utils::generate_random_string(40960); test_utils::execute_query(session_, str(boost::format("INSERT INTO high_load (key, a, b, c) VALUES (%d, '%s', '%s', '%s')") % n % text_a % text_b % text_c)); test_utils::CassResultPtr result; test_utils::execute_query(session_, str(boost::format("SELECT * FROM high_load WHERE key = %d") % n), &result); BOOST_REQUIRE_EQUAL(cass_result_column_count(result.get()), 4u); BOOST_REQUIRE_EQUAL(cass_result_row_count(result.get()), 1u); const CassRow* row = cass_result_first_row(result.get()); const CassValue* value; value = cass_row_get_column_by_name(row, "key"); BOOST_REQUIRE(value != NULL); cass_int32_t key; BOOST_REQUIRE_EQUAL(cass_value_get_int32(value, &key), CASS_OK); BOOST_CHECK_EQUAL(key, n); value = cass_row_get_column_by_name(row, "a"); BOOST_REQUIRE(value != NULL); CassString a; BOOST_REQUIRE_EQUAL(cass_value_get_string(value, &a.data, &a.length), CASS_OK); BOOST_CHECK(test_utils::Value<CassString>::equal(a, CassString(text_a.c_str()))); value = cass_row_get_column_by_name(row, "b"); BOOST_REQUIRE(value != NULL); CassString b; BOOST_REQUIRE_EQUAL(cass_value_get_string(value, &b.data, &b.length), CASS_OK); BOOST_CHECK(test_utils::Value<CassString>::equal(b, CassString(text_b.c_str()))); value = cass_row_get_column_by_name(row, "c"); BOOST_REQUIRE(value != NULL); CassString c; BOOST_REQUIRE_EQUAL(cass_value_get_string(value, &c.data, &c.length), CASS_OK); BOOST_CHECK(test_utils::Value<CassString>::equal(c, CassString(text_c.c_str()))); } //Drop the table and keyspace test_utils::execute_query(session_, "DROP TABLE high_load"); test_utils::execute_query(session_, str(boost::format("DROP KEYSPACE %s") % test_utils::SIMPLE_KEYSPACE)); }
CassError select_from_maps(CassSession* session, const char* key) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; const char* query = "SELECT items FROM examples.maps WHERE key = ?"; statement = cass_statement_new(query, 1); cass_statement_bind_string(statement, 0, key); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } else { const CassResult* result = cass_future_get_result(future); if (cass_result_row_count(result) > 0) { const CassRow* row = cass_result_first_row(result); CassIterator* iterator = cass_iterator_from_map( cass_row_get_column(row, 0)); while (cass_iterator_next(iterator)) { const char* key; size_t key_length; cass_int32_t value; cass_value_get_string(cass_iterator_get_map_key(iterator), &key, &key_length); cass_value_get_int32(cass_iterator_get_map_value(iterator), &value); printf("item: '%.*s' : %d \n", (int)key_length, key, value); } cass_iterator_free(iterator); } cass_result_free(result); } cass_future_free(future); cass_statement_free(statement); return rc; }
CassError select_from_basic(CassSession* session, const char* key, Basic* basic) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; const char* query = "SELECT * FROM examples.basic WHERE key = ?"; statement = cass_statement_new(query, 1); cass_statement_bind_string_by_name(statement, "key", key); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } else { const CassResult* result = cass_future_get_result(future); CassIterator* iterator = cass_iterator_from_result(result); if (cass_iterator_next(iterator)) { const CassRow* row = cass_iterator_get_row(iterator); cass_value_get_bool(cass_row_get_column_by_name(row, "BLN"), &basic->bln); cass_value_get_double(cass_row_get_column_by_name(row, "dbl"), &basic->dbl); cass_value_get_float(cass_row_get_column_by_name(row, "flt"), &basic->flt); cass_value_get_int32(cass_row_get_column_by_name(row, "\"i32\""), &basic->i32); cass_value_get_int64(cass_row_get_column_by_name(row, "i64"), &basic->i64); } cass_result_free(result); cass_iterator_free(iterator); } cass_future_free(future); cass_statement_free(statement); return rc; }
CassError CassandraFS::read_physical_file_info(struct stat* stat, struct cfs_attrs* cfs_attrs) { CassStatement* statement = cass_statement_new("SELECT size, block_size FROM physical_files WHERE id = ?", 1); cass_statement_bind_uuid(statement, 0, cfs_attrs->physical_file_id); CassFuture* result_future = cass_session_execute(ctxt->session, statement); cass_statement_free(statement); CassError error = cass_future_error_code(result_future); if (error == 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* size_value = cass_row_get_column_by_name(row, "size"); if (!cass_value_is_null(size_value)) { cass_int64_t size; cass_value_get_int64(size_value, &size); stat->st_size = size; } const CassValue* block_size_value = cass_row_get_column_by_name(row, "block_size"); if (!cass_value_is_null(block_size_value)) { int size; cass_value_get_int32(block_size_value, &size); cfs_attrs->block_size = size; } } cass_result_free(result); cass_iterator_free(rows); } else { /* Handle error */ cassandra_log_error(result_future); } cass_future_free(result_future); return error; }
CassError select_from_basic(CassSession* session, const CassPrepared * prepared, const char* key, Basic* basic) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; statement = cass_prepared_bind(prepared); cass_statement_bind_string(statement, 0, key); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } else { const CassResult* result = cass_future_get_result(future); CassIterator* iterator = cass_iterator_from_result(result); if (cass_iterator_next(iterator)) { const CassRow* row = cass_iterator_get_row(iterator); cass_value_get_bool(cass_row_get_column(row, 1), &basic->bln); cass_value_get_double(cass_row_get_column(row, 2), &basic->dbl); cass_value_get_float(cass_row_get_column(row, 3), &basic->flt); cass_value_get_int32(cass_row_get_column(row, 4), &basic->i32); cass_value_get_int64(cass_row_get_column(row, 5), &basic->i64); } cass_result_free(result); cass_iterator_free(iterator); } cass_future_free(future); cass_statement_free(statement); return rc; }
/** * Verify the address UDT (field names and results) * * @param value The address UDT value to iterate over * @param expected_street Expected street address to verify against result * @param expected_zip Expected zip code to verify against result * @param expected_phone_numbers Expected numbers to verify against result */ void verify_address_udt(const CassValue* value, CassString expected_street, cass_int32_t expected_zip, PhoneMap expected_phone_numbers) { // Verify field names for address UDT and create the iterator for validation verify_address_udt_field_names(value); test_utils::CassIteratorPtr iterator(cass_iterator_from_user_type(value)); // Verify street result BOOST_REQUIRE(cass_iterator_next(iterator.get())); const CassValue* street_value = cass_iterator_get_user_type_field_value(iterator.get()); BOOST_REQUIRE_EQUAL(cass_value_type(street_value), CASS_VALUE_TYPE_VARCHAR); CassString street_result; BOOST_REQUIRE_EQUAL(cass_value_get_string(street_value, &street_result.data, &street_result.length), CASS_OK); BOOST_REQUIRE(test_utils::Value<CassString>::equal(street_result, expected_street)); // Verify zip result BOOST_REQUIRE(cass_iterator_next(iterator.get())); const CassValue* zip_value = cass_iterator_get_user_type_field_value(iterator.get()); BOOST_REQUIRE_EQUAL(cass_value_type(zip_value), CASS_VALUE_TYPE_INT); cass_int32_t zip_result; BOOST_REQUIRE_EQUAL(cass_value_get_int32(zip_value, &zip_result), CASS_OK); BOOST_REQUIRE(test_utils::Value<cass_int32_t>::equal(zip_result, expected_zip)); // Verify phone numbers result BOOST_REQUIRE(cass_iterator_next(iterator.get())); const CassValue* phone_numbers_value = cass_iterator_get_user_type_field_value(iterator.get()); BOOST_REQUIRE_EQUAL(cass_value_type(phone_numbers_value), CASS_VALUE_TYPE_SET); BOOST_REQUIRE_EQUAL(cass_value_item_count(phone_numbers_value), expected_phone_numbers.size()); test_utils::CassIteratorPtr phone_numbers_iterator(cass_iterator_from_collection(phone_numbers_value)); unsigned int count = 0; PhoneMap::iterator phone_iterator = expected_phone_numbers.begin(); while (cass_iterator_next(phone_numbers_iterator.get()) && phone_iterator != expected_phone_numbers.end()) { verify_phone_udt(cass_iterator_get_value(phone_numbers_iterator.get()), phone_iterator->first, phone_iterator->second); ++phone_iterator; ++count; } BOOST_REQUIRE_EQUAL(expected_phone_numbers.size(), count); }
static CassError get(const CassValue* value, cass_int32_t* output) { return cass_value_get_int32(value, output); }
CassError select_from_udt(CassSession* session) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; const char* query = "SELECT * FROM examples.udt"; statement = cass_statement_new(query, 0); future = cass_session_execute(session, statement); cass_future_wait(future); rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } else { const CassResult* result = NULL; CassIterator* rows = NULL; result = cass_future_get_result(future); rows = cass_iterator_from_result(result); while(cass_iterator_next(rows)) { CassUuid id; char id_str[CASS_UUID_STRING_LENGTH]; const CassRow* row = cass_iterator_get_row(rows); const CassValue* id_value = cass_row_get_column_by_name(row, "id"); const CassValue* address_value = cass_row_get_column_by_name(row, "address"); CassIterator* fields = cass_iterator_fields_from_user_type(address_value); cass_value_get_uuid(id_value, &id); cass_uuid_string(id, id_str); printf("id %s ", id_str); while(fields != NULL && cass_iterator_next(fields)) { const char* field_name; size_t field_name_length; const CassValue* field_value = NULL; cass_iterator_get_user_type_field_name(fields, &field_name, &field_name_length); field_value = cass_iterator_get_user_type_field_value(fields); printf("%.*s ", (int)field_name_length, field_name); if (!cass_value_is_null(field_value)) { if (cass_value_type(field_value) == CASS_VALUE_TYPE_VARCHAR) { const char* text; size_t text_length; cass_value_get_string(field_value, &text, &text_length); printf("\"%.*s\" ", (int)text_length, text); } else if (cass_value_type(field_value) == CASS_VALUE_TYPE_INT) { cass_int32_t i; cass_value_get_int32(field_value, &i); printf("%d ", i); } else if (cass_value_type(field_value) == CASS_VALUE_TYPE_SET) { CassIterator* phone_numbers = cass_iterator_from_collection(field_value); while (cass_iterator_next(phone_numbers)) { const CassValue* phone_value = cass_iterator_get_value(phone_numbers); CassIterator* phone_fields = cass_iterator_fields_from_user_type(phone_value); assert(cass_value_type(phone_value) == CASS_VALUE_TYPE_UDT); while (cass_iterator_next(phone_fields)) { const CassValue* phone_number_value = cass_iterator_get_user_type_field_value(phone_fields); cass_int32_t i; cass_value_get_int32(phone_number_value, &i); printf("%d ", i); } } } else { printf("<invalid> "); } } else { printf("<null> "); } } printf("\n"); } cass_result_free(result); cass_iterator_free(rows); } cass_future_free(future); cass_statement_free(statement); return rc; }
int CassandraFS::getattr(const char* path, struct stat *stbuf, struct cfs_attrs *cfs_attrs) { int found = 0; memset(stbuf, 0, sizeof(struct stat)); memset(cfs_attrs, 0, sizeof(struct cfs_attrs)); CassStatement* statement = cass_statement_new("SELECT mode, modified_at, physical_file_id 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); 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* value = cass_row_get_column_by_name(row, "mode"); const CassValue* modified_at_value = cass_row_get_column_by_name(row, "modified_at"); int mode; cass_value_get_int32(value, &mode); stbuf->st_mode = mode; // regular file if (S_ISREG(mode)) { const CassValue* file_id_value = cass_row_get_column_by_name(row, "physical_file_id"); if (!cass_value_is_null(file_id_value)) { cass_value_get_uuid(file_id_value, &(cfs_attrs->physical_file_id)); } CassError phys_err = read_physical_file_info(stbuf, cfs_attrs); if (phys_err != CASS_OK) { error = -EIO; } } if (!cass_value_is_null(modified_at_value)) { cass_int64_t modified_at; cass_value_get_int64(modified_at_value, &modified_at); stbuf->st_mtime = modified_at / 1000; } stbuf->st_nlink = 1; found = 1; } cass_result_free(result); cass_iterator_free(rows); } else { /* Handle error */ error = -EIO; cassandra_log_error(result_future); } cass_future_free(result_future); if (!found) { return -ENOENT; } return error; }
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; }