Datomic queries and laziness

I was surprised to find that query results in datomic are not lazy when entities.

Is there an obvious rationale for this choice that I do not see? It seems reasonable that someone wants to want to (call some-fn (take 100 query-result-containing-millions)), but it will make you evaluate the whole set of entity identifiers, no?

Is there a way to get lazy seq (entity identifiers) directly back from the request, or do they always need to be loaded into memory first, from laziness, accessible only through the entity?

+6
source share
1 answer

You can use datomic.api/datoms fn to access objects in a lazy way.

Please note that you need to specify the type of index when calling datoms , and the types of indexes available to you depend on the type of attribute you are interested in. For example, the :avet index is only available if your attribute has :db/index set in the schema, and the :vaet is only available if your attribute is of the type :db.type/ref .

We use something similar at work (note: for this there must be a ref-attr attribute ref-attr ):

 (defn datoms-by-ref-value "Returns a lazy seq of all the datoms in the database matching the given reference attribute value." [db ref-attr value] (d/datoms db :vaet value ref-attr)) 

The datoms documentation datoms bit sparse, but with some trial error, you can probably decide what you need. There's a post from August Lilleaas about using an index :avet (which requires an index for an attribute in a datomic schema), which I found somewhat useful.

+4
source

Source: https://habr.com/ru/post/971847/


All Articles