1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-06 17:41: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 committed by Frédéric Guillot
parent aed99e65c1
commit 99c5bcdb01

View file

@ -18,6 +18,8 @@ import (
"github.com/lib/pq" "github.com/lib/pq"
) )
const truncationLen = 500000
// CountAllEntries returns the number of entries for each status in the database. // CountAllEntries returns the number of entries for each status in the database.
func (s *Storage) CountAllEntries() map[string]int64 { func (s *Storage) CountAllEntries() map[string]int64 {
rows, err := s.db.Query(`SELECT status, count(*) FROM entries GROUP BY status`) rows, err := s.db.Query(`SELECT status, count(*) FROM entries GROUP BY status`)
@ -78,12 +80,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), truncateString(entry.Content), 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 +124,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 +132,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),
entry.Hash, entry.Hash,
entry.URL, entry.URL,
entry.CommentsURL, entry.CommentsURL,
entry.Date, entry.Date,
entry.Content, truncateString(entry.Content),
entry.Author, entry.Author,
entry.UserID, entry.UserID,
entry.FeedID, entry.FeedID,
@ -178,7 +180,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 +189,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),
entry.URL, entry.URL,
entry.CommentsURL, entry.CommentsURL,
entry.Content, truncateString(entry.Content),
entry.Author, entry.Author,
entry.ReadingTime, entry.ReadingTime,
entry.UserID, entry.UserID,
@ -642,3 +644,10 @@ func removeEmpty(l []string) []string {
} }
return finalSlice return finalSlice
} }
func truncateString(s string) string {
if len(s) > truncationLen {
return s[:truncationLen]
}
return s
}