From 049334e423241426c0eef526eda8818e96f5b3ca Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 13 Nov 2020 21:18:48 +0000 Subject: Remove redundant sqlite-reset calls Which is most of them. Not sure why I started resetting statements after their use, but it's unnecessary. --- guix-build-coordinator/datastore/sqlite.scm | 646 +++++++++++----------------- 1 file changed, 249 insertions(+), 397 deletions(-) diff --git a/guix-build-coordinator/datastore/sqlite.scm b/guix-build-coordinator/datastore/sqlite.scm index af496f6..8a76e50 100644 --- a/guix-build-coordinator/datastore/sqlite.scm +++ b/guix-build-coordinator/datastore/sqlite.scm @@ -176,17 +176,13 @@ SELECT description FROM agents WHERE id = :id" statement #:id uuid) - (let ((result - (match (sqlite-map - (match-lambda - (#(description) - `((description . ,description)))) - statement) - (() #f) - ((agent) agent)))) - (sqlite-reset statement) - - result))))) + (match (sqlite-map + (match-lambda + (#(description) + `((description . ,description)))) + statement) + (() #f) + ((agent) agent)))))) (define-method (datastore-new-agent (datastore ) @@ -210,15 +206,12 @@ SELECT description FROM agents WHERE id = :id" SELECT id, description FROM agents ORDER BY id" #:cache? #t))) - (let ((agents (sqlite-map - (match-lambda - (#(id description) - `((uuid . ,id) - (description . ,description)))) - statement))) - (sqlite-reset statement) - - agents))))) + (sqlite-map + (match-lambda + (#(id description) + `((uuid . ,id) + (description . ,description)))) + statement))))) (define-method (datastore-new-agent-password (datastore ) @@ -250,13 +243,9 @@ WHERE agent_id = :agent_id AND password = :password" #:agent_id uuid #:password password) - (let ((result - (match (sqlite-step statement) - (#f #f) - (#(1) #t)))) - (sqlite-reset statement) - - result))))) + (match (sqlite-step statement) + (#f #f) + (#(1) #t)))))) (define-method (datastore-store-derivation (datastore ) @@ -313,13 +302,10 @@ WHERE related_derivations.name != :derivation statement #:derivation derivation) - (let ((result (sqlite-map - (match-lambda - (#(derivation) derivation)) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(derivation) derivation)) + statement))))) (define-method (datastore-list-failed-builds-with-blocking-count (datastore )) @@ -369,16 +355,13 @@ WHERE builds.processed = 1 ) ORDER BY 3 DESC, 2, 1" #:cache? #t))) - (let ((result (sqlite-map - (match-lambda - (#(uuid derivation-name blocked-count) - `((uuid . ,uuid) - (derivation_name . ,derivation-name) - (blocked_count . ,blocked-count)))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(uuid derivation-name blocked-count) + `((uuid . ,uuid) + (derivation_name . ,derivation-name) + (blocked_count . ,blocked-count)))) + statement))))) (define-method (datastore-list-builds-for-derivation-recursive-inputs (datastore ) @@ -410,13 +393,10 @@ INNER JOIN related_derivations statement #:derivation derivation) - (let ((result (sqlite-map - (match-lambda - (#(uuid) uuid)) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(uuid) uuid)) + statement))))) (define-method (datastore-store-build (datastore ) @@ -496,15 +476,11 @@ INSERT INTO build_tags (build_id, tag_id) VALUES (:build_id, :tag_id)" SELECT agent_id, result, COUNT(*) FROM build_results GROUP BY agent_id, result" #:cache? #t))) - (let ((result - (sqlite-map - (match-lambda - (#(agent_id result count) - (cons (list agent_id result) count))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(agent_id result count) + (cons (list agent_id result) count))) + statement))))) (define-method (datastore-store-build-result (datastore ) @@ -597,10 +573,7 @@ WHERE builds.uuid = :build_id AND derivation_outputs.name = :name" #:name name) (match (sqlite-step statement) - (#(id) - (sqlite-reset statement) - - id)))) + (#(id) id)))) (sqlite-exec db @@ -697,17 +670,13 @@ ORDER BY start_time DESC" statement #:build_id build-id) - (let ((result - (sqlite-map - (match-lambda - (#(start_time agent_id) - `((start-time . ,(match (strptime "%F %T" start_time) - ((parts . _) parts))) - (agent-id . ,agent_id)))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(start_time agent_id) + `((start-time . ,(match (strptime "%F %T" start_time) + ((parts . _) parts))) + (agent-id . ,agent_id)))) + statement))))) (define (insert-setup-failure-and-remove-allocation db @@ -798,13 +767,10 @@ WHERE setup_failure_id = :id" statement #:id setup-failure-id) - (let ((result (sqlite-map - (match-lambda - (#(missing-input) missing-input)) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(missing-input) missing-input)) + statement))))) (define-method (datastore-store-setup-failure (datastore ) @@ -835,15 +801,11 @@ INNER JOIN derivations GROUP BY derivations.system" #:cache? #t))) - (let ((result - (sqlite-map - (match-lambda - (#(system count) - (cons system count))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(system count) + (cons system count))) + statement))))) (define-method (datastore-for-each-build (datastore ) @@ -864,10 +826,7 @@ SELECT uuid FROM builds ORDER BY uuid" (proc uuid) (loop (sqlite-step statement))) - (#f - (sqlite-reset statement) - - #t))))))) + (#f #t))))))) (define-method (datastore-find-build (datastore ) @@ -888,28 +847,24 @@ WHERE uuid = :uuid" statement #:uuid uuid) - (let ((result - (match (sqlite-step statement) - (#(uuid derivation_name priority processed created_at end_time) - `((uuid . ,uuid) - (derivation-name . ,derivation_name) - (priority . ,priority) - (processed . ,(cond - ((= 0 processed) #f) - ((= 1 processed) #t) - (else - (error "unknown processed value")))) - (created-at . ,(if (string? created_at) - (match (strptime "%F %T" created_at) - ((parts . _) parts)) - #f)) - (end-time . ,(if (string? end_time) - (match (strptime "%F %T" end_time) - ((parts . _) parts)) - #f))))))) - (sqlite-reset statement) - - result))))) + (match (sqlite-step statement) + (#(uuid derivation_name priority processed created_at end_time) + `((uuid . ,uuid) + (derivation-name . ,derivation_name) + (priority . ,priority) + (processed . ,(cond + ((= 0 processed) #f) + ((= 1 processed) #t) + (else + (error "unknown processed value")))) + (created-at . ,(if (string? created_at) + (match (strptime "%F %T" created_at) + ((parts . _) parts)) + #f)) + (end-time . ,(if (string? end_time) + (match (strptime "%F %T" end_time) + ((parts . _) parts)) + #f))))))))) (define-method (datastore-find-build-result (datastore ) @@ -930,16 +885,12 @@ WHERE build_id = :build_id" statement #:build_id build-id) - (let ((result - (match (sqlite-step statement) - (#(agent_id result failure_reason) - `((agent_id . ,agent_id) - (result . ,result) - (failure_reason . ,failure_reason))) - (#f #f)))) - (sqlite-reset statement) - - result))))) + (match (sqlite-step statement) + (#(agent_id result failure_reason) + `((agent_id . ,agent_id) + (result . ,result) + (failure_reason . ,failure_reason))) + (#f #f)))))) (define-method (datastore-find-build-derivation-system (datastore ) @@ -961,12 +912,8 @@ WHERE builds.uuid = :build_id" statement #:build_id build-id) - (let ((system - (match (sqlite-step statement) - (#(system) system)))) - (sqlite-reset statement) - - system))))) + (match (sqlite-step statement) + (#(system) system)))))) (define-method (datastore-list-builds-for-output (datastore ) @@ -995,19 +942,15 @@ WHERE derivation_outputs.output = :output" statement #:output output) - (let ((result - (sqlite-map - (match-lambda - (#(uuid derivation priority processed result) - `((uuid . ,uuid) - (derivation . ,derivation) - (priority . ,priority) - (processed . ,processed) - (result . ,result)))) - statement))) - (sqlite-reset statement) - - result))))))) + (sqlite-map + (match-lambda + (#(uuid derivation priority processed result) + `((uuid . ,uuid) + (derivation . ,derivation) + (priority . ,priority) + (processed . ,processed) + (result . ,result)))) + statement))))))) (define-method (datastore-list-builds-for-output-and-system (datastore ) @@ -1035,16 +978,12 @@ WHERE derivation_outputs.output = :output #:output output #:system system) - (let ((result - (sqlite-map - (match-lambda - (#(uuid derivation) - `((uuid . ,uuid) - (derivation . ,derivation)))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(uuid derivation) + `((uuid . ,uuid) + (derivation . ,derivation)))) + statement))))) (define-method (datastore-list-builds-for-derivation (datastore ) @@ -1063,15 +1002,11 @@ SELECT uuid FROM builds WHERE derivation_name = :derivation" statement #:derivation derivation) - (let ((result - (sqlite-map - (match-lambda - (#(uuid) - `((uuid . ,uuid)))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(uuid) + `((uuid . ,uuid)))) + statement))))) (define-method (datastore-update (datastore )) @@ -1094,15 +1029,11 @@ FROM setup_failures GROUP BY agent_id, failure_reason" #:cache? #t))) - (let ((result - (sqlite-map - (match-lambda - (#(agent_id failure_reason count) - (cons (list agent_id failure_reason) count))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(agent_id failure_reason count) + (cons (list agent_id failure_reason) count))) + statement))))) (define-method (datastore-list-setup-failures-for-build (datastore ) @@ -1123,16 +1054,13 @@ WHERE build_id = :build_id" statement #:build_id build-id) - (let ((result (sqlite-map - (match-lambda - (#(id agent-id failure-reason) - `((id . ,id) - (agent-id . ,agent-id) - (failure-reason . ,failure-reason)))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(id agent-id failure-reason) + `((id . ,id) + (agent-id . ,agent-id) + (failure-reason . ,failure-reason)))) + statement))))) (define-method (datastore-fetch-setup-failures (datastore )) @@ -1150,26 +1078,23 @@ INNER JOIN builds WHERE builds.processed = 0" #:cache? #t))) - (let ((result (sqlite-fold - (lambda (row result) - (match row - (#(id build-id agent-id failure-reason) - (let ((failures-for-build-id - (or (hash-ref result build-id) - '()))) - (hash-set! - result - build-id - (cons `((id . ,id) - (agent-id . ,agent-id) - (failure-reason . ,failure-reason)) - failures-for-build-id))))) - result) - (make-hash-table) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-fold + (lambda (row result) + (match row + (#(id build-id agent-id failure-reason) + (let ((failures-for-build-id + (or (hash-ref result build-id) + '()))) + (hash-set! + result + build-id + (cons `((id . ,id) + (agent-id . ,agent-id) + (failure-reason . ,failure-reason)) + failures-for-build-id))))) + result) + (make-hash-table) + statement))))) (define-method (datastore-list-processed-builds (datastore )) @@ -1183,16 +1108,13 @@ WHERE builds.processed = 0" SELECT uuid, derivation_name, priority FROM builds WHERE processed = 1" #:cache? #t))) - (let ((builds (sqlite-map - (match-lambda - (#(uuid derivation_name priority) - `((uuid . ,uuid) - (derivation-name . ,derivation_name) - (priority . ,priority)))) - statement))) - (sqlite-reset statement) - - builds))))) + (sqlite-map + (match-lambda + (#(uuid derivation_name priority) + `((uuid . ,uuid) + (derivation-name . ,derivation_name) + (priority . ,priority)))) + statement))))) (define-method (datastore-list-unprocessed-builds (datastore )) @@ -1209,16 +1131,13 @@ WHERE processed = 0 ORDER BY priority DESC" #:cache? #t))) - (let ((builds (sqlite-map - (match-lambda - (#(uuid derivation_name priority) - `((uuid . ,uuid) - (derivation-name . ,derivation_name) - (priority . ,priority)))) - statement))) - (sqlite-reset statement) - - builds))))) + (sqlite-map + (match-lambda + (#(uuid derivation_name priority) + `((uuid . ,uuid) + (derivation-name . ,derivation_name) + (priority . ,priority)))) + statement))))) (define-method (datastore-fetch-build-ids-and-propagated-priorities-for-unprocessed-builds (datastore ) @@ -1313,16 +1232,14 @@ WHERE builds.processed = 0 GROUP BY builds_with_derived_priority.uuid" #:cache? #t))) - (let ((result (sqlite-fold - (lambda (row result) - (match row - (#(uuid derived-priority) - (hash-set! result uuid derived-priority))) - result) - (make-hash-table 10000) - statement))) - - result))) + (sqlite-fold + (lambda (row result) + (match row + (#(uuid derived-priority) + (hash-set! result uuid derived-priority))) + result) + (make-hash-table 10000) + statement))) (datastore-call-with-transaction datastore @@ -1356,8 +1273,7 @@ VALUES (:event, :arguments)"))) (lambda (port) (write arguments port)))) - (sqlite-step statement) - (sqlite-reset statement)) + (sqlite-step statement)) #t) (define-method (datastore-count-unprocessed-hook-events @@ -1372,15 +1288,12 @@ VALUES (:event, :arguments)"))) SELECT event, COUNT(*) FROM unprocessed_hook_events GROUP BY event" #:cache? #t))) - (let ((counts (sqlite-map - (match-lambda - (#(event count) - `((event . ,event) - (count . ,count)))) - statement))) - (sqlite-reset statement) - - counts))))) + (sqlite-map + (match-lambda + (#(event count) + `((event . ,event) + (count . ,count)))) + statement))))) (define-method (datastore-list-unprocessed-hook-events (datastore ) @@ -1404,18 +1317,15 @@ LIMIT :limit" #:event (symbol->string event) #:limit limit) - (let ((events (sqlite-map - (match-lambda - (#(id event arguments) - (list id - (string->symbol event) - (call-with-input-string arguments - (lambda (port) - (read port)))))) - statement))) - (sqlite-reset statement) - - events))))) + (sqlite-map + (match-lambda + (#(id event arguments) + (list id + (string->symbol event) + (call-with-input-string arguments + (lambda (port) + (read port)))))) + statement))))) (define-method (datastore-delete-unprocessed-hook-event (datastore ) @@ -1444,15 +1354,11 @@ FROM build_allocation_plan GROUP BY agent_id" #:cache? #t))) - (let ((result - (sqlite-map - (match-lambda - (#(agent_id count) - (cons agent_id count))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(agent_id count) + (cons agent_id count))) + statement))))) (define-method (datastore-replace-build-allocation-plan (datastore ) @@ -1505,15 +1411,11 @@ INSERT INTO build_allocation_plan (build_id, agent_id, ordering) VALUES " SELECT agent_id, COUNT(*) FROM allocated_builds GROUP BY agent_id" #:cache? #t))) - (let ((result - (sqlite-map - (match-lambda - (#(agent_id count) - (cons agent_id count))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(agent_id count) + (cons agent_id count))) + statement))))) (define-method (datastore-agent-requested-systems (datastore ) @@ -1535,13 +1437,9 @@ ORDER BY system ASC" statement #:agent_id agent-id) - (let ((result - (sqlite-map - (match-lambda (#(system) system)) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda (#(system) system)) + statement))))) (define-method (datastore-update-agent-requested-systems (datastore ) @@ -1724,15 +1622,12 @@ LIMIT :limit" #:agent_id agent-id #:limit limit) - (let ((builds (sqlite-map - (match-lambda - (#(uuid derivation_name) - `((uuid . ,uuid) - (derivation-name . ,derivation_name)))) - statement))) - (sqlite-reset statement) - - builds))))) + (sqlite-map + (match-lambda + (#(uuid derivation_name) + `((uuid . ,uuid) + (derivation-name . ,derivation_name)))) + statement))))) (define-method (datastore-list-agent-builds (datastore ) @@ -1755,16 +1650,13 @@ WHERE allocated_builds.agent_id = :agent_id" statement #:agent_id agent-id) - (let ((builds (sqlite-map - (match-lambda - (#(uuid derivation_name priority) - `((uuid . ,uuid) - (derivation-name . ,derivation_name) - (priority . ,priority)))) - statement))) - (sqlite-reset statement) - - builds))))) + (sqlite-map + (match-lambda + (#(uuid derivation_name priority) + `((uuid . ,uuid) + (derivation-name . ,derivation_name) + (priority . ,priority)))) + statement))))) (define-method (datastore-agent-for-build (datastore ) @@ -1788,13 +1680,9 @@ WHERE build_results.build_id = :build_id" statement #:build_id build-id) - (let ((result - (match (sqlite-step statement) - (#(agent-id) agent-id) - (#f #f)))) - (sqlite-reset statement) - - result))))) + (match (sqlite-step statement) + (#(agent-id) agent-id) + (#f #f)))))) (define (db-open database) (define flags @@ -1841,13 +1729,9 @@ WHERE build_results.build_id = :build_id" db "SELECT changes();" #:cache? #t))) - (let ((count - (vector-ref (sqlite-step statement) - 0))) - (sqlite-reset statement) - - count))) + (vector-ref (sqlite-step statement) + 0))) (define (last-insert-rowid db) (let ((statement @@ -1855,13 +1739,9 @@ WHERE build_results.build_id = :build_id" db "SELECT last_insert_rowid();" #:cache? #t))) - (let ((id - (vector-ref (sqlite-step statement) - 0))) - - (sqlite-reset statement) - id))) + (vector-ref (sqlite-step statement) + 0))) (define (select-derivation-outputs db derivation-name) (let ((statement @@ -1875,14 +1755,11 @@ SELECT name, id FROM derivation_outputs WHERE derivation_name = :derivation_name statement #:derivation_name derivation-name) - (let ((outputs (sqlite-map - (match-lambda - (#(name output-id) - (cons name output-id))) - statement))) - (sqlite-reset statement) - - outputs))) + (sqlite-map + (match-lambda + (#(name output-id) + (cons name output-id))) + statement))) (define (db-find-derivation db name) (let ((statement @@ -1898,18 +1775,14 @@ WHERE name = :name" statement #:name name) - (let ((result - (match (sqlite-step statement) - (#f #f) - (#(system fixed_output) - `((system . ,system) - (fixed-output? . ,(cond - ((eq? fixed_output 0) #f) - ((eq? fixed_output 1) #t) - (else fixed_output)))))))) - (sqlite-reset statement) - - result))) + (match (sqlite-step statement) + (#f #f) + (#(system fixed_output) + `((system . ,system) + (fixed-output? . ,(cond + ((eq? fixed_output 0) #f) + ((eq? fixed_output 1) #t) + (else fixed_output)))))))) (define-method (datastore-find-derivation (datastore ) @@ -1938,16 +1811,12 @@ WHERE derivation_name = :derivation_name" statement #:derivation_name derivation-name) - (let ((result - (sqlite-map - (match-lambda - (#(name output) - `((name . ,name) - (output . ,output)))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(name output) + `((name . ,name) + (output . ,output)))) + statement))))) (define-method (datastore-list-build-outputs (datastore ) @@ -1973,22 +1842,18 @@ WHERE builds.uuid = :build_id" statement #:build_id build-id) - (let ((result - (sqlite-map - (match-lambda - (#(name output hash size store_references) - `((name . ,name) - (output . ,output) - (hash . ,hash) - (size . ,size) - (references . ,(and store_references - (list->vector - (string-split store_references - #\space))))))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(name output hash size store_references) + `((name . ,name) + (output . ,output) + (hash . ,hash) + (size . ,size) + (references . ,(and store_references + (list->vector + (string-split store_references + #\space))))))) + statement))))) (define-method (datastore-find-derivation-system (datastore ) @@ -2009,12 +1874,8 @@ WHERE name = :name" statement #:name derivation-name) - (let ((system - (match (sqlite-step statement) - (#(system) system)))) - (sqlite-reset statement) - - system))))) + (match (sqlite-step statement) + (#(system) system)))))) (define-method (datastore-find-derivation-inputs (datastore ) @@ -2038,17 +1899,13 @@ WHERE derivation_inputs.derivation_name = :derivation_name" statement #:derivation_name derivation-name) - (let ((result - (sqlite-map - (match-lambda - (#(derivation output-name output) - `((derivation . ,derivation) - (output_name . ,output-name) - (output . ,output)))) - statement))) - (sqlite-reset statement) - - result))))) + (sqlite-map + (match-lambda + (#(derivation output-name output) + `((derivation . ,derivation) + (output_name . ,output-name) + (output . ,output)))) + statement))))) (define-method (datastore-find-derivation-for-output (datastore ) @@ -2084,13 +1941,9 @@ WHERE output = :output #:derivation start-derivation-name #:output output) - (let ((result - (match (sqlite-step statement) - (#f #f) - (#(derivation) derivation)))) - (sqlite-reset statement) - - result))))) + (match (sqlite-step statement) + (#f #f) + (#(derivation) derivation)))))) (define (insert-derivation-and-return-outputs db derivation) (define derivation-name @@ -2126,7 +1979,6 @@ INSERT OR IGNORE INTO derivations (name, system, fixed_output) #:fixed_output (if fixed-output? 1 0)) (sqlite-step statement) - (sqlite-reset statement) (changes-count db))))) @@ -2235,8 +2087,8 @@ VALUES (:uuid, :derivation_name, :priority, datetime('now'))" #:derivation_name derivation-name #:priority priority) - (sqlite-step statement) - (sqlite-reset statement))) + (sqlite-step statement)) + #t) (define (insert-agent db uuid description) (let ((statement @@ -2252,8 +2104,8 @@ VALUES (:id, :description)" #:id uuid #:description description) - (sqlite-step statement) - (sqlite-reset statement))) + (sqlite-step statement)) + #t) (define (insert-agent-password db uuid password) (let ((statement @@ -2269,5 +2121,5 @@ VALUES (:agent_id, :password)" #:agent_id uuid #:password password) - (sqlite-step statement) - (sqlite-reset statement))) + (sqlite-step statement)) + #t) -- cgit v1.2.3