Class: PgDice::PartitionManager

Inherits:
Object
  • Object
show all
Includes:
TableFinder
Defined in:
lib/pgdice/partition_manager.rb

Overview

PartitionManager is a class used to fulfill high-level tasks for partitioning

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TableFinder

#batched_tables, #find_droppable_partitions, #table_tester, #tables_newer_than, #tables_older_than, #tables_to_grab

Methods included from DateHelper

#pad_date, #safe_date_builder, #truncate_date

Constructor Details

#initialize(opts = {}) ⇒ PartitionManager

Returns a new instance of PartitionManager.



12
13
14
15
16
17
18
19
20
21
# File 'lib/pgdice/partition_manager.rb', line 12

def initialize(opts = {})
  @logger = opts.fetch(:logger)
  @batch_size = opts.fetch(:batch_size)
  @validation = opts.fetch(:validation)
  @approved_tables = opts.fetch(:approved_tables)
  @partition_adder = opts.fetch(:partition_adder)
  @partition_lister = opts.fetch(:partition_lister)
  @partition_dropper = opts.fetch(:partition_dropper)
  @current_date_provider = opts.fetch(:current_date_provider, proc { Time.now.utc.to_date })
end

Instance Attribute Details

#approved_tablesObject (readonly)

Returns the value of attribute approved_tables.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def approved_tables
  @approved_tables
end

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def batch_size
  @batch_size
end

#current_date_providerObject (readonly)

Returns the value of attribute current_date_provider.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def current_date_provider
  @current_date_provider
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def logger
  @logger
end

#partition_adderObject (readonly)

Returns the value of attribute partition_adder.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def partition_adder
  @partition_adder
end

#partition_dropperObject (readonly)

Returns the value of attribute partition_dropper.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def partition_dropper
  @partition_dropper
end

#partition_listerObject (readonly)

Returns the value of attribute partition_lister.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def partition_lister
  @partition_lister
end

#validationObject (readonly)

Returns the value of attribute validation.



9
10
11
# File 'lib/pgdice/partition_manager.rb', line 9

def validation
  @validation
end

Instance Method Details

#add_new_partitions(table_name, params = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/pgdice/partition_manager.rb', line 23

def add_new_partitions(table_name, params = {})
  all_params = approved_tables.smash(table_name, params)
  logger.debug { "add_new_partitions has been called with params: #{all_params}" }
  validation.validate_parameters(all_params)
  partition_adder.call(all_params)
end

#drop_old_partitions(table_name, params = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/pgdice/partition_manager.rb', line 30

def drop_old_partitions(table_name, params = {})
  all_params = approved_tables.smash(table_name, params)
  all_params[:older_than] = current_date_provider.call
  logger.debug { "drop_old_partitions has been called with params: #{all_params}" }

  validation.validate_parameters(all_params)
  drop_partitions(all_params)
end

#list_droppable_partitions(table_name, params = {}) ⇒ Object



46
47
48
49
50
# File 'lib/pgdice/partition_manager.rb', line 46

def list_droppable_partitions(table_name, params = {})
  all_params = approved_tables.smash(table_name, params)
  validation.validate_parameters(all_params)
  droppable_partitions(all_params)
end

#list_droppable_partitions_by_batch_size(table_name, params = {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/pgdice/partition_manager.rb', line 52

def list_droppable_partitions_by_batch_size(table_name, params = {})
  all_params = approved_tables.smash(table_name, params)
  validation.validate_parameters(all_params)
  droppable_tables = batched_droppable_partitions(all_params)
  logger.debug { "Batched partitions eligible for dropping are: #{droppable_tables}" }
  droppable_tables
end

#list_partitions(table_name, params = {}) ⇒ Object

Grabs only tables that start with the base_table_name and end in numbers



40
41
42
43
44
# File 'lib/pgdice/partition_manager.rb', line 40

def list_partitions(table_name, params = {})
  all_params = approved_tables.smash(table_name, params)
  validation.validate_parameters(all_params)
  partitions(all_params)
end