Class: Cassandra::DynamicComposite

Inherits:
Composite
  • Object
show all
Defined in:
lib/cassandra/dynamic_composite.rb

Instance Attribute Summary collapse

Attributes inherited from Composite

#column_slice, #parts

Instance Method Summary collapse

Methods inherited from Composite

#<=>, #[], #inspect, #make_from_parts, new_from_packed, new_from_parts, #slice_end_of_component, #to_s

Constructor Details

#initialize(*parts) ⇒ DynamicComposite

Returns a new instance of DynamicComposite.

Raises:

  • (ArgumentError)

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cassandra/dynamic_composite.rb', line 5

def initialize(*parts)
  return if parts.empty?

  options = {}
  if parts.last.is_a?(Hash)
    options = parts.pop
  end
  @column_slice = options[:slice]
  raise ArgumentError if @column_slice != nil && ![:before, :after].include?(@column_slice)

  if parts.length == 1 && parts[0].instance_of?(self.class)
    @column_slice = parts[0].column_slice
    @parts = parts[0].parts
    @types = parts[0].types
  elsif parts.length == 1 && parts[0].instance_of?(String) && @column_slice.nil? && try_packed_composite(parts[0])
    @hash = parts[0].hash
  else
    @types, @parts = parts.transpose
  end
end

Instance Attribute Details

#typesObject

Returns the value of attribute types


3
4
5
# File 'lib/cassandra/dynamic_composite.rb', line 3

def types
  @types
end

Instance Method Details

#fast_unpack(packed_string) ⇒ Object


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
# File 'lib/cassandra/dynamic_composite.rb', line 47

def fast_unpack(packed_string)
  @hash = packed_string.hash

  @types = []
  @parts = []

  offset = 0
  length = nil
  while offset < packed_string.length
    if packed_string[offset].ord & 0x80 != 0
      @types << packed_string[offset+1]
      offset += 2
    else
      length = packed_string.slice(offset, 2).unpack('n')[0]
      offset += 2
      @types << packed_string.slice(offset, length)
      offset += length
    end
    length = packed_string.slice(offset, 2).unpack('n')[0]
    offset += 2
    @parts << packed_string.slice(offset, length)
    offset += length + 1
  end

  @column_slice = :after if packed_string[-1] == "\x01"
  @column_slice = :before if packed_string[-1] == "\xFF"
end

#packObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cassandra/dynamic_composite.rb', line 26

def pack
  packed_parts = @parts.map do |part|
    [part.length].pack('n') + part + "\x00"
  end

  if @column_slice
    part = @parts[-1]
    packed_parts[-1] = [part.length].pack('n') + part + slice_end_of_component
  end

  packed_types = @types.map do |type|
    if type.length == 1
      [0x8000 | type[0].ord].pack('n')
    else
      [type.length].pack('n') + type
    end
  end

  return packed_types.zip(packed_parts).flatten.join('')
end