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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_id:, events:, commit_lsn: nil, committed_at: nil, 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: {})

    transaction metadata



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

def initialize(transaction_id:, events:, commit_lsn: nil, committed_at: nil, 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_lsnObject, ... (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



16
17
18
# File 'lib/cdc/core/transaction_envelope.rb', line 16

def commit_lsn
  @commit_lsn
end

#committed_atObject, ... (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



16
17
18
# File 'lib/cdc/core/transaction_envelope.rb', line 16

def committed_at
  @committed_at
end

#eventsObject, ... (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



16
17
18
# File 'lib/cdc/core/transaction_envelope.rb', line 16

def events
  @events
end

#metadataObject, ... (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



16
17
18
# File 'lib/cdc/core/transaction_envelope.rb', line 16

def 
  @metadata
end

#transaction_idObject, ... (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



16
17
18
# File 'lib/cdc/core/transaction_envelope.rb', line 16

def transaction_id
  @transaction_id
end

Instance Method Details

#empty?Boolean

Whether the envelope has no events.

Returns:

  • (Boolean)


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

def empty? = events.empty?

#sizeInteger

Number of events in the envelope.

Returns:

  • (Integer)


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

def size = events.size

#to_hHash{String=>Object,nil}

Convert the transaction envelope into a Ractor-shareable hash.

Returns:

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


47
48
49
50
51
52
53
54
55
# File 'lib/cdc/core/transaction_envelope.rb', line 47

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