def submit_single(
            self, c_key, c_len, value, item, key_options, global_options, mres):

        value_format = get_option('format', key_options, global_options)
        if item:
            if self.OPTYPE in (C.LCB_APPEND, C.LCB_PREPEND):
                try:
                    value = key_options['fragment']
                except (KeyError, TypeError):
                    pycbc_exc_args('append/prepend must provide `fragment`')


        try:
            v_enc, flags = self.parent._tc.encode_value(value, value_format)
            if not isinstance(v_enc, bytes):
                raise ValueFormatError.pyexc("Value was not bytes", obj=v_enc)
            s_val = ffi.new('char[]', v_enc)
        except CouchbaseError:
            raise
        except Exception as ex:
            raise ValueFormatError.pyexc(str(value_format), inner=ex)

        C._Cb_set_key(self.c_command, c_key, c_len)
        C._Cb_set_val(self.c_command, s_val, len(v_enc))
        if self.OPTYPE not in (C.LCB_APPEND, C.LCB_PREPEND):
            try:
                self.c_command.flags = flags
            except OverflowError:
                pycbc_exc_enc('Invalid flags value', obj=flags)

        self.c_command.exptime = get_ttl(key_options, global_options, item)
        self.c_command.cas = get_cas(key_options, global_options, item)
        self.c_command.operation = self.OPTYPE
        return C.lcb_store3(self.instance, mres._cdata, self.c_command)
    def process_single_command(self, req, koptions):
        _tmp = koptions
        koptions = self._koptions.copy()
        koptions.update(_tmp)
        try:
            v_enc, flags = self._conn._tc.encode_value(koptions['value'],
                                                       koptions['format'])
            if not isinstance(v_enc, bytes):
                raise ValueFormatError.pyexc("Value was not bytes", obj=v_enc)

            s_val = ffi.new('char[]', v_enc)

        except CouchbaseError:
            raise

        except Exception as ex:
            raise ValueFormatError.pyexc(str(ex))

        self._cdata.append(s_val)

        req.bytes = s_val
        req.nbytes = len(v_enc)

        self.extract_cas(req, koptions)
        self.extract_ttl(req, koptions)

        try:
            req.flags = flags
        except Exception as e:
            raise ValueFormatError.pyexc(obj=flags, inner=e)

        req.operation = self._mode
Exemple #3
0
    def process_single_command(self, req, koptions):
        _tmp = koptions
        koptions = self._koptions.copy()
        koptions.update(_tmp)
        try:
            v_enc, flags = self._conn._tc.encode_value(koptions['value'],
                                                       koptions['format'])
            if not isinstance(v_enc, bytes):
                raise ValueFormatError.pyexc("Value was not bytes", obj=v_enc)

            s_val = ffi.new('char[]', v_enc)

        except CouchbaseError:
            raise

        except Exception as ex:
            raise ValueFormatError.pyexc(str(ex))

        self._cdata.append(s_val)

        req.bytes = s_val
        req.nbytes = len(v_enc)

        self.extract_cas(req, koptions)
        self.extract_ttl(req, koptions)

        try:
            req.flags = flags
        except Exception as e:
            raise ValueFormatError.pyexc(obj=flags, inner=e)

        req.operation = self._mode
Exemple #4
0
    def submit_single(self, c_key, c_len, value, item, key_options,
                      global_options, mres):

        value_format = get_option('format', key_options, global_options)
        if item:
            if self.OPTYPE in (C.LCB_APPEND, C.LCB_PREPEND):
                try:
                    value = key_options['fragment']
                except (KeyError, TypeError):
                    pycbc_exc_args('append/prepend must provide `fragment`')

        try:
            v_enc, flags = self.parent._tc.encode_value(value, value_format)
            if not isinstance(v_enc, bytes):
                raise ValueFormatError.pyexc("Value was not bytes", obj=v_enc)
            s_val = ffi.new('char[]', v_enc)
        except CouchbaseError:
            raise
        except Exception as ex:
            raise ValueFormatError.pyexc(str(value_format), inner=ex)

        C._Cb_set_key(self.c_command, c_key, c_len)
        C._Cb_set_val(self.c_command, s_val, len(v_enc))
        if self.OPTYPE not in (C.LCB_APPEND, C.LCB_PREPEND):
            try:
                self.c_command.flags = flags
            except OverflowError:
                pycbc_exc_enc('Invalid flags value', obj=flags)

        self.c_command.exptime = get_ttl(key_options, global_options, item)
        self.c_command.cas = get_cas(key_options, global_options, item)
        self.c_command.operation = self.OPTYPE
        return C.lcb_store3(self.instance, mres._cdata, self.c_command)
    def _get_callback(self, instance, cookie, err, resp):
        res = ValueResult()
        res.cas = resp.v.v0.cas
        res.flags = resp.v.v0.flags
        res.rc = err

        if not err:
            value = ffi.buffer(resp.v.v0.bytes, resp.v.v0.nbytes)[:]
            effective_format = resp.v.v0.flags
            if self.data_passthrough or self._cur.ctx.no_format:
                effective_format = FMT_BYTES

            try:
                value = self._tc.decode_value(value, effective_format)
                res.value = value

            except Exception as e:
                res.value = None
                exc = ValueFormatError.pyexc(inner=e, obj=value)
                self._cur.throw(exc)

        else:
            res.value = None

        self._add_response(resp, res)
    def _m_single(self, key, value, **kwargs):
        try:
            kv = { key: value }
        except Exception as e:
            raise ValueFormatError.pyexc("Unhashable key", obj=key, inner=e)

        ctx = StoreCommandContext(self, mode, kv, **kwargs)
        return self._invoke_common(C.lcb_store, ctx, False)
    def _build_single(self, kv, kviter, ix):
        try:
            cur = kviter.next()
        except AttributeError:
            cur = kviter.__next__()

        if isinstance(kv, dict):
            key, koptions = cur
            if not isinstance(koptions, Options):
                if isinstance(koptions, OperationResult):
                    koptions = Options(cas=koptions.cas)
                else:
                    koptions = self.convert_to_koptions(koptions)

        else:
            key = cur
            koptions = None

            if isinstance(key, Result):
                if isinstance(key, OperationResult):
                    koptions = Options(cas=key.cas)
                key = key.key

            if koptions is None:
                koptions = Options()

        cmd = self._cmdlist[ix]
        self._cmdpp[ix] = ffi.addressof(cmd)

        try:
            k_enc = self._conn._tc.encode_key(key)
            if not k_enc:
                if not key:
                    excls = ArgumentError
                else:
                    excls = ValueFormatError

                raise excls.pyexc("Key is empty", obj=key)

            s_key = ffi.new('char[]', k_enc)

        except CouchbaseError:
            raise
        except Exception as e:
            raise ValueFormatError.pyexc(obj=key, inner=e)

        self._cdata.append(s_key)

        req = cmd.v.v0
        req.key = s_key
        req.nkey = len(k_enc)

        self.process_single_command(req, koptions)
