Example #1
1
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;
}
Example #2
0
CassFuture* CassandraFS::sub_entries(const char* path, int limit) {
    CassStatement* statement = NULL;

    if (limit > 0) {
        statement = cass_statement_new("select sub_path FROM sub_entries WHERE parent_path = ? LIMIT ?", 2);
        cass_statement_bind_int32(statement, 1, limit);
    } else {
        statement = cass_statement_new("select sub_path FROM sub_entries WHERE parent_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;
}
Example #3
0
CassFuture* CassandraFS::create_file_entry(const char* path, CassUuid* uuid, mode_t mode) {
    CassStatement* statement = cass_statement_new("INSERT INTO entries(path, mode, created_at, modified_at, physical_file_id) VALUES(?,?,?,?, ?)", 5);

    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);
    cass_statement_bind_uuid(statement, 4, *uuid);

    CassFuture* result_future = cass_session_execute(ctxt->session, statement);

    cass_statement_free(statement);

    return result_future;
}
Example #4
0
void DbCassandra::Delete( const std::string &key )
{
    DbPtr db = db_;
    if ( db )
    {
        auto statement = cass_prepared_bind( db_->GetPreparedDelete() );
        cass_statement_bind_string( statement, 0, key.c_str() );
        auto query_future = cass_session_execute( db_->GetSession(), statement );
        cass_statement_free( statement );
        CassError err = cass_future_error_code( query_future );
        cass_future_free( query_future );
        if ( err != CASS_OK )
            throw std::logic_error( cass_error_desc( err ) );
    }
}
Example #5
0
CassFuture* CassandraFS::remove_sub_entry(const char* path) {
    CassStatement* statement = cass_statement_new("DELETE FROM sub_entries WHERE parent_path = ? AND sub_path = ?", 2);
    char *subpathc = strdup(path);
    char *parentpathc = strdup(path);

    cass_statement_bind_string(statement, 0, dirname(parentpathc));
    cass_statement_bind_string(statement, 1, basename(subpathc));

    free(subpathc);
    free(parentpathc);

    CassFuture* result_future = cass_session_execute(ctxt->session, statement);
    cass_statement_free(statement);

    return result_future;
}
Example #6
0
CassFuture* CassandraFS::create_sub_entry(const char* path) {
    CassStatement* statement = cass_statement_new("INSERT INTO sub_entries(sub_path, parent_path) VALUES(?,?)", 2);

    char *subpathc = strdup(path);
    char *parentpathc = strdup(path);

    cass_statement_bind_string(statement, 0, basename(subpathc));
    cass_statement_bind_string(statement, 1, dirname(parentpathc));

    CassFuture* result_future = cass_session_execute(ctxt->session, statement);
    cass_statement_free(statement);
    free(subpathc);
    free(parentpathc);

    return result_future;
}
Example #7
0
CassError execute_query(CassSession* session, const char* query) {
  CassError rc = CASS_OK;
  CassStatement* statement = cass_statement_new(query, 0);
  CassFuture* 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;
}
Example #8
0
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;
}
Example #9
0
CassError select_from_collections(CassSession* session, const char* key) {
  CassError rc = CASS_OK;
  CassStatement* statement = NULL;
  CassFuture* future = NULL;
  const char* query = "SELECT items FROM examples.collections 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);
    CassIterator* iterator = cass_iterator_from_result(result);

    if (cass_iterator_next(iterator)) {
      const CassValue* value = NULL;
      const CassRow* row = cass_iterator_get_row(iterator);
      CassIterator* items_iterator = NULL;

      value = cass_row_get_column(row, 0);
      items_iterator = cass_iterator_from_collection(value);
      while (cass_iterator_next(items_iterator)) {
        const char* item;
        size_t item_length;
        cass_value_get_string(cass_iterator_get_value(items_iterator), &item, &item_length);
        printf("item: %.*s\n", (int)item_length, item);
      }
      cass_iterator_free(items_iterator);
    }

    cass_result_free(result);
    cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);

  return rc;
}
Example #10
0
void on_insert(CassFuture* future, void* data) {
  CassError code = cass_future_error_code(future);
  if (code != CASS_OK) {
    print_error(future);
    signal_exit((CassSession*)data);
  } else {
    const char* select_query = "SELECT * FROM callbacks";
    CassStatement* statement
        = cass_statement_new(select_query, 0);
    CassFuture* select_future
        = cass_session_execute((CassSession*)data, statement);

    cass_future_set_callback(select_future, on_select, data);

    cass_statement_free(statement);
    cass_future_free(select_future);
  }
}
Example #11
0
CassError select_from(CassSession* session, const char* key) {
  CassError rc = CASS_OK;
  CassStatement* statement = NULL;
  CassFuture* future = NULL;
  const char* query = "SELECT * FROM examples.date_time 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);
    CassIterator* iterator = cass_iterator_from_result(result);

    if (cass_iterator_next(iterator)) {
      cass_uint32_t d;
      cass_int64_t t;
      time_t time;
      const CassRow* row = cass_iterator_get_row(iterator);
      cass_value_get_uint32(cass_row_get_column(row, 1), &d);
      cass_value_get_int64(cass_row_get_column(row, 2), &t);

      time = (time_t)cass_date_time_to_epoch(d, t);

      printf("Date and time: %s", asctime(localtime(&time)));
    }

    cass_result_free(result);
    cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);

  return rc;
}
Example #12
0
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;
}
Example #13
0
int CassandraFS::update_mode(const char* path, mode_t new_mode) {
    CassStatement* statement = cass_statement_new("UPDATE entries SET mode = ? WHERE path = ?", 2);
    cass_statement_bind_int32(statement, 0, new_mode);
    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;
}
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;
	
}
Example #15
0
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;
}
Example #16
0
void insert_into_perf(CassSession* session, const char* query, const CassPrepared* prepared) {
  int i;
  CassFuture* futures[NUM_CONCURRENT_REQUESTS];

  CassCollection* collection = cass_collection_new(CASS_COLLECTION_TYPE_SET, 2);
  cass_collection_append_string(collection, "jazz");
  cass_collection_append_string(collection, "2013");

  for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) {
    CassUuid id;
    CassStatement* statement;

    if (prepared != NULL) {
      statement = cass_prepared_bind(prepared);
    } else {
      statement = cass_statement_new(query, 5);
    }

    cass_uuid_gen_time(uuid_gen, &id);
    cass_statement_bind_uuid(statement, 0, id);
    cass_statement_bind_string(statement, 1, big_string);
    cass_statement_bind_string(statement, 2, big_string);
    cass_statement_bind_string(statement, 3, big_string);
    cass_statement_bind_collection(statement, 4, collection);

    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);
    }
    cass_future_free(future);
  }

  cass_collection_free(collection);
}
Example #17
0
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;
}
void CassDriver::Store(void (*cob)(CassFuture* future, void* data),
                       const Message& message, void* cb_data) {
  CassStatement* statement = cass_prepared_bind(insert_prepared_);
  cass_statement_bind_string(statement, 0,
                             cass_string_init(message.receiver_id.c_str()));
  cass_statement_bind_string(statement, 1,
                             cass_string_init(message.timestamp.c_str()));
  cass_statement_bind_string(statement, 2,
                             cass_string_init(message.msg_id.c_str()));
  cass_statement_bind_string(statement, 3,
                             cass_string_init(message.group_id.c_str()));
  cass_statement_bind_string(statement, 4,
                             cass_string_init(message.msg.c_str()));
  cass_statement_bind_string(statement, 5,
                             cass_string_init(message.sender_id.c_str()));

  CassFuture* future = cass_session_execute(session_, statement);
  cass_future_set_callback(future, cob, cb_data);
  cass_future_free(future);
  cass_statement_free(statement);
}
Example #19
0
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;
}
Example #20
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);
  }
}
Example #21
0
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;
}
inline CassError CassandraClient::executeStatement(std::string cqlStatement) {

    CassError rc = CASS_OK;
    CassFuture* result_future = NULL;

    logger.trace("Executing CQL Command");

    CassStatement* statement = cass_statement_new(cqlStatement.c_str(), 0);
    result_future = cass_session_execute(session, statement);
    cass_future_wait(result_future);

    rc = cass_future_error_code(result_future);
    if (rc == CASS_OK) {
        logger.trace("Executing CQL Command: Success");
    } else {
        return printError(rc);
    }

    cass_statement_free(statement);
    cass_future_free(result_future);

    return rc;
}
Example #23
0
void DbCassandra::GetAll( GetCallback callback )
{
    DbPtr db = db_;
    if ( db )
    {
        auto statement = cass_statement_new( "SELECT * FROM prun.jobs;", 0 );
        auto query_future = cass_session_execute( db_->GetSession(), statement );
        cass_statement_free( statement );

        CassError err = cass_future_error_code( query_future );
        if ( err != CASS_OK )
        {
            cass_future_free( query_future );
            throw std::logic_error( cass_error_desc( err ) );
        }

        auto result = cass_future_get_result( query_future );
        cass_future_free( query_future );

        auto iterator = cass_iterator_from_result( result );

        while( cass_iterator_next( iterator ) )
        {
            const CassRow *row = cass_iterator_get_row( iterator );

            const char *job_id, *job_descr;
            size_t job_id_length, job_descr_length;
            cass_value_get_string( cass_row_get_column( row, 0 ), &job_id, &job_id_length );
            cass_value_get_string( cass_row_get_column( row, 1 ), &job_descr, &job_descr_length );
            callback( std::string( job_id, job_id_length ), std::string( job_descr, job_descr_length ) );
        }

        cass_iterator_free( iterator );
        cass_result_free( result );
    }
}
 inline ~CassandraStatement()
 {
     if (statement)
         cass_statement_free(statement);
 }
