class Cassandra::Cluster::Schema::Fetchers::V1_2_x

rubocop:disable Naming/ClassAndModuleCamelCase

Constants

SELECT_COLUMNS
SELECT_KEYSPACE
SELECT_KEYSPACES
SELECT_KEYSPACE_COLUMNS
SELECT_KEYSPACE_TABLES
SELECT_TABLE
SELECT_TABLES
SELECT_TABLE_COLUMNS

Public Class Methods

new(type_parser, schema) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
322 def initialize(type_parser, schema)
323   @type_parser = type_parser
324   @schema      = schema
325 end

Private Instance Methods

create_column(column_data) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
536 def create_column(column_data)
537   name      = column_data['column_name']
538   is_static = (column_data['type'] == 'STATIC')
539   type, order, is_frozen =
540     @type_parser.parse(column_data['validator']).results.first
541   Column.new(name, type, order, is_static, is_frozen)
542 end
create_compaction_strategy(table_data) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
544 def create_compaction_strategy(table_data)
545   klass = table_data['compaction_strategy_class']
546   klass.slice!('org.apache.cassandra.db.compaction.')
547   options = ::JSON.load(table_data['compaction_strategy_options'])
548   ColumnContainer::Compaction.new(klass, options)
549 end
create_index(table, column, row_column) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
508 def create_index(table, column, row_column)
509   # Most of this logic was taken from the Java driver.
510   options = {}
511   # For some versions of C*, this field could have a literal string 'null' value.
512   if !row_column['index_options'].nil? && row_column['index_options'] != 'null' &&
513      !row_column['index_options'].empty?
514     options = ::JSON.load(row_column['index_options'])
515   end
516   column_name = Util.escape_name(column.name)
517   target = if options.key?('index_keys')
518              "keys(#{column_name})"
519            elsif options.key?('index_keys_and_values')
520              "entries(#{column_name})"
521            elsif column.frozen? && (column.type == Cassandra::Types::Set ||
522                column.type == Cassandra::Types::List ||
523                column.type == Cassandra::Types::Map)
524              "full(#{column_name})"
525            else
526              column_name
527            end
528 
529   table.add_index(Cassandra::Index.new(table,
530                                        row_column['index_name'],
531                                        row_column['index_type'].downcase.to_sym,
532                                        target,
533                                        options))
534 end
create_keyspace(keyspace_data, rows_tables, rows_columns, rows_types, rows_functions, rows_aggregates, rows_views, rows_indexes, rows_triggers) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
369 def create_keyspace(keyspace_data, rows_tables, rows_columns,
370                     rows_types, rows_functions, rows_aggregates,
371                     rows_views, rows_indexes, rows_triggers)
372   keyspace_name = keyspace_data['keyspace_name']
373   replication   = create_replication(keyspace_data)
374   types = rows_types.each_with_object({}) do |row, h|
375     h[row['type_name']] = create_type(row)
376   end
377 
378   # Create a FunctionCollection for the functions and aggregates.
379   functions = Cassandra::FunctionCollection.new
380   rows_functions.each do |row|
381     functions.add_or_update(create_function(row))
382   end
383 
384   aggregates = Cassandra::FunctionCollection.new
385   rows_aggregates.each do |row|
386     aggregates.add_or_update(create_aggregate(row, functions))
387   end
388 
389   lookup_columns = map_rows_by(rows_columns, 'columnfamily_name')
390   lookup_indexes = map_rows_by(rows_indexes, 'columnfamily_name')
391   lookup_triggers = map_rows_by(rows_triggers, 'columnfamily_name')
392   tables = rows_tables.each_with_object({}) do |row, h|
393     table_name = row['columnfamily_name']
394     h[table_name] = create_table(row,
395                                  lookup_columns[table_name],
396                                  lookup_indexes[table_name],
397                                  lookup_triggers[table_name])
398   end
399 
400   Keyspace.new(keyspace_name,
401                keyspace_data['durable_writes'],
402                replication,
403                tables,
404                types,
405                functions,
406                aggregates,
407                {})
408 end
create_replication(keyspace_data) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
362 def create_replication(keyspace_data)
363   klass = keyspace_data['strategy_class']
364   klass.slice!(REPLICATION_PACKAGE_PREFIX)
365   options = ::JSON.load(keyspace_data['strategy_options'])
366   Keyspace::Replication.new(klass, options)
367 end
create_table(table_data, rows_columns, rows_indexes, rows_triggers) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
410 def create_table(table_data, rows_columns, rows_indexes, rows_triggers)
411   keyspace_name   = table_data['keyspace_name']
412   table_name      = table_data['columnfamily_name']
413   key_validator   = @type_parser.parse(table_data['key_validator'])
414   comparator      = @type_parser.parse(table_data['comparator'])
415   column_aliases  = ::JSON.load(table_data['column_aliases'])
416 
417   if comparator.collections.nil?
418     is_compact = true
419     if !column_aliases.empty? || rows_columns.empty?
420       has_value = true
421       clustering_size = comparator.results.size
422     else
423       has_value = false
424       clustering_size = 0
425     end
426   else
427     size = comparator.results.size
428     if !comparator.collections.empty?
429       is_compact = false
430       has_value  = false
431       clustering_size = size - 2
432     elsif column_aliases.size == size - 1 &&
433           comparator.results.last.first == Cassandra::Types.varchar
434       is_compact = false
435       has_value  = false
436       clustering_size = size - 1
437     else
438       is_compact = true
439       has_value  = (!column_aliases.empty? || rows_columns.empty?)
440       clustering_size = size
441     end
442   end
443 
444   # Separate out the partition-key, clustering-columns, and other-columns
445   partition_key      = []
446   clustering_columns = []
447   clustering_order   = []
448   other_columns = []
449 
450   compaction_strategy = create_compaction_strategy(table_data)
451   table_options =
452     create_table_options(table_data, compaction_strategy, is_compact)
453 
454   key_aliases = ::JSON.load(table_data['key_aliases'])
455 
456   key_validator.results.each_with_index do |(type, order, is_frozen), i|
457     key_alias = key_aliases.fetch(i) { i.zero? ? 'key' : "key#{i + 1}" }
458 
459     partition_key[i] = Column.new(key_alias, type, order, false, is_frozen)
460   end
461 
462   clustering_size.times do |i|
463     column_alias = column_aliases.fetch(i) { "column#{i + 1}" }
464     type, order, is_frozen = comparator.results.fetch(i)
465 
466     clustering_columns[i] =
467       Column.new(column_alias, type, order, false, is_frozen)
468     clustering_order[i]   = order
469   end
470 
471   if has_value
472     value_alias   = table_data['value_alias']
473     value_alias ||= 'value'
474 
475     unless value_alias.empty?
476       type, order, is_frozen =
477         @type_parser.parse(table_data['default_validator']).results.first
478       other_columns <<
479         Column.new(value_alias, type, order, false, is_frozen)
480     end
481   end
482 
483   index_rows = []
484   rows_columns.each do |row|
485     column = create_column(row)
486     other_columns << column
487 
488     # In C* 1.2.x, index info is in the column metadata; rows_indexes is [].
489     index_rows << [column, row] unless row['index_type'].nil?
490   end
491 
492   table = Cassandra::Table.new(@schema.keyspace(keyspace_name),
493                                table_name,
494                                partition_key,
495                                clustering_columns,
496                                other_columns,
497                                table_options,
498                                clustering_order,
499                                table_data['id'])
500 
501   # Create Index objects and add them to the table.
502   index_rows.each do |column, row|
503     create_index(table, column, row)
504   end
505   table
506 end
create_table_options(table_data, compaction_strategy, is_compact) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
551 def create_table_options(table_data, compaction_strategy, is_compact)
552   compression_parameters = ::JSON.load(table_data['compression_parameters'])
553   if compression_parameters['sstable_compression']
554     compression_parameters['sstable_compression']
555       .slice!(COMPRESSION_PACKAGE_PREFIX)
556   end
557   Cassandra::ColumnContainer::Options.new(
558     table_data['comment'],
559     table_data['read_repair_chance'],
560     table_data['local_read_repair_chance'],
561     table_data['gc_grace_seconds'],
562     table_data['caching'],
563     table_data['bloom_filter_fp_chance'] || 0.01,
564     table_data['populate_io_cache_on_flush'],
565     table_data['memtable_flush_period_in_ms'],
566     table_data['default_time_to_live'],
567     nil,
568     nil,
569     table_data['replicate_on_write'],
570     nil,
571     nil,
572     compaction_strategy,
573     compression_parameters,
574     is_compact,
575     table_data['crc_check_chance'],
576     table_data['extensions'],
577     nil
578   )
579 end
select_columns(connection) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
337 def select_columns(connection)
338   send_select_request(connection, SELECT_COLUMNS)
339 end
select_keyspace(connection, keyspace_name) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
341 def select_keyspace(connection, keyspace_name)
342   send_select_request(connection, SELECT_KEYSPACE % keyspace_name)
343 end
select_keyspace_columns(connection, keyspace_name) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
349 def select_keyspace_columns(connection, keyspace_name)
350   send_select_request(connection, format(SELECT_KEYSPACE_COLUMNS, keyspace_name))
351 end
select_keyspace_tables(connection, keyspace_name) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
345 def select_keyspace_tables(connection, keyspace_name)
346   send_select_request(connection, format(SELECT_KEYSPACE_TABLES, keyspace_name))
347 end
select_keyspaces(connection) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
329 def select_keyspaces(connection)
330   send_select_request(connection, SELECT_KEYSPACES)
331 end
select_table(connection, keyspace_name, table_name) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
353 def select_table(connection, keyspace_name, table_name)
354   send_select_request(connection, format(SELECT_TABLE, keyspace_name, table_name))
355 end
select_table_columns(connection, keyspace_name, table_name) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
357 def select_table_columns(connection, keyspace_name, table_name)
358   send_select_request(connection,
359                       format(SELECT_TABLE_COLUMNS, keyspace_name, table_name))
360 end
select_tables(connection) click to toggle source
    # File lib/cassandra/cluster/schema/fetchers.rb
333 def select_tables(connection)
334   send_select_request(connection, SELECT_TABLES)
335 end