Class: Cassandra::Composite
- Inherits:
-
Object
- Object
- Cassandra::Composite
- Includes:
- Comparable
- Defined in:
- lib/cassandra/composite.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#column_slice ⇒ Object
readonly
Returns the value of attribute column_slice.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #[](*args) ⇒ Object
- #fast_unpack(packed_string) ⇒ Object
-
#initialize(*parts) ⇒ Composite
constructor
A new instance of Composite.
- #inspect ⇒ Object
- #pack ⇒ Object
- #slice_end_of_component ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*parts) ⇒ Composite
Returns a new instance of Composite.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/cassandra/composite.rb', line 7 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 elsif parts.length == 1 && parts[0].instance_of?(String) && @column_slice.nil? && try_packed_composite(parts[0]) @hash = parts[0].hash else @parts = parts end end |
Instance Attribute Details
#column_slice ⇒ Object (readonly)
Returns the value of attribute column_slice.
5 6 7 |
# File 'lib/cassandra/composite.rb', line 5 def column_slice @column_slice end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
4 5 6 |
# File 'lib/cassandra/composite.rb', line 4 def parts @parts end |
Class Method Details
.new_from_packed(packed) ⇒ Object
27 28 29 30 31 |
# File 'lib/cassandra/composite.rb', line 27 def self.new_from_packed(packed) obj = new obj.fast_unpack(packed) return obj end |
Instance Method Details
#<=>(other) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cassandra/composite.rb', line 52 def <=>(other) if !other.instance_of?(self.class) return @parts.first <=> other end eoc = slice_end_of_component.unpack('c')[0] other_eoc = other.slice_end_of_component.unpack('c')[0] @parts.zip(other.parts).each do |a, b| next if a == b if a.nil? && b.nil? return eoc <=> other_eoc end if a.nil? return @column_slice == :after ? 1 : -1 end if b.nil? return other.column_slice == :after ? -1 : 1 end return -1 if a < b return 1 if a > b end return 0 end |
#[](*args) ⇒ Object
33 34 35 |
# File 'lib/cassandra/composite.rb', line 33 def [](*args) return @parts[*args] end |
#fast_unpack(packed_string) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cassandra/composite.rb', line 89 def fast_unpack(packed_string) @hash = packed_string.hash @parts = [] end_of_component = packed_string.slice(packed_string.length-1, 1) while packed_string.length > 0 length = packed_string.unpack('n')[0] @parts << packed_string.slice(2, length) packed_string.slice!(0, length+3) end @column_slice = :after if end_of_component == "\x01" @column_slice = :before if end_of_component == "\xFF" end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/cassandra/composite.rb', line 76 def inspect return "#<#{self.class}:#{@column_slice} #{@parts.inspect}>" end |
#pack ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cassandra/composite.rb', line 37 def pack packed = @parts.map do |part| [part.length].pack('n') + part + "\x00" end if @column_slice part = @parts[-1] packed[-1] = [part.length].pack('n') + part + slice_end_of_component end return packed.join('') end |
#slice_end_of_component ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/cassandra/composite.rb', line 80 def slice_end_of_component ret = "\x00" ret = "\x01" if @column_slice == :after ret = "\xFF" if @column_slice == :before ret.force_encoding('BINARY') if ret.respond_to?(:force_encoding) return ret end |
#to_s ⇒ Object
48 49 50 |
# File 'lib/cassandra/composite.rb', line 48 def to_s return pack end |