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 
15   if parts.length == 1 && parts[0].instance_of?(self.class)
16     make_from_parts(parts[0].parts, :slice => parts[0].column_slice)
17   elsif parts.length == 1 && parts[0].instance_of?(String) && @column_slice.nil? && try_packed_composite(parts[0])
18     @hash = parts[0].hash
19   else
20     make_from_parts(parts, options)
21   end
22 end
new_from_packed(packed) click to toggle source
   # File lib/cassandra/composite.rb
24 def self.new_from_packed(packed)
25   obj = new
26   obj.fast_unpack(packed)
27   return obj
28 end
new_from_parts(parts, args={}) click to toggle source
   # File lib/cassandra/composite.rb
30 def self.new_from_parts(parts, args={})
31   obj = new
32   obj.make_from_parts(parts, args)
33 
34   return obj
35 end

Public Instance Methods

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

Private Instance Methods

eql?(other) click to toggle source
    # File lib/cassandra/composite.rb
143 def eql?(other)
144   return to_s == other.to_s
145 end
hash() click to toggle source
    # File lib/cassandra/composite.rb
139 def hash
140   return @hash ||= pack.hash
141 end
try_packed_composite(packed_string) click to toggle source
    # File lib/cassandra/composite.rb
116 def try_packed_composite(packed_string)
117   parts = []
118   end_of_component = nil
119   while packed_string.length > 0
120     length = packed_string.slice(0, 2).unpack('n')[0]
121     return false if length.nil? || length + 3 > packed_string.length
122 
123     parts << packed_string.slice(2, length)
124     end_of_component = packed_string.slice(2 + length, 1)
125     if length + 3 != packed_string.length
126       return false if end_of_component != "\x00"
127     end
128 
129     packed_string = packed_string.slice(3 + length, packed_string.length)
130   end
131 
132   @column_slice = :after if end_of_component == "\x01"
133   @column_slice = :before if end_of_component == "\xFF"
134   @parts = parts
135 
136   return true
137 end