Procházet zdrojové kódy

perf(storage): factorize away a query

In InsertEntryForFeed, there is no need to check if an entry exists and then
get its hash. Instead, try to get its hash, and consider the ErrNoRows error as
an existence check.
jvoisin před 2 měsíci
rodič
revize
5f3049d1ce
1 změnil soubory, kde provedl 7 přidání a 7 odebrání
  1. 7 7
      internal/storage/entry.go

+ 7 - 7
internal/storage/entry.go

@@ -245,6 +245,9 @@ func (s *Storage) getEntryIDByHash(tx *sql.Tx, feedID int64, entryHash string) (
 		entryHash,
 	).Scan(&entryID)
 
+	if err == sql.ErrNoRows {
+		return 0, nil
+	}
 	if err != nil {
 		return 0, fmt.Errorf(`store: unable to fetch entry ID: %v`, err)
 	}
@@ -264,16 +267,13 @@ func (s *Storage) InsertEntryForFeed(userID, feedID int64, entry *model.Entry) (
 	}
 	defer tx.Rollback()
 
-	exists, err := s.entryExists(tx, entry)
+	entryID, err := s.getEntryIDByHash(tx, entry.FeedID, entry.Hash)
 	if err != nil {
 		return false, err
 	}
+	alreadyExistingEntry := entryID > 0
 
-	if exists {
-		entryID, err := s.getEntryIDByHash(tx, entry.FeedID, entry.Hash)
-		if err != nil {
-			return false, err
-		}
+	if alreadyExistingEntry {
 		entry.ID = entryID
 	} else {
 		if err := s.createEntry(tx, entry); err != nil {
@@ -285,7 +285,7 @@ func (s *Storage) InsertEntryForFeed(userID, feedID int64, entry *model.Entry) (
 		return false, err
 	}
 
-	return !exists, nil
+	return !alreadyExistingEntry, nil
 }
 
 func (s *Storage) IsNewEntry(feedID int64, entryHash string) bool {