Class: CleverSequence::LowerBoundFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/demo_mode/clever_sequence/lower_bound_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, column_name, block) ⇒ LowerBoundFinder

Returns a new instance of LowerBoundFinder.



7
8
9
10
11
# File 'lib/demo_mode/clever_sequence/lower_bound_finder.rb', line 7

def initialize(klass, column_name, block)
  @klass = klass
  @column_name = column_name
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/demo_mode/clever_sequence/lower_bound_finder.rb', line 5

def block
  @block
end

#column_nameObject (readonly)

Returns the value of attribute column_name.



5
6
7
# File 'lib/demo_mode/clever_sequence/lower_bound_finder.rb', line 5

def column_name
  @column_name
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/demo_mode/clever_sequence/lower_bound_finder.rb', line 5

def klass
  @klass
end

Instance Method Details

#lower_bound(hint: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/demo_mode/clever_sequence/lower_bound_finder.rb', line 13

def lower_bound(hint: nil)
  with_lock do
    start = hint && hint >= 1 ? hint : 1
    # If the hint overshoots the actual data, return it directly.
    # The hint is a previously-known high-water mark, so it's a valid
    # lower bound. Callers pass the result through GREATEST against the
    # PG sequence, so a higher value is always safe and avoids a costly
    # binary search back down to data that won't be used anyway.
    next hint if start > 1 && !exists?(start)

    _lower_bound(start, 0, Float::INFINITY)
  end
end