From patchwork Thu Feb 27 20:41:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Arun Isaac X-Patchwork-Id: 20459 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 0015A27BBE4; Thu, 27 Feb 2020 20:43:19 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTP id D660527BBEE for ; Thu, 27 Feb 2020 20:43:18 +0000 (GMT) Received: from localhost ([::1]:38032 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7Q06-0005Fi-Cx for patchwork@mira.cbaines.net; Thu, 27 Feb 2020 15:43:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59501) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7Pzs-0005F8-Mz for guix-patches@gnu.org; Thu, 27 Feb 2020 15:43:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7Pzr-0008Vb-G5 for guix-patches@gnu.org; Thu, 27 Feb 2020 15:43:04 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:54385) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j7Pzr-0008VU-Cl for guix-patches@gnu.org; Thu, 27 Feb 2020 15:43:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1j7Pzr-0008Kl-AZ for guix-patches@gnu.org; Thu, 27 Feb 2020 15:43:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#39258] [PATCH 4/4] gnu: Use xapian index for package search. Resent-From: Arun Isaac Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 27 Feb 2020 20:43:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 39258 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: 39258@debbugs.gnu.org Cc: Arun Isaac , ludo@gnu.org, zimon.toutoune@gmail.com Received: via spool by 39258-submit@debbugs.gnu.org id=B39258.158283614031946 (code B ref 39258); Thu, 27 Feb 2020 20:43:03 +0000 Received: (at 39258) by debbugs.gnu.org; 27 Feb 2020 20:42:20 +0000 Received: from localhost ([127.0.0.1]:60351 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1j7PzA-0008J1-0f for submit@debbugs.gnu.org; Thu, 27 Feb 2020 15:42:20 -0500 Received: from mugam.systemreboot.net ([139.59.75.54]:37294) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1j7Pz5-0008II-Gz for 39258@debbugs.gnu.org; Thu, 27 Feb 2020 15:42:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=systemreboot.net; s=default; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=sQEq9FYeMCTpeCbSa0BT6wsOvHihjAdebVbxE4b6Xv0=; b=bKhUzRcGGgmRLO/tm9x6FZnZcW bThUjGRDJfczKHWi5PQTz+OMXPgjEoQz6nd8m0hlmkhQmv24gRbDHZAM7YWZW/oeMGiQxw7YsFMx0 Nxi30o2OIP32Xu1mksoUUGmUhblw11KvXPZh8tDxgt0tXEjlNFRtLJjS8EcF8siBXvW0=; Received: from [192.168.2.1] (helo=steel.lan) by systemreboot.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1j7Pyz-001ckB-W4; Fri, 28 Feb 2020 02:12:10 +0530 From: Arun Isaac Date: Fri, 28 Feb 2020 02:11:50 +0530 Message-Id: <20200227204150.30985-5-arunisaac@systemreboot.net> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200227204150.30985-1-arunisaac@systemreboot.net> References: <20200227204150.30985-1-arunisaac@systemreboot.net> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 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" X-getmail-retrieved-from-mailbox: Patches * gnu/packages.scm (search-package-index): New function. * guix/scripts/package.scm (find-packages-by-description): Search using the xapian package index if search patterns are literal strings. Else, search using fold-packages. --- gnu/packages.scm | 17 +++++++++++- guix/scripts/package.scm | 57 +++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/gnu/packages.scm b/gnu/packages.scm index e91753e2a8..5b5b29bf84 100644 --- a/gnu/packages.scm +++ b/gnu/packages.scm @@ -67,7 +67,8 @@ specifications->manifest generate-package-cache - generate-package-search-index)) + generate-package-search-index + search-package-index)) ;;; Commentary: ;;; @@ -453,6 +454,20 @@ reducing the memory footprint." db-path) +(define (search-package-index profile querystring) + (let ((offset 0) + (pagesize 10)) + (call-with-database (string-append profile %package-search-index) + (lambda (db) + (let ((query (parse-query querystring #:stemmer (make-stem "en")))) + (mset-fold (lambda (item result) + (match (find-packages-by-name + (document-data (mset-item-document item))) + ((package _ ...) + (append result `((,package . ,(mset-item-weight item))))))) + '() + (enquire-mset (enquire db query) offset pagesize))))))) + (define %sigint-prompt ;; The prompt to jump to upon SIGINT. diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm index 1cb0d382bf..6a3b9002dd 100644 --- a/guix/scripts/package.scm +++ b/guix/scripts/package.scm @@ -7,6 +7,7 @@ ;;; Copyright © 2016 Benz Schenk ;;; Copyright © 2016 Chris Marusich ;;; Copyright © 2019 Tobias Geerinckx-Rice +;;; Copyright © 2020 Arun Isaac ;;; ;;; This file is part of GNU Guix. ;;; @@ -178,31 +179,40 @@ hooks\" run when building the profile." ;;; Package specifications. ;;; -(define (find-packages-by-description regexps) +(define (find-packages-by-description patterns) "Return a list of pairs: packages whose name, synopsis, description, or output matches at least one of REGEXPS sorted by relevance, and its non-zero relevance score." - (let ((matches (fold-packages (lambda (package result) - (if (package-superseded package) - result - (match (package-relevance package - regexps) - ((? zero?) - result) - (score - (cons (cons package score) - result))))) - '()))) - (sort matches - (lambda (m1 m2) - (match m1 - ((package1 . score1) - (match m2 - ((package2 . score2) - (if (= score1 score2) - (string>? (package-full-name package1) - (package-full-name package2)) - (> score1 score2)))))))))) + (define (regexp? str) + (string-any + (char-set #\. #\[ #\{ #\} #\( #\) #\\ #\* #\+ #\? #\| #\^ #\$) + str)) + + (if (and (current-profile) + (not (any regexp? patterns))) + (search-package-index (current-profile) (string-join patterns " ")) + (let* ((regexps (map (cut make-regexp* <> regexp/icase) patterns)) + (matches (fold-packages (lambda (package result) + (if (package-superseded package) + result + (match (package-relevance package + regexps) + ((? zero?) + result) + (score + (cons (cons package score) + result))))) + '()))) + (sort matches + (lambda (m1 m2) + (match m1 + ((package1 . score1) + (match m2 + ((package2 . score2) + (if (= score1 score2) + (string>? (package-full-name package1) + (package-full-name package2)) + (> score1 score2))))))))))) (define (transaction-upgrade-entry store entry transaction) "Return a variant of TRANSACTION that accounts for the upgrade of ENTRY, a @@ -777,8 +787,7 @@ processed, #f otherwise." (('query 'search rx) rx) (_ #f)) opts)) - (regexps (map (cut make-regexp* <> regexp/icase) patterns)) - (matches (find-packages-by-description regexps))) + (matches (find-packages-by-description patterns))) (leave-on-EPIPE (display-search-results matches (current-output-port))) #t))