From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id CDkFLSIA1WXZIAEA62LTzQ:P1 (envelope-from ) for ; Tue, 20 Feb 2024 20:40:18 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id CDkFLSIA1WXZIAEA62LTzQ (envelope-from ) for ; Tue, 20 Feb 2024 20:40:18 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1708458018; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=wdBaDuGGD/S2+jLVBapx90+RVa6kO3AAZ1c7/IOuMsM=; b=tnf9nZNwJ2JZPmNqOO28YEfI9RBnqFiNx/nsChj/pvbqrGix+KktBQjf+JghPPyQ9Hl2hl OwOdsGlSpXR1gMFtm38/j2R9oZAF5pQ1GqaLPK3je2DgTPvxQkSOvCjBhKoKaNTX0Pd6SA 8wzjlP43uDWUrQ6B8eYYaisryCjMtEodQ8elhBeI9JVyF3enMH9S/W3Eq+RqHOUI8lQlJm WYt5LHPKSkB2kQTvtjHCQ6MKbW5fRl4ikLVbQTfJVVya3cwQUTmXQRA+9O60ru3e0YWSjD 9xbtlJ5DrhblWTv/hoF2Qe100X2AaC/iQ1/aGZzIO3KSDVRPjshdhwCQGzb2xQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=none ARC-Seal: i=1; s=key1; d=yhetil.org; t=1708458018; a=rsa-sha256; cv=none; b=Z2Y4+T8lZG1EeljXWcof5JXytD4UAK6oxfDKCAZ5WdURTAM53EAc6jIxqBLZYtRUgiL0sW +OZczoj3K06Y4wnhhQT0rWi5JoqnriL8nkMmYP8jRktluepp6xo7m135sipRMVg3aSTtqi uPKstv0A17F+zHl9LIzLEp6Q5i+doULxbrGTpYpxaaYj78paeFcHmS3CPaDH8xmbzCg8jL B5p5NdSXPNEp5psLvhS/fPfWdgLHSrRUtQQaHWxsf2tvkOUeSCHJvDo7YHQRIfDl6tj2Js jkbEwvFDNxZ5fUh0KnAlPG9QrXAIfs2cyVvGHUyP846n+d6ahI3ck/GVxfsQCg== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 3E4454580B for ; Tue, 20 Feb 2024 20:40:18 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rcVy2-0002Mu-2V; Tue, 20 Feb 2024 14:39:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rcVxw-0002Lk-H4 for guix-patches@gnu.org; Tue, 20 Feb 2024 14:39:44 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rcVxv-0000Pa-Ld; Tue, 20 Feb 2024 14:39:43 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rcVyF-0002SH-8A; Tue, 20 Feb 2024 14:40:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#69292] [PATCH 5/6] store: database: Refactor sqlite-register. Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Tue, 20 Feb 2024 19:40:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69292 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 69292@debbugs.gnu.org Cc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 69292-submit@debbugs.gnu.org id=B69292.17084579779365 (code B ref 69292); Tue, 20 Feb 2024 19:40:03 +0000 Received: (at 69292) by debbugs.gnu.org; 20 Feb 2024 19:39:37 +0000 Received: from localhost ([127.0.0.1]:46610 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rcVxp-0002Qy-5P for submit@debbugs.gnu.org; Tue, 20 Feb 2024 14:39:37 -0500 Received: from mira.cbaines.net ([212.71.252.8]:43144) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rcVxi-0002QX-6o for 69292@debbugs.gnu.org; Tue, 20 Feb 2024 14:39:33 -0500 Received: from localhost (unknown [212.132.255.10]) by mira.cbaines.net (Postfix) with ESMTPSA id C06C227BBEC for <69292@debbugs.gnu.org>; Tue, 20 Feb 2024 19:39:07 +0000 (GMT) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 150cb772 for <69292@debbugs.gnu.org>; Tue, 20 Feb 2024 19:39:07 +0000 (UTC) From: Christopher Baines Date: Tue, 20 Feb 2024 19:39:05 +0000 Message-ID: <11b24242dd71349c09486341ed0d01b55f5f8f9a.1708457946.git.mail@cbaines.net> X-Mailer: git-send-email 2.41.0 In-Reply-To: <4b6a268daab5e0b307dff2229d551a47c9fe1ebc.1708457946.git.mail@cbaines.net> References: <4b6a268daab5e0b307dff2229d551a47c9fe1ebc.1708457946.git.mail@cbaines.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -3.48 X-Spam-Score: -3.48 X-Migadu-Queue-Id: 3E4454580B X-Migadu-Scanner: mx11.migadu.com X-TUID: 1g+zk+Icf85w The update-or-insert procedure name was unhelpfully generic, and these changes should improve the code readability. * guix/store/database.scm (update-or-insert): Remove procedure and inline functionality in to sqlite-register. Change-Id: Ifab0cdb7972d095460cc1f79b8b2f0e9b958059c --- guix/store/database.scm | 132 ++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/guix/store/database.scm b/guix/store/database.scm index 8d8b7346e0..0b570eabcd 100644 --- a/guix/store/database.scm +++ b/guix/store/database.scm @@ -194,54 +194,15 @@ (define* (path-id db path) (#(id) id) (#f #f)))) -(define-inlinable (assert-integer proc in-range? key number) - (unless (integer? number) - (throw 'wrong-type-arg proc - "Wrong type argument ~A: ~S" (list key number) - (list number))) - (unless (in-range? number) - (throw 'out-of-range proc - "Integer ~A out of range: ~S" (list key number) - (list number)))) - -(define* (update-or-insert db #:key path deriver hash nar-size time) - "The classic update-if-exists and insert-if-doesn't feature that sqlite -doesn't exactly have... they've got something close, but it involves deleting -and re-inserting instead of updating, which causes problems with foreign keys, -of course. Returns the row id of the row that was modified or inserted." - - ;; Make sure NAR-SIZE is valid. - (assert-integer "update-or-insert" positive? #:nar-size nar-size) - (assert-integer "update-or-insert" (cut >= <> 0) #:time time) - - (let ((id (path-id db path))) - (if id - (let ((stmt (sqlite-prepare - db - " -UPDATE ValidPaths -SET hash = :hash, - registrationTime = :time, - deriver = :deriver, - narSize = :size -WHERE id = :id" - #:cache? #t))) - (sqlite-bind-arguments stmt #:id id - #:deriver deriver - #:hash hash #:size nar-size #:time time) - (sqlite-step-and-reset stmt) - id) - (let ((stmt (sqlite-prepare - db - " -INSERT INTO ValidPaths (path, hash, registrationTime, deriver, narSize) -VALUES (:path, :hash, :time, :deriver, :size)" - #:cache? #t))) - (sqlite-bind-arguments stmt - #:path path #:deriver deriver - #:hash hash #:size nar-size #:time time) - (sqlite-step-and-reset stmt) - (last-insert-row-id db))))) +(define (timestamp) + "Return a timestamp, either the current time of SOURCE_DATE_EPOCH." + (match (getenv "SOURCE_DATE_EPOCH") + (#f + (current-time time-utc)) + ((= string->number seconds) + (if seconds + (make-time time-utc 0 seconds) + (current-time time-utc))))) (define (add-references db referrer references) "REFERRER is the id of the referring store item, REFERENCES is a list @@ -258,15 +219,15 @@ (define (add-references db referrer references) (sqlite-step-and-reset stmt)) references))) -(define (timestamp) - "Return a timestamp, either the current time of SOURCE_DATE_EPOCH." - (match (getenv "SOURCE_DATE_EPOCH") - (#f - (current-time time-utc)) - ((= string->number seconds) - (if seconds - (make-time time-utc 0 seconds) - (current-time time-utc))))) +(define-inlinable (assert-integer proc in-range? key number) + (unless (integer? number) + (throw 'wrong-type-arg proc + "Wrong type argument ~A: ~S" (list key number) + (list number))) + (unless (in-range? number) + (throw 'out-of-range proc + "Integer ~A out of range: ~S" (list key number) + (list number)))) (define* (sqlite-register db #:key path (references '()) deriver hash nar-size @@ -279,15 +240,54 @@ (define* (sqlite-register db #:key path (references '()) the database or #f, meaning \"right now\". Every store item in REFERENCES must already be registered." - (let ((id (update-or-insert db #:path path - #:deriver deriver - #:hash hash - #:nar-size nar-size - #:time (time-second time)))) - ;; Call 'path-id' on each of REFERENCES. This ensures we get a - ;; "non-NULL constraint" failure if one of REFERENCES is unregistered. - (add-references db id - (map (cut path-id db <>) references)))) + + (define registration-time + (time-second time)) + + ;; Make sure NAR-SIZE is valid. + (assert-integer "sqlite-register" positive? #:nar-size nar-size) + (assert-integer "sqlite-register" (cut >= <> 0) #:time registration-time) + + (define id + (let ((existing-id (path-id db path))) + (if existing-id + (let ((stmt (sqlite-prepare + db + " +UPDATE ValidPaths +SET hash = :hash, + registrationTime = :time, + deriver = :deriver, + narSize = :size +WHERE id = :id" + #:cache? #t))) + (sqlite-bind-arguments stmt + #:id existing-id + #:deriver deriver + #:hash hash + #:size nar-size + #:time registration-time) + (sqlite-step-and-reset stmt) + existing-id) + (let ((stmt (sqlite-prepare + db + " +INSERT INTO ValidPaths (path, hash, registrationTime, deriver, narSize) +VALUES (:path, :hash, :time, :deriver, :size)" + #:cache? #t))) + (sqlite-bind-arguments stmt + #:path path + #:deriver deriver + #:hash hash + #:size nar-size + #:time registration-time) + (sqlite-step-and-reset stmt) + (last-insert-row-id db))))) + + ;; Call 'path-id' on each of REFERENCES. This ensures we get a + ;; "non-NULL constraint" failure if one of REFERENCES is unregistered. + (add-references db id + (map (cut path-id db <>) references))) ;;; -- 2.41.0