Class: CreatedId::IdRange

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/created_id/id_range.rb

Overview

This model stores the id ranges for other models by the hour. It is not meant to be accessed directly.

Class Method Summary collapse

Class Method Details

.id_range(klass, time) ⇒ Array<Integer>

Get the minimum and maximum ids for a model created in a given hour. This method is used in indexing the ranges.

Parameters:

  • klass (Class)

    The class to get the id range for.

  • time (Time)

    The hour to get the id range for.

Returns:

  • (Array<Integer>)

    The minimum and maximum ids for the class created in the given hour.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/created_id/id_range.rb', line 57

def id_range(klass, time)
  klass = klass.base_class
  hour = CreatedId.coerce_hour(time)
  next_hour = hour + 3600
  prev_hour = hour - 3600

  finder = klass.unscoped.where(created_at: (hour...next_hour))

  prev_id = CreatedId::IdRange.min_id(self, prev_hour)
  if prev_id
    finder = finder.where(klass.arel_table[:id].gt(prev_id)) if prev_id > 0

    next_id = CreatedId::IdRange.min_id(self, next_hour + 3600)
    if next_id
      finder = finder.where(klass.arel_table[:id].lt(next_id)) if next_id > prev_id
    end
  end

  [finder.minimum(:id), finder.maximum(:id)]
end

.max_id(klass, time) ⇒ Integer

Get the maximum id for a class created in a given hour.

Parameters:

  • klass (Class)

    The class to get the maximum id for.

  • time (Time)

    The hour to get the maximum id for.

Returns:

  • (Integer)

    The maximum id for the class created in the given hour.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/created_id/id_range.rb', line 36

def max_id(klass, time)
  id = for_class(klass).created_after(CreatedId.coerce_hour(time)).order(hour: :asc).first&.max_id

  unless id
    col_limit = klass.columns.detect { |c| c.name == klass.primary_key }.limit
    id = if col_limit && col_limit > 0
      ((256**col_limit) / 2) - 1
    else
      klass.base_class.unscoped.maximum(:id).to_i
    end
  end

  id
end

.min_id(klass, time) ⇒ Integer

Get the minimum id for a class created in a given hour.

Parameters:

  • klass (Class)

    The class to get the minimum id for.

  • time (Time)

    The hour to get the minimum id for.

Returns:

  • (Integer)

    The minimum id for the class created in the given hour.



27
28
29
# File 'lib/created_id/id_range.rb', line 27

def min_id(klass, time)
  for_class(klass).created_before(time).order(hour: :desc).first&.min_id || 0
end

.save_created_id(klass, time, min_id, max_id) ⇒ void

This method returns an undefined value.

Save the minimum and maximum ids for a class created in a given hour.

Parameters:

  • klass (Class)

    The class to save the id range for.

  • time (Time)

    The hour to save the id range for.

  • min_id (Integer)

    The minimum id for the class created in the given hour.

  • max_id (Integer)

    The maximum id for the class created in the given hour.



85
86
87
88
89
90
# File 'lib/created_id/id_range.rb', line 85

def save_created_id(klass, time, min_id, max_id)
  record = find_or_initialize_by(class_name: klass.base_class.name, hour: CreatedId.coerce_hour(time))
  record.min_id = min_id
  record.max_id = max_id
  record.save!
end