Exemple #8
0
    def _build_single(self, kv, kviter, ix):
        try:
            cur = kviter.next()
        except AttributeError:
            cur = kviter.__next__()

        if isinstance(kv, dict):
            key, koptions = cur
            if not isinstance(koptions, Options):
                if isinstance(koptions, OperationResult):
                    koptions = Options(cas=koptions.cas)
                else:
                    koptions = self.convert_to_koptions(koptions)

        else:
            key = cur
            koptions = None

            if isinstance(key, Result):
                if isinstance(key, OperationResult):
                    koptions = Options(cas=key.cas)
                key = key.key

            if koptions is None:
                koptions = Options()

        cmd = self._cmdlist[ix]
        self._cmdpp[ix] = ffi.addressof(cmd)

        try:
            k_enc = self._conn._tc.encode_key(key)
            if not k_enc:
                if not key:
                    excls = ArgumentError
                else:
                    excls = ValueFormatError

                raise excls.pyexc("Key is empty", obj=key)

            s_key = ffi.new('char[]', k_enc)

        except CouchbaseError:
            raise
        except Exception as e:
            raise ValueFormatError.pyexc(obj=key, inner=e)

        self._cdata.append(s_key)

        req = cmd.v.v0
        req.key = s_key
        req.nkey = len(k_enc)

        self.process_single_command(req, koptions)
    def add_single(self, pres, key, conn):
        pres.key = key
        try:
            if key in self.res:
                self._warn_dup_key(key, conn)

            self.res[key] = pres

        except TypeError as e:
            self.throw(ValueFormatError.pyexc("Unhashable key", obj=key))
            return

        self.update_single(pres, conn)
    def _process_value(self, op, pyvalue, cspec, bm):
        MULTIVAL_OPS = (
            C.LCB_SDCMD_ARRAY_ADD_FIRST,
            C.LCB_SDCMD_ARRAY_ADD_LAST, C.LCB_SDCMD_ARRAY_INSERT)
        if isinstance(pyvalue, PyCBC.sd_multival_type) and op in MULTIVAL_OPS:
            is_multival = True
        else:
            is_multival = False

        encoded, _ = self.parent._tc.encode_value(pyvalue, FMT_JSON)
        if is_multival:
            encoded = encoded.strip()[1:-1]

        if not len(encoded):
            raise ValueFormatError.pyexc('Empty value passed', obj=pyvalue)

        c_value, c_len = bm.new_cbuf(encoded)
        # print(cspec, c_value, c_len)
        C.CBFFI_set_sdval(cspec, c_value, c_len)
def create_key(tc, pykey):
    """
    Creates a C key from a Python key
    :param tc: The transcoder to use
    :param pykey: The python key to encode
    :return: A tuple of `encoded_key, encoded_length`
    """
    try:
        k_enc = tc.encode_key(pykey)
        if not k_enc:
            if not pykey:
                excls = ArgumentError
            else:
                excls = ValueFormatError
            raise excls.pyexc("Key is empty", obj=pykey)

    except (ValueFormatError, ArgumentError):
        raise
    except Exception as e:
        raise ValueFormatError.pyexc(inner=e)

    s_key = ffi.new('char[]', k_enc)
    return s_key, len(k_enc)
Exemple #12
0
def create_key(tc, pykey):
    """
    Creates a C key from a Python key
    :param tc: The transcoder to use
    :param pykey: The python key to encode
    :return: A tuple of `encoded_key, encoded_length`
    """
    try:
        k_enc = tc.encode_key(pykey)
        if not k_enc:
            if not pykey:
                excls = ArgumentError
            else:
                excls = ValueFormatError
            raise excls.pyexc("Key is empty", obj=pykey)

    except (ValueFormatError, ArgumentError):
        raise
    except Exception as e:
        raise ValueFormatError.pyexc(inner=e)

    s_key = ffi.new('char[]', k_enc)
    return s_key, len(k_enc)