Class: Imap::Backup::Serializer::DelayedMetadataSerializer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/imap/backup/serializer/delayed_metadata_serializer.rb

Overview

Wraps the Serializer, delaying metadata changes

Instance Method Summary collapse

Constructor Details

#initialize(serializer:) ⇒ DelayedMetadataSerializer

Returns a new instance of DelayedMetadataSerializer.

Parameters:

  • serializer (Serializer)

    the serializer for a folder



20
21
22
23
# File 'lib/imap/backup/serializer/delayed_metadata_serializer.rb', line 20

def initialize(serializer:)
  @serializer = serializer
  @tsx = nil
end

Instance Method Details

#append(uid, message, flags) ⇒ void

This method returns an undefined value.

Appends a message to the mbox file and adds the appended message’s metadata to the transaction

Parameters:

  • uid (Integer)

    the UID of the message

  • message (String)

    the message

  • flags (Array<Symbol>)

    the flags for the message



66
67
68
69
70
71
72
# File 'lib/imap/backup/serializer/delayed_metadata_serializer.rb', line 66

def append(uid, message, flags)
  tsx.fail_outside_transaction!(:append)
  mboxrd_message = Email::Mboxrd::Message.new(message)
  serialized = mboxrd_message.to_serialized
  tsx.data[:appends] << {uid: uid, length: serialized.bytesize, flags: flags}
  mbox.append(serialized)
end

#apply_uid_validity(uid_validity) ⇒ void

This method returns an undefined value.

Sets the folder’s UID validity via the serializer

Parameters:

  • uid_validity (Integer)

    the UID validity to apply

Raises:

  • (RuntimeError)

    if called inside a transaction



50
51
52
53
54
55
56
57
# File 'lib/imap/backup/serializer/delayed_metadata_serializer.rb', line 50

def apply_uid_validity(uid_validity)
  tsx.fail_in_transaction!(
    :transaction,
    message: "UID validity cannot be changed in a transaction"
  )

  serializer.apply_uid_validity(uid_validity)
end

#transaction(&block) ⇒ void

This method returns an undefined value.

Initializes the metadata and mailbox transactions, then calls the supplied block. Once the block has finished, commits changes to metadata

Parameters:

  • block (block)

    the block that is wrapped by the transaction

Raises:

  • any error ocurring during the commit phase



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/imap/backup/serializer/delayed_metadata_serializer.rb', line 31

def transaction(&block)
  tsx.fail_in_transaction!(:transaction, message: "nested transactions are not supported")

  tsx.begin({appends: [], updates: []}) do
    mbox.transaction do
      block.call

      commit

      serializer.reload
    end
  end
end

#update(uid, length: nil, flags: nil) ⇒ void

This method returns an undefined value.

Stores changes to a message’s metadata for later update

Parameters:

  • uid (Integer)

    the UID of the message

  • length (Integer) (defaults to: nil)

    the length of the message

  • flags (Array<Symbol>) (defaults to: nil)

    the flags for the message



80
81
82
83
# File 'lib/imap/backup/serializer/delayed_metadata_serializer.rb', line 80

def update(uid, length: nil, flags: nil)
  tsx.fail_outside_transaction!(:update)
  tsx.data[:updates] << {uid: uid, length: length, flags: flags}
end