Exemplo n.º 1
0
 def annotate_exception(self, exception, number, command):
     """
     """
     cmd = unicode(' ').join(imap(unicode, command))
     msg = unicode('Command # {0} ({1}) of pipeline caused error: {2}').format(
         number, cmd, unicode(exception.args[0]))
     exception.args = (msg,) + exception.args[1:]
Exemplo n.º 2
0
    def pack_command(self, *args):
        "Pack a series of arguments into the Redis protocol"
        output = []
        # the client might have included 1 or more literal arguments in
        # the command name, e.g., 'CONFIG GET'. The Redis server expects these
        # arguments to be sent separately, so split the first argument
        # manually. All of these arguements get wrapped in the Token class
        # to prevent them from being encoded.
        command = args[0]
        if ' ' in command:
            args = tuple(Token.get_token(s)
                         for s in command.split()) + args[1:]
        else:
            args = (Token.get_token(command), ) + args[1:]

        buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))

        buffer_cutoff = self._buffer_cutoff
        for arg in imap(self.encoder.encode, args):
            # to avoid large string mallocs, chunk the command into the
            # output list if we're sending large values
            if len(buff) > buffer_cutoff or len(arg) > buffer_cutoff:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, str(len(arg)).encode(), SYM_CRLF))
                output.append(buff)
                output.append(arg)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, str(len(arg)).encode(), SYM_CRLF, arg,
                     SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 3
0
    def pack_command(self, *args):
        "Pack a series of arguments into the Redis protocol"
        output = []
        # the client might have included 1 or more literal arguments in
        # the command name, e.g., 'CONFIG GET'. The Redis server expects these
        # arguments to be sent separately, so split the first argument
        # manually. These arguments should be bytestrings so that they are
        # not encoded.
        if isinstance(args[0], unicode):
            args = tuple(args[0].encode().split()) + args[1:]
        elif b' ' in args[0]:
            args = tuple(args[0].split()) + args[1:]

        buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))

        buffer_cutoff = self._buffer_cutoff
        for arg in imap(self.encoder.encode, args):
            # to avoid large string mallocs, chunk the command into the
            # output list if we're sending large values
            if len(buff) > buffer_cutoff or len(arg) > buffer_cutoff:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, str(len(arg)).encode(), SYM_CRLF))
                output.append(buff)
                output.append(arg)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, str(len(arg)).encode(),
                     SYM_CRLF, arg, SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 4
0
    def tsrem(self, name, *times, **options):
        """
        Removes one or more specified ``times`` from the time series
        that is specified by the given key ``name``.

        ``times`` can be of different types. They are converted to
        timestamps - 64 bit signed integers - using a callable
        ``time_cast_func`` within options.

        ``times`` should be python datetimes if ``time_cast_func``
        isn't set. A naive datetime is accounted an UTC datetime.

        ``times`` could be the time in seconds since the epoch (unix time)
        as a floating point number in case ``time_cast_func`` is
        set to ``unixtime_to_timestamp`` for instance.
        Note: The unix time is naive, that is, it doesn't know its timezone.
        ``times`` MUST be in UTC!

        To provide raw timestamps set ``time_cast_func`` to None.
        """
        pieces = ['TSREM', name]
        if times:
            time_cast_func = options.get('time_cast_func',
                                         DatetimeToTimestamp(pytz.utc))
            if time_cast_func is None:
                pieces.extend(times)
            else:
                pieces.extend(imap(time_cast_func, times))
        return self.execute_command(*pieces)
Exemplo n.º 5
0
 def pack_command(self, *args):
     args_output = SYM_EMPTY.join([
         SYM_EMPTY.join((SYM_DOLLAR, b(str(len(k))), SYM_CRLF, k, SYM_CRLF))
         for k in imap(self.encode, args)])
     output = SYM_EMPTY.join(
         (SYM_STAR, b(str(len(args))), SYM_CRLF, args_output))
     return output
Exemplo n.º 6
0
    def pack_command(self, *args):
        "Pack a series of arguments into the Redis protocol"
        output = []
        # the client might have included 1 or more literal arguments in
        # the command name, e.g., 'CONFIG GET'. The Redis server expects these
        # arguments to be sent separately, so split the first argument
        # manually. All of these arguements get wrapped in the Token class
        # to prevent them from being encoded.
        command = args[0]
        if ' ' in command:
            args = tuple([Token(s) for s in command.split(' ')]) + args[1:]
        else:
            args = (Token(command),) + args[1:]

        buff = SYM_EMPTY.join(
            (SYM_STAR, b(str(len(args))), SYM_CRLF))

        for arg in imap(self.encode, args):
            # to avoid large string mallocs, chunk the command into the
            # output list if we're sending large values
            if len(buff) > self._buffer_cutoff or \
               len(arg) > self._buffer_cutoff:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF))
                output.append(buff)
                output.append(arg)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(arg))),
                                       SYM_CRLF, arg, SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 7
