Class: Aerospike::CDT::Context

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

Overview

Nested CDT context. Identifies the location of nested list/map to apply the operation. for the current level. An array of CTX identifies location of the list/map on multiple levels on nesting.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, value) ⇒ Context

Returns a new instance of Context.



33
34
35
36
# File 'lib/aerospike/cdt/context.rb', line 33

def initialize(id, value)
  @id = id
  @value = value
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



31
32
33
# File 'lib/aerospike/cdt/context.rb', line 31

def id
  @id
end

#valueObject

Returns the value of attribute value.



31
32
33
# File 'lib/aerospike/cdt/context.rb', line 31

def value
  @value
end

Class Method Details

.base64(ctx) ⇒ Object

Encodes the context array to messagepack and then encodes the resulting byte array to base64.



163
164
165
166
167
168
169
# File 'lib/aerospike/cdt/context.rb', line 163

def self.base64(ctx)
  unless ctx.to_a.empty?
    data = self.bytes(ctx)
    return Base64.strict_encode64(data).force_encoding("binary")
  end
  ""
end

.bytes(ctx) ⇒ Object

Encodes the context via message pack and return the results.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/aerospike/cdt/context.rb', line 126

def self.bytes(ctx)
  unless ctx.to_a.empty?
    Packer.use do |packer|
      packer.write_array_header(ctx.length * 2)
      ctx.each do |c|
        packer.write(c.id)
        Value.of(c.value).pack(packer)
      end
      return packer.bytes
    end
  end
  nil
end

.from_base64(buf) ⇒ Object

Decodes the byte array to messagepack and then decodes the resulting byte array to an array of Context.



174
175
176
177
# File 'lib/aerospike/cdt/context.rb', line 174

def self.from_base64(buf)
  bytes = Base64.strict_decode64(buf)
  self.from_bytes(bytes)
end

.from_bytes(buf) ⇒ Object

decodes the base64 encoded messagepack byte array and converts it to an array of Context.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/aerospike/cdt/context.rb', line 147

def self.from_bytes(buf)
  list = nil
  Unpacker.use do |unpacker|
    list = unpacker.unpack(buf)
  end

  unless list.length % 2 == 0
    raise Exceptions::Aerospike.new(Aerospike::ResultCode::PARAMETER_ERROR, "Invalid buffer")
  end

  list.each_slice(2).map { |id, value| Context.new(id, value) }
end

.list_index(index) ⇒ Object

Lookup list by index offset. If the index is negative, the resolved index starts backwards from end of list. If an index is out of bounds, a parameter error will be returned. Examples: 0: First item. 4: Fifth item. -1: Last item. -3: Third to last item.



53
54
55
# File 'lib/aerospike/cdt/context.rb', line 53

def self.list_index(index)
  Context.new(0x10, index)
end

.list_index_create(index, order, pad) ⇒ Object

Create list with given type at index offset, given an order and pad.



40
41
42
# File 'lib/aerospike/cdt/context.rb', line 40

def self.list_index_create(index, order, pad)
  Context.new(0x10 | ListOrder.flag(order, pad), index)
end

.list_rank(rank) ⇒ Object

Lookup list by rank. 0 = smallest value N = Nth smallest value -1 = largest value



62
63
64
# File 'lib/aerospike/cdt/context.rb', line 62

def self.list_rank(rank)
  Context.new(0x11, rank)
end

.list_value(key) ⇒ Object

Lookup list by value.



68
69
70
# File 'lib/aerospike/cdt/context.rb', line 68

def self.list_value(key)
  Context.new(0x13, key)
end

.map_index(index) ⇒ Object

Lookup map by index offset. If the index is negative, the resolved index starts backwards from end of list. If an index is out of bounds, a parameter error will be returned. Examples: 0: First item. 4: Fifth item. -1: Last item. -3: Third to last item.



81
82
83
# File 'lib/aerospike/cdt/context.rb', line 81

def self.map_index(index)
  Context.new(0x20, index)
end

.map_key(key) ⇒ Object

Lookup map by key.



96
97
98
# File 'lib/aerospike/cdt/context.rb', line 96

def self.map_key(key)
  Context.new(0x22, key)
end

.map_key_create(key, order) ⇒ Object

Create map with given type at map key.



102
103
104
# File 'lib/aerospike/cdt/context.rb', line 102

def self.map_key_create(key, order)
  Context.new(0x22 | order[:flag], key)
end

.map_rank(rank) ⇒ Object

Lookup map by rank. 0 = smallest value N = Nth smallest value -1 = largest value



90
91
92
# File 'lib/aerospike/cdt/context.rb', line 90

def self.map_rank(rank)
  Context.new(0x21, rank)
end

.map_value(key) ⇒ Object

Lookup map by value.



108
109
110
# File 'lib/aerospike/cdt/context.rb', line 108

def self.map_value(key)
  Context.new(0x23, key)
end

.pack(packer, ctx) ⇒ Object

Encodes the context via message pack.



114
115
116
117
118
119
120
121
122
# File 'lib/aerospike/cdt/context.rb', line 114

def self.pack(packer, ctx)
  unless ctx.to_a.empty?
    packer.write_array_header(2)
    ctx.each do |c|
      packer.write(c.id)
      Value.of(c.value)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  self.id == other.id && self.value == other.value
end