Class: Cassandra::DynamicComposite
- Defined in:
- lib/cassandra/dynamic_composite.rb
Instance Attribute Summary collapse
-
#types ⇒ Object
Returns the value of attribute types.
Attributes inherited from Composite
Instance Method Summary collapse
- #fast_unpack(packed_string) ⇒ Object
-
#initialize(*parts) ⇒ DynamicComposite
constructor
A new instance of DynamicComposite.
- #pack ⇒ Object
Methods inherited from Composite
#<=>, #[], #inspect, new_from_packed, #slice_end_of_component, #to_s
Constructor Details
#initialize(*parts) ⇒ DynamicComposite
Returns a new instance of DynamicComposite.
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? = {} if parts.last.is_a?(Hash) = parts.pop end @column_slice = [: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
#types ⇒ Object
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 |
#pack ⇒ Object
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 |