Example #25
0
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;
}
Example #26
0
static void
free_statement(void *statement)
{
  cass_statement_free((CassStatement *) statement);
}
	static std::string LoadFile(Upload& upload)
	{

		std::stringstream ss;

		DatabaseSession session;

		try {

			CassError rc = CASS_OK;

			//std::string str = ("SELECT upload_id, model_id, date, processed, result {includeFile} FROM prophet.models {onlyPending}");
			std::string str = "SELECT chunk from prophet.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);

			rc = cass_future_error_code(future);

			if (rc != CASS_OK) {

				auto err = std::string("Failed to execute query: ")
					+ CassandraUtils::GetCassandraError(future);

				cass_future_free(future);
				cass_statement_free(statement);
				throw err;
			}
			else {
				const CassResult* result = cass_future_get_result(future);
				CassIterator* iterator = cass_iterator_from_result(result);

				while (cass_iterator_next(iterator)) {

					const CassRow* row = cass_iterator_get_row(iterator);

					std::string buffer;

					CassString::Load(buffer, cass_row_get_column(row, 0));

					ss << buffer;

				}

				cass_result_free(result);
				cass_iterator_free(iterator);

			}

			cass_future_free(future);
			cass_statement_free(statement);

		}
		catch (...) {
			std::exception_ptr e = std::current_exception();
			try {
				if (e) {
					std::rethrow_exception(e);
				}
			}
			catch (const std::exception& ex) {
				std::cout << "Caught exception \"" << ex.what() << "\"\n";
			}
		}
		return ss.str();

	}
