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:



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

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

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

#position_for(event) ⇒ EventPosition

Derive an event position for an event.

Parameters:

Returns:



52
53
54
55
56
57
58
59
60
# File 'lib/cdc/core/ordering_policy.rb', line 52

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})


65
66
67
68
69
70
71
# File 'lib/cdc/core/ordering_policy.rb', line 65

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)


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

def transaction_aware? = transaction_aware