class Aerospike::CDT::BitOperation

Bit operations. Create bit operations used by client operate command. Offset orientation is left-to-right. Negative offsets are supported. If the offset is negative, the offset starts backwards from end of the bitmap. If an offset is out of bounds, a parameter error will be returned.

Nested CDT operations are supported by optional context arguments. Example: bin = [[0b00000001, 0b01000010],] Resize first bitmap (in a list of bitmaps) to 3 bytes. BitOperation.resize(“bin”, 3, BitResizeFlags::DEFAULT, ctx: [Context.list_index(0)]) bin result = [[0b00000001, 0b01000010, 0b00000000],]

Constants

ADD
AND
COUNT
GET
GET_INT
INSERT
INT_FLAGS_SIGNED
LSCAN
LSHIFT
NOT
OR
REMOVE
RESIZE
RSCAN
RSHIFT
SET
SET_INT
SUBTRACT
XOR

Attributes

arguments[R]
bit_op[R]
ctx[R]
policy[R]

Public Class Methods

add( bin_name, bit_offset, bit_size, value, signed, action, ctx: nil, policy: BitPolicy::DEFAULT ) click to toggle source

BitAddOp creates bit “add” operation. Server adds value to byte[] bin starting at bit_offset for bit_size. Bit_size must be <= 64. Signed indicates if bits should be treated as a signed number. If add overflows/underflows, {BitOverflowAction} is used. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 24 bit_size = 16 value = 128 signed = false bin result = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b10000101]

# File lib/aerospike/cdt/bit_operation.rb, line 206
def self.add(
  bin_name,
  bit_offset,
  bit_size,
  value,
  signed,
  action,
  ctx: nil,
  policy: BitPolicy::DEFAULT
)
  actionFlags = action
  actionFlags |= INT_FLAGS_SIGNED if signed

  BitOperation.new(Operation::BIT_MODIFY, ADD, bin_name, bit_offset, bit_size, value, policy.flags, actionFlags, ctx: ctx, policy: policy)
