Fork me on GitHub
#datomic
<
2022-02-03
>
Benjamin09:02:13

Jo I have a tx (eid) and now I'd like to say as-of 1 before that. Use case is I know a "bad" tx and want to check the db right before I made it.

Benjamin13:02:18

With the client api. Let's say I like pull syntax and I want to pull a lot of entities, do I still use the q ? Because I'm naively querying eids and then pulling them one by one. It's basically just for the convenience of building a map out of the data

favila13:02:04

Could you be more specific? Maybe show some code that’s “I do this” vs “should I do this instead”?

Benjamin14:02:32

;; I do roughly this
(->>
 (d/q
  '[:find
    ?e
    :where
    [?e :bot.discord/thread-id ?thread-id]]
  db)
 (map first)
 (map
  (fn
    [e]
    (d/pull
     db
     '[:bot.discord/user-id
       :bot.discord/thread-id
       {:bot/thread-user
        [:bot.discord/user-id]}]
     e))))

;; and I wonder if I should do this
(->>
 (d/q
  '[:find ?id ?user-id
    :where
    [?e :bot.discord/thread-id ?id]
    [?e :bot/thread-user ?user]
    [?user :bot.discord/user-id ?user-id]
    ;;  ... more
    ]
  (get-db))
 (map
  (fn [[id user-id]]
    {:bot.discord/thread-id id
     :bot.discord/user
     {:bot.discord/user-id user-id}})))

Benjamin14:02:19

The inconvenient part is building the map in the second part of the second example

favila14:02:48

these are slightly different. The second one will never produce a nil/absent user-id, the first will

Benjamin14:02:13

yea actually I realized that is also a constraint for my use case. I do want the nil

Benjamin14:02:49

aside from that I found the :keys second arg to q

favila14:02:24

you should probably be doing something like this

favila14:02:26

(->> (d/q  ; or qseq
      '[:find (pull ?e pull-expr)
        :in $ pull-expr
        :where [?e :bot.discord/thread-id ?thread-id]]
      db
      '[:bot.discord/user-id
        :bot.discord/thread-id
        {:bot/thread-user
         [:bot.discord/user-id]}])
     (map peek))

👀 1
favila14:02:50

evaluate the pull in the query, possibly parameterize the pull

Benjamin14:02:28

ah sweet I didn't know yet I can use pull in find

Benjamin14:02:02

qseq returns a lazy seq right that's the difference?

favila14:02:16

using pull in query is more important on client api than on-prem. On the client api, each pull is another (possible) network round-trip

favila14:02:38

qseq evaluates the result set eagerly, but does the pull only as it’s consumed

favila14:02:13

it reduces latency to time-to-first-result, and can avoid doing IO for results you don’t consume

Benjamin14:02:14

cool thanks

Benjamin14:02:37

your instinct to parameterize the pull-expr was really nice because I could plug my old code into it, beautiful

jdkealy20:02:19

What happens if i configure a transactor using a host and alt-host and then i want to move the host / alt host ? For example, i start the transactor, it writes its location to storage, then I use storage and transactor in prod, then someone for whatever reason wants to switch the transactor host. If I start a new transactor with a new host / alt host, will it overwrite its location in storage? If not, how do i get around this ? Would you go into postgres/dynamo/whatever and update the map with the new transactor host ? One such reason might be, you used the public hostname for an EC2 instance and then you needed to restart and you got assigned a new address, or you were using a CNAME and forgot to renew your registration in godaddy and lost the host name.

jaret20:02:08

Jdkealy, host and alt-host are written by the transactor to storage. Peers attempt to connect to host and then alt-host. You should be able to launch a new transactor serving the same system with new host/alt-host values (that transactor will be in standby mode) and then kill your old transactor and HA failover will make the standby the active transactor and it will write it's value to storage to allow peers to locate it. Peers will then connect provided they have the proper permissions to connect there. https://docs.datomic.com/on-prem/operation/deployment.html#upgrading-live-system

jaret20:02:03

In this scenario your sql-url will not change so you are still running a transactor against that storage, but the transactor will have new host/alt host values