示例#1
0
PyObject *
pycbc_Bucket__fts_query(pycbc_Bucket *self, PyObject *args, PyObject *kwargs)
{
    int rv;
    PyObject *ret = NULL;
    pycbc_MultiResult *mres;
    pycbc_ViewResult *vres;
    lcb_error_t rc;
    lcb_CMDFTS cmd = { 0 };
    const char *params;
    pycbc_strlen_t nparams;

    static char *kwlist[] = { "params", NULL };
    rv = PyArg_ParseTupleAndKeywords(args, kwargs,
        "s#", kwlist, &params, &nparams);

    if (!rv) {
        PYCBC_EXCTHROW_ARGS();
        return NULL;
    }
    if (-1 == pycbc_oputil_conn_lock(self)) {
        return NULL;
    }
    if (self->pipeline_queue) {
        PYCBC_EXC_WRAP(PYCBC_EXC_PIPELINE, 0,
                       "FTS queries cannot be executed in pipeline context");
    }

    mres = (pycbc_MultiResult *)pycbc_multiresult_new(self);
    vres = (pycbc_ViewResult *)PYCBC_TYPE_CTOR(&pycbc_ViewResultType);
    pycbc_httpresult_init(&vres->base, mres);
    vres->rows = PyList_New(0);
    vres->base.format = PYCBC_FMT_JSON;
    vres->base.htype = PYCBC_HTTP_HN1QL;

    cmd.callback = fts_row_callback;
    cmd.query = params;
    cmd.nquery = nparams;
    cmd.handle = &vres->base.u.fts;
    rc = lcb_fts_query(self->instance, mres, &cmd);

    if (rc != LCB_SUCCESS) {
        PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, rc, "Couldn't schedule fts query");
        goto GT_DONE;
    }

    ret = (PyObject *)mres;
    mres = NULL;

    GT_DONE:
    Py_XDECREF(mres);
    pycbc_oputil_conn_unlock(self);
    return ret;
}
示例#2
0
/**
 * Fetches a bunch of results from the network. Returns False when
 * no more results remain.
 */
PyObject *
pycbc_HttpResult__fetch(pycbc_HttpResult *self)
{
    lcb_error_t err;
    PyObject *ret = NULL;

    if (-1 == pycbc_oputil_conn_lock(self->parent)) {
        return NULL;
    }

    if (!self->htreq) {
        ret = Py_None;
        Py_INCREF(ret);
        goto GT_RET;
    }

    if (self->parent->flags & PYCBC_CONN_F_ASYNC) {
        PYCBC_EXC_WRAP(PYCBC_EXC_ARGUMENTS, 0,
                       "_fetch() should not be called with an async "
                       "connection");
        goto GT_RET;
    } else if (self->parent->pipeline_queue) {
        PYCBC_EXC_WRAP(PYCBC_EXC_PIPELINE, 0,
                       "HTTP requests cannot be executed in pipeline context");
    }

    if (!self->rowsbuf) {
        self->rowsbuf = PyList_New(0);
    }

    err = pycbc_oputil_wait_common(self->parent);

    if (err != LCB_SUCCESS) {
        PYCBC_EXCTHROW_WAIT(err);
        goto GT_RET;

    } else {
        if (maybe_raise(self)) {
            goto GT_RET;
        }

        ret = self->rowsbuf;
        self->rowsbuf = NULL;
    }

    if (!pycbc_assert(self->parent->nremaining == 0)) {
        fprintf(stderr, "Remaining count unexpected. Adjusting");
        self->parent->nremaining = 0;
    }

    GT_RET:
    pycbc_oputil_conn_unlock(self->parent);
    return ret;
}
/**
 * Fetches a bunch of results from the network. Returns False when
 * no more results remain.
 */
