Module: PartitionGardener::Templates

Defined in:
lib/partition_gardener/templates.rb

Class Method Summary collapse

Class Method Details

.branch_config_for(parent_config, branch) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/partition_gardener/templates.rb', line 249

def branch_config_for(parent_config, branch)
  branch_table_name = "#{parent_config[:table_name]}_#{branch.fetch(:name)}"
  child = branch.dup
  child.delete(:name)
  child[:table_name] = branch_table_name
  child[:parent_table_name] = parent_config[:table_name]
  child[:layout] ||= parent_config.fetch(:default_child_layout, :hash_branches)

  if child[:layout] == :sliding_window
    child[:bucket] ||= parent_config.fetch(:default_child_bucket, :month)
    child[:partition_key_column] ||= branch.fetch(:partition_key_column)
    return sliding_window_for_bucket(
      child[:bucket],
      table_name: branch_table_name,
      partition_key_column: child[:partition_key_column],
      conflict_key: parent_config[:conflict_key],
      parent_table_name: parent_config[:table_name],
      split_row_threshold: child[:split_row_threshold],
      active_months: child[:active_months],
      active_days: child[:active_days],
      active_weeks: child[:active_weeks],
      active_quarters: child[:active_quarters]
    )
  end

  if child[:layout] == :list_split
    child[:branches] = branch.fetch(:branches)
    child[:conflict_key] = parent_config[:conflict_key]
    return list_split(
      table_name: branch_table_name,
      branches: child[:branches],
      conflict_key: child[:conflict_key],
      partition_key_column: child[:partition_key_column],
      parent_table_name: parent_config[:table_name]
    )
  end

  normalize(child)
end

