Class: CDC::Core::TransactionEnvelope

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

Overview

Immutable group of change events committed in one transaction.

TransactionEnvelope is useful when a downstream processor needs transaction boundaries instead of isolated row-level events. The contained events and metadata are Ractor-shareable when construction succeeds.

Constant Summary collapse

EMPTY_METADATA =
Ractor.make_shareable(
  {} # : Hash[untyped, untyped]
    .freeze
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_id:, events:, commit_lsn: nil, committed_at: nil, metadata: EMPTY_METADATA) ⇒ TransactionEnvelope

Build a transaction envelope.

Parameters:

  • transaction_id (Object) —

    upstream transaction identifier

  • events (Array<ChangeEvent>) —

    committed events

  • commit_lsn (#to_s, nil) (defaults to: nil) —

    commit log sequence number

  • committed_at (Time, nil) (defaults to: nil) —

    commit timestamp

  • metadata (Hash, EventMetadata) (defaults to: EMPTY_METADATA) —

    transaction metadata



30
31
32
33
34
35
36
37
# File 'lib/cdc/core/transaction_envelope.rb', line 30

def initialize(transaction_id:, events:, commit_lsn: nil, committed_at: nil, metadata: EMPTY_METADATA)
  @transaction_id = transaction_id
  @events = Ractor.make_shareable(events.freeze)
  @commit_lsn = commit_lsn&.to_s&.freeze
  @committed_at = committed_at
  @metadata = .is_a?(EventMetadata) ?  : EventMetadata.new()
  Ractor.make_shareable(self)
end

Instance Attribute Details

#commit_lsn ⇒ Object, ... (readonly)

Returns:

  • (Object) —

    transaction identifier

  • (Array<ChangeEvent>) —

    events committed by the transaction

  • (String, nil) —

    commit log sequence number

  • (Time, nil) —

    commit timestamp

  • (EventMetadata) —

    transaction metadata



21
22
23
# File 'lib/cdc/core/transaction_envelope.rb', line 21

def commit_lsn
  @commit_lsn
end

#committed_at ⇒ Object, ... (readonly)

Returns:

  • (Object) —

    transaction identifier

  • (Array<ChangeEvent>) —

    events committed by the transaction

  • (String, nil) —

    commit log sequence number

  • (Time, nil) —

    commit timestamp

  • (EventMetadata) —

    transaction metadata



21
22
23
# File 'lib/cdc/core/transaction_envelope.rb', line 21

def committed_at
  @committed_at
end

#events ⇒ Object, ... (readonly)

Returns:

  • (Object) —

    transaction identifier

  • (Array<ChangeEvent>) —

    events committed by the transaction

  • (String, nil) —

    commit log sequence number

  • (Time, nil) —

    commit timestamp

  • (EventMetadata) —

    transaction metadata



21
22
23
# File 'lib/cdc/core/transaction_envelope.rb', line 21

def events
  @events
end

#metadata ⇒ Object, ... (readonly)

Returns:

  • (Object) —

    transaction identifier

  • (Array<ChangeEvent>) —

    events committed by the transaction

  • (String, nil) —

    commit log sequence number

  • (Time, nil) —

    commit timestamp

  • (EventMetadata) —

    transaction metadata



21
22
23
# File 'lib/cdc/core/transaction_envelope.rb', line 21

def 
  @metadata
end

#transaction_id ⇒ Object, ... (readonly)

Returns:

  • (Object) —

    transaction identifier

  • (Array<ChangeEvent>) —

    events committed by the transaction

  • (String, nil) —

    commit log sequence number

  • (Time, nil) —

    commit timestamp

  • (EventMetadata) —

    transaction metadata



21
22
23
# File 'lib/cdc/core/transaction_envelope.rb', line 21

def transaction_id
  @transaction_id
end

Instance Method Details

#empty? ⇒ Boolean

Whether the envelope has no events.

Returns:

  • (Boolean) —

    true when the envelope has no events



42
# File 'lib/cdc/core/transaction_envelope.rb', line 42

def empty? = events.empty?

#size ⇒ Integer

Number of events in the envelope.

Returns:

  • (Integer) —

    number of events in the envelope



47
# File 'lib/cdc/core/transaction_envelope.rb', line 47

def size = events.size

#to_h ⇒ Hash{String=>Object,nil}

Convert the transaction envelope into a Ractor-shareable hash.

Returns:

  • (Hash{String=>Object,nil}) —

    Ractor-shareable transaction representation



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

def to_h
  Ractor.make_shareable({
    'transaction_id' => transaction_id,
    'events' => events.map(&:to_h).freeze,
    'commit_lsn' => commit_lsn,
    'committed_at' => committed_at,
    'metadata' => .to_h
  }.freeze)
end