From 719bf34be42a0156d194f17bd76c2ef94fb424b3 Mon Sep 17 00:00:00 2001
From: Arun Isaac
Date: Mon, 4 Jul 2022 00:32:37 +0530
Subject: bin: Separate out indexing into new function.

* bin/tissue (index): New function.
(main): Use index.
---
 bin/tissue | 56 ++++++++++++++++++++++++++++++--------------------------
 1 file changed, 30 insertions(+), 26 deletions(-)

(limited to 'bin')

diff --git a/bin/tissue b/bin/tissue
index bcc85f9..60e3769 100755
--- a/bin/tissue
+++ b/bin/tissue
@@ -304,6 +304,34 @@ To get usage information for one of these sub-commands, run
                        (negate (cut member <> (list "." "..")))))
     (rmdir directory)))
 
+(define (index db-path)
+  "Index current repository into xapian database at DB-PATH. If DB-PATH
+already exists and is up to date, do nothing."
+  (let ((current-head
+         (oid->string (reference-name->oid (current-git-repository) "HEAD"))))
+    (unless (and (file-exists? db-path)
+                 (string=? (call-with-database db-path
+                             (cut Database-get-metadata <> "commit"))
+                           current-head))
+      (guard (c (else (delete-directory db-path)
+                      (format (current-error-port)
+                              "Building xapian index failed.~%")
+                      (raise c)))
+        (delete-directory db-path)
+        (call-with-writable-database db-path
+          (lambda (db)
+            (for-each (lambda (indexed-document)
+                        (let* ((document (slot-set ((indexed-document-reader indexed-document))
+                                                   'web-uri
+                                                   (indexed-document-web-uri indexed-document)))
+                               (term-generator (document-term-generator document)))
+                          (index-text! term-generator (document-type document) #:prefix "XT")
+                          (replace-document! db
+                                             (document-id-term document)
+                                             (TermGenerator-get-document term-generator))))
+                      (tissue-configuration-indexed-documents (load-config)))
+            (WritableDatabase-set-metadata db "commit" current-head)))))))
+
 (define main
   (match-lambda*
     ((_ (or "-h" "--help"))
@@ -318,32 +346,8 @@ To get usage information for one of these sub-commands, run
              ;; Create hidden tissue directory unless it exists.
              (unless (file-exists? %state-directory)
                (mkdir %state-directory))
-             ;; Ensure index exists rebuilding it if it is stale.
-             (let ((current-head
-                    (oid->string (reference-name->oid
-                                  (current-git-repository) "HEAD"))))
-               (unless (and (file-exists? %xapian-index)
-                            (string=? (call-with-database %xapian-index
-                                        (cut Database-get-metadata <> "commit"))
-                                      current-head))
-                 (guard (c (else (delete-directory %xapian-index)
-                                 (display "Building xapian index failed."
-                                          (current-error-port))
-                                 (raise c)))
-                   (delete-directory %xapian-index)
-                   (call-with-writable-database %xapian-index
-                     (lambda (db)
-                       (for-each (lambda (indexed-document)
-                                   (let* ((document (slot-set ((indexed-document-reader indexed-document))
-                                                              'web-uri
-                                                              (indexed-document-web-uri indexed-document)))
-                                          (term-generator (document-term-generator document)))
-                                     (index-text! term-generator (document-type document) #:prefix "XT")
-                                     (replace-document! db
-                                                        (document-id-term document)
-                                                        (TermGenerator-get-document term-generator))))
-                                 (tissue-configuration-indexed-documents config))
-                       (WritableDatabase-set-metadata db "commit" current-head))))))
+             ;; Ensure index exists, rebuilding it if it is stale.
+             (index %xapian-index)
              ;; Handle sub-command.
              (apply (match command
                       ("search" tissue-search)
-- 
cgit v1.2.3