end
and(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitAndOp creates bit “and” operation. Server performs bitwise “and” on value and byte[] bin at bit_offset for bit_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 23 bit_size = 9 value = [0b00111100, 0b10000000] bin result = [0b00000001, 0b01000010, 0b00000010, 0b00000000, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 152
def self.and(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, AND, bin_name, bit_offset, bit_size, value_to_bytes(value), policy.flags, ctx: ctx, policy: policy)
end
count(bin_name, bit_offset, bit_size, ctx: nil) click to toggle source

BitCountOp creates bit “count” operation. Server returns integer count of set bits from byte[] bin starting at bit_offset for bit_size. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 20 bit_size = 4 returns 2

# File lib/aerospike/cdt/bit_operation.rb, line 281
def self.count(bin_name, bit_offset, bit_size, ctx: nil)
  BitOperation.new(Operation::BIT_READ, COUNT, bin_name, bit_offset, bit_size, ctx: ctx)
end
get(bin_name, bit_offset, bit_size, ctx: nil) click to toggle source

BitGetOp creates bit “get” operation. Server returns bits from byte[] bin starting at bit_offset for bit_size. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 9 bit_size = 5 returns [0b1000000]

# File lib/aerospike/cdt/bit_operation.rb, line 270
def self.get(bin_name, bit_offset, bit_size, ctx: nil)
  BitOperation.new(Operation::BIT_READ, GET, bin_name, bit_offset, bit_size, ctx: ctx)
end
get_int(bin_name, bit_offset, bit_size, signed, ctx: nil) click to toggle source

BitGetIntOp creates bit “get integer” operation. Server returns integer from byte[] bin starting at bit_offset for bit_size. Signed indicates if bits should be treated as a signed number. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 8 bit_size = 16 signed = false returns 16899

# File lib/aerospike/cdt/bit_operation.rb, line 320
def self.get_int(bin_name, bit_offset, bit_size, signed, ctx: nil)
  if signed
    BitOperation.new(Operation::BIT_READ, GET_INT, bin_name, bit_offset, bit_size, INT_FLAGS_SIGNED, ctx: ctx)
  else
    BitOperation.new(Operation::BIT_READ, GET_INT, bin_name, bit_offset, bit_size, ctx: ctx)
  end
end
insert(bin_name, byte_offset, value, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitInsertOp creates byte “insert” operation. Server inserts value bytes into byte[] bin at byte_offset. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] byte_offset = 1 value = [0b11111111, 0b11000111] bin result = [0b00000001, 0b11111111, 0b11000111, 0b01000010, 0b00000011, 0b00000100, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 88
def self.insert(bin_name, byte_offset, value, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, INSERT, bin_name, byte_offset, value_to_bytes(value), policy.flags, ctx: ctx, policy: policy)
end
lscan(bin_name, bit_offset, bit_size, value, ctx: nil) click to toggle source

BitLScanOp creates bit “left scan” operation. Server returns integer bit offset of the first specified value bit in byte[] bin starting at bit_offset for bit_size. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 24 bit_size = 8 value = true returns 5

# File lib/aerospike/cdt/bit_operation.rb, line 294
def self.lscan(bin_name, bit_offset, bit_size, value, ctx: nil)
  BitOperation.new(Operation::BIT_READ, LSCAN, bin_name, bit_offset, bit_size, value && true, ctx: ctx)
end
lshift(bin_name, bit_offset, bit_size, shift, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitLShiftOp creates bit “left shift” operation. Server shifts left byte[] bin starting at bit_offset for bit_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 32 bit_size = 8 shift = 3 bin result = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00101000]

# File lib/aerospike/cdt/bit_operation.rb, line 177
def self.lshift(bin_name, bit_offset, bit_size, shift, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, LSHIFT, bin_name, bit_offset, bit_size, shift, policy.flags, ctx: ctx, policy: policy)
end
new(op_type, bit_op, bin_name, *arguments, ctx: nil, policy: nil) click to toggle source
# File lib/aerospike/cdt/bit_operation.rb, line 59
def initialize(op_type, bit_op, bin_name, *arguments, ctx: nil, policy: nil)
  @op_type = op_type
  @bin_name = bin_name
  @bin_value = nil
  @bit_op = bit_op
  @ctx = ctx
  @arguments = arguments
end
not(bin_name, bit_offset, bit_size, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitNotOp creates bit “not” operation. Server negates byte[] bin starting at bit_offset for bit_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 25 bit_size = 6 bin result = [0b00000001, 0b01000010, 0b00000011, 0b01111010, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 164
def self.not(bin_name, bit_offset, bit_size, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, NOT, bin_name, bit_offset, bit_size, policy.flags, ctx: ctx, policy: policy)
end
or(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitOrOp creates bit “or” operation. Server performs bitwise “or” on value and byte[] bin at bit_offset for bit_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 17 bit_size = 6 value = [0b10101000] bin result = [0b00000001, 0b01000010, 0b01010111, 0b00000100, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 126
def self.or(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, OR, bin_name, bit_offset, bit_size, value_to_bytes(value), policy.flags, ctx: ctx, policy: policy)
end
remove(bin_name, byte_offset, byte_size, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitRemoveOp creates byte “remove” operation. Server removes bytes from byte[] bin at byte_offset for byte_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] byte_offset = 2 byte_size = 3 bin result = [0b00000001, 0b01000010]

# File lib/aerospike/cdt/bit_operation.rb, line 100
def self.remove(bin_name, byte_offset, byte_size, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, REMOVE, bin_name, byte_offset, byte_size, policy.flags, ctx: ctx, policy: policy)
end
resize(bin_name, byte_size, resize_flags, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitResizeOp creates byte “resize” operation. Server resizes byte[] to byte_size according to resize_flags (See {BitResizeFlags}). Server does not return a value. Example: bin = [0b00000001, 0b01000010] byte_size = 4 resize_flags = 0 bin result = [0b00000001, 0b01000010, 0b00000000, 0b00000000]

# File lib/aerospike/cdt/bit_operation.rb, line 76
def self.resize(bin_name, byte_size, resize_flags, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, RESIZE, bin_name, byte_size, policy.flags, resize_flags, ctx: ctx, policy: policy)
end
rscan(bin_name, bit_offset, bit_size, value, ctx: nil) click to toggle source

BitRScanOp creates bit “right scan” operation. Server returns integer bit offset of the last specified value bit in byte[] bin starting at bit_offset for bit_size. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 32 bit_size = 8 value = true returns 7

# File lib/aerospike/cdt/bit_operation.rb, line 307
def self.rscan(bin_name, bit_offset, bit_size, value, ctx: nil)
  BitOperation.new(Operation::BIT_READ, RSCAN, bin_name, bit_offset, bit_size, value && true, ctx: ctx)
end
rshift(bin_name, bit_offset, bit_size, shift, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitRShiftOp creates bit “right shift” operation. Server shifts right byte[] bin starting at bit_offset for bit_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 0 bit_size = 9 shift = 1 bin result = [0b00000000, 0b11000010, 0b00000011, 0b00000100, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 190
def self.rshift(bin_name, bit_offset, bit_size, shift, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, RSHIFT, bin_name, bit_offset, bit_size, shift, policy.flags, ctx: ctx, policy: policy)
end
set(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitSetOp creates bit “set” operation. Server sets value on byte[] bin at bit_offset for bit_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 13 bit_size = 3 value = [0b11100000] bin result = [0b00000001, 0b01000111, 0b00000011, 0b00000100, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 113
def self.set(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, SET, bin_name, bit_offset, bit_size, value_to_bytes(value), policy.flags, ctx: ctx, policy: policy)
end
set_int(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitSetIntOp creates bit “setInt” operation. Server sets value to byte[] bin starting at bit_offset for bit_size. Size must be <= 64. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 1 bit_size = 8 value = 127 bin result = [0b00111111, 0b11000010, 0b00000011, 0b0000100, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 259
def self.set_int(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, SET_INT, bin_name, bit_offset, bit_size, value, policy.flags, ctx: ctx, policy: policy)
end
subtract( bin_name, bit_offset, bit_size, value, signed, action, ctx: nil, policy: BitPolicy::DEFAULT ) click to toggle source

BitSubtractOp creates bit “subtract” operation. Server subtracts value from byte[] bin starting at bit_offset for bit_size. Bit_size must be <= 64. Signed indicates if bits should be treated as a signed number. If add overflows/underflows, {BitOverflowAction} is used. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 24 bit_size = 16 value = 128 signed = false bin result = [0b00000001, 0b01000010, 0b00000011, 0b0000011, 0b10000101]

# File lib/aerospike/cdt/bit_operation.rb, line 234
def self.subtract(
  bin_name,
  bit_offset,
  bit_size,
  value,
  signed,
  action,
  ctx: nil,
  policy: BitPolicy::DEFAULT
)
  actionFlags = action
  actionFlags |= INT_FLAGS_SIGNED if signed

  BitOperation.new(Operation::BIT_MODIFY, SUBTRACT, bin_name, bit_offset, bit_size, value, policy.flags, actionFlags, ctx: ctx, policy: policy)
end
value_to_bytes(value) click to toggle source
# File lib/aerospike/cdt/bit_operation.rb, line 334
def self.value_to_bytes(value)
  case value
  when Integer
    [value].pack('C*')
  when Array
    value.pack('C*')
  when String
    BytesValue.new(value)
  when StringValue
    BytesValue.new(value.get)
  else
    value
  end
end
xor(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT) click to toggle source

BitXorOp creates bit “exclusive or” operation. Server performs bitwise “xor” on value and byte[] bin at bit_offset for bit_size. Server does not return a value. Example: bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101] bit_offset = 17 bit_size = 6 value = [0b10101100] bin result = [0b00000001, 0b01000010, 0b01010101, 0b00000100, 0b00000101]

# File lib/aerospike/cdt/bit_operation.rb, line 139
def self.xor(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT)
  BitOperation.new(Operation::BIT_MODIFY, XOR, bin_name, bit_offset, bit_size, value_to_bytes(value), policy.flags, ctx: ctx, policy: policy)
end

Public Instance Methods

bin_value() click to toggle source
# File lib/aerospike/cdt/bit_operation.rb, line 328
def bin_value
  @bin_value ||= pack_bin_value
end

Private Instance Methods

pack_bin_value() click to toggle source
# File lib/aerospike/cdt/bit_operation.rb, line 349
def pack_bin_value
  bytes = nil
  args = arguments.dup
  Packer.use do |packer|
    if !@ctx.nil? && @ctx.length > 0
      packer.write_array_header(3)
      Value.of(0xff).pack(packer)

      packer.write_array_header(@ctx.length*2)
      @ctx.each do |ctx|
        Value.of(ctx.id).pack(packer)
        Value.of(ctx.value, true).pack(packer)
      end
    end

    packer.write_array_header(args.length+1)
    Value.of(@bit_op, true).pack(packer)
    args.each do |value|
      Value.of(value, true).pack(packer)
    end
    bytes = packer.bytes
  end
  BytesValue.new(bytes)
end