1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
# frozen_string_literal: true
class Redis
module Commands
module Bitmaps
# Sets or clears the bit at offset in the string value stored at key.
#
# @param [String] key
# @param [Integer] offset bit offset
# @param [Integer] value bit value `0` or `1`
# @return [Integer] the original bit value stored at `offset`
def setbit(key, offset, value)
send_command([:setbit, key, offset, value])
end
# Returns the bit value at offset in the string value stored at key.
#
# @param [String] key
# @param [Integer] offset bit offset
# @return [Integer] `0` or `1`
def getbit(key, offset)
send_command([:getbit, key, offset])
end
# Count the number of set bits in a range of the string value stored at key.
#
# @param [String] key
# @param [Integer] start start index
# @param [Integer] stop stop index
# @param [String, Symbol] scale the scale of the offset range
# e.g. 'BYTE' - interpreted as a range of bytes, 'BIT' - interpreted as a range of bits
# @return [Integer] the number of bits set to 1
def bitcount(key, start = 0, stop = -1, scale: nil)
command = [:bitcount, key, start, stop]
command << scale if scale
send_command(command)
end
# Perform a bitwise operation between strings and store the resulting string in a key.
#
# @param [String] operation e.g. `and`, `or`, `xor`, `not`
# @param [String] destkey destination key
# @param [String, Array<String>] keys one or more source keys to perform `operation`
# @return [Integer] the length of the string stored in `destkey`
def bitop(operation, destkey, *keys)
keys.flatten!(1)
command = [:bitop, operation, destkey]
command.concat(keys)
send_command(command)
end
# Return the position of the first bit set to 1 or 0 in a string.
#
# @param [String] key
# @param [Integer] bit whether to look for the first 1 or 0 bit
# @param [Integer] start start index
# @param [Integer] stop stop index
# @param [String, Symbol] scale the scale of the offset range
# e.g. 'BYTE' - interpreted as a range of bytes, 'BIT' - interpreted as a range of bits
# @return [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.
def bitpos(key, bit, start = nil, stop = nil, scale: nil)
raise(ArgumentError, 'stop parameter specified without start parameter') if stop && !start
command = [:bitpos, key, bit]
command << start if start
command << stop if stop
command << scale if scale
send_command(command)
end
end
end
end
|