From patchwork Sun Apr 21 09:42:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Baines X-Patchwork-Id: 63265 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 5328F27BBE2; Sun, 21 Apr 2024 10:45:55 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 1786827BBEA for ; Sun, 21 Apr 2024 10:45:54 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ryTk0-0005eB-TO; Sun, 21 Apr 2024 05:44:08 -0400 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 1ryTjm-0005Rz-MS for guix-patches@gnu.org; Sun, 21 Apr 2024 05:43:55 -0400 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 1ryTjm-0002pL-DD; Sun, 21 Apr 2024 05:43:54 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ryTk0-0006VM-1B; Sun, 21 Apr 2024 05:44:08 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#70494] [PATCH 10/23] store: database: Add procedures for querying valid paths. 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: Sun, 21 Apr 2024 09:44:07 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70494 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 70494@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 70494-submit@debbugs.gnu.org id=B70494.171369262624672 (code B ref 70494); Sun, 21 Apr 2024 09:44:07 +0000 Received: (at 70494) by debbugs.gnu.org; 21 Apr 2024 09:43:46 +0000 Received: from localhost ([127.0.0.1]:41780 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ryTjb-0006Pa-T4 for submit@debbugs.gnu.org; Sun, 21 Apr 2024 05:43:45 -0400 Received: from mira.cbaines.net ([2a01:7e00:e000:2f8:fd4d:b5c7:13fb:3d27]:45147) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ryTiy-0006I6-EY for 70494@debbugs.gnu.org; Sun, 21 Apr 2024 05:43:06 -0400 Received: from localhost (unknown [212.132.255.10]) by mira.cbaines.net (Postfix) with ESMTPSA id 7CFE327BBF2 for <70494@debbugs.gnu.org>; Sun, 21 Apr 2024 10:42:47 +0100 (BST) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 5123b2fe for <70494@debbugs.gnu.org>; Sun, 21 Apr 2024 09:42:47 +0000 (UTC) From: Christopher Baines Date: Sun, 21 Apr 2024 10:42:28 +0100 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: <87bk632h36.fsf@cbaines.net> References: <87bk632h36.fsf@cbaines.net> MIME-Version: 1.0 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+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/store/database.scm (valid-path, all-valid-paths, valid-path-from-hash-part, valid-path-references): New procedures. Change-Id: Ib08837ee20f5a5a24a8089e611b5d67b003b62cc --- guix/store/database.scm | 88 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/guix/store/database.scm b/guix/store/database.scm index 07bd501644..8a3436368e 100644 --- a/guix/store/database.scm +++ b/guix/store/database.scm @@ -55,9 +55,13 @@ (define-module (guix store database) %epoch reset-timestamps vacuum-database + valid-path + all-valid-paths + valid-path-from-hash-part outputs-exist? file-closure - all-transitive-inputs)) + all-transitive-inputs + valid-path-references)) ;;; Code for working with the store database directly. @@ -447,6 +451,63 @@ (define (vacuum-database) (sqlite-exec db "VACUUM;") (sqlite-close db))) +(define (valid-path db store-filename) + (let ((statement + (sqlite-prepare + db + " +SELECT id, hash, registrationTime, deriver, narSize +FROM ValidPaths +WHERE path = :path" + #:cache? #t))) + + (sqlite-bind-arguments + statement + #:path store-filename) + + (let ((result (sqlite-step statement))) + (sqlite-reset statement) + + result))) + +(define (all-valid-paths db) + (let ((statement + (sqlite-prepare + db + " +SELECT path FROM ValidPaths" + #:cache? #t))) + + (let ((result + (sqlite-map + (match-lambda + (#(path) path)) + statement))) + (sqlite-reset statement) + + result))) + +(define (valid-path-from-hash-part db hash) + (let ((statement + (sqlite-prepare + db + " +SELECT path FROM ValidPaths WHERE path >= :path LIMIT 1" + #:cache? #t)) + (path-prefix + (string-append (%store-prefix) "/" hash))) + + (sqlite-bind-arguments + statement + #:path path-prefix) + + (let ((result + (sqlite-step statement))) + + (if (and result (string-prefix? path-prefix result)) + result + #f)))) + (define (outputs-exist? db drv-path outputs) "Determine whether all output labels in OUTPUTS exist as built outputs of DRV-PATH." @@ -527,3 +588,28 @@ (define (all-transitive-inputs db drv) vlist-null `(,@(derivation-sources drv) ,@input-paths))))) + +(define (valid-path-references db valid-path-id) + (let ((statement + (sqlite-prepare + db + " +SELECT ValidPaths.path +FROM Refs +INNER JOIN ValidPaths ON Refs.reference = ValidPaths.id +WHERE referrer = :id" + #:cache? #t))) + + (sqlite-bind-arguments + statement + #:id valid-path-id) + + (let ((result (sqlite-fold + (lambda (row result) + (cons (vector-ref row 0) + result)) + '() + statement))) + (sqlite-reset statement) + + result)))