Class: Couchbase::Bucket::CouchRequest

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Bucket::CouchRequest) initialize

Initialize new CouchRequest

Since:

  • 1.2.0



4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
# File 'ext/couchbase_ext/couchbase_ext.c', line 4352

static VALUE
cb_couch_request_init(int argc, VALUE *argv, VALUE self)
{
    struct couch_request_st *request = DATA_PTR(self);
    VALUE bucket, path, opts, on_body, mm, pp, body;
    rb_scan_args(argc, argv, "22", &bucket, &pp, &opts, &on_body);

    if (NIL_P(on_body) && rb_block_given_p()) {
        on_body = rb_block_proc();
    }
    path = StringValue(pp); /* convert path to string */
    request->path = strdup(RSTRING_PTR(path));
    request->npath = RSTRING_LEN(path);
    request->on_body_callback = on_body;
    if (CLASS_OF(bucket) != cBucket) {
        rb_raise(rb_eTypeError, "wrong argument type (expected Couchbase::Bucket)");
    }
    request->bucket = DATA_PTR(bucket);
    request->bucket_obj = bucket;
    request->method = LIBCOUCHBASE_HTTP_METHOD_GET;
    request->extended = Qfalse;
    request->chunked = Qfalse;
    if (opts != Qnil) {
        Check_Type(opts, T_HASH);
        request->extended = RTEST(rb_hash_aref(opts, sym_extended));
        request->chunked = RTEST(rb_hash_aref(opts, sym_chunked));
        if ((mm = rb_hash_aref(opts, sym_method)) != Qnil) {
            if (mm == sym_get) {
                request->method = LIBCOUCHBASE_HTTP_METHOD_GET;
            } else if (mm == sym_post) {
                request->method = LIBCOUCHBASE_HTTP_METHOD_POST;
            } else if (mm == sym_put) {
                request->method = LIBCOUCHBASE_HTTP_METHOD_PUT;
            } else if (mm == sym_delete) {
                request->method = LIBCOUCHBASE_HTTP_METHOD_DELETE;
            } else {
                rb_raise(rb_eArgError, "unsupported HTTP method");
            }
        }
        if ((body = rb_hash_aref(opts, sym_body)) != Qnil) {
            Check_Type(body, T_STRING);
            request->body = strdup(RSTRING_PTR(body));
            request->nbody = RSTRING_LEN(body);
        }
    }

    return self;
}

Instance Attribute Details

- (Boolean) chunked (readonly) Also known as: chunked?

false if library should collect whole response before yielding, true if the client is ready to handle response in chunks.

Returns:

  • (Boolean)

    false if library should collect whole response before yielding, true if the client is ready to handle response in chunks.

Since:

  • 1.2.0



4524
4525
4526
4527
4528
4529
# File 'ext/couchbase_ext/couchbase_ext.c', line 4524

static VALUE
cb_couch_request_chunked_get(VALUE self)
{
    struct couch_request_st *req = DATA_PTR(self);
    return RTEST(req->chunked);
}

- (Boolean) extended (readonly) Also known as: extended?

If false the callbacks should receive just the data, and Result instance otherwise.

Returns:

  • (Boolean)

    if false the callbacks should receive just the data, and Result instance otherwise.

Since:

  • 1.2.0



4538
4539
4540
4541
4542
4543
# File 'ext/couchbase_ext/couchbase_ext.c', line 4538

static VALUE
cb_couch_request_extended_get(VALUE self)
{
    struct couch_request_st *req = DATA_PTR(self);
    return RTEST(req->extended);
}

- (String) path (readonly)

The requested path

Returns:

  • (String)

    the requested path

Since:

  • 1.2.0



4510
4511
4512
4513
4514
4515
# File 'ext/couchbase_ext/couchbase_ext.c', line 4510

static VALUE
cb_couch_request_path_get(VALUE self)
{
    struct couch_request_st *req = DATA_PTR(self);
    return rb_str_new2(req->path);
}

Instance Method Details

- (Object) continue