void CassDriver::Retrieve(
    std::tr1::function<void(bool success, Wrapper* data)> cob,
    const std::string& receiver_id, Wrapper* data_wrapper) {
  CassStatement* statement = cass_prepared_bind(select_prepared_);
  cass_statement_bind_string(statement, 0,
                             cass_string_init(receiver_id.c_str()));
  cass_statement_set_paging_size(statement, FLAGS_page_size);

  CassFuture* future = cass_session_execute(session_, statement);
  auto retrieve_cb = [](CassFuture* future, void* data) {
    CassError rc = cass_future_error_code(future);
    Wrapper* wrapper = (Wrapper*)data;
    if (rc == CASS_OK) {
      const CassResult* result = cass_future_get_result(future);
      if (cass_result_row_count(result)) {
        CassIterator* iterator = cass_iterator_from_result(result);
        CassString cass_receiver, cass_time, cass_msg_id,
                   cass_group_id, cass_msg, cass_sender;
        while (cass_iterator_next(iterator)) {
          const CassRow* row = cass_iterator_get_row(iterator);
          cass_value_get_string(cass_row_get_column(row, 0), &cass_receiver);
          cass_value_get_string(cass_row_get_column(row, 1), &cass_time);
          cass_value_get_string(cass_row_get_column(row, 2), &cass_msg_id);
          cass_value_get_string(cass_row_get_column(row, 3), &cass_group_id);
          cass_value_get_string(cass_row_get_column(row, 4), &cass_msg);
          cass_value_get_string(cass_row_get_column(row, 5), &cass_sender);
            
          std::string receiver(cass_receiver.data, cass_receiver.length);
          std::string time(cass_time.data, cass_time.length);
          std::string msg_id(cass_msg_id.data, cass_msg_id.length);
          std::string group_id(cass_group_id.data, cass_group_id.length);
          std::string msg(cass_msg.data, cass_msg.length);
          std::string sender(cass_sender.data, cass_sender.length);

          boost::shared_ptr<Message> message(new Message());
          message->__set_receiver_id(receiver);
          message->__set_timestamp(time);
          message->__set_msg_id(msg_id);
          message->__set_group_id(group_id);
          message->__set_msg(msg);
          message->__set_sender_id(sender);
          wrapper->pmsgs->push_back(message);
        }
        cass_bool_t has_more_pages = cass_result_has_more_pages(result);
        if (has_more_pages) {
          cass_statement_set_paging_state(wrapper->statement, result);
          (wrapper->func)();
        } else {
          cass_statement_free(wrapper->statement);
          CassStatement* statement =
              cass_prepared_bind(wrapper->this_obj->delete_prepared_);
          cass_statement_bind_string(statement, 0, cass_receiver);
          CassFuture* delete_future =
              cass_session_execute(wrapper->this_obj->session_, statement);
          cass_future_free(delete_future);
          cass_statement_free(statement);

          (wrapper->cob)(true, wrapper);
        }
        cass_iterator_free(iterator);
      } else {
        cass_statement_free(wrapper->statement);
        (wrapper->cob)(true, wrapper);
      }
      cass_result_free(result);
    } else {
      cass_statement_free(wrapper->statement);
      wrapper->this_obj->PrintError(future);
      (wrapper->cob)(false, wrapper);
    }
  };

  data_wrapper->this_obj = this;
  data_wrapper->cob = cob;
  data_wrapper->statement = statement;
  data_wrapper->func = [=]() {
    CassFuture* future = cass_session_execute(session_, statement);
    cass_future_set_callback(future, retrieve_cb, data_wrapper);
    cass_future_free(future);
  };

  cass_future_set_callback(future, retrieve_cb, data_wrapper);
  cass_future_free(future);
}
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;

}
Example #30
0
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;
}