Fixing stupid memory leak caused by excessive pointer dereference.

This commit is contained in:
Alexey Skobkin 2019-10-19 04:07:24 +03:00
parent 64bd20e0bf
commit 3e601ea2cb
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
2 changed files with 10 additions and 11 deletions

View file

@ -11,13 +11,12 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
"net/url" "net/url"
"time"
) )
var ( var (
goDatabaseUrl = kingpin.Flag("go-database", "magneticod Go version database URL.").Short('g').String() goDatabaseUrl = kingpin.Flag("go-database", "magneticod Go version database URL.").Short('g').String()
pyDatabaseUrl = kingpin.Flag("py-database", "Python version database URL.").Short('p').Required().String() pyDatabaseUrl = kingpin.Flag("py-database", "Python version database URL.").Short('p').Required().String()
chunkSize = kingpin.Flag("chunk-size", "How many torrents to retrieve from old DB at once").Short('c').Default("50").Uint64() chunkSize = kingpin.Flag("chunk-size", "How many torrents to retrieve from old DB at once").Short('c').Default("200").Uint64()
progress pb.ProgressBar progress pb.ProgressBar
) )
@ -46,16 +45,15 @@ func main() {
} }
defer pyDatabase.Close() defer pyDatabase.Close()
torrentsTotal, err := old.GetNumberOfTorrents(*pyDatabase) torrentsTotal, err := old.GetNumberOfTorrents(pyDatabase)
if err != nil { if err != nil {
zap.L().Panic("Couldn't count torrents", zap.Error(err)) zap.L().Panic("Couldn't count torrents", zap.Error(err))
} }
progress = *pb.Full.New(int(torrentsTotal)) progress = *pb.Full.New(int(torrentsTotal))
progress.SetRefreshRate(time.Second)
progress.Start() progress.Start()
err = mergeDatabases(*pyDatabase, goDatabase) err = mergeDatabases(pyDatabase, goDatabase)
if err != nil { if err != nil {
logger.Error("Error while processing data", zap.Error(err)) logger.Error("Error while processing data", zap.Error(err))
} }
@ -63,7 +61,7 @@ func main() {
progress.Finish() progress.Finish()
} }
func mergeDatabases(pyDb sql.DB, goDb persistence.Database) error { func mergeDatabases(pyDb *sql.DB, goDb persistence.Database) error {
maxId, err := old.GetLastTorrentId(pyDb) maxId, err := old.GetLastTorrentId(pyDb)
if err != nil { if err != nil {
zap.L().Fatal("Error while getting last torrent ID from old database") zap.L().Fatal("Error while getting last torrent ID from old database")

View file

@ -16,10 +16,11 @@ type TorrentData struct {
func OpenSqliteDb(url_ url.URL) (*sql.DB, error) { func OpenSqliteDb(url_ url.URL) (*sql.DB, error) {
url_.Scheme = "" url_.Scheme = ""
return sql.Open("sqlite3", url_.String()) return sql.Open("sqlite3", url_.String())
} }
func GetNumberOfTorrents(db sql.DB) (uint, error) { func GetNumberOfTorrents(db *sql.DB) (uint, error) {
rows, err := db.Query("SELECT COUNT(id) FROM torrents;") rows, err := db.Query("SELECT COUNT(id) FROM torrents;")
if err != nil { if err != nil {
return 0, err return 0, err
@ -43,7 +44,7 @@ func GetNumberOfTorrents(db sql.DB) (uint, error) {
} }
} }
func GetLastTorrentId(db sql.DB) (uint64, error) { func GetLastTorrentId(db *sql.DB) (uint64, error) {
rows, err := db.Query("SELECT MAX(id) FROM torrents;") rows, err := db.Query("SELECT MAX(id) FROM torrents;")
if err != nil { if err != nil {
return 0, err return 0, err
@ -67,8 +68,8 @@ func GetLastTorrentId(db sql.DB) (uint64, error) {
} }
} }
func GetTorrentsChunk(db sql.DB, fromId uint64, size uint64) ([]TorrentData, error) { func GetTorrentsChunk(db *sql.DB, fromId uint64, size uint64) ([]TorrentData, error) {
chunk := make([]TorrentData, 0) chunk := make([]TorrentData, 0, size)
// Selecting torrents // Selecting torrents
tRows, err := db.Query(` tRows, err := db.Query(`
@ -106,7 +107,7 @@ func GetTorrentsChunk(db sql.DB, fromId uint64, size uint64) ([]TorrentData, err
return chunk, nil return chunk, nil
} }
func GetFilesForTorrent(pyDb sql.DB, torrent persistence.TorrentMetadata) ([]persistence.File, error) { func GetFilesForTorrent(pyDb *sql.DB, torrent persistence.TorrentMetadata) ([]persistence.File, error) {
files := make([]persistence.File, 0) files := make([]persistence.File, 0)
// Selecting files for torrent // Selecting files for torrent