0
    def pack_command(self, *args):
        """用于将要执行的命令按照redis的协议进行打包"""
        output = []
        # the client might have included 1 or more literal arguments in
        # the command name, e.g., 'CONFIG GET'. The Redis server expects these
        # arguments to be sent separately, so split the first argument
        # manually. All of these arguements get wrapped in the Token class
        # to prevent them from being encoded.
        command = args[0]
        if ' ' in command:
            args = tuple([Token(s) for s in command.split(' ')]) + args[1:]
        else:
            args = (Token(command), ) + args[1:]

        buff = SYM_EMPTY.join((SYM_STAR, b(str(len(args))), SYM_CRLF))

        for arg in imap(self.encode, args):
            # to avoid large string mallocs, chunk the command into the
            # output list if we're sending large values
            if len(buff) > 6000 or len(arg) > 6000:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF))
                output.append(buff)
                output.append(arg)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(arg))),
                                       SYM_CRLF, arg, SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 8
0
 def pack_command(self, *args):
     "Pack a series of arguments into a value Redis command"
     args_output = SYM_EMPTY.join([
         SYM_EMPTY.join((SYM_DOLLAR, b(str(len(k))), SYM_CRLF, k, SYM_CRLF))
         for k in imap(self.encode, args)])
     output = SYM_EMPTY.join(
         (SYM_STAR, b(str(len(args))), SYM_CRLF, args_output))
     return output
Exemplo n.º 9
0
 def annotate_exception(self, exception, number, command):
     """
     """
     cmd = unicode(' ').join(imap(unicode, command))
     msg = unicode(
         'Command # {0} ({1}) of pipeline caused error: {2}').format(
             number, cmd, unicode(exception.args[0]))
     exception.args = (msg, ) + exception.args[1:]
Exemplo n.º 10
0
 def pack_command(self, *args):
     "Pack a series of arguments into a value Redis command"
     args_output = SYM_EMPTY.join([
         SYM_EMPTY.join((SYM_DOLLAR, b(str(len(k))), SYM_CRLF, k, SYM_CRLF))
         for k in imap(self.encode, args)])
     output = SYM_EMPTY.join(
         (SYM_STAR, b(str(len(args))), SYM_CRLF, args_output))
     return output
Exemplo n.º 11
0
 def pack_command(self, *args):
     "Pack a series of arguments into a value Redis command"
     output = SYM_STAR + b(str(len(args))) + SYM_CRLF
     for enc_value in imap(self.encode, args):
         output += SYM_DOLLAR
         output += b(str(len(enc_value)))
         output += SYM_CRLF
         output += enc_value
         output += SYM_CRLF
     return output
Exemplo n.º 12
0
def timeseries_time_value_pairs(response, **options):
    """
    Return the ``response`` as a list of (time, value) pairs.

    ``options`` may contain a callable ``timestamp_cast_func``
    used to cast the timestamp return values to times.
    Timestamps are milliseconds since the epoch as 64 bit signed integers.
    """
    if not response:
        return response
    timestamp_cast_func = options.get('timestamp_cast_func', int)
    it = iter(response)
    return list(izip(imap(timestamp_cast_func, it), it))
Exemplo n.º 13
0
 def pack_command(self, *args):
     "Pack a series of arguments into a value Redis command"
     output = BytesIO()
     output.write(SYM_STAR)
     output.write(b(str(len(args))))
     output.write(SYM_CRLF)
     for enc_value in imap(self.encode, args):
         output.write(SYM_DOLLAR)
         output.write(b(str(len(enc_value))))
         output.write(SYM_CRLF)
         output.write(enc_value)
         output.write(SYM_CRLF)
     return output.getvalue()
Exemplo n.º 14
0
 def pack_command(self, *args):
     "Pack a series of arguments into a value Redis command"
     output = BytesIO()
     output.write(SYM_STAR)
     output.write(b(str(len(args))))
     output.write(SYM_CRLF)
     for enc_value in imap(self.encode, args):
         output.write(SYM_DOLLAR)
         output.write(b(str(len(enc_value))))
         output.write(SYM_CRLF)
         output.write(enc_value)
         output.write(SYM_CRLF)
     return output.getvalue()
Exemplo n.º 15
0
    def pack_command(self, *args):
        output = []
        buff = SYM_EMPTY.join((SYM_STAR, b(str(len(args))), SYM_CRLF))

        for k in imap(self.encoder.encode, args):
            if len(buff) > 6000 or len(k) > 6000:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, b(str(len(k))), SYM_CRLF))
                output.append(buff)
                output.append(k)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, b(str(len(k))), SYM_CRLF, k, SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 16
