       (ffirst results))))
 
 (defn datoms-of-attrs-as-of
+  "Returns the datoms from the log where each datom's entity id matches entid
+  and its attribute entity ID is of an attribute entity ID belonging to attrs,
+  and with a transaction date as of as-of-inst."
   [conn entid attrs as-of-inst]
   (let [attr-entids (map #(entity-id-for-schema-attribute conn %) attrs)]
     (let [txns (->> (seq (d/tx-range (d/log conn) as-of-inst nil))

                     (when last-datom
                       (not (.added last-datom)))))
                 attr-entids))))

                            []))]
      [txns attr-entids])))

(defn entities-of-attrs-as-of
  "Returns the set of entity IDs (including retracted ones) that contains at
  least one attribute from attrs, and exists as of as-of-inst."
  [conn attrs as-of-inst]
  (let [attr-entids (map #(entity-id-for-schema-attribute conn %) attrs)]
    (let [txns (->> (seq (d/tx-range (d/log conn) as-of-inst nil))
                    (reduce (fn [txns txn]
                              (let [{datums :data} txn]
                                (concat txns
                                        (filter #(some #{(.a %)} attr-entids)
                                                datums))))
                            []))]
      (distinct (map #(.e %) txns)))))

(defn entities-updated-as-of
  ([conn
    as-of-instant
    attrs]
   (entities-updated-as-of conn as-of-instant attrs nil))
  ([conn
    as-of-instant
    attrs
    transform-fn]
   (let [db (d/db conn)
         log (d/log conn)]
     (reduce (fn [entities attr]
               (let [qry '[:find ?e ?op
                           :in ?log ?t1 ?attr $
                           :where [(tx-ids ?log ?t1 nil) [?tx ...]]
                           [(tx-data ?log ?tx) [[?e _ _ _ ?op]]]
                           [$ ?e ?attr _]]
                     results (d/q qry log as-of-instant attr db)]
                 (if (> (count results) 0)
                   (apply conj
                          entities
                          (map (fn [result-tuple]
                                 (let [user-entid (first result-tuple)
                                       entity (into {:db/id user-entid} (d/entity db user-entid))]
                                   (if transform-fn
                                     (transform-fn entity)
                                     entity)))
                               results))
                   entities)))
             []
             attrs))))

(defn are-attributes-retracted-as-of
  [conn entid attrs as-of-inst]
  (let [[datoms attr-entids] (datoms-of-attrs-as-of conn entid attrs as-of-inst)]
