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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/aerospike/cdt/bit_operation.rb', line 207

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]



153
154
155
# File 'lib/aerospike/cdt/bit_operation.rb', line 153

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



282
283
284
# File 'lib/aerospike/cdt/bit_operation.rb', line 282

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]



271
272
273
# File 'lib/aerospike/cdt/bit_operation.rb', line 271

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



321
322
323
324
325
326
327
# File 'lib/aerospike/cdt/bit_operation.rb', line 321

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]



89
90
91
# File 'lib/aerospike/cdt/bit_operation.rb', line 89

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



295
296
297
# File 'lib/aerospike/cdt/bit_operation.rb', line 295

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]



178
179
180
# File 'lib/aerospike/cdt/bit_operation.rb', line 178

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]



165
166
167
# File 'lib/aerospike/cdt/bit_operation.rb', line 165

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]



127
128
129
# File 'lib/aerospike/cdt/bit_operation.rb', line 127

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]



101
102
103
# File 'lib/aerospike/cdt/bit_operation.rb', line 101

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 BitResizeFlags). Server does not return a value. Example: bin = [0b00000001, 0b01000010] byte_size = 4 resize_flags = 0 bin result = [0b00000001, 0b01000010, 0b00000000, 0b00000000]



77
78
79
# File 'lib/aerospike/cdt/bit_operation.rb', line 77

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



308
309
310
# File 'lib/aerospike/cdt/bit_operation.rb', line 308

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]



191
192
193
# File 'lib/aerospike/cdt/bit_operation.rb', line 191

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]



114
115
116
# File 'lib/aerospike/cdt/bit_operation.rb', line 114

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]



260
261
262
# File 'lib/aerospike/cdt/bit_operation.rb', line 260

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/aerospike/cdt/bit_operation.rb', line 235

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]



140
141
142
# File 'lib/aerospike/cdt/bit_operation.rb', line 140

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



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

def bin_value
  @bin_value ||= pack_bin_value
end