4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
# File 'ext/couchbase_ext/couchbase_ext.c', line 4480

static VALUE
cb_couch_request_continue(VALUE self)
{
    VALUE exc, *rv;
    struct couch_request_st *req = DATA_PTR(self);

    if (req->running) {
        libcouchbase_wait(req->bucket->handle);
        if (req->completed) {
            exc = req->ctx->exception;
            rv = req->ctx->rv;
            xfree(req->ctx);
            if (exc != Qnil) {
                cb_gc_unprotect(req->bucket, exc);
                rb_exc_raise(exc);
            }
            return *rv;
        }
    } else {
        cb_couch_request_perform(self);
    }
    return Qnil;
}

- (String) inspect

Returns a string containing a human-readable representation of the CouchRequest.

Returns:

  • (String)

Since:

  • 1.2.0



4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
# File 'ext/couchbase_ext/couchbase_ext.c', line 4327

static VALUE
cb_couch_request_inspect(VALUE self)
{
    VALUE str;
    struct couch_request_st *req = DATA_PTR(self);
    char buf[200];

    str = rb_str_buf_new2("#<");
    rb_str_buf_cat2(str, rb_obj_classname(self));
    snprintf(buf, 20, ":%p \"", (void *)self);
    rb_str_buf_cat2(str, buf);
    rb_str_buf_cat2(str, req->path);
    snprintf(buf, 100, "\" chunked:%s>", req->chunked ? "true" : "false");
    rb_str_buf_cat2(str, buf);

    return str;
}

- (Object) on_body

Set on_body callback

Since:

  • 1.2.0



4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
# File 'ext/couchbase_ext/couchbase_ext.c', line 4406

static VALUE
cb_couch_request_on_body(VALUE self)
{
    struct couch_request_st *request = DATA_PTR(self);
    VALUE old = request->on_body_callback;
    if (rb_block_given_p()) {
        request->on_body_callback = rb_block_proc();
    }
    return old;
}

- (Object) pause



4472
4473
4474
4475
4476
4477
4478
# File 'ext/couchbase_ext/couchbase_ext.c', line 4472

static VALUE
cb_couch_request_pause(VALUE self)
{
    struct couch_request_st *req = DATA_PTR(self);
    req->bucket->io->stop_event_loop(req->bucket->io);
    return Qnil;
}

- (Object) perform

Since:

  • 1.2.0



4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
# File 'ext/couchbase_ext/couchbase_ext.c', line 4422

static VALUE
cb_couch_request_perform(VALUE self)
{
    struct couch_request_st *req = DATA_PTR(self);
    struct context_st *ctx;
    VALUE rv, exc;
    libcouchbase_error_t err;
    struct bucket_st *bucket;

    ctx = xcalloc(1, sizeof(struct context_st));
    if (ctx == NULL) {
        rb_raise(eClientNoMemoryError, "failed to allocate memory");
    }
    rv = Qnil;
    ctx->rv = &rv;
    ctx->bucket = bucket = req->bucket;
    ctx->proc = rb_block_given_p() ? rb_block_proc() : req->on_body_callback;
    ctx->extended = req->extended;
    ctx->request = req;

    req->request = libcouchbase_make_couch_request(bucket->handle, (const void *)ctx,
            req->path, req->npath, req->body, req->nbody, req->method,
            req->chunked, &err);
    exc = cb_check_error(err, "failed to schedule document request",
            rb_str_new(req->path, req->npath));
    if (exc != Qnil) {
        xfree(ctx);
        rb_exc_raise(exc);
    }
    req->running = 1;
    req->ctx = ctx;
    if (bucket->async) {
        return Qnil;
    } else {
        libcouchbase_wait(bucket->handle);
        if (req->completed) {
            exc = ctx->exception;
            xfree(ctx);
            if (exc != Qnil) {
                cb_gc_unprotect(bucket, exc);
                rb_exc_raise(exc);
            }
            return rv;
        } else {
            return Qnil;
        }
    }
    return Qnil;
}