Ejemplo n.º 1
0
 inline CassandraStatement(const CassandraPrepared *_prepared) : statement(cass_prepared_bind(*_prepared))
 {
     const char *queryString = _prepared->queryQueryString(); // Only set when tracing..
     if (queryString)
         query.set(queryString);
     _prepared->Release();
 }
Ejemplo n.º 2
0
  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;
  }
Ejemplo n.º 3
0
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);
  }
}
Ejemplo n.º 4
0
 bool insert_prepared_task(const CassPrepared* prepared, CassConsistency consistency, int rows_per_id) {
     bool is_successful = true;
     for (int i = 0; i < rows_per_id; ++i) {
         test_utils::CassStatementPtr statement(cass_prepared_bind(prepared));
         cass_statement_set_consistency(statement.get(), consistency);
         if (!bind_and_execute_insert(statement.get())) {
             is_successful = false;
         }
     }
     return is_successful;
 }
Ejemplo n.º 5
0
static void alter_schmea_version(void *arg) {
  ClusterInit* cluster_init = static_cast<ClusterInit*>(arg);
  test_utils::CassLog::reset("No schema agreement on live nodes after ");
  test_utils::CassStatementPtr schema_stmt(cass_prepared_bind(cluster_init->schema_alter_prepared.get()));
  boost::chrono::steady_clock::time_point end =
    boost::chrono::steady_clock::now() + boost::chrono::milliseconds(MAX_SCHEMA_AGREEMENT_WAIT_MS + 1000);
  // mess with system.peers for node 1 more than the wait time; targetting specific nodes
  do {
    cass_statement_bind_uuid(schema_stmt.get(), 0, test_utils::generate_random_uuid(cluster_init->inst.uuid_gen));
    test_utils::CassFuturePtr future(cass_session_execute(cluster_init->schema_alter_session, schema_stmt.get()));
    cass_future_wait(future.get());
    BOOST_REQUIRE_EQUAL(cass_future_error_code(future.get()), CASS_OK);
  } while (boost::chrono::steady_clock::now() < end && test_utils::CassLog::message_count() == 0);
}
Ejemplo n.º 6
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 ) );
    }
}
Ejemplo n.º 7
0
CassError insert_into_batch_with_prepared(CassSession* session, const CassPrepared* prepared, const Pair* pairs) {
  CassError rc = CASS_OK;
  CassFuture* future = NULL;
  CassBatch* batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED);

  const Pair* pair;
  for (pair = pairs; pair->key != NULL; pair++) {
    CassStatement* statement = cass_prepared_bind(prepared);
    cass_statement_bind_string(statement, 0, pair->key);
    cass_statement_bind_string(statement, 1, pair->value);
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  {
    CassStatement* statement = cass_statement_new("INSERT INTO examples.pairs (key, value) VALUES ('c', '3')", 0);
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  {
    CassStatement* statement = cass_statement_new("INSERT INTO examples.pairs (key, value) VALUES (?, ?)", 2);
    cass_statement_bind_string(statement, 0, "d");
    cass_statement_bind_string(statement, 1, "4");
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  future = cass_session_execute_batch(session, batch);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    print_error(future);
  }

  cass_future_free(future);
  cass_batch_free(batch);

  return rc;
}
Ejemplo n.º 8
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);
}
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);
}
Ejemplo n.º 10
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;
}
  /**
   * Check the column count of a bound statement before and after adding a
   * column to a table.
   *
   * @param expected_column_count_after_update
   */
  void prepared_check_column_count_after_alter(size_t expected_column_count_after_update) {
    test_utils::CassSessionPtr session(test_utils::create_session(cluster));

    test_utils::execute_query(session.get(), str(boost::format("USE %s") % keyspace));

    test_utils::CassFuturePtr future(cass_session_prepare(session.get(), "SELECT * FROM test WHERE k = 'key1'"));
    BOOST_REQUIRE_EQUAL(cass_future_error_code(future.get()), CASS_OK);

    test_utils::CassPreparedPtr prepared(cass_future_get_prepared(future.get()));
    BOOST_REQUIRE(prepared);

    test_utils::CassStatementPtr bound_statement(cass_prepared_bind(prepared.get()));
    BOOST_REQUIRE(bound_statement);

    // Verify that the table has two columns in the metadata
    {
      test_utils::CassFuturePtr result_future(cass_session_execute(session.get(), bound_statement.get()));
      BOOST_REQUIRE_EQUAL(cass_future_error_code(result_future.get()), CASS_OK);

      test_utils::CassResultPtr result(cass_future_get_result(result_future.get()));

      BOOST_CHECK_EQUAL(cass_result_column_count(result.get()), 2u);
    }

    // Add a column to the table
    test_utils::execute_query(session.get(), "ALTER TABLE test ADD v2 int");

    // The column count shouldn't have changed
    {
      test_utils::CassFuturePtr result_future(cass_session_execute(session.get(), bound_statement.get()));
      BOOST_REQUIRE_EQUAL(cass_future_error_code(result_future.get()), CASS_OK);

      test_utils::CassResultPtr result(cass_future_get_result(result_future.get()));

      BOOST_CHECK_EQUAL(cass_result_column_count(result.get()), expected_column_count_after_update);
    }
  }
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);
}
Ejemplo n.º 13
0
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);
}