Class: Kcl::Types::ExtendedSequenceNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/kcl/types/extended_sequence_number.rb

Constant Summary collapse

TRIM_HORIZON_VALUE =
BigDecimal(-2)
LATEST_VALUE =
BigDecimal(-1)
AT_TIMESTAMP_VALUE =
BigDecimal(-3)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequence_number, sub_sequence_number = 0) ⇒ ExtendedSequenceNumber

Returns a new instance of ExtendedSequenceNumber.

Parameters:

  • sequence_number (String)
  • sub_sequence_number (Number) (defaults to: 0)


55
56
57
58
# File 'lib/kcl/types/extended_sequence_number.rb', line 55

def initialize(sequence_number, sub_sequence_number = 0)
  @sequence_number     = sequence_number
  @sub_sequence_number = sub_sequence_number
end

Instance Attribute Details

#sequence_numberObject (readonly)

Returns the value of attribute sequence_number.



5
6
7
# File 'lib/kcl/types/extended_sequence_number.rb', line 5

def sequence_number
  @sequence_number
end

#sub_sequence_numberObject (readonly)

Returns the value of attribute sub_sequence_number.



5
6
7
# File 'lib/kcl/types/extended_sequence_number.rb', line 5

def sub_sequence_number
  @sub_sequence_number
end

Class Method Details

.digits?(str) ⇒ Boolean

Parameters:

  • str (String)

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/kcl/types/extended_sequence_number.rb', line 48

def self.digits?(str)
  return false if str.nil? || str.empty?
  (str =~ /\A[0-9]+\z/) != nil
end

.digits_or_sentinel?(str) ⇒ Boolean

Parameters:

  • str (String)

Returns:

  • (Boolean)


28
29
30
# File 'lib/kcl/types/extended_sequence_number.rb', line 28

def self.digits_or_sentinel?(str)
  digits?(str) || sentinel?(str)
end

.latestKcl::Types::ExtendedSequenceNumber



12
13
14
# File 'lib/kcl/types/extended_sequence_number.rb', line 12

def self.latest
  @_latest ||= self.new(Kcl::Checkpoints::Sentinel::LATEST)
end

.sentinel?(str) ⇒ Boolean

Parameters:

  • str (String)

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kcl/types/extended_sequence_number.rb', line 34

def self.sentinel?(str)
  case str
  when Kcl::Checkpoints::Sentinel::TRIM_HORIZON,
    Kcl::Checkpoints::Sentinel::LATEST,
    Kcl::Checkpoints::Sentinel::SHARD_END,
    Kcl::Checkpoints::Sentinel::AT_TIMESTAMP
    true
  else
    false
  end
end

.shard_endKcl::Types::ExtendedSequenceNumber



17
18
19
# File 'lib/kcl/types/extended_sequence_number.rb', line 17

def self.shard_end
  @_shard_end ||= self.new(Kcl::Checkpoints::Sentinel::SHARD_END)
end

.trim_horizonKcl::Types::ExtendedSequenceNumber



22
23
24
# File 'lib/kcl/types/extended_sequence_number.rb', line 22

def self.trim_horizon
  @_trim_horizon ||= self.new(Kcl::Checkpoints::Sentinel::TRIM_HORIZON)
end

Instance Method Details

#equals(extended_sequence_number) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/kcl/types/extended_sequence_number.rb', line 82

def equals(extended_sequence_number)
  if @sequence_number != extended_sequence_number.sequence_number
    return false
  end
  @sub_sequence_number == extended_sequence_number.sub_sequence_number
end

#valueBigDecimal

Returns:

  • (BigDecimal)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kcl/types/extended_sequence_number.rb', line 61

def value
  if self.class.digits?(@sequence_number)
    return BigDecimal(@sequence_number)
  end

  case @sequence_number
  when Kcl::Checkpoints::Sentinel::LATEST
    LATEST_VALUE
  when Kcl::Checkpoints::Sentinel::TRIM_HORIZON
    TRIM_HORIZON_VALUE
  when Kcl::Checkpoints::Sentinel::AT_TIMESTAMP
    AT_TIMESTAMP_VALUE
  else
    raise Kcl::Errors::IllegalArgumentError.new(
      'Expected a string of digits, TRIM_HORIZON, LATEST or AT_TIMESTAMP but received ' + @sequence_number
    )
  end
end