.calendar_year(table_name:, partition_key_column:, conflict_key:, active_years: 2, split_row_threshold: nil, **options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/partition_gardener/templates.rb', line 123

def calendar_year(table_name:, partition_key_column:, conflict_key:, active_years: 2, split_row_threshold: nil, **options)
  normalize({
    table_name: table_name,
    layout: :calendar_year,
    bucket: :year,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    active_years: active_years,
    partition_name_format: options.fetch(:partition_name_format) {
      ->(identifier) { DateBucket.partition_name(table_name, identifier, :year) }
    },
    partition_definition: options.fetch(:partition_definition) {
      ->(date) { DateBucket.partition_definition_clause(date, :year) }
    },
    extract_partition_identifier: options.fetch(:extract_partition_identifier) {
      ->(date_value) { DateBucket.beginning_of_bucket(date_value, :year) }
    }
  }.merge(options.except(:partition_name_format, :partition_definition, :extract_partition_identifier)).tap do |config|
    config[:split_row_threshold] = split_row_threshold if split_row_threshold
  end)
end

.composite_list_hash(parent_table:, discriminator_column:, branches:, conflict_key:, **options) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/partition_gardener/templates.rb', line 188

def composite_list_hash(parent_table:, discriminator_column:, branches:, conflict_key:, **options)
  composite_with_branches(
    parent_table: parent_table,
    discriminator_column: discriminator_column,
    branches: branches,
    conflict_key: conflict_key,
    parent_mode: :list,
    default_child_layout: :hash_branches,
    **options
  )
end

.composite_list_range(parent_table:, discriminator_column:, branches:, conflict_key:, bucket: :month, **options) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/partition_gardener/templates.rb', line 200

def composite_list_range(parent_table:, discriminator_column:, branches:, conflict_key:, bucket: :month, **options)
  composite_with_branches(
    parent_table: parent_table,
    discriminator_column: discriminator_column,
    branches: branches,
    conflict_key: conflict_key,
    parent_mode: :list,
    default_child_layout: :sliding_window,
    default_child_bucket: bucket,
    **options
  )
end

.composite_range_hash(parent_table:, partition_key_column:, branches:, conflict_key:, bucket: :month, **options) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/partition_gardener/templates.rb', line 223

def composite_range_hash(parent_table:, partition_key_column:, branches:, conflict_key:, bucket: :month, **options)
  composite_with_branches(
    parent_table: parent_table,
    partition_key_column: partition_key_column,
    branches: branches,
    conflict_key: conflict_key,
    parent_mode: :range,
    parent_bucket: bucket,
    default_child_layout: :hash_branches,
    **options
  )
end

.composite_range_list(parent_table:, partition_key_column:, branches:, conflict_key:, bucket: :month, **options) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/partition_gardener/templates.rb', line 236

def composite_range_list(parent_table:, partition_key_column:, branches:, conflict_key:, bucket: :month, **options)
  composite_with_branches(
    parent_table: parent_table,
    partition_key_column: partition_key_column,
    branches: branches,
    conflict_key: conflict_key,
    parent_mode: :range,
    parent_bucket: bucket,
    default_child_layout: :list_split,
    **options
  )
end

.composite_with_branches(parent_table:, branches:, conflict_key:, parent_mode:, default_child_layout:, discriminator_column: nil, partition_key_column: nil, parent_bucket: :month, default_child_bucket: :month, **options) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/partition_gardener/templates.rb', line 318

def composite_with_branches(parent_table:, branches:, conflict_key:, parent_mode:, default_child_layout:, discriminator_column: nil, partition_key_column: nil, parent_bucket: :month, default_child_bucket: :month, **options)
  list_branch_entries = Predicate.list_branch_entries(branches, discriminator_column: discriminator_column) if parent_mode == :list

  child_branches = branches.map do |branch|
    entry = {
      name: branch.fetch(:name),
      layout: branch.fetch(:layout, default_child_layout)
    }
    entry[:partition_key_column] = branch[:partition_key_column] if branch[:partition_key_column]
    entry[:hash_modulus] = branch.fetch(:hash_modulus, Strategy::HashBranches::DEFAULT_MODULUS) if entry[:layout] == :hash_branches
    entry[:bucket] = branch.fetch(:bucket, default_child_bucket) if entry[:layout] == :sliding_window
    entry[:active_months] = branch[:active_months] if branch[:active_months]
    entry[:active_days] = branch[:active_days] if branch[:active_days]
    entry[:active_weeks] = branch[:active_weeks] if branch[:active_weeks]
    entry[:active_quarters] = branch[:active_quarters] if branch[:active_quarters]
    entry[:split_row_threshold] = branch[:split_row_threshold] if branch[:split_row_threshold]
    entry[:branches] = branch[:branches] if branch[:branches]
    entry
  end

  normalize({
    table_name: parent_table,
    layout: :composite,
    parent_mode: parent_mode,
    bucket: parent_bucket,
    discriminator_column: discriminator_column,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    default_child_layout: default_child_layout,
    default_child_bucket: default_child_bucket,
    list_branches: (parent_mode == :list) ? list_branch_entries : nil,
    branches: child_branches
  }.compact.merge(options))
end

.date_layout?(layout) ⇒ Boolean

Returns:

  • (Boolean)


289
290
291
# File 'lib/partition_gardener/templates.rb', line 289

def date_layout?(layout)
  layout == :sliding_window || layout == :calendar_year || layout == :premake_monthly || layout == :rolling_current
end

.expand(config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/partition_gardener/templates.rb', line 12

def expand(config)
  normalized = normalize(config)
  return [normalized] unless normalized[:layout] == :composite

  configs = []
  parent_mode = normalized.fetch(:parent_mode, :list)

  case parent_mode
  when :list
    if normalized[:list_branches]
      configs << list_split(
        table_name: normalized[:table_name],
        branches: normalized[:list_branches],
        conflict_key: normalized[:conflict_key],
        partition_key_column: normalized.fetch(:partition_key_column, normalized[:discriminator_column])
      )
    end
  when :range
    configs << range_parent_config_for(normalized)
  end

  normalized.fetch(:branches, []).each do |branch|
    configs << branch_config_for(normalized, branch)
  end

  configs
end

.hash_branches(table_name:, partition_key_column:, conflict_key:, hash_modulus: nil, split_row_threshold: nil, **options) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/partition_gardener/templates.rb', line 160

def hash_branches(table_name:, partition_key_column:, conflict_key:, hash_modulus: nil, split_row_threshold: nil, **options)
  normalize({
    table_name: table_name,
    layout: :hash_branches,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    hash_modulus: hash_modulus || Strategy::HashBranches::DEFAULT_MODULUS
  }.merge(options).tap do |config|
    config[:split_row_threshold] = split_row_threshold if split_row_threshold
  end)
end

.integer_window(table_name:, partition_key_column:, conflict_key:, active_id_lo: 0, active_id_width: nil, current_band_size: nil, split_row_threshold: nil, **options) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/partition_gardener/templates.rb', line 145

def integer_window(table_name:, partition_key_column:, conflict_key:, active_id_lo: 0, active_id_width: nil, current_band_size: nil, split_row_threshold: nil, **options)
  normalize({
    table_name: table_name,
    layout: :integer_window,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    active_id_lo: active_id_lo,
    active_id_width: active_id_width || Strategy::IntegerRange::DEFAULT_ACTIVE_ID_WIDTH,
    current_band_size: current_band_size || Strategy::IntegerRange::DEFAULT_CURRENT_BAND_SIZE,
    archive_band_size: options.fetch(:archive_band_size, Strategy::IntegerRange::DEFAULT_ARCHIVE_BAND_SIZE)
  }.merge(options).tap do |config|
    config[:split_row_threshold] = split_row_threshold if split_row_threshold
  end)
end

.list_range(parent_table:, discriminator_column:, branches:, conflict_key:, **options) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/partition_gardener/templates.rb', line 213

def list_range(parent_table:, discriminator_column:, branches:, conflict_key:, **options)
  composite_list_range(
    parent_table: parent_table,
    discriminator_column: discriminator_column,
    branches: branches,
    conflict_key: conflict_key,
    **options
  )
end

.list_split(table_name:, branches:, conflict_key:, partition_key_column: nil, **options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/partition_gardener/templates.rb', line 172

def list_split(table_name:, branches:, conflict_key:, partition_key_column: nil, **options)
  discriminator_column = partition_key_column || branches.first&.fetch(:discriminator_column, nil)
  normalized_branches = Predicate.normalize_branches!(
    branches,
    discriminator_column: discriminator_column
  )

  normalize({
    table_name: table_name,
    layout: :list_split,
    branches: normalized_branches,
    conflict_key: conflict_key,
    partition_key_column: partition_key_column || discriminator_column || "id"
  }.merge(options))
end

.normalize(config) ⇒ Object



5
6
7
8
9
10
# File 'lib/partition_gardener/templates.rb', line 5

def normalize(config)
  config = config.dup
  config[:layout] ||= :sliding_window
  config[:bucket] ||= :month if date_layout?(config[:layout])
  config
end

.premake_monthly(table_name:, partition_key_column:, conflict_key:, premake_months: 3, split_row_threshold: nil, **options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/partition_gardener/templates.rb', line 100

def premake_monthly(table_name:, partition_key_column:, conflict_key:, premake_months: 3, split_row_threshold: nil, **options)
  normalize({
    table_name: table_name,
    layout: :premake_monthly,
    bucket: :month,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    premake_months: premake_months,
    maintenance_backend: options.fetch(:maintenance_backend, :gardener),
    partition_name_format: options.fetch(:partition_name_format) {
      ->(identifier) { DateBucket.partition_name(table_name, identifier, :month) }
    },
    partition_definition: options.fetch(:partition_definition) {
      ->(date) { DateBucket.partition_definition_clause(date, :month) }
    },
    extract_partition_identifier: options.fetch(:extract_partition_identifier) {
      ->(date_value) { DateBucket.beginning_of_bucket(date_value, :month) }
    }
  }.merge(options.except(:partition_name_format, :partition_definition, :extract_partition_identifier)).tap do |config|
    config[:split_row_threshold] = split_row_threshold if split_row_threshold
  end)
end

.range_parent_config_for(parent_config) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/partition_gardener/templates.rb', line 353

def range_parent_config_for(parent_config)
  bucket = parent_config.fetch(:bucket, :month)
  base = {
    table_name: parent_config[:table_name],
    partition_key_column: parent_config.fetch(:partition_key_column),
    conflict_key: parent_config[:conflict_key]
  }
  passthrough = parent_config.slice(
    :active_months, :active_days, :active_weeks, :active_quarters, :active_years,
    :split_row_threshold, :maintenance_backend, :retention_months, :move_batch_size,
    :statement_timeout, :incremental_rebalance, :analyze_after_rebalance
  )

  if bucket == :year
    calendar_year(**base, **passthrough)
  else
    sliding_window_for_bucket(bucket, **base, **passthrough)
  end
end

.rolling_current_monthly(table_name:, partition_key_column:, conflict_key:, active_months: 12, **options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/partition_gardener/templates.rb', line 88

def rolling_current_monthly(table_name:, partition_key_column:, conflict_key:, active_months: 12, **options)
  sliding_window_monthly(
    table_name: table_name,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    active_months: active_months,
    layout: :rolling_current,
    split_row_threshold: Float::INFINITY,
    **options
  )
end

.sliding_window_daily(table_name:, partition_key_column:, conflict_key:, active_days: 90, split_row_threshold: nil, **options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/partition_gardener/templates.rb', line 52

def sliding_window_daily(table_name:, partition_key_column:, conflict_key:, active_days: 90, split_row_threshold: nil, **options)
  sliding_window_for_bucket(
    :day,
    table_name: table_name,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    active_days: active_days,
    split_row_threshold: split_row_threshold,
    **options
  )
end

.sliding_window_for_bucket(bucket, table_name:, partition_key_column:, conflict_key:, split_row_threshold: nil, layout: :sliding_window, **options) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/partition_gardener/templates.rb', line 293

def sliding_window_for_bucket(bucket, table_name:, partition_key_column:, conflict_key:, split_row_threshold: nil, layout: :sliding_window, **options)
  active_key = DateBucket.active_key(bucket)
  active_value = options[active_key] || DateBucket.default_active_span(bucket)

  normalize({
    :table_name => table_name,
    :layout => layout,
    :bucket => bucket,
    :partition_key_column => partition_key_column,
    :conflict_key => conflict_key,
    active_key => active_value,
    :partition_name_format => options.fetch(:partition_name_format) {
      ->(identifier) { DateBucket.partition_name(table_name, identifier, bucket) }
    },
    :partition_definition => options.fetch(:partition_definition) {
      ->(date) { DateBucket.partition_definition_clause(date, bucket) }
    },
    :extract_partition_identifier => options.fetch(:extract_partition_identifier) {
      ->(date_value) { DateBucket.beginning_of_bucket(date_value, bucket) }
    }
  }.merge(options.except(:partition_name_format, :partition_definition, :extract_partition_identifier, active_key)).tap do |config|
    config[:split_row_threshold] = split_row_threshold if split_row_threshold
  end)
end

.sliding_window_monthly(table_name:, partition_key_column:, conflict_key:, active_months: 12, split_row_threshold: nil, **options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/partition_gardener/templates.rb', line 40

def sliding_window_monthly(table_name:, partition_key_column:, conflict_key:, active_months: 12, split_row_threshold: nil, **options)
  sliding_window_for_bucket(
    :month,
    table_name: table_name,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    active_months: active_months,
    split_row_threshold: split_row_threshold,
    **options
  )
end

.sliding_window_quarterly(table_name:, partition_key_column:, conflict_key:, active_quarters: 8, split_row_threshold: nil, **options) ⇒ Object



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

def sliding_window_quarterly(table_name:, partition_key_column:, conflict_key:, active_quarters: 8, split_row_threshold: nil, **options)
  sliding_window_for_bucket(
    :quarter,
    table_name: table_name,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    active_quarters: active_quarters,
    split_row_threshold: split_row_threshold,
    **options
  )
end

.sliding_window_weekly(table_name:, partition_key_column:, conflict_key:, active_weeks: 52, split_row_threshold: nil, **options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/partition_gardener/templates.rb', line 64

def sliding_window_weekly(table_name:, partition_key_column:, conflict_key:, active_weeks: 52, split_row_threshold: nil, **options)
  sliding_window_for_bucket(
    :week,
    table_name: table_name,
    partition_key_column: partition_key_column,
    conflict_key: conflict_key,
    active_weeks: active_weeks,
    split_row_threshold: split_row_threshold,
    **options
  )
end