class Cassandra::Index

Represents an index on a cassandra table

Attributes

kind[R]

@return [Symbol] kind of index: `:keys`, `:composites`, or `:custom`.

name[R]

@return [String] name of the index.

options[R]

@return [Hash] options of the index.

table[R]

@return [Cassandra::Table] table that the index applies to.

target[R]

@return [String] name of column that the index applies to.

Public Class Methods

new(table, name, kind, target, options) click to toggle source

@private

   # File lib/cassandra/index.rb
34 def initialize(table,
35                name,
36                kind,
37                target,
38                options)
39   @table = table
40   @name = name.freeze
41   @kind = kind
42   @options = options.freeze
43 
44   # Target is a bit tricky; it may be an escaped name or not
45   # depending on C* version. Unify to be unescaped since a user
46   # who wants to know the target would want the bare column name.
47 
48   @target = if target[0] == '"'
49               target[1..-2]
50             else
51               target
52             end.freeze
53 end

Public Instance Methods

==(other)
Alias for: eql?
custom_class_name() click to toggle source

@return [String] name of the index class if this is a custom index; nil otherwise.

   # File lib/cassandra/index.rb
61 def custom_class_name
62   @options['class_name']
63 end
custom_index?() click to toggle source

@return [Boolean] whether or not this index uses a custom class.

   # File lib/cassandra/index.rb
56 def custom_index?
57   !@options['class_name'].nil?
58 end
eql?(other) click to toggle source

@private

   # File lib/cassandra/index.rb
85 def eql?(other)
86   other.is_a?(Index) &&
87     @table == other.table &&
88     @name == other.name &&
89     @kind == other.kind &&
90     @target == other.target &&
91     @options == other.options
92 end
Also aliased as: ==
inspect() click to toggle source

@private

   # File lib/cassandra/index.rb
96 def inspect
97   "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
98       "@name=#{@name.inspect} @table=#{@table.inspect} @kind=#{@kind.inspect} @target=#{@target.inspect}>"
99 end
to_cql() click to toggle source

@return [String] a cql representation of this index

   # File lib/cassandra/index.rb
66 def to_cql
67   keyspace_name = Util.escape_name(@table.keyspace.name)
68   table_name = Util.escape_name(@table.name)
69   index_name = Util.escape_name(@name)
70 
71   # Target is interesting in that it's not necessarily a column name,
72   # so we can't simply escape it. If it contains a paren, we take it as is,
73   # otherwise assume it's a column name and escape accordingly.
74   escaped_target = @target.include?('(') ? @target : Util.escape_name(@target)
75 
76   if custom_index?
77     "CREATE CUSTOM INDEX #{index_name} ON #{keyspace_name}.#{table_name} (#{escaped_target}) " \
78     "USING '#{@options['class_name']}'#{options_cql};"
79   else
80     "CREATE INDEX #{index_name} ON #{keyspace_name}.#{table_name} (#{escaped_target});"
81   end
82 end

Private Instance Methods

options_cql() click to toggle source
    # File lib/cassandra/index.rb
103 def options_cql
104   # exclude 'class_name', 'target' keys
105   filtered_options = @options.reject do |key, _|
106     key == 'class_name' || key == 'target'
107   end
108   return '' if filtered_options.empty?
109 
110   result = ' WITH OPTIONS = {'
111   result << filtered_options.map do |key, value|
112     "'#{key}': '#{value}'"
113   end.join(', ')
114   result << '}'
115   result
116 end