aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Magorsch <arzano@gentoo.org>2020-06-22 19:32:48 +0200
committerMax Magorsch <arzano@gentoo.org>2020-06-22 19:32:48 +0200
commit0f5a1f528fe4f5453f315564b448cfb1f9fea711 (patch)
tree32d8e114de3681499bac8eaba3212be5ba81ceaf /pkg/importer/importer.go
parentRework the data model to improve the performance (diff)
downloadarchives-0f5a1f528fe4f5453f315564b448cfb1f9fea711.tar.gz
archives-0f5a1f528fe4f5453f315564b448cfb1f9fea711.tar.bz2
archives-0f5a1f528fe4f5453f315564b448cfb1f9fea711.zip
Improve the performance of the importer
Signed-off-by: Max Magorsch <arzano@gentoo.org>
Diffstat (limited to 'pkg/importer/importer.go')
-rw-r--r--pkg/importer/importer.go80
1 files changed, 77 insertions, 3 deletions
diff --git a/pkg/importer/importer.go b/pkg/importer/importer.go
index cdb278d..76ba8e7 100644
--- a/pkg/importer/importer.go
+++ b/pkg/importer/importer.go
@@ -2,14 +2,88 @@ package importer
import (
"archives/pkg/config"
+ "archives/pkg/database"
+ "archives/pkg/models"
"fmt"
+ "os"
"path/filepath"
+ "strconv"
+ "strings"
+ "time"
)
+var overAllcounter int
+var importedCounter int
+var startTime time.Time
+
+
func FullImport() {
+
+ fmt.Println("Init import...")
+ filepath.Walk(config.MailDirPath(), initImport)
+
+ overAllcounter = 0
+ importedCounter = 0
+ startTime = time.Now()
+ filepath.Walk(config.MailDirPath(), func(path string, info os.FileInfo, err error) error {
+ if overAllcounter % 1000 == 0 {
+ fmt.Println(strconv.Itoa(overAllcounter) + ": " + time.Now().Sub(startTime).String())
+ }
+ overAllcounter++
+ if err != nil {
+ return err
+ }
+ if !info.IsDir() && getDepth(path, config.MailDirPath()) >= 1 && isPublicList(path) {
+ importedCounter++
+ importMail(path, info.Name())
+ }
+ return nil
+ })
+
+ fmt.Println("Finished full import. Imported " + strconv.Itoa(importedCounter) + " messages.")
+}
+
+func IncrementalImport() {
+ var messages []*models.Message
+ err := database.DBCon.Model(&messages).
+ Column("filename").
+ Select()
+
+ if err != nil {
+ fmt.Println("Problem during import, aborting:")
+ fmt.Println(err)
+ return
+ }
+
fmt.Println("Init import...")
filepath.Walk(config.MailDirPath(), initImport)
- fmt.Println("Start import...")
- filepath.Walk(config.MailDirPath(), importMail)
- fmt.Println("Finished import.")
+
+ overAllcounter = 0
+ importedCounter = 0
+ startTime = time.Now()
+ filepath.Walk(config.MailDirPath(), func(path string, info os.FileInfo, err error) error {
+ if overAllcounter % 1000 == 0 {
+ fmt.Println(strconv.Itoa(overAllcounter) + ": " + time.Now().Sub(startTime).String())
+ }
+ overAllcounter++
+ if err != nil {
+ return err
+ }
+ if !info.IsDir() && getDepth(path, config.MailDirPath()) >= 1 && isPublicList(path) && !fileIsAlreadyPresent(path, messages) {
+ importedCounter++
+ importMail(path, info.Name())
+ }
+ return nil
+ })
+
+ fmt.Println("Finished incremental import. Imported " + strconv.Itoa(importedCounter) + " new messages.")
}
+
+func fileIsAlreadyPresent(path string, messages []*models.Message) bool {
+ for _, message := range messages {
+ if strings.Contains(strings.TrimRight(path, ",S"), strings.TrimRight(message.Filename, ",S")){
+ return true
+ }
+ }
+ return false
+} \ No newline at end of file