Class: CDC::Core::OrderingPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/cdc/core/ordering_policy.rb

Overview

Immutable description of how ordered CDC work should be grouped.

OrderingPolicy defines the vocabulary for ordering guarantees without performing any scheduling itself. Runtime layers can consume the policy to route events into ordered lanes.

Constant Summary collapse

SUPPORTED_POSITIONS =

Position strategies supported by the core contract.

Ractor.make_shareable(
  %i[commit_lsn transaction_id sequence_number occurred_at].freeze
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, position: :commit_lsn, transaction_aware: true) ⇒ OrderingPolicy

Build an ordering policy.

Parameters:

  • scope (#to_sym)

    ordering scope

  • position (#to_sym) (defaults to: :commit_lsn)

    position strategy

  • transaction_aware (Boolean) (defaults to: true)

    whether transaction boundaries matter



26
27
28
29
30
31
# File 'lib/cdc/core/ordering_policy.rb', line 26

def initialize(scope:, position: :commit_lsn, transaction_aware: true)
  @scope = OrderingScope.normalize(scope)
  @position = normalize_position(position)
  @transaction_aware = transaction_aware == true
  Ractor.make_shareable(self)
end

Instance Attribute Details

#positionSymbol, Boolean (readonly)

Returns:

  • (Symbol)

    ordering scope

  • (Symbol)

    position strategy

  • (Boolean)

    whether transaction boundaries should be preserved



19
20
21
# File 'lib/cdc/core/ordering_policy.rb', line 19

def position
  @position
end

#scopeSymbol, Boolean (readonly)

Returns:

  • (Symbol)

    ordering scope

  • (Symbol)

    position strategy

  • (Boolean)

    whether transaction boundaries should be preserved



19
20
21
# File 'lib/cdc/core/ordering_policy.rb', line 19

def scope
  @scope
end

#transaction_awareSymbol, Boolean (readonly)

Returns:

  • (Symbol)

    ordering scope

  • (Symbol)

    position strategy

  • (Boolean)

    whether transaction boundaries should be preserved



19
20
21
# File 'lib/cdc/core/ordering_policy.rb', line 19

def transaction_aware
  @transaction_aware
end

Instance Method Details

#key_for(event) ⇒ OrderingKey?

Derive an ordering key for an event.

Parameters:

Returns:

  • (OrderingKey, nil)

    ordering key for the event, or nil when no key applies



42
43
44
45
46
47
48
49
# File 'lib/cdc/core/ordering_policy.rb', line 42

def key_for(event)
  return nil if scope == OrderingScope::NONE

  components = key_components(event)
  return nil if components.nil?

  OrderingKey.new(scope: scope, components: components)
end

#position_for(event) ⇒ EventPosition

Derive an event position for an event.

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
# File 'lib/cdc/core/ordering_policy.rb', line 55

def position_for(event)
  EventPosition.new(
    strategy: position,
    value: event.public_send(position),
    transaction_id: event.transaction_id,
    sequence_number: event.sequence_number,
    occurred_at: event.occurred_at
  )
end

#to_hHash{String=>Object}

Convert the policy into a Ractor-shareable hash.

Returns:

  • (Hash{String=>Object})

    Ractor-shareable policy representation



68
69
70
71
72
73
74
# File 'lib/cdc/core/ordering_policy.rb', line 68

def to_h
  Ractor.make_shareable({
    'scope' => scope,
    'position' => position,
    'transaction_aware' => transaction_aware
  }.freeze)
end

#transaction_aware?Boolean

Whether transaction boundaries should be preserved.

Returns:

  • (Boolean)

    true when transaction boundaries should be preserved



36
# File 'lib/cdc/core/ordering_policy.rb', line 36

def transaction_aware? = transaction_aware