-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
154 lines (135 loc) · 3.61 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Meilindex - mail indexing and search tool.
* Copyright (C) 2020 Tero Vierimaa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
*/
package indexer
import (
"github.com/emersion/go-mbox"
"github.com/emersion/go-message/mail"
"github.com/sirupsen/logrus"
"io"
"os"
"path/filepath"
"tryffel.net/go/meilindex/config"
"tryffel.net/go/meilindex/external"
)
// ReadFiles reads files and flushes batched mails to flushFunc
func ReadFiles(file string, recursive bool, flushFunc func(mails []*Mail) error) ([]*Mail, error) {
var files []external.MboxFile
var err error
if recursive {
files, err = external.MboxFiles(file, recursive)
if err != nil {
return nil, err
}
if len(files) == 0 {
logrus.Warning("Did not find any suitable Mbox files")
return nil, nil
}
logrus.Infof("Found %d folders", len(files))
} else {
folder, _ := filepath.Abs(file)
files = append(files, external.MboxFile{
File: file,
Name: folder,
})
}
batchSize := config.Conf.File.BatchSize
mails := []*Mail{}
totalMails := 0
// read file and send email batches if batch is full. If smaller, return array of mails.
// Try to always push reasonable batch size, even if single file contains less mails. Not batching
// small files increases indexing time significantly.
for i, v := range files {
logrus.Infof("Indexing (%d / %d): %s", i, len(files), v.Name)
mail, indexed, err := readFile(v.File, v.Name, flushFunc)
if err != nil {
logrus.Error(err)
continue
}
totalMails += indexed
mails = append(mails, mail...)
if len(mails) >= batchSize {
err = flushFunc(mails)
if err != nil {
logrus.Error(err)
}
totalMails += len(mails)
mails = []*Mail{}
}
}
if len(mails) > 0 {
err = flushFunc(mails)
totalMails += len(mails)
}
logrus.Infof("Successfully indexed %d mails from %d folders", totalMails, len(files))
return mails, nil
}
func readFile(file, folder string, flushFunc func(mails []*Mail) error) ([]*Mail, int, error) {
batchSize := 1000
batch := 0
currentBatchSize := 0
totalMails := 0
fd, err := os.Open(file)
if err != nil {
return nil, 0, err
}
reader := mbox.NewReader(fd)
msg, err := reader.NextMessage()
var mails []*Mail
for true {
msg, err = reader.NextMessage()
if err != nil {
if err == io.EOF {
break
}
return mails, 0, err
}
if msg == nil {
break
}
parsed, err := mail.CreateReader(msg)
if err != nil {
logrus.Warningf("(skip) parse mail: %v", err)
} else {
var m *Mail
m, err = mailToMail(parsed)
m.Folder = folder
mails = append(mails, m)
currentBatchSize += 1
if currentBatchSize == batchSize {
batch += 1
totalMails += currentBatchSize
err := flushFunc(mails)
if err != nil {
logrus.Errorf("Flush email batch: %v", err)
}
mails = []*Mail{}
currentBatchSize = 0
}
}
}
err = fd.Close()
if err != nil {
logrus.Errorf("close file: %v", err)
}
if batch > 0 {
logrus.Infof("Flushed %d batches, %d mails", batch, totalMails)
}
return mails, totalMails, nil
}