2008-02-29 00:37:38 +01:00
|
|
|
;; Tekuti
|
2012-02-11 18:38:21 +01:00
|
|
|
;; Copyright (C) 2008, 2010, 2012 Andy Wingo <wingo at pobox dot com>
|
2008-02-29 00:37:38 +01:00
|
|
|
|
2012-02-12 20:48:50 +01:00
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
|
|
;; modify it under the terms of the GNU General Public License as
|
|
|
|
;; published by the Free Software Foundation; either version 3 of
|
|
|
|
;; the License, or (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
2008-02-29 00:37:38 +01:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program; if not, contact:
|
|
|
|
;;
|
|
|
|
;; Free Software Foundation Voice: +1-617-542-5942
|
|
|
|
;; 59 Temple Place - Suite 330 Fax: +1-617-542-2652
|
|
|
|
;; Boston, MA 02111-1307, USA gnu@gnu.org
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; Indexing of the persistent data store.
|
|
|
|
;;
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(define-module (tekuti index)
|
2017-03-02 16:28:55 +01:00
|
|
|
#:use-module (ice-9 match)
|
2008-02-29 00:37:38 +01:00
|
|
|
#:use-module ((srfi srfi-1) #:select (fold))
|
2012-02-11 18:38:21 +01:00
|
|
|
#:use-module (system repl error-handling)
|
2008-02-29 00:37:38 +01:00
|
|
|
#:use-module (tekuti util)
|
|
|
|
#:use-module (tekuti git)
|
|
|
|
#:use-module (tekuti post)
|
|
|
|
#:use-module (tekuti tags)
|
2010-12-05 20:23:01 +01:00
|
|
|
#:use-module (tekuti cache)
|
2017-03-06 09:25:39 +01:00
|
|
|
#:use-module (tekuti classifier)
|
2010-12-05 20:23:01 +01:00
|
|
|
#:export (maybe-reindex read-index update-index))
|
2008-02-29 00:37:38 +01:00
|
|
|
|
2017-03-06 09:25:39 +01:00
|
|
|
;; Additionally an index has an "index" field, indicating the commit
|
|
|
|
;; that it was saved in, and a "master" field, indicating the commit
|
|
|
|
;; that it indexes.
|
2008-02-29 00:37:38 +01:00
|
|
|
(define index-specs
|
2017-03-06 09:25:39 +01:00
|
|
|
`((master #f ,write ,read)
|
|
|
|
(posts ,reindex-posts ,write-hash ,read-hash)
|
stop hitting git when fetching post metadata
Inspired by a patch from Aleix Conchillo Flaqué. Thanks, Aleix!
* tekuti/index.scm (index-specs):
* tekuti/post.scm (reindex-posts, reindex-posts-by-date): Change to have
the "posts" index be a hash table mapping key -> post.
"posts-by-date" is a list of keys, from newest to oldest.
(post-from-git): This is what post-from-key was.
(post-from-key): New function, pulls out a post from the posts hash
table.
(munge-post): Use post-from-git.
(delete-post): Take the post directly.
(latest-posts): New awesome helper for fetching the lastest N posts
that match some predicates.
* tekuti/page-helpers.scm (find-posts-matching): Adapt to post-from-key
change.
(published-posts): Remove, replaced with latest-posts.
* tekuti/page.scm (page-admin, page-admin-posts, page-admin-post)
(page-archives, page-feed-atom): Use the new latest-posts helper.
(page-admin-delete-post, page-admin-delete-comment, page-show-post)
(page-new-comment, page-show-tag): Adapt to post-from-key change.
* tekuti/tags.scm (compute-related-posts, compute-related-tags): Adapt
to post-from-key change.
(reindex-tags): Use latest-posts.
2012-02-11 18:54:51 +01:00
|
|
|
(posts-by-date ,reindex-posts-by-date ,write ,read)
|
2010-12-05 20:23:01 +01:00
|
|
|
(tags ,reindex-tags ,write-hash ,read-hash)
|
2017-03-06 09:25:39 +01:00
|
|
|
(legit-comments ,reindex-legit-comments ,write-hash ,read-hash)
|
|
|
|
(bogus-comments ,reindex-bogus-comments ,write-hash ,read-hash)
|
|
|
|
(classifier ,reindex-classifier #f #f)
|
|
|
|
(cache ,(lambda _ (make-empty-cache)) #f #f)))
|
2008-02-29 00:37:38 +01:00
|
|
|
|
|
|
|
(define (reindex oldindex master)
|
2017-03-06 09:25:39 +01:00
|
|
|
;; Leave off "index" field.
|
2010-11-13 18:41:23 +01:00
|
|
|
(with-time-debugging
|
2017-03-06 09:25:39 +01:00
|
|
|
(fold (lambda (spec index)
|
|
|
|
(match spec
|
|
|
|
((key reindex write read)
|
|
|
|
(acons key (with-time-debugging (begin (pk key) (reindex oldindex index))) index))))
|
2010-11-13 18:41:23 +01:00
|
|
|
(acons 'master master '())
|
2017-03-06 09:25:39 +01:00
|
|
|
;; Skip past "master" as we handle that one specially.
|
|
|
|
(match index-specs
|
|
|
|
((('master . _) . specs) specs)))))
|
2008-02-29 00:37:38 +01:00
|
|
|
|
|
|
|
(define (write-index index oldref)
|
|
|
|
(let ((new (git-commit-tree
|
|
|
|
(git-mktree
|
2010-12-05 20:23:01 +01:00
|
|
|
(let lp ((index index))
|
2017-03-06 09:25:39 +01:00
|
|
|
(match index
|
|
|
|
(() '())
|
|
|
|
(((k . v) . index)
|
|
|
|
(match (assq k index-specs)
|
|
|
|
((_ reindex write read)
|
|
|
|
(if write
|
|
|
|
(cons (list k (with-output-to-blob (write v)) 'blob)
|
|
|
|
(lp index))
|
|
|
|
(lp index)))
|
|
|
|
(_ (lp index)))))))
|
2008-02-29 00:37:38 +01:00
|
|
|
oldref "reindex\n"
|
|
|
|
(commit-utc-timestamp (assq-ref index 'master)))))
|
|
|
|
(or (false-if-git-error
|
|
|
|
(git "update-ref" "refs/heads/index" new (or oldref "")))
|
|
|
|
(warn "could not update indexes ref"))
|
|
|
|
new))
|
|
|
|
|
|
|
|
(define (read-index)
|
2017-03-06 09:25:39 +01:00
|
|
|
(pk 'reading-index)
|
2017-03-02 16:28:55 +01:00
|
|
|
(match (false-if-git-error (git-rev-parse "refs/heads/index"))
|
|
|
|
(#f (maybe-reindex '()))
|
|
|
|
(ref
|
|
|
|
(let ((dents (git-ls-tree ref #f)))
|
2017-03-06 09:25:39 +01:00
|
|
|
(fold (lambda (spec index)
|
|
|
|
(match spec
|
|
|
|
((key reindex write read)
|
|
|
|
(pk 'read-index-key key)
|
|
|
|
(acons key
|
|
|
|
(cond
|
|
|
|
((and read (assoc (symbol->string key) dents))
|
|
|
|
=> (match-lambda
|
|
|
|
((_ sha1 'blob)
|
|
|
|
(with-input-from-blob sha1 (read)))))
|
|
|
|
(else
|
|
|
|
(reindex '() index)))
|
|
|
|
index))))
|
|
|
|
`((index . ,ref))
|
|
|
|
index-specs)))))
|
2008-02-29 00:37:38 +01:00
|
|
|
|
|
|
|
(define (maybe-reindex old-index)
|
2010-12-05 20:23:01 +01:00
|
|
|
(let ((master (git-rev-parse "refs/heads/master")))
|
|
|
|
(if (equal? (assq-ref old-index 'master) master)
|
2008-02-29 00:37:38 +01:00
|
|
|
old-index
|
2012-02-11 18:38:21 +01:00
|
|
|
(call-with-error-handling
|
|
|
|
(lambda ()
|
|
|
|
(let ((new-index (reindex old-index master)))
|
|
|
|
(acons
|
|
|
|
'index (write-index new-index (assq-ref old-index 'index))
|
|
|
|
new-index)))
|
|
|
|
#:on-error 'backtrace
|
|
|
|
#:post-error (lambda _ old-index)))))
|
2010-12-05 20:23:01 +01:00
|
|
|
|
|
|
|
(define (update-index index key update)
|
|
|
|
(cond
|
|
|
|
((null? index) (acons key (update '()) '()))
|
|
|
|
((eq? (caar index) key) (acons key (update index) (cdr index)))
|
|
|
|
(else (cons (car index) (update-index (cdr index) key update)))))
|