Class: Ilios::Cassandra::Future

Inherits:
Object
  • Object
show all
Defined in:
ext/ilios/ilios.c

Instance Method Summary collapse

Instance Method Details

#awaitCassandra::Future

Wait to complete a future’s statement.

Returns:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'ext/ilios/future.c', line 286

static VALUE future_await(VALUE self)
{
    CassandraFuture *cassandra_future;

    GET_FUTURE(self, cassandra_future);

    if (cassandra_future->already_waited) {
        rb_raise(eExecutionError, "It should not call twice");
    }

    if (cassandra_future->on_success_block || cassandra_future->on_failure_block) {
        nogvl_sem_wait(&cassandra_future->sem);
    } else {
        nogvl_future_wait(cassandra_future->future);
    }
    cassandra_future->already_waited = true;
    return self;
}

#on_failureCassandra::Future

Run block when future resolves to error.

Returns:

Raises:



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'ext/ilios/future.c', line 241

static VALUE future_on_failure(VALUE self)
{
    CassandraFuture *cassandra_future;
    bool wakeup_thread = false;

    GET_FUTURE(self, cassandra_future);

    if (cassandra_future->on_failure_block) {
        rb_raise(eExecutionError, "It should not call twice");
    }
    if (!rb_block_given_p()) {
        rb_raise(rb_eArgError, "no block given");
    }

    rb_mutex_lock(cassandra_future->proc_mutex);

    if (!cassandra_future->on_success_block) {
        // Invoke the callback with thread pool only once
        wakeup_thread = true;
    }

    cassandra_future->on_failure_block = rb_block_proc();

    if (cass_future_ready(cassandra_future->future)) {
        rb_mutex_unlock(cassandra_future->proc_mutex);
        uv_sem_post(&cassandra_future->sem);
        if (cass_future_error_code(cassandra_future->future) != CASS_OK) {
            future_result_failure_yield(cassandra_future);
        }
        return self;
    }

    if (wakeup_thread) {
        future_queue_push(future_thread_pool_get(cassandra_future), self);
    }
    rb_mutex_unlock(cassandra_future->proc_mutex);

    return self;
}

#on_success {|value| ... } ⇒ Cassandra::Future

Run block when future resolves to a value.

Yield Parameters:

  • value (Cassandra::Statement, Cassandra::Result)

    A value. Yields Cassandra::Statement object when future was created by Cassandra::Session#prepare_async. Yields Cassandra::Result object when future was created by Cassandra::Session#execute_async.

Returns:

Raises:



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'ext/ilios/future.c', line 194

static VALUE future_on_success(VALUE self)
{
    CassandraFuture *cassandra_future;
    bool wakeup_thread = false;

    GET_FUTURE(self, cassandra_future);

    if (cassandra_future->on_success_block) {
        rb_raise(eExecutionError, "It should not call twice");
    }
    if (!rb_block_given_p()) {
        rb_raise(rb_eArgError, "no block given");
    }

    rb_mutex_lock(cassandra_future->proc_mutex);

    if (!cassandra_future->on_failure_block) {
        // Invoke the callback with thread pool only once
        wakeup_thread = true;
    }

    cassandra_future->on_success_block = rb_block_proc();

    if (cass_future_ready(cassandra_future->future)) {
        rb_mutex_unlock(cassandra_future->proc_mutex);
        uv_sem_post(&cassandra_future->sem);
        if (cass_future_error_code(cassandra_future->future) == CASS_OK) {
            future_result_success_yield(cassandra_future);
        }
        return self;
    }

    if (wakeup_thread) {
        future_queue_push(future_thread_pool_get(cassandra_future), self);
    }
    rb_mutex_unlock(cassandra_future->proc_mutex);

    return self;
}