Class: Aerospike::CDT::BitOperation

Inherits:
Operation
  • Object
show all
Defined in:
lib/aerospike/cdt/bit_operation.rb

Overview

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],]

Constant Summary collapse

RESIZE =
0
INSERT =
1
REMOVE =
2
SET =
3
OR =
4
XOR =
5
AND =
6
NOT =
7
LSHIFT =
8
RSHIFT =
9
ADD =
10
SUBTRACT =
11
SET_INT =
12
GET =
50
COUNT =
51
LSCAN =
52
RSCAN =
53
GET_INT =
54
INT_FLAGS_SIGNED =
1

Constants inherited from Operation

Operation::APPEND, Operation::BIT_MODIFY, Operation::BIT_READ, Operation::CDT_MODIFY, Operation::CDT_READ, Operation::DELETE, Operation::EXP_MODIFY, Operation::EXP_READ, Operation::HLL_MODIFY, Operation::HLL_READ, Operation::PREPEND, Operation::READ, Operation::READ_HEADER, Operation::TOUCH, Operation::WRITE

Instance Attribute Summary collapse

Attributes inherited from Operation

#bin_name, #op_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Operation

append, #bin, delete, get_header, #is_write?, prepend, put, touch

Constructor Details

#initialize(op_type, bit_op, bin_name, *arguments, ctx: nil, policy: nil) ⇒ BitOperation

Returns a new instance of BitOperation.



59
60
61
62
63
64
65
66
# 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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



57
58
59
# File 'lib/aerospike/cdt/bit_operation.rb', line 57

def arguments
  @arguments
end

#bit_opObject (readonly)

Returns the value of attribute bit_op.



57
58
59
# File 'lib/aerospike/cdt/bit_operation.rb', line 57

def bit_op
  @bit_op
end

#ctxObject (readonly)

Returns the value of attribute ctx.



57
58
59
# File 'lib/aerospike/cdt/bit_operation.rb', line 57

def ctx
  @ctx
end

#policyObject (readonly)

Returns the value of attribute policy.



57
58
59
# File 'lib/aerospike/cdt/bit_operation.rb', line 57

def policy
  @policy
end

Class Method Details

.add(bin_name, bit_offset, bit_size, value, signed, action, ctx: nil, policy: BitPolicy::DEFAULT) ⇒ Object

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, Aerospike::CDT::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]



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# 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) ⇒ Object

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]



152
153
154
# 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) ⇒ Object

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



281
282
283
# 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) ⇒ Object

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]



270
271
272
# 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) ⇒ Object

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



320
321
322
323
324
325
326
# 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) ⇒ Object

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]



88
89
90
# 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) ⇒ Object

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



294
295
296
# 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) ⇒ Object

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]



177
178
179
# 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

.not(bin_name, bit_offset, bit_size, ctx: nil, policy: BitPolicy::DEFAULT) ⇒ Object

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]



164
165
166
# 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) ⇒ Object

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]



126
127
128
# 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) ⇒ Object

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]



100
101
102
# 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) ⇒ Object

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



76
77
78
# 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) ⇒ Object

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



307
308
309
# 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) ⇒ Object

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]



190
191
192
# 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) ⇒ Object

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]



113
114
115
# 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) ⇒ Object

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]



259
260
261
# 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) ⇒ Object

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, Aerospike::CDT::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]



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# 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

.xor(bin_name, bit_offset, bit_size, value, ctx: nil, policy: BitPolicy::DEFAULT) ⇒ Object

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]



139
140
141
# 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

Instance Method Details

#bin_valueObject



328
329
330
# File 'lib/aerospike/cdt/bit_operation.rb', line 328

def bin_value
  @bin_value ||= pack_bin_value
end