Couchbase-Client

 view release on metacpan or  search on metacpan

xs/Client.xs  view on Meta::CPAN

    if(object->fld) { \
        SvREFCNT_dec(object->fld); object->fld = NULL; \
}
    _free_cv(cv_compress); _free_cv(cv_decompress);
    _free_cv(cv_serialize); _free_cv(cv_deserialize);
#undef _free_cv
}

void plcb_errstack_push(PLCB_t *object, libcouchbase_error_t err,
                        const char *errinfo)
{
    libcouchbase_t instance;
    SV *errsvs[2];

    instance = object->instance;
    if(!errinfo) {
        errinfo = libcouchbase_strerror(instance, err);
    }
    errsvs[0] = newSViv(err);
    errsvs[1] = newSVpv(errinfo, 0);
    av_push(object->errors,
            newRV_noinc( (SV*)av_make(2, errsvs)));
}

/*Construct a new libcouchbase object*/
SV *PLCB_construct(const char *pkg, AV *options)
{
    libcouchbase_t instance;
    libcouchbase_error_t err;
    struct libcouchbase_io_opt_st *io_ops;
    SV *blessed_obj;
    PLCB_t *object;

    char *host = NULL, *username = NULL, *password = NULL, *bucket = NULL;

    plcb_ctor_cbc_opts(options,
                         &host, &username, &password, &bucket);


    io_ops = libcouchbase_create_io_ops(
        LIBCOUCHBASE_IO_OPS_DEFAULT, NULL, &err);

    if(io_ops == NULL && err != LIBCOUCHBASE_SUCCESS) {
        die("Couldn't create new IO operations: %d", err);
    }

    instance = libcouchbase_create(host, username, password, bucket, io_ops);

    if(!instance) {
        die("Failed to create instance");
    }

    Newxz(object, 1, PLCB_t);

    object->io_ops = io_ops;
    plcb_ctor_conversion_opts(object, options);
    plcb_ctor_init_common(object, instance, options);

    libcouchbase_set_cookie(instance, object);

    plcb_callbacks_setup(object);

    blessed_obj = newSV(0);
    sv_setiv(newSVrv(blessed_obj, "Couchbase::Client"), PTR2IV(object));

    if( (object->my_flags & PLCBf_NO_CONNECT) == 0) {
        PLCB_connect(blessed_obj);
    }

    return blessed_obj;
}


#define mk_instance_vars(sv, inst_name, obj_name) \
    if(!SvROK(sv)) { die("self must be a reference"); } \
    obj_name = NUM2PTR(PLCB_t*, SvIV(SvRV(sv))); \
    if(!obj_name) { die("tried to access de-initialized PLCB_t"); } \
    inst_name = obj_name->instance;

#define bless_return(object, rv, av) \
    return plcb_ret_blessed_rv(object, av);


int
PLCB_connect(SV *self)
{
    libcouchbase_t instance;
    libcouchbase_error_t err;
    AV *retav;
    PLCB_t *object;

    mk_instance_vars(self, instance, object);

    av_clear(object->errors);

    if(object->connected) {
        warn("Already connected");
        return 1;
    } else {
        if( (err = libcouchbase_connect(instance)) == LIBCOUCHBASE_SUCCESS) {
            libcouchbase_wait(instance);
            if(av_len(object->errors) > -1) {
                return 0;
            }
            object->connected = 1;
            return 1;
        } else {
            plcb_errstack_push(object, err, NULL);
        }
    }
    return 0;
}


#define _sync_return_single(object, err, syncp) \
    if(err != LIBCOUCHBASE_SUCCESS ) { \
        plcb_ret_set_err(object, syncp->ret, err); \
    } else { \
        wait_for_single_response(object); \
    } \
    return plcb_ret_blessed_rv(object, syncp->ret);



( run in 0.500 second using v1.01-cache-2.11-cpan-140bd7fdf52 )