1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

perf(storage): truncate strings on go's side instead of pgsql's

There is no need to send the whole title and content to have them truncated on
postgresql's side when we can do this client-side. This should save some
memory on the database's side, as well as some bandwidth
if it's located on another server. And it makes the SQL queries a tad more
readable as well.
This commit is contained in:
jvoisin 2025-06-21 01:04:37 +02:00
parent 93b17af78b
commit 863e6946e2

View file

@ -78,12 +78,12 @@ func (s *Storage) UpdateEntryTitleAndContent(entry *model.Entry) error {
title=$1, title=$1,
content=$2, content=$2,
reading_time=$3, reading_time=$3,
document_vectors = setweight(to_tsvector(left(coalesce($1, ''), 500000)), 'A') || setweight(to_tsvector(left(coalesce($2, ''), 500000)), 'B') document_vectors = setweight(to_tsvector($1), 'A') || setweight(to_tsvector($2), 'B')
WHERE WHERE
id=$4 AND user_id=$5 id=$4 AND user_id=$5
` `
if _, err := s.db.Exec(query, entry.Title, entry.Content, entry.ReadingTime, entry.ID, entry.UserID); err != nil { if _, err := s.db.Exec(query, truncateString(entry.Title, 500000), truncateString(entry.Content, 500000), entry.ReadingTime, entry.ID, entry.UserID); err != nil {
return fmt.Errorf(`store: unable to update entry #%d: %v`, entry.ID, err) return fmt.Errorf(`store: unable to update entry #%d: %v`, entry.ID, err)
} }
@ -122,7 +122,7 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
$9, $9,
$10, $10,
now(), now(),
setweight(to_tsvector(left(coalesce($1, ''), 500000)), 'A') || setweight(to_tsvector(left(coalesce($6, ''), 500000)), 'B'), setweight(to_tsvector($1), 'A') || setweight(to_tsvector($6), 'B'),
$11 $11
) )
RETURNING RETURNING
@ -130,12 +130,12 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
` `
err := tx.QueryRow( err := tx.QueryRow(
query, query,
entry.Title, truncateString(entry.Title, 500000),
entry.Hash, entry.Hash,
entry.URL, entry.URL,
entry.CommentsURL, entry.CommentsURL,
entry.Date, entry.Date,
entry.Content, truncateString(entry.Content, 500000),
entry.Author, entry.Author,
entry.UserID, entry.UserID,
entry.FeedID, entry.FeedID,
@ -178,7 +178,7 @@ func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
content=$4, content=$4,
author=$5, author=$5,
reading_time=$6, reading_time=$6,
document_vectors = setweight(to_tsvector(left(coalesce($1, ''), 500000)), 'A') || setweight(to_tsvector(left(coalesce($4, ''), 500000)), 'B'), document_vectors = setweight(to_tsvector($1), 'A') || setweight(to_tsvector($4), 'B'),
tags=$10 tags=$10
WHERE WHERE
user_id=$7 AND feed_id=$8 AND hash=$9 user_id=$7 AND feed_id=$8 AND hash=$9
@ -187,10 +187,10 @@ func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
` `
err := tx.QueryRow( err := tx.QueryRow(
query, query,
entry.Title, truncateString(entry.Title, 500000),
entry.URL, entry.URL,
entry.CommentsURL, entry.CommentsURL,
entry.Content, truncateString(entry.Content, 500000),
entry.Author, entry.Author,
entry.ReadingTime, entry.ReadingTime,
entry.UserID, entry.UserID,
@ -642,3 +642,10 @@ func removeEmpty(l []string) []string {
} }
return finalSlice return finalSlice
} }
func truncateString(s string, maxlen int) string {
if len(s) > maxlen {
return s[:maxlen]
}
return s
}