diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2024-01-20 18:15:12 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2024-01-20 18:15:12 +0100 |
commit | 059825f1698eaf2c282bf5a925feb157954d430f (patch) | |
tree | 0b3e7492a234a6a35b1b86fce21d79e25b07b9bc | |
parent | 4b054ac9ccf22b2eb87641e0cb1784bc92663777 (diff) | |
download | gallery-059825f1698eaf2c282bf5a925feb157954d430f.tar.gz gallery-059825f1698eaf2c282bf5a925feb157954d430f.tar.xz gallery-059825f1698eaf2c282bf5a925feb157954d430f.zip |
gallery: add dhashes in one big DB transaction
iotop showed gigabytes of writes for a DB in the order of 100 MB.
-rw-r--r-- | main.go | 23 |
1 files changed, 21 insertions, 2 deletions
@@ -2390,14 +2390,29 @@ func cmdDhash(fs *flag.FlagSet, args []string) error { } } - stmt, err := db.Prepare(`UPDATE image SET dhash = ? WHERE sha1 = ?`) + // Commits are very IO-expensive in both WAL and non-WAL SQLite, + // so write this in one go. For a middle ground, we could batch the updates. + tx, err := db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + // Mild hack: upgrade the transaction to a write one straight away, + // in order to rule out deadlocks (preventable failure). + if _, err := tx.Exec(`END TRANSACTION; + BEGIN IMMEDIATE TRANSACTION`); err != nil { + return err + } + + stmt, err := tx.Prepare(`UPDATE image SET dhash = ? WHERE sha1 = ?`) if err != nil { return err } defer stmt.Close() var mu sync.Mutex - return parallelize(hexSHA1, func(sha1 string) (message string, err error) { + err = parallelize(hexSHA1, func(sha1 string) (message string, err error) { hash, err := makeDhash(sha1) if errors.Is(err, errIsAnimation) { // Ignoring this common condition. @@ -2411,6 +2426,10 @@ func cmdDhash(fs *flag.FlagSet, args []string) error { _, err = stmt.Exec(int64(hash), sha1) return "", err }) + if err != nil { + return err + } + return tx.Commit() } // --- Main -------------------------------------------------------------------- |