Class: SqlPartitioner::BasePartitionsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_partitioner/base_partitions_manager.rb

Direct Known Subclasses

PartitionsManager

Constant Summary collapse

FUTURE_PARTITION_NAME =
'future'
FUTURE_PARTITION_VALUE =
'MAXVALUE'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BasePartitionsManager

Returns a new instance of BasePartitionsManager.

Parameters:

  • options ({Symbol=>Object}) (defaults to: {})


24
25
26
27
28
29
30
31
32
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 24

def initialize(options = {})
  @adapter            = options[:adapter]
  @tuc                = TimeUnitConverter.new(options[:time_unit] || :seconds)

  @current_timestamp  = @tuc.from_seconds((options[:current_time] || Time.now).to_i)
  @table_name         = options[:table_name]
  @logger             = options[:logger]
  @lock_wait_timeout  = options[:lock_wait_timeout]
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



4
5
6
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 4

def adapter
  @adapter
end

#current_timestampObject

Returns the value of attribute current_timestamp.



4
5
6
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 4

def current_timestamp
  @current_timestamp
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 4

def logger
  @logger
end

#table_nameObject

Returns the value of attribute table_name.



4
5
6
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 4

def table_name
  @table_name
end

Instance Method Details

#drop_partitions(partition_names, dry_run = false) ⇒ String

Drop partitions by name

Parameters:

  • partition_names (Array<String>)

    array of String partition_names

  • dry_run (Boolean) (defaults to: false)

    Defaults to false. If true, query wont be executed.

Returns:

  • (String)

    drop sql if dry run is true

Raises:

  • (ArgumentError)

    if input is not an Array or if partition name is not a string



63
64
65
66
67
68
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 63

def drop_partitions(partition_names, dry_run = false)
  _validate_drop_partitions_names(partition_names)

  drop_sql = SqlPartitioner::SQL.drop_partitions(table_name, partition_names)
  _execute_and_display_partition_info(drop_sql, dry_run)
end

#initialize_partitioning(partition_data, dry_run = false) ⇒ Object

initialize partitioning on the given table based on partition_data provided. partition data should be of form

{partition_name1 => partition_timestamp_1 ,
 partition_name2 => partition_timestamp_2...}

For example:

{'until_2014_03_17' => 1395077901193149,
 'until_2014_04_01' => 1396373901193398}

Parameters:

  • partition_data (Hash<String,Fixnum>)

    of form { partition_name1 => timestamp1..}

  • dry_run (Boolean) (defaults to: false)

    Defaults to false. If true, query wont be executed.

Raises:

  • (ArgumentError)

    if partition data is not hash or if one of name id is not a String or if one of the value is not Integer



48
49
50
51
52
53
54
55
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 48

def initialize_partitioning(partition_data, dry_run = false)
  partition_data = partition_data.merge(FUTURE_PARTITION_NAME => FUTURE_PARTITION_VALUE)

  _validate_partition_data(partition_data)

  init_sql = SqlPartitioner::SQL.initialize_partitioning(table_name, partition_data)
  _execute_and_display_partition_info(init_sql, dry_run)
end

#log(message, prefix = true) ⇒ Object



89
90
91
92
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 89

def log(message, prefix = true)
  message = "[#{self.class.name}]#{message}" if prefix
  @logger.info "#{message}"
end

#name_from_timestamp(timestamp) ⇒ String

generates name of for “until_yyyy_mm_dd” from the given timestamp. returns future partition name if value is FUTURE_PARTITION_VALUE

Parameters:

  • timestamp (Fixnum)

    timestamp for which the name has to be generated.

Returns:

  • (String)

    partition_name



101
102
103
104
105
106
107
108
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 101

def name_from_timestamp(timestamp)
  if timestamp == FUTURE_PARTITION_VALUE
     FUTURE_PARTITION_NAME
  else
    seconds = @tuc.to_seconds(timestamp)
    "until_#{Time.at(seconds).utc.strftime("%Y_%m_%d")}"
  end
end

#reorg_future_partition(partition_data, dry_run = false) ⇒ Boolean, String

Reorgs future partition into partitions provided as input.

Parameters:

  • partition_data (Hash<String,Fixnum>)

    of form { partition_name1 => timestamp1..}

  • dry_run (Boolean) (defaults to: false)

    Defaults to false. If true, query wont be executed.

Returns:

  • (Boolean)

    true if not dry run and query is executed else false

  • (String)

    sql if dry_run is true



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sql_partitioner/base_partitions_manager.rb', line 76

def reorg_future_partition(partition_data, dry_run = false)
  partition_data = partition_data.dup

  if partition_data.any?
    partition_data[FUTURE_PARTITION_NAME] = FUTURE_PARTITION_VALUE
  end

  _validate_partition_data(partition_data)

  reorg_sql = SqlPartitioner::SQL.reorg_partitions(table_name, partition_data, FUTURE_PARTITION_NAME)
  _execute_and_display_partition_info(reorg_sql, dry_run)
end