PHP_METHOD(FuturePreparedStatement, get)
{
  zval* timeout = NULL;
  cassandra_prepared_statement* prepared_statement = NULL;

  cassandra_future_prepared_statement* self =
    (cassandra_future_prepared_statement*) zend_object_store_get_object(getThis() TSRMLS_CC);

  if (self->prepared_statement) {
    RETURN_ZVAL(self->prepared_statement, 1, 0);
  }

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) {
    return;
  }

  if (php_cassandra_future_wait_timed(self->future, timeout TSRMLS_CC) == FAILURE) {
    return;
  }

  if (php_cassandra_future_is_error(self->future TSRMLS_CC) == FAILURE) {
    return;
  }

  object_init_ex(return_value, cassandra_prepared_statement_ce);
  self->prepared_statement = return_value;
  Z_ADDREF_P(self->prepared_statement);

  prepared_statement = (cassandra_prepared_statement*) zend_object_store_get_object(return_value TSRMLS_CC);

  prepared_statement->prepared = cass_future_get_prepared(self->future);
}
PHP_METHOD(FuturePreparedStatement, get)
{
  zval *timeout = NULL;
  cassandra_statement *prepared_statement = NULL;

  cassandra_future_prepared_statement *self = PHP_CASSANDRA_GET_FUTURE_PREPARED_STATEMENT(getThis());

  if (!PHP5TO7_ZVAL_IS_UNDEF(self->prepared_statement)) {
    RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->prepared_statement), 1, 0);
  }

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) {
    return;
  }

  if (php_cassandra_future_wait_timed(self->future, timeout TSRMLS_CC) == FAILURE) {
    return;
  }

  if (php_cassandra_future_is_error(self->future TSRMLS_CC) == FAILURE) {
    return;
  }

  object_init_ex(return_value, cassandra_statement_ce);
  PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->prepared_statement), return_value);

  prepared_statement = PHP_CASSANDRA_GET_STATEMENT(return_value);

  prepared_statement->prepared = cass_future_get_prepared(self->future);
}
Example #3
0
PHP_METHOD(FutureRows, get)
{
  zval* timeout = NULL;
  cassandra_rows* rows = NULL;
  const CassResult* result = NULL;

  cassandra_future_rows* self =
    (cassandra_future_rows*) zend_object_store_get_object(getThis() TSRMLS_CC);

  if (self->rows) {
    *return_value = *self->rows;
    Z_ADDREF_P(return_value);
    return;
  }

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) {
    return;
  }

  if (php_cassandra_future_wait_timed(self->future, timeout TSRMLS_CC) == FAILURE) {
    return;
  }

  if (php_cassandra_future_is_error(self->future TSRMLS_CC) == FAILURE) {
    return;
  }

  result = cass_future_get_result(self->future);

  if (!result) {
    zend_throw_exception_ex(cassandra_runtime_exception_ce, 0 TSRMLS_CC,
                            "Future doesn't contain a result.");
    return;
  }

  MAKE_STD_ZVAL(self->rows);
  object_init_ex(self->rows, cassandra_rows_ce);
  Z_ADDREF_P(self->rows);
  rows = (cassandra_rows*) zend_object_store_get_object(self->rows TSRMLS_CC);

  if (php_cassandra_get_result(result, &rows->rows TSRMLS_CC) == FAILURE) {
    cass_result_free(result);
    zval_ptr_dtor(&self->rows);
    return;
  }

  if (self->statement) {
    Z_ADDREF_P(self->session);
    rows->statement = php_cassandra_add_ref(self->statement);
    rows->session   = self->session;
    rows->result    = result;
  } else {
    cass_result_free(result);
  }

  *return_value = *self->rows;
  Z_ADDREF_P(return_value);
}
Example #4
0
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;
}
Example #5
0
PHP_METHOD(Rows, nextPage)
{
  zval* timeout = NULL;
  cassandra_session* session = NULL;
  CassFuture* future = NULL;
  const CassResult* result = NULL;
  cassandra_rows* rows = NULL;
  cassandra_future_rows* future_rows = NULL;

  cassandra_rows* self = (cassandra_rows*) zend_object_store_get_object(getThis() TSRMLS_CC);

  if (self->next_page) {
    RETURN_ZVAL(self->next_page, 1, 0);
  }

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) {
    return;
  }

  if (self->future_next_page) {
    if (!instanceof_function(Z_OBJCE_P(self->future_next_page),
                             cassandra_future_rows_ce TSRMLS_CC)) {
      zend_throw_exception_ex(cassandra_runtime_exception_ce, 0 TSRMLS_CC,
                              "Unexpected future instance.");
      return;
    }

    future_rows = (cassandra_future_rows*) zend_object_store_get_object(self->future_next_page TSRMLS_CC);

    if (php_cassandra_future_wait_timed(future_rows->future, timeout TSRMLS_CC) == FAILURE) {
      return;
    }

    if (php_cassandra_future_is_error(future_rows->future TSRMLS_CC) == FAILURE) {
      return;
    }

    result = cass_future_get_result(future_rows->future);
  } else {
    if (self->result == NULL) {
      return;
    }

    ASSERT_SUCCESS(cass_statement_set_paging_state((CassStatement*) self->statement->data, self->result));

    session = (cassandra_session*) zend_object_store_get_object(self->session TSRMLS_CC);
    future = cass_session_execute(session->session, (CassStatement*) self->statement->data);

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

  if (!result) {
    zend_throw_exception_ex(cassandra_runtime_exception_ce, 0 TSRMLS_CC,
                            "Future doesn't contain a result.");
    return;
  }

  MAKE_STD_ZVAL(self->next_page);
  object_init_ex(self->next_page, cassandra_rows_ce);
  rows = (cassandra_rows*) zend_object_store_get_object(self->next_page TSRMLS_CC);

  if (php_cassandra_get_result(result, &rows->rows TSRMLS_CC) == FAILURE) {
    cass_result_free(result);
    zval_dtor(self->next_page);
    self->next_page = NULL;
    return;
  }

  if (self->future_next_page) {
    zval_ptr_dtor(&self->future_next_page);
    self->future_next_page = NULL;
  }

  if (cass_result_has_more_pages(result)) {
    Z_ADDREF_P(self->session);
    rows->statement = php_cassandra_add_ref(self->statement);
    rows->session   = self->session;
    rows->result    = result;
  } else {
    cass_result_free(result);
  }

  php_cassandra_rows_clear(self);
  RETURN_ZVAL(self->next_page, 1, 0);
}
Example #6
0
PHP_METHOD(DefaultCluster, connect)
{
  CassFuture* future = NULL;
  char* hash_key;
  int   hash_key_len = 0;
  char* keyspace = NULL;
  int   keyspace_len;
  zval* timeout = NULL;
  cassandra_psession* psession;
  cassandra_cluster* cluster = NULL;
  cassandra_session* session = NULL;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sz", &keyspace, &keyspace_len, &timeout) == FAILURE) {
    return;
  }

  cluster = (cassandra_cluster*) zend_object_store_get_object(getThis() TSRMLS_CC);

  object_init_ex(return_value, cassandra_default_session_ce);
  session = (cassandra_session*) zend_object_store_get_object(return_value TSRMLS_CC);

  session->default_consistency = cluster->default_consistency;
  session->default_page_size   = cluster->default_page_size;
  session->default_timeout     = cluster->default_timeout;
  session->persist             = cluster->persist;

  if (session->default_timeout) {
    Z_ADDREF_P(session->default_timeout);
  }

  if (session->persist) {
    zend_rsrc_list_entry *le;

    hash_key_len = spprintf(&hash_key, 0, "%s:session:%s",
                            cluster->hash_key, SAFE_STR(keyspace));

    if (zend_hash_find(&EG(persistent_list), hash_key, hash_key_len + 1, (void **)&le) == SUCCESS &&
        Z_TYPE_P(le) == php_le_cassandra_session()) {
      psession = (cassandra_psession*) le->ptr;
      session->session = psession->session;
      future = psession->future;
    }
  }

  if (future == NULL) {
    session->session = cass_session_new();

    if (keyspace) {
      future = cass_session_connect_keyspace(session->session, cluster->cluster, keyspace);
    } else {
      future = cass_session_connect(session->session, cluster->cluster);
    }

    if (session->persist) {
      zend_rsrc_list_entry pe;
      psession = (cassandra_psession*) pecalloc(1, sizeof(cassandra_psession), 1);
      psession->session = session->session;
      psession->future  = future;

      pe.type = php_le_cassandra_session();
      pe.ptr  = psession;

      zend_hash_update(&EG(persistent_list), hash_key, hash_key_len + 1, &pe, sizeof(zend_rsrc_list_entry), NULL);
      CASSANDRA_G(persistent_sessions)++;
    }
  }

  if (php_cassandra_future_wait_timed(future, timeout TSRMLS_CC) == FAILURE) {
    if (session->persist) {
      efree(hash_key);
    } else {
      cass_future_free(future);
    }

    return;
  }

  if (php_cassandra_future_is_error(future TSRMLS_CC) == FAILURE) {
    if (session->persist) {
      if (zend_hash_del(&EG(persistent_list), hash_key, hash_key_len + 1) == SUCCESS) {
        session->session = NULL;
      }

      efree(hash_key);
    } else {
      cass_future_free(future);
    }

    return;
  }

  if (session->persist)
    efree(hash_key);
}
Example #7
0
PHP_METHOD(DefaultCluster, connect)
{
  CassFuture *future = NULL;
  char *hash_key;
  php5to7_size hash_key_len = 0;
  char *keyspace = NULL;
  php5to7_size keyspace_len;
  zval *timeout = NULL;
  cassandra_psession *psession;
  cassandra_cluster *cluster = NULL;
  cassandra_session *session = NULL;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sz", &keyspace, &keyspace_len, &timeout) == FAILURE) {
    return;
  }

  cluster = PHP_CASSANDRA_GET_CLUSTER(getThis());

  object_init_ex(return_value, cassandra_default_session_ce);
  session = PHP_CASSANDRA_GET_SESSION(return_value);

  session->default_consistency = cluster->default_consistency;
  session->default_page_size   = cluster->default_page_size;
  session->persist             = cluster->persist;

  if (!PHP5TO7_ZVAL_IS_UNDEF(session->default_timeout)) {
    PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(session->default_timeout),
                      PHP5TO7_ZVAL_MAYBE_P(cluster->default_timeout));
  }

  if (session->persist) {
    php5to7_zend_resource_le *le;

    hash_key_len = spprintf(&hash_key, 0, "%s:session:%s",
                            cluster->hash_key, SAFE_STR(keyspace));

    if (PHP5TO7_ZEND_HASH_FIND(&EG(persistent_list), hash_key, hash_key_len + 1, le) &&
        Z_RES_P(le)->type == php_le_cassandra_session()) {
      psession = (cassandra_psession *) Z_RES_P(le)->ptr;
      session->session = psession->session;
      future = psession->future;
    }
  }

  if (future == NULL) {
    php5to7_zend_resource_le resource;

    session->session = cass_session_new();

    if (keyspace) {
      future = cass_session_connect_keyspace(session->session, cluster->cluster, keyspace);
    } else {
      future = cass_session_connect(session->session, cluster->cluster);
    }

    if (session->persist) {
      psession = (cassandra_psession *) pecalloc(1, sizeof(cassandra_psession), 1);
      psession->session = session->session;
      psession->future  = future;

#if PHP_MAJOR_VERSION >= 7
      ZVAL_NEW_PERSISTENT_RES(&resource, 0, psession, php_le_cassandra_session());
      PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, &resource, sizeof(php5to7_zend_resource_le));
      CASSANDRA_G(persistent_sessions)++;
#else
      resource.type = php_le_cassandra_session();
      resource.ptr = psession;
      PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, resource, sizeof(php5to7_zend_resource_le));
      CASSANDRA_G(persistent_sessions)++;
#endif
    }
  }

  if (php_cassandra_future_wait_timed(future, timeout TSRMLS_CC) == FAILURE) {
    if (session->persist) {
      efree(hash_key);
    } else {
      cass_future_free(future);
    }

    return;
  }

  if (php_cassandra_future_is_error(future TSRMLS_CC) == FAILURE) {
    if (session->persist) {
      if (PHP5TO7_ZEND_HASH_DEL(&EG(persistent_list), hash_key, hash_key_len + 1)) {
        session->session = NULL;
      }

      efree(hash_key);
    } else {
      cass_future_free(future);
    }

    return;
  }

  if (session->persist)
    efree(hash_key);
}