소스 검색

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.
jvoisin 1 년 전
부모
커밋
99c5bcdb01
1개의 변경된 파일17개의 추가작업 그리고 8개의 파일을 삭제
  1. 17 8
      internal/storage/entry.go

+ 17 - 8
internal/storage/entry.go

@@ -18,6 +18,8 @@ import (
 	"github.com/lib/pq"
 )
 
+const truncationLen = 500000
+
 // CountAllEntries returns the number of entries for each status in the database.
 func (s *Storage) CountAllEntries() map[string]int64 {
 	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,
 			content=$2,
 			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
 			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)
 	}
 
@@ -122,7 +124,7 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
 				$9,
 				$10,
 				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
 			)
 		RETURNING
@@ -130,12 +132,12 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
 	`
 	err := tx.QueryRow(
 		query,
-		entry.Title,
+		truncateString(entry.Title),
 		entry.Hash,
 		entry.URL,
 		entry.CommentsURL,
 		entry.Date,
-		entry.Content,
+		truncateString(entry.Content),
 		entry.Author,
 		entry.UserID,
 		entry.FeedID,
@@ -178,7 +180,7 @@ func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
 			content=$4,
 			author=$5,
 			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
 		WHERE
 			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(
 		query,
-		entry.Title,
+		truncateString(entry.Title),
 		entry.URL,
 		entry.CommentsURL,
-		entry.Content,
+		truncateString(entry.Content),
 		entry.Author,
 		entry.ReadingTime,
 		entry.UserID,
@@ -642,3 +644,10 @@ func removeEmpty(l []string) []string {
 	}
 	return finalSlice
 }
+
+func truncateString(s string) string {
+	if len(s) > truncationLen {
+		return s[:truncationLen]
+	}
+	return s
+}