~ubuntu-branches/debian/sid/php-cassandra/sid

« back to all changes in this revision

Viewing changes to cassandra-1.3.0/util/future.c

  • Committer: Package Import Robot
  • Author(s): Ondřej Surý
  • Date: 2017-04-18 17:16:30 UTC
  • Revision ID: package-import@ubuntu.com-20170418171630-fw8udixss0879s32
Tags: upstream-1.3.0
Import upstream version 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2015-2016 DataStax, Inc.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "php_driver.h"
 
18
#include "php_driver_types.h"
 
19
#include "future.h"
 
20
 
 
21
int
 
22
php_driver_future_wait_timed(CassFuture *future, zval *timeout TSRMLS_DC)
 
23
{
 
24
  cass_duration_t timeout_us;
 
25
 
 
26
  if (cass_future_ready(future)) return SUCCESS;
 
27
 
 
28
#if PHP_MAJOR_VERSION >= 7
 
29
  if (timeout == NULL ||
 
30
      Z_TYPE_P(timeout) == IS_NULL ||
 
31
      Z_TYPE_P(timeout) == IS_UNDEF) {
 
32
#else
 
33
  if (timeout == NULL || Z_TYPE_P(timeout) == IS_NULL) {
 
34
#endif
 
35
 
 
36
    cass_future_wait(future);
 
37
  } else {
 
38
    if ((Z_TYPE_P(timeout) == IS_LONG && Z_LVAL_P(timeout) > 0)) {
 
39
      timeout_us = Z_LVAL_P(timeout) * 1000000;
 
40
    } else if ((Z_TYPE_P(timeout) == IS_DOUBLE && Z_DVAL_P(timeout) > 0)) {
 
41
      timeout_us = ceil(Z_DVAL_P(timeout) * 1000000);
 
42
    } else {
 
43
      INVALID_ARGUMENT_VALUE(timeout, "an positive number of seconds or null", FAILURE);
 
44
    }
 
45
 
 
46
    if (!cass_future_wait_timed(future, timeout_us)) {
 
47
      zend_throw_exception_ex(php_driver_timeout_exception_ce, 0 TSRMLS_CC,
 
48
                              "Future hasn't resolved within %f seconds", timeout_us / 1000000.0);
 
49
      return FAILURE;
 
50
    }
 
51
  }
 
52
 
 
53
  return SUCCESS;
 
54
}
 
55
 
 
56
int
 
57
php_driver_future_is_error(CassFuture *future TSRMLS_DC)
 
58
{
 
59
  int rc = cass_future_error_code(future);
 
60
  if (rc != CASS_OK) {
 
61
    const char *message;
 
62
    size_t      message_len;
 
63
    cass_future_error_message(future, &message, &message_len);
 
64
    zend_throw_exception_ex(exception_class(rc), rc TSRMLS_CC,
 
65
                            "%.*s", (int) message_len, message);
 
66
    return FAILURE;
 
67
  }
 
68
  return SUCCESS;
 
69
}