0
    def pack_command(self, *args):
        output = []
        buff = SYM_EMPTY.join(
            (SYM_STAR, str(len(args)).encode(), SYM_CRLF))

        for k in imap(self.encoder.encode, args):
            if len(buff) > 6000 or len(k) > 6000:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF))
                output.append(buff)
                output.append(k)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, str(len(k)).encode(),
                                       SYM_CRLF, k, SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 17
0
    def pack_command(self, *args):
        "Pack a series of arguments into a value Redis command"
        output = []
        buff = SYM_EMPTY.join(
            (SYM_STAR, b(str(len(args))), SYM_CRLF))

        for k in imap(self.encode, args):
            if len(buff) > 6000 or len(k) > 6000:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, b(str(len(k))), SYM_CRLF))
                output.append(buff)
                output.append(k)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(k))),
                                       SYM_CRLF, k, SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 18
0
    def tsadd(self, name, *times_and_values, **options):
        """
        Add any number of ``time``, ``value`` pairs to the time series
        that is specified by the given key ``name``.

        Pairs can be specified as ``times_and_values``, in the form of:
        ``time1``, ``value1``, ``time2``, ``value2``,...

        This is useful in storing time series like data where the ``name``
        could define a metric, the ``time`` is the time when the metric was
        generated and ``value`` is the value of the metric at the given time.

        ``times`` can be of different types. They are converted to
        timestamps - 64 bit signed integers - using a callable
        ``time_cast_func`` within options.

        ``times`` should be python datetimes if ``time_cast_func``
        isn't set. A naive datetime is accounted an UTC datetime.

        ``times`` could be the time in seconds since the epoch (unix time)
        as a floating point number in case ``time_cast_func`` is
        set to ``unixtime_to_timestamp`` for instance.
        Note: The unix time is naive, that is, it doesn't know its timezone.
        ``times`` MUST be in UTC!

        To provide raw timestamps set ``time_cast_func`` to None.
        """
        pieces = ['TSADD', name]
        if times_and_values:
            if len(times_and_values) % 2 != 0:
                raise RedisError("TSADD requires an equal number of "
                                 "times and values")
            time_cast_func = options.get('time_cast_func',
                                         DatetimeToTimestamp(pytz.utc))
            if time_cast_func is None:
                pieces.extend(times_and_values)
            else:
                pieces.extend(
                    chain.from_iterable(izip(imap(time_cast_func,
                                                  times_and_values[0::2]),
                                             times_and_values[1::2])))
        return self.execute_command(*pieces)
Exemplo n.º 19
0
 def subscribe(self, *args, **kwargs):
     """
     Subscribe to channels. Channels supplied as keyword arguments expect
     a channel name as the key and a callable as the value. A channel's
     callable will be invoked automatically when a message is received on
     that channel rather than producing a message via ``listen()`` or
     ``get_message()``.
     """
     if args:
         args = list_or_args(args[0], args[1:])
     new_channels = {}
     new_channels.update(dict.fromkeys(imap(self.encode, args)))
     for channel, handler in iteritems(kwargs):
         new_channels[self.encode(channel)] = handler
     ret_val = self.execute_command('SUBSCRIBE', *iterkeys(new_channels))
     # update the channels dict AFTER we send the command. we don't want to
     # subscribe twice to these channels, once for the command and again
     # for the reconnection.
     self.channels.update(new_channels)
     return ret_val
Exemplo n.º 20
0
    def pack_command(self, *args):
        output = []
        command = args[0]
        if ' ' in command:
            args = tuple([Token.get_token(s)
                          for s in command.split()]) + args[1:]
        else:
            args = (Token.get_token(command), ) + args[1:]

        buff = SYM_EMPTY.join((SYM_STAR, b(str(len(args))), SYM_CRLF))

        for arg in imap(self.encoder.encode, args):
            if len(buff) > 6000 or len(arg) > 6000:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF))
                output.append(buff)
                output.append(arg)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(arg))),
                                       SYM_CRLF, arg, SYM_CRLF))
        output.append(buff)
        return output
Exemplo n.º 21
0
 def annotate_exception(self, exception, number, command):
     cmd = unicode(' ').join(imap(unicode, command))
     msg = unicode('Command # %d (%s) of pipeline caused error: %s') % (
         number, cmd, unicode(exception.args[0]))
     exception.args = (msg, ) + exception.args[1:]
Exemplo n.º 22
0
 def annotate_exception(self, exception, number, command):
     cmd = unicode(' ').join(imap(unicode, command))
     msg = unicode('Command # %d (%s) of pipeline caused error: %s') % (
         number, cmd, unicode(exception.args[0]))
     exception.args = (msg,) + exception.args[1:]