Change: (org-ql-query) Call --select-cached with query, not pred

Since we will be pre-processing and modifying the query, the predicate
may not correspond exactly to the query, so we should key the cache on
the original query.
This commit is contained in:
Adam Porter 2019-07-16 09:31:48 -05:00
parent 014e0f1388
commit 3adaf4e5fc

View file

@ -209,7 +209,7 @@ a list of defined `org-ql' sorting methods: `date', `deadline',
(--map (with-current-buffer it (--map (with-current-buffer it
(unless (derived-mode-p 'org-mode) (unless (derived-mode-p 'org-mode)
(user-error "Not an Org buffer: %s" (buffer-name))) (user-error "Not an Org buffer: %s" (buffer-name)))
(org-ql--select-cached :predicate predicate :action action :narrow narrow))) (org-ql--select-cached :query query :predicate predicate :action action :narrow narrow)))
;; Flatten items ;; Flatten items
(-flatten-n 1)))) (-flatten-n 1))))
;; Sort items ;; Sort items
@ -230,27 +230,29 @@ a list of defined `org-ql' sorting methods: `date', `deadline',
"Return results for ARGS and current buffer using cache." "Return results for ARGS and current buffer using cache."
;; MAYBE: Timeout cached queries. Probably not necessarily since they will be removed when a ;; MAYBE: Timeout cached queries. Probably not necessarily since they will be removed when a
;; buffer is closed, or when a query is run after modifying a buffer. ;; buffer is closed, or when a query is run after modifying a buffer.
(if-let* ((buffer-cache (gethash (current-buffer) org-ql-cache)) (-let (((&plist :query query :action action :narrow narrow) args))
(query-cache (cadr buffer-cache)) (if-let* ((buffer-cache (gethash (current-buffer) org-ql-cache))
(modified-tick (car buffer-cache)) (query-cache (cadr buffer-cache))
(buffer-unmodified-p (eq (buffer-modified-tick) modified-tick)) (modified-tick (car buffer-cache))
(cached-result (gethash args query-cache))) (buffer-unmodified-p (eq (buffer-modified-tick) modified-tick))
(pcase cached-result (cache-key (list query action narrow))
('org-ql-nil nil) (cached-result (gethash cache-key query-cache)))
(_ cached-result)) (pcase cached-result
(let ((new-result (apply #'org-ql--select args))) ('org-ql-nil nil)
(cond ((or (not query-cache) (_ cached-result))
(not buffer-unmodified-p)) (let ((new-result (apply #'org-ql--select args)))
(puthash (current-buffer) (cond ((or (not query-cache)
(list (buffer-modified-tick) (not buffer-unmodified-p))
(let ((table (make-hash-table :test 'org-ql-hash-test))) (puthash (current-buffer)
(puthash args (or new-result 'org-ql-nil) table) (list (buffer-modified-tick)
table)) (let ((table (make-hash-table :test 'org-ql-hash-test)))
org-ql-cache)) (puthash args (or new-result 'org-ql-nil) table)
(t (puthash args (or new-result 'org-ql-nil) query-cache))) table))
new-result))) org-ql-cache))
(t (puthash args (or new-result 'org-ql-nil) query-cache)))
new-result))))
(cl-defun org-ql--select (&key predicate action narrow) (cl-defun org-ql--select (&key predicate action narrow &allow-other-keys)
"Return results of mapping function ACTION across entries in current buffer matching function PREDICATE. "Return results of mapping function ACTION across entries in current buffer matching function PREDICATE.
If NARROW is non-nil, buffer will not be widened." If NARROW is non-nil, buffer will not be widened."
;; Since the mappings are stored in the variable `org-ql-predicates', macros like `flet' ;; Since the mappings are stored in the variable `org-ql-predicates', macros like `flet'