Class: Cassandra::Composite

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cassandra/composite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*parts) ⇒ Composite

Returns a new instance of Composite.

Raises:

  • (ArgumentError)


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

def initialize(*parts)
  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
  elsif parts.length == 1 && parts[0].instance_of?(String) && @column_slice.nil? && valid_packed_composite?(parts[0])
    unpack(parts[0])
  else
    @parts = parts
  end
end

Instance Attribute Details

#column_sliceObject (readonly)

Returns the value of attribute column_slice.



6
7
8
# File 'lib/cassandra/composite.rb', line 6

def column_slice
  @column_slice
end

#partsObject (readonly)

Returns the value of attribute parts.



5
6
7
# File 'lib/cassandra/composite.rb', line 5

def parts
  @parts
end

Instance Method Details

#<=>(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cassandra/composite.rb', line 45

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



26
27
28
# File 'lib/cassandra/composite.rb', line 26

def [](*args)
  return @parts[*args]
end

#inspectObject



69
70
71
# File 'lib/cassandra/composite.rb', line 69

def inspect
  return "#<Composite:#{@column_slice} #{@parts.inspect}>"
end

#packObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/cassandra/composite.rb', line 30

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_componentObject



73
74
75
76
77
# File 'lib/cassandra/composite.rb', line 73

def slice_end_of_component
  return "\x01" if @column_slice == :after
  return "\xFF" if @column_slice == :before
  return "\x00"
end

#to_sObject



41
42
43
# File 'lib/cassandra/composite.rb', line 41

def to_s
  return pack
end