aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2024-02-18 13:38:17 +0000
committerChristopher Baines <mail@cbaines.net>2024-04-03 17:18:39 +0100
commit511d68c71d9c676aeb3a97936a984fb95a75f5a2 (patch)
tree830d39c63b8232ea6e98bb044ac16376375169da
parentcdd4a0c3c952c207ec9460c0a693cb7b30f2085a (diff)
downloadguix-511d68c71d9c676aeb3a97936a984fb95a75f5a2.tar
guix-511d68c71d9c676aeb3a97936a984fb95a75f5a2.tar.gz
store: database: Stop finalizing prepared statements.
Especially since we're asking for these to be cached. Management of prepared statements isn't trivial, since you don't want to keep them forever as this can lead to poor query performance, but I don't think that finalizing them immediately is the right solution. Change-Id: I61706b4d09d771835bb8f074b8f6a6ee871f5e2d * guix/store/database.scm (sqlite-step-and-reset): New procedure. (last-insert-row, path-id, update-or-insert, add-references): Don't finalize prepared statements. Change-Id: I2a2c6deb43935d67df9e43000a5105343d72b3e6
-rw-r--r--guix/store/database.scm40
1 files changed, 18 insertions, 22 deletions
diff --git a/guix/store/database.scm b/guix/store/database.scm
index e958ef4d36..178f46e405 100644
--- a/guix/store/database.scm
+++ b/guix/store/database.scm
@@ -167,16 +167,19 @@ If FILE doesn't exist, create it and initialize it as a new database. Pass
((_ file db exp ...)
(call-with-database file (lambda (db) exp ...)))))
+(define (sqlite-step-and-reset statement)
+ (let ((val (sqlite-step statement)))
+ (sqlite-reset statement)
+ val))
+
(define (last-insert-row-id db)
;; XXX: (sqlite3) currently lacks bindings for 'sqlite3_last_insert_rowid'.
;; Work around that.
- (let* ((stmt (sqlite-prepare db "SELECT last_insert_rowid();"
- #:cache? #t))
- (result (sqlite-fold cons '() stmt)))
- (sqlite-finalize stmt)
- (match result
- ((#(id)) id)
- (_ #f))))
+ (let ((stmt (sqlite-prepare db
+ "SELECT last_insert_rowid();"
+ #:cache? #t)))
+ (vector-ref (sqlite-step-and-reset stmt)
+ 0)))
(define* (path-id db path)
"If PATH exists in the 'ValidPaths' table, return its numerical
@@ -187,11 +190,9 @@ identifier. Otherwise, return #f."
SELECT id FROM ValidPaths WHERE path = :path"
#:cache? #t)))
(sqlite-bind-arguments stmt #:path path)
- (let ((result (sqlite-fold cons '() stmt)))
- (sqlite-finalize stmt)
- (match result
- ((#(id) . _) id)
- (_ #f)))))
+ (match (sqlite-step-and-reset stmt)
+ (#(id) id)
+ (#f #f))))
(define-inlinable (assert-integer proc in-range? key number)
(unless (integer? number)
@@ -225,9 +226,8 @@ WHERE id = :id"
(sqlite-bind-arguments stmt #:id id
#:deriver deriver
#:hash hash #:size nar-size #:time time)
- (sqlite-fold cons '() stmt)
- (sqlite-finalize stmt)
- (last-insert-row-id db))
+ (sqlite-step-and-reset stmt)
+ id)
(let ((stmt (sqlite-prepare
db
"
@@ -237,8 +237,7 @@ VALUES (:path, :hash, :time, :deriver, :size)"
(sqlite-bind-arguments stmt
#:path path #:deriver deriver
#:hash hash #:size nar-size #:time time)
- (sqlite-fold cons '() stmt) ;execute it
- (sqlite-finalize stmt)
+ (sqlite-step-and-reset stmt)
(last-insert-row-id db)))))
(define (add-references db referrer references)
@@ -251,13 +250,10 @@ INSERT OR REPLACE INTO Refs (referrer, reference)
VALUES (:referrer, :reference)"
#:cache? #t)))
(for-each (lambda (reference)
- (sqlite-reset stmt)
(sqlite-bind-arguments stmt #:referrer referrer
#:reference reference)
- (sqlite-fold cons '() stmt) ;execute it
- (last-insert-row-id db))
- references)
- (sqlite-finalize stmt)))
+ (sqlite-step-and-reset stmt))
+ references)))
(define (timestamp)
"Return a timestamp, either the current time of SOURCE_DATE_EPOCH."