Class: Cassandra::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra/index.rb

Overview

Represents an index on a cassandra table

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#kindSymbol (readonly)

Returns kind of index: :keys, :composites, or :custom.

Returns:

  • (Symbol)

    kind of index: :keys, :composites, or :custom.



27
28
29
# File 'lib/cassandra/index.rb', line 27

def kind
  @kind
end

#nameString (readonly)

Returns name of the index.

Returns:

  • (String)

    name of the index.



25
26
27
# File 'lib/cassandra/index.rb', line 25

def name
  @name
end

#optionsHash (readonly)

Returns options of the index.

Returns:

  • (Hash)

    options of the index.



31
32
33
# File 'lib/cassandra/index.rb', line 31

def options
  @options
end

#tableCassandra::Table (readonly)

Returns table that the index applies to.

Returns:



23
24
25
# File 'lib/cassandra/index.rb', line 23

def table
  @table
end

#targetString (readonly)

Returns name of column that the index applies to.

Returns:

  • (String)

    name of column that the index applies to.



29
30
31
# File 'lib/cassandra/index.rb', line 29

def target
  @target
end

Instance Method Details

#custom_class_nameString

Returns name of the index class if this is a custom index; nil otherwise.

Returns:

  • (String)

    name of the index class if this is a custom index; nil otherwise.



61
62
63
# File 'lib/cassandra/index.rb', line 61

def custom_class_name
  @options['class_name']
end

#custom_index?Boolean

Returns whether or not this index uses a custom class.

Returns:

  • (Boolean)

    whether or not this index uses a custom class.



56
57
58
# File 'lib/cassandra/index.rb', line 56

def custom_index?
  !@options['class_name'].nil?
end

#to_cqlString

Returns a cql representation of this index.

Returns:

  • (String)

    a cql representation of this index



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cassandra/index.rb', line 66

def to_cql
  keyspace_name = Util.escape_name(@table.keyspace.name)
  table_name = Util.escape_name(@table.name)
  index_name = Util.escape_name(@name)

  # Target is interesting in that it's not necessarily a column name,
  # so we can't simply escape it. If it contains a paren, we take it as is,
  # otherwise assume it's a column name and escape accordingly.
  escaped_target = @target.include?('(') ? @target : Util.escape_name(@target)

  if custom_index?
    "CREATE CUSTOM INDEX #{index_name} ON #{keyspace_name}.#{table_name} (#{escaped_target}) " \
    "USING '#{@options['class_name']}'#{options_cql};"
  else
    "CREATE INDEX #{index_name} ON #{keyspace_name}.#{table_name} (#{escaped_target});"
  end
end