optimization memory

This commit is contained in:
YouROK
2023-02-03 12:44:48 +03:00
parent aeb85d965e
commit cc61b723eb

View File

@@ -1,7 +1,6 @@
package rutor package rutor
import ( import (
"bytes"
"compress/flate" "compress/flate"
"encoding/json" "encoding/json"
"github.com/agnivade/levenshtein" "github.com/agnivade/levenshtein"
@@ -100,24 +99,50 @@ func updateDB() {
func loadDB() { func loadDB() {
log.TLogln("Load rutor db") log.TLogln("Load rutor db")
buf, err := os.ReadFile(filepath.Join(settings.Path, "rutor.ls")) ff, err := os.Open(filepath.Join(settings.Path, "rutor.ls"))
if err == nil { if err == nil {
r := flate.NewReader(bytes.NewReader(buf)) defer ff.Close()
buf, err = io.ReadAll(r) r := flate.NewReader(ff)
r.Close() defer r.Close()
var ftorrs []*models.TorrentDetails
dec := json.NewDecoder(r)
_, err := dec.Token()
if err != nil {
log.TLogln("Error read token rutor db:", err)
return
}
var channel = make(chan *models.TorrentDetails, 0)
go func() {
for torr := range channel {
ftorrs = append(ftorrs, torr)
}
torrs = ftorrs
}()
for dec.More() {
var torr *models.TorrentDetails
err = dec.Decode(&torr)
if err == nil { if err == nil {
var ftors []*models.TorrentDetails channel <- torr
err = json.Unmarshal(buf, &ftors) } else {
if err == nil { log.TLogln("Error read rutor db:", err)
torrs = ftors }
}
close(channel)
log.TLogln("Index rutor db") log.TLogln("Index rutor db")
torrsearch.NewIndex(torrs) torrsearch.NewIndex(torrs)
} else {
log.TLogln("Error read rutor db:", err) //err = dec.Decode(&ftorrs)
} //if err == nil {
} else { // torrs = ftorrs
log.TLogln("Error read rutor db:", err) // log.TLogln("Index rutor db")
} // torrsearch.NewIndex(torrs)
//} else {
// log.TLogln("Error read rutor db:", err)
//}
} else { } else {
log.TLogln("Error load rutor db:", err) log.TLogln("Error load rutor db:", err)
} }