PyObject *
pycbc_HttpResult__fetch(pycbc_HttpResult *self)
{
    lcb_error_t err;
    PyObject *ret = NULL;

    if (-1 == pycbc_oputil_conn_lock(self->parent)) {
        return NULL;
    }

    if (!self->htreq) {
        ret = Py_None;
        Py_INCREF(ret);
        goto GT_RET;
    }

    if (!self->rowsbuf) {
        self->rowsbuf = PyList_New(0);
    }

    err = pycbc_oputil_wait_common(self->parent);

    if (err != LCB_SUCCESS) {
        PYCBC_EXCTHROW_WAIT(err);
        goto GT_RET;

    } else {
        if (maybe_raise(self)) {
            goto GT_RET;
        }

        ret = self->rowsbuf;
        self->rowsbuf = NULL;
    }

    if (!pycbc_assert(self->parent->nremaining == 0)) {
        fprintf(stderr, "Remaining count unexpected. Adjusting");
        self->parent->nremaining = 0;
    }

    GT_RET:
    pycbc_oputil_conn_unlock(self->parent);
    return ret;
}
示例#4
0
static PyObject *
ViewResult_fetch(pycbc_ViewResult *self, PyObject *args)
{
    PyObject *ret = NULL;
    pycbc_MultiResult *mres = NULL;
    pycbc_Bucket *bucket;
    int rv;

    rv = PyArg_ParseTuple(args, "O", &mres);
    if (!rv) {
        PYCBC_EXCTHROW_ARGS();
        return NULL;
    }

    bucket = mres->parent;

    if (bucket->flags & PYCBC_CONN_F_ASYNC) {
        PYCBC_EXC_WRAP(PYCBC_EXC_INTERNAL, 0, "Cannot use fetch with async");
        return NULL;
    }

    if (-1 == pycbc_oputil_conn_lock(bucket)) {
        return NULL;
    }

    if (!self->base.done) {
        pycbc_oputil_wait_common(bucket);
    }

    if (pycbc_multiresult_maybe_raise(mres)) {
        goto GT_DONE;
    }

    ret = self->rows ? self->rows : PyList_New(0);
    self->rows = PyList_New(0);

    GT_DONE:
    pycbc_oputil_conn_unlock(bucket);
    return ret;
}
示例#5
0
PyObject *
pycbc_Bucket__view_request(pycbc_Bucket *self, PyObject *args, PyObject *kwargs)
{
    int rv;
    PyObject *ret = NULL;
    pycbc_MultiResult *mres = NULL;
    pycbc_ViewResult *vres = NULL;
    lcb_CMDVIEWQUERY vcmd = { 0 };
    viewpath_st vp = { NULL };
    lcb_error_t rc;
    const char *view = NULL, *design = NULL;
    PyObject *options = NULL;
    int flags;

    static char *kwlist[] = { "design", "view", "options", "_flags", NULL };

    rv = PyArg_ParseTupleAndKeywords(args, kwargs, "ss|Oi", kwlist,
                                     &design, &view, &options, &flags);
    if (!rv) {
        PYCBC_EXCTHROW_ARGS();
        return NULL;
    }
    if (-1 == pycbc_oputil_conn_lock(self)) {
        return NULL;
    }

    if (self->pipeline_queue) {
        PYCBC_EXC_WRAP(PYCBC_EXC_PIPELINE, 0,
                       "HTTP/View Requests cannot be executed in "
                       "pipeline context");
        goto GT_DONE;
    }

    mres = (pycbc_MultiResult *)pycbc_multiresult_new(self);
    vres = (pycbc_ViewResult *)PYCBC_TYPE_CTOR(&pycbc_ViewResultType);
    vres->base.htype = PYCBC_HTTP_HVIEW;

    pycbc_httpresult_init(&vres->base, mres);

    rv = get_viewpath_str(self, &vp, options);
    if (rv != 0) {
        goto GT_DONE;
    }

    vcmd.ddoc = design;
    vcmd.nddoc = strlen(design);
    vcmd.view = view;
    vcmd.nview = strlen(view);
    vcmd.optstr = vp.optstr;
    vcmd.noptstr = vp.noptstr;
    vcmd.postdata = vp.body;
    vcmd.npostdata = vp.nbody;
    vcmd.handle = &vres->base.u.vh;
    vcmd.callback = row_callback;
    vcmd.cmdflags = flags;

    vres->rows = PyList_New(0);
    vres->base.format = PYCBC_FMT_JSON;

    rc = lcb_view_query(self->instance, mres, &vcmd);

    if (rc != LCB_SUCCESS) {
        PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, rc, "Couldn't schedule view");
        goto GT_DONE;
    }

    ret = (PyObject*)mres;
    mres = NULL; /* Avoid GT_DONE decref */

    GT_DONE:
    Py_XDECREF(mres);
    Py_XDECREF(vp.bk);
    pycbc_oputil_conn_unlock(self);
    return ret;
}
示例#6
0
PyObject *
pycbc_Bucket__n1ql_query(pycbc_Bucket *self, PyObject *args, PyObject *kwargs)
{
    int rv;
    PyObject *ret = NULL;
    pycbc_MultiResult *mres;
    pycbc_ViewResult *vres;
    lcb_error_t rc;
    lcb_CMDN1QL cmd = { 0 };
    const char *params;
    pycbc_strlen_t nparams;
    int prepared = 0, cross_bucket = 0;

    static char *kwlist[] = { "params", "prepare", "cross_bucket", NULL };
    rv = PyArg_ParseTupleAndKeywords(
        args, kwargs, "s#|ii", kwlist, &params, &nparams, &prepared, &cross_bucket);

    if (!rv) {
        PYCBC_EXCTHROW_ARGS();
        return NULL;
    }
    if (-1 == pycbc_oputil_conn_lock(self)) {
        return NULL;
    }
    if (self->pipeline_queue) {
        PYCBC_EXC_WRAP(PYCBC_EXC_PIPELINE, 0,
                       "N1QL queries cannot be executed in "
                       "pipeline context");
    }

    mres = (pycbc_MultiResult *)pycbc_multiresult_new(self);
    vres = (pycbc_ViewResult *)PYCBC_TYPE_CTOR(&pycbc_ViewResultType);
    pycbc_httpresult_init(&vres->base, mres);
    vres->rows = PyList_New(0);
    vres->base.format = PYCBC_FMT_JSON;
    vres->base.htype = PYCBC_HTTP_HN1QL;

    cmd.content_type = "application/json";
    cmd.callback = n1ql_row_callback;
    cmd.query = params;
    cmd.nquery = nparams;
    cmd.handle = &vres->base.u.nq;
    if (prepared) {
        cmd.cmdflags |= LCB_CMDN1QL_F_PREPCACHE;
    }
    if (cross_bucket) {
        cmd.cmdflags |= LCB_CMD_F_MULTIAUTH;
    }
    rc = lcb_n1ql_query(self->instance, mres, &cmd);

    if (rc != LCB_SUCCESS) {
        PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, rc, "Couldn't schedule n1ql query");
        goto GT_DONE;
    }

    ret = (PyObject *)mres;
    mres = NULL;

    GT_DONE:
    Py_XDECREF(mres);
    pycbc_oputil_conn_unlock(self);
    return ret;
}
PyObject *
pycbc_Connection__http_request(pycbc_Connection *self,
                               PyObject *args,
                               PyObject *kwargs)
{
    int rv;
    int method;
    int reqtype;
    unsigned short value_format = 0;
    lcb_error_t err;

    const char *body = NULL;
    PyObject *ret = NULL;
    PyObject *quiet_O = NULL;
    PyObject *chunked_O = NULL;
    PyObject *fetch_headers_O = Py_False;
    pycbc_strlen_t nbody = 0;
    const char *path = NULL;
    const char *content_type = NULL;
    pycbc_HttpResult *htres;
    lcb_http_request_t l_htreq;

    lcb_http_cmd_t htcmd = { 0 };

    static char *kwlist[] = {
            "type", "method", "path", "content_type", "post_data",
            "response_format", "quiet", "fetch_headers",
            "chunked", NULL
    };

    rv = PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "iis|zz#HOOO", kwlist,
                                     &reqtype,
                                     &method,
                                     &path,
                                     &content_type,
                                     &body,
                                     &nbody,
                                     &value_format,
                                     &quiet_O,
                                     &fetch_headers_O,
                                     &chunked_O);
    if (!rv) {
        PYCBC_EXCTHROW_ARGS();
        return NULL;
    }

    if (-1 == pycbc_oputil_conn_lock(self)) {
        return NULL;
    }


    htres = pycbc_httpresult_new(self);
    htres->key = pycbc_SimpleStringZ(path);
    htres->format = value_format;
    htres->htflags = 0;

    if (quiet_O != NULL && quiet_O != Py_None && PyObject_IsTrue(quiet_O)) {
        htres->htflags |= PYCBC_HTRES_F_QUIET;
    }

    if (fetch_headers_O && PyObject_IsTrue(fetch_headers_O)) {
        htres->headers = PyDict_New();
    }

    if (chunked_O && PyObject_IsTrue(chunked_O)) {
        htcmd.v.v0.chunked = 1;
        htres->rctx = lcbex_vrow_create();
        htres->rctx->callback = http_vrow_callback;
        htres->rctx->user_cookie = htres;
        htres->htflags |= PYCBC_HTRES_F_CHUNKED;
    }

    htcmd.v.v1.body = body;
    htcmd.v.v1.nbody = nbody;
    htcmd.v.v1.content_type = content_type;
    htcmd.v.v1.path = path;
    htcmd.v.v1.npath = strlen(path);
    htcmd.v.v1.method = method;


    err = lcb_make_http_request(self->instance,
                                htres,
                                reqtype,
                                &htcmd,
                                &l_htreq);

    if (err != LCB_SUCCESS) {
        PYCBC_EXCTHROW_SCHED(err);
        goto GT_DONE;
    }

    htres->htreq = l_htreq;

    if (htcmd.v.v0.chunked) {
        ret = (PyObject*)htres;
        htres = NULL;
        goto GT_DONE;
    }

    self->nremaining++;
    err = pycbc_oputil_wait_common(self);

    if (err != LCB_SUCCESS) {
        self->nremaining--;
        PYCBC_EXCTHROW_WAIT(err);
        goto GT_DONE;
    }

    if (maybe_raise(htres)) {
        goto GT_DONE;
    }

    ret = (PyObject*)htres;
    htres = NULL;

    GT_DONE:
    Py_XDECREF(htres);
    pycbc_oputil_conn_unlock(self);
    return ret;
}