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

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

Overview

Stores message metadata

Constant Summary collapse

CURRENT_VERSION =

The version number to store in the metadata file

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path) ⇒ Imap

Returns a new instance of Imap.

Parameters:

  • folder_path (String)

    The path of the imap metadata file, without the ‘.imap’ extension



18
19
20
21
22
23
24
25
# File 'lib/imap/backup/serializer/imap.rb', line 18

def initialize(folder_path)
  @folder_path = folder_path
  @loaded = false
  @uid_validity = nil
  @messages = nil
  @version = nil
  @tsx = nil
end

Instance Attribute Details

#folder_pathObject (readonly)

Returns the value of attribute folder_path.



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

def folder_path
  @folder_path
end

Instance Method Details

#append(uid, length, flags: []) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/imap/backup/serializer/imap.rb', line 69

def append(uid, length, flags: [])
  offset =
    if messages.empty?
      0
    else
      last_message = messages[-1]
      last_message.offset + last_message.length
    end

  messages << Serializer::Message.new(
    uid: uid, offset: offset, length: length, mbox: mbox, flags: flags
  )

  save
end

#deleteObject



89
90
91
92
93
94
95
96
97
# File 'lib/imap/backup/serializer/imap.rb', line 89

def delete
  return if !exist?

  FileUtils.rm(pathname)
  @loaded = false
  @messages = nil
  @uid_validity = nil
  @version = nil
end

#exist?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/imap/backup/serializer/imap.rb', line 57

def exist?
  File.exist?(pathname)
end

#get(uid) ⇒ Object



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

def get(uid)
  messages.find { |m| m.uid == uid }
end

#messagesObject

Make private



121
122
123
124
# File 'lib/imap/backup/serializer/imap.rb', line 121

def messages
  ensure_loaded
  @messages
end

#pathnameObject



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

def pathname
  "#{folder_path}.imap"
end

#rename(new_path) ⇒ Object



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

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

#rollbackObject



44
45
46
47
48
49
50
51
# File 'lib/imap/backup/serializer/imap.rb', line 44

def rollback
  tsx.fail_outside_transaction!(:rollback)

  @messages = tsx.data[:savepoint][:messages]
  @uid_validity = tsx.data[:savepoint][:uid_validity]

  tsx.clear
end

#saveObject



144
145
146
147
148
149
150
# File 'lib/imap/backup/serializer/imap.rb', line 144

def save
  return if tsx.in_transaction?

  ensure_loaded

  save_internal(version: version, uid_validity: uid_validity, messages: messages)
end

#transaction(&block) ⇒ Object



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

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

  ensure_loaded
  # rubocop:disable Lint/RescueException
  tsx.begin({savepoint: {messages: messages.dup, uid_validity: uid_validity}}) do
    block.call

    save_internal(version: version, uid_validity: uid_validity, messages: messages) if tsx.data
  rescue Exception => e
    Logger.logger.error "#{self.class} handling #{e.class}"
    rollback
    raise e
  end
  # rubocop:enable Lint/RescueException
end

#uid_validityObject



109
110
111
112
# File 'lib/imap/backup/serializer/imap.rb', line 109

def uid_validity
  ensure_loaded
  @uid_validity
end

#uid_validity=(value) ⇒ Object



114
115
116
117
118
# File 'lib/imap/backup/serializer/imap.rb', line 114

def uid_validity=(value)
  ensure_loaded
  @uid_validity = value
  save
end

#uidsObject

Deprecated



127
128
129
# File 'lib/imap/backup/serializer/imap.rb', line 127

def uids
  messages.map(&:uid)
end

#update_uid(old, new) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/imap/backup/serializer/imap.rb', line 131

def update_uid(old, new)
  index = messages.find_index { |m| m.uid == old }
  return if index.nil?

  messages[index].uid = new
  save
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  return false if !exist?
  return false if version != CURRENT_VERSION
  return false if !uid_validity

  true
end

#versionObject



139
140
141
142
# File 'lib/imap/backup/serializer/imap.rb', line 139

def version
  ensure_loaded
  @version
end