Dumb and inefficient version implemented.

This commit is contained in:
Alexey Skobkin 2019-10-09 01:36:11 +03:00
parent 70c2bb5b1f
commit f509f20bcc
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7

View file

@ -2,8 +2,10 @@ package main
import (
"database/sql"
"encoding/hex"
"fmt"
"github.com/boramalper/magnetico/pkg/persistence"
"github.com/cheggaaa/pb/v3"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/zap"
"gopkg.in/alecthomas/kingpin.v2"
@ -46,14 +48,71 @@ func main() {
}
func process(pyDb sql.DB, goDb persistence.Database) error {
//torrentsTotal, err := getNumberOfTorrents(pyDb)
//if err != nil {
// zap.L().Panic("Couldn't count torrents", zap.Error(err))
//}
//
//bar := pb.StartNew(int(torrentsTotal))
torrentsTotal, err := getNumberOfTorrents(pyDb)
if err != nil {
zap.L().Panic("Couldn't count torrents", zap.Error(err))
}
// TODO
bar := pb.New(int(torrentsTotal))
//bar.SetRefreshRate(time.Second)
bar.Start()
// Selecting torrents
tRows, err := pyDb.Query("SELECT id, info_hash, name, total_size, discovered_on FROM torrents ORDER BY id ASC;")
if err != nil {
zap.L().Fatal("Error when querying torrents from old database.")
}
defer tRows.Close()
torrentLoop:
for tRows.Next() {
var torrent persistence.TorrentMetadata
bar.Increment()
err := tRows.Scan(&torrent.ID, &torrent.InfoHash, &torrent.Name, &torrent.Size, &torrent.DiscoveredOn)
if err != nil {
zap.L().Error("Error scanning torrent row.", zap.Error(err))
continue
}
// Selecting files for torrent
fRows, err := pyDb.Query("SELECT f.path, f.size FROM files AS f WHERE f.torrent_id = ? ORDER BY f.id ASC;", torrent.ID)
if err != nil {
zap.L().Fatal("Error when querying files for torrent.", zap.Uint64("torrent_id", torrent.ID))
}
defer fRows.Close()
files := make([]persistence.File, 0)
for fRows.Next() {
var file persistence.File
err := fRows.Scan(&file.Path, &file.Size)
fRows.Close()
if err != nil {
zap.L().Error("Error scanning file row", zap.Uint64("torrent_id", torrent.ID), zap.Error(err))
continue torrentLoop
}
files = append(files, file)
}
exists, err := goDb.DoesTorrentExist(torrent.InfoHash)
if err != nil {
zap.L().Error("Error checking torrent existence.", zap.String("hash", hex.EncodeToString(torrent.InfoHash)), zap.Error(err))
continue
}
if !exists {
err = goDb.AddNewTorrent(torrent.InfoHash, torrent.Name, files)
if err != nil {
zap.L().Error("Error adding torrent to Go database.", zap.String("hash", hex.EncodeToString(torrent.InfoHash)), zap.Error(err))
}
}
}
bar.Finish()
return nil
}