Module: Redis::Commands::Bitmaps

Included in:
Redis::Commands
Defined in:
lib/redis/commands/bitmaps.rb

Instance Method Summary collapse

Instance Method Details

#bitcount(key, start = 0, stop = -1)) ⇒ Integer

Count the number of set bits in a range of the string value stored at key.

Parameters:

  • key (String)
  • start (Integer) (defaults to: 0)

    start index

  • stop (Integer) (defaults to: -1))

    stop index

Returns:

  • (Integer)

    the number of bits set to 1



31
32
33
# File 'lib/redis/commands/bitmaps.rb', line 31

def bitcount(key, start = 0, stop = -1)
  send_command([:bitcount, key, start, stop])
end

#bitop(operation, destkey, *keys) ⇒ Integer

Perform a bitwise operation between strings and store the resulting string in a key.

Parameters:

  • operation (String)

    e.g. ‘and`, `or`, `xor`, `not`

  • destkey (String)

    destination key

  • keys (String, Array<String>)

    one or more source keys to perform ‘operation`

Returns:

  • (Integer)

    the length of the string stored in ‘destkey`



41
42
43
44
45
46
# File 'lib/redis/commands/bitmaps.rb', line 41

def bitop(operation, destkey, *keys)
  keys.flatten!(1)
  command = [:bitop, operation, destkey]
  command.concat(keys)
  send_command(command)
end

#bitpos(key, bit, start = nil, stop = nil) ⇒ Integer

Return the position of the first bit set to 1 or 0 in a string.

Parameters:

  • key (String)
  • bit (Integer)

    whether to look for the first 1 or 0 bit

  • start (Integer) (defaults to: nil)

    start index

  • stop (Integer) (defaults to: nil)

    stop index

Returns:

  • (Integer)

    the position of the first 1/0 bit. -1 if looking for 1 and it is not found or start and stop are given.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
# File 'lib/redis/commands/bitmaps.rb', line 56

def bitpos(key, bit, start = nil, stop = nil)
  raise(ArgumentError, 'stop parameter specified without start parameter') if stop && !start

  command = [:bitpos, key, bit]
  command << start if start
  command << stop if stop
  send_command(command)
end

#getbit(key, offset) ⇒ Integer

Returns the bit value at offset in the string value stored at key.

Parameters:

  • key (String)
  • offset (Integer)

    bit offset

Returns:

  • (Integer)

    ‘0` or `1`



21
22
23
# File 'lib/redis/commands/bitmaps.rb', line 21

def getbit(key, offset)
  send_command([:getbit, key, offset])
end

#setbit(key, offset, value) ⇒ Integer

Sets or clears the bit at offset in the string value stored at key.

Parameters:

  • key (String)
  • offset (Integer)

    bit offset

  • value (Integer)

    bit value ‘0` or `1`

Returns:

  • (Integer)

    the original bit value stored at ‘offset`



12
13
14
# File 'lib/redis/commands/bitmaps.rb', line 12

def setbit(key, offset, value)
  send_command([:setbit, key, offset, value])
end