d12frosted / vulpea

A collection of functions for note taking based on `org` and `org-roam`.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Combining conditionals in vulpea-db-query

dmitrym0 opened this issue · comments

Hello @d12frosted

I'm curious if there's a better way to do this, I suspect vulpea-db-query can do it but I can't quite wrap my head around it.

Every time I read a book, I insert a link to the year (2022 note) and the books note (books). I want to see all the book I've read in 2022.

This is what I came up with, is there a better way via vulpea-db-query?

  ;; get everything linking to 2022 note
  (setq twenty22 (vulpea-db-query-by-links-some
                 (list (cons "id" "6DB243A1-031D-4447-8C3F-70615A2237DB" ))))

  ;; get everything linking to books note
  (setq books (vulpea-db-query-by-links-some
               (list (cons "id" "D67FF1DE-AD9E-4DEB-846B-C36BF011D443"))))

  ;; extract the ones that link to both (intersection)
  (setq dm/vulpea-results (seq-intersection twenty22 books
                                            '(lambda (el1 el2)
                                               (equal (vulpea-note-id el1) (vulpea-note-id el2)))))

  ;; do stuff with it
  (seq-do (lambda (note)
            (goto-char (point-max))
            (insert (format "%s\n" (vulpea-note-title note))))
          dm/vulpea-results)

Hello @dmitrym0,

There are two helper functions that allow to query by cross-links: vulpea-db-query-by-links-some and vulpea-db-query-by-links-every. Both of them take a list of links and return either 'union' or 'intersection' respectively.

So if you want to find all notes that contain links to both books and 2022 notes then use vulpea-db-query-by-links-every:

(vulpea-db-query-by-links-every
 '(("id" . "D67FF1DE-AD9E-4DEB-846B-C36BF011D443")
   ("id" . "6DB243A1-031D-4447-8C3F-70615A2237DB")))

And you are right that you can use vulpea-db-query as it's the most powerful function in terms of 'querying' on the Emacs Lisp level. Specialized functions (like vulpea-db-query-by-links-every) exist for performance reasons as they are optimized for querying by specific slots. There is some information here - https://d12frosted.io/posts/2022-07-14-vulpea-0.3.html.

;; slower than `vulpea-db-query-by-links-every' as `vulpea-db-query' reads ALL entries.
;; on smaller notes set the difference might be neglectable
;; p.s. it would be nice if `vulpea' provided functions like `vulpea-note-tagged-all-p` for links.
(vulpea-db-query
 (lambda (note)
   (let ((links (vulpea-note-links note)))
     (and (seq-contains-p links '("id" . "D67FF1DE-AD9E-4DEB-846B-C36BF011D443"))
          (seq-contains-p links '("id" . "6DB243A1-031D-4447-8C3F-70615A2237DB"))))))

Hope that helps :)

Wonderful, thank you!

Your version is significantly faster.