class Cassandra::Composite

Attributes

column_slice[R]
parts[R]

Public Class Methods

new(*parts) click to toggle source
   # File lib/cassandra/composite.rb
 7 def initialize(*parts)
 8   return if parts.empty?
 9 
10   options = {}
11   if parts.last.is_a?(Hash)
12     options = parts.pop
13   end
14   @column_slice = options[:slice]
15   raise ArgumentError if @column_slice != nil && ![:before, :after].include?(@column_slice)
16 
17   if parts.length == 1 && parts[0].instance_of?(self.class)
18     @column_slice = parts[0].column_slice
19     @parts = parts[0].parts
20   elsif parts.length == 1 && parts[0].instance_of?(String) && @column_slice.nil? && try_packed_composite(parts[0])
21     @hash = parts[0].hash
22   else
23     @parts = parts
24   end
25 end
new_from_packed(packed) click to toggle source
   # File lib/cassandra/composite.rb
27 def self.new_from_packed(packed)
28   obj = new
29   obj.fast_unpack(packed)
30   return obj
31 end

Public Instance Methods

<=>(other) click to toggle source
   # File lib/cassandra/composite.rb
52 def <=>(other)
53   if !other.instance_of?(self.class)
54     return @parts.first <=> other
55   end
56   eoc = slice_end_of_component.unpack('c')[0]
57   other_eoc = other.slice_end_of_component.unpack('c')[0]
58   @parts.zip(other.parts).each do |a, b|
59     next if a == b
60     if a.nil? && b.nil?
61       return eoc <=> other_eoc
62     end
63 
64     if a.nil?
65       return @column_slice == :after ? 1 : -1
66     end
67     if b.nil?
68       return other.column_slice == :after ? -1 : 1
69     end
70     return -1 if a < b
71     return 1 if a > b
72   end
73   return 0
74 end
[](*args) click to toggle source
   # File lib/cassandra/composite.rb
33 def [](*args)
34   return @parts[*args]
35 end
fast_unpack(packed_string) click to toggle source
    # File lib/cassandra/composite.rb
 89 def fast_unpack(packed_string)
 90   @hash = packed_string.hash
 91 
 92   @parts = []
 93   end_of_component = packed_string.slice(packed_string.length-1, 1)
 94   while packed_string.length > 0
 95     length = packed_string.unpack('n')[0]
 96     @parts << packed_string.slice(2, length)
 97 
 98     packed_string.slice!(0, length+3)
 99   end
100 
101   @column_slice = :after if end_of_component == "\x01"
102   @column_slice = :before if end_of_component == "\xFF"
103 end
inspect() click to toggle source
   # File lib/cassandra/composite.rb
76 def inspect
77   return "#<#{self.class}:#{@column_slice} #{@parts.inspect}>"
78 end
pack() click to toggle source
   # File lib/cassandra/composite.rb
37 def pack
38   packed = @parts.map do |part|
39     [part.length].pack('n') + part + "\x00"
40   end
41   if @column_slice
42     part = @parts[-1]
43     packed[-1] = [part.length].pack('n') + part + slice_end_of_component
44   end
45   return packed.join('')
46 end
slice_end_of_component() click to toggle source
   # File lib/cassandra/composite.rb
80 def slice_end_of_component
81   ret = "\x00"
82   ret = "\x01" if @column_slice == :after
83   ret = "\xFF" if @column_slice == :before
84 
85   ret.force_encoding('BINARY') if ret.respond_to?(:force_encoding)
86   return ret
87 end
to_s() click to toggle source
   # File lib/cassandra/composite.rb
48 def to_s
49   return pack
50 end

Private Instance Methods

eql?(other) click to toggle source
    # File lib/cassandra/composite.rb
133 def eql?(other)
134   return to_s == other.to_s
135 end
hash() click to toggle source
    # File lib/cassandra/composite.rb
129 def hash
130   return @hash ||= pack.hash
131 end
try_packed_composite(packed_string) click to toggle source
    # File lib/cassandra/composite.rb
106 def try_packed_composite(packed_string)
107   parts = []
108   end_of_component = nil
109   while packed_string.length > 0
110     length = packed_string.slice(0, 2).unpack('n')[0]
111     return false if length.nil? || length + 3 > packed_string.length
112 
113     parts << packed_string.slice(2, length)
114     end_of_component = packed_string.slice(2 + length, 1)
115     if length + 3 != packed_string.length
116       return false if end_of_component != "\x00"
117     end
118 
119     packed_string = packed_string.slice(3 + length, packed_string.length)
120   end
121 
122   @column_slice = :after if end_of_component == "\x01"
123   @column_slice = :before if end_of_component == "\xFF"
124   @parts = parts
125 
126   return true
127 end