Class: Imap::Backup::Serializer::Mbox

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/serializer/mbox.rb

Overview

Stores messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files_path:) ⇒ Mbox



14
15
16
17
# File 'lib/imap/backup/serializer/mbox.rb', line 14

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

Instance Attribute Details

#files_path ⇒ String (readonly)



11
12
13
# File 'lib/imap/backup/serializer/mbox.rb', line 11

def files_path
  @files_path
end

Instance Method Details

#append(message) ⇒ void

This method returns an undefined value.

Serializes a message



53
54
55
56
57
# File 'lib/imap/backup/serializer/mbox.rb', line 53

def append(message)
  File.open(pathname, "ab") do |file|
    file.write message
  end
end

#delete ⇒ void

This method returns an undefined value.

Deletes the mailbox



72
73
74
75
76
77
# File 'lib/imap/backup/serializer/mbox.rb', line 72

def delete
  return if !exist?

  Logger.logger.info("Deleting mailbox '#{pathname}'")
  FileUtils.rm(pathname)
end

#exist? ⇒ Boolean



79
80
81
# File 'lib/imap/backup/serializer/mbox.rb', line 79

def exist?
  File.exist?(pathname)
end

#length ⇒ Integer



84
85
86
87
88
# File 'lib/imap/backup/serializer/mbox.rb', line 84

def length
  return nil if !exist?

  File.stat(pathname).size
end

#pathname ⇒ String



91
92
93
# File 'lib/imap/backup/serializer/mbox.rb', line 91

def pathname
  "#{files_path}.mbox"
end

#read(offset, length) ⇒ String

Reads a message from disk



63
64
65
66
67
68
# File 'lib/imap/backup/serializer/mbox.rb', line 63

def read(offset, length)
  File.open(pathname, "rb") do |f|
    f.seek offset
    f.read length
  end
end

#rename(new_path) ⇒ void

This method returns an undefined value.

Renames the mailbox, if it exists, otherwise, simply stores the new name



99
100
101
102
103
104
105
106
107
# File 'lib/imap/backup/serializer/mbox.rb', line 99

def rename(new_path)
  if exist?
    old_pathname = pathname
    @files_path = new_path
    File.rename(old_pathname, pathname)
  else
    @files_path = new_path
  end
end

#rollback ⇒ void

This method returns an undefined value.

Returns to the pre-transaction state



40
41
42
43
44
# File 'lib/imap/backup/serializer/mbox.rb', line 40

def rollback
  tsx.fail_outside_transaction!(:rollback)

  rewind(tsx.data[:savepoint][:length])
end

#touch ⇒ void

This method returns an undefined value.

Sets the mailbox file's updated time to the current time



111
112
113
# File 'lib/imap/backup/serializer/mbox.rb', line 111

def touch
  FileUtils.touch(pathname)
end

#transaction(&block) ⇒ void

This method returns an undefined value.

Starts a transaction

Raises:

  • re-raises errors which occur in the block



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/imap/backup/serializer/mbox.rb', line 23

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

  tsx.begin({savepoint: {length: length}}) do
    block.call
  rescue StandardError => e
    rollback
    raise e
  rescue SignalException => e
    Logger.logger.error "#{self.class} handling #{e.class}"
    rollback
    raise e
  end
end

#valid? ⇒ Boolean



46
47
48
# File 'lib/imap/backup/serializer/mbox.rb', line 46

def valid?
  exist?
end