Possible data sources

Showing 1-7 of 7 messages
Possible data sources Thomas G. Kristensen 11/29/12 12:25 AM
Hi all,

I find it hard to figure out which data sources exist for Datomic. We all know the one returned by datomic.api/db, and collections of tuples have cropped up in a few examples. Collections of maps, such as this example

(q '[:find ?m
     :where [?m :foo :bar]]
   [{:foo :bar :n 42}
    {:foo :baz :n 1}])

does not seem to work, even though they could define an interesting [entity attribute value] relation ([the-map-itself a-key the-keys-value]).

Is there a comprehensive list of Datomic data sources?

What protocol is needed to add a data source to the query engine?

Cheers,

Thomas
Re: Possible data sources David Powell 11/29/12 6:50 AM

Does this help:


-- 
Dave



--
-- You received this message because you are subscribed to the Google Groups Datomic group. To post to this group, send email to dat...@googlegroups.com. To unsubscribe from this group, send email to datomic+u...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/datomic?hl=en
 
 

Re: Possible data sources Rich Hickey 11/29/12 6:58 AM
There isn't an integration protocol at the moment.

Any iterable source of indexable (nth-able) collections will work.

So, Java or Clojure collections of vectors/arrays/arraylists/etc


Re: Possible data sources Thomas G. Kristensen 11/29/12 7:14 AM
David, thanks, that was an excellent set of examples.

Rich, thanks for the info. I guess the solution is to take the data and convert it to a collection of tuples before feeding it into datomic.api/q.
Re: Possible data sources Rich Hickey 11/29/12 8:54 AM

On Nov 29, 2012, at 10:14 AM, Thomas G. Kristensen wrote:

> David, thanks, that was an excellent set of examples.
>
> Rich, thanks for the info. I guess the solution is to take the data and convert it to a collection of tuples before feeding it into datomic.api/q.
>

You don't need to convert the data, just provide an indexed view, like this:

(defn maprel [maps & keys]
  (let [ik (zipmap (range) keys)]
    (map #(reify clojure.lang.Indexed
                 (nth [_ i] (if (< i (count keys))
                              (get % (ik i))
                              (throw (IndexOutOfBoundsException.))))
                 (nth [_ i not-found] (get % (ik i) not-found)))
         maps)))

(d/q '[:find ?b :where [?c ?b]]
     (maprel [{:a 1 :b 2 :c 3}
              {:a 11 :b 12 :c 13}
              {:a 21 :b 22 :c 23}
              {:a 31 :b 32 :c 33}]
             :c :b))


> On Thursday, November 29, 2012 3:58:04 PM UTC+1, Rich Hickey wrote:
>
> On Nov 29, 2012, at 3:25 AM, Thomas G. Kristensen wrote:
>
> > Hi all,
> >
> > I find it hard to figure out which data sources exist for Datomic. We all know the one returned by datomic.api/db, and collections of tuples have cropped up in a few examples. Collections of maps, such as this example
> >
> > (q '[:find ?m
> >      :where [?m :foo :bar]]
> >    [{:foo :bar :n 42}
> >     {:foo :baz :n 1}])
> >
> > does not seem to work, even though they could define an interesting [entity attribute value] relation ([the-map-itself a-key the-keys-value]).
> >
> > Is there a comprehensive list of Datomic data sources?
> >
> > What protocol is needed to add a data source to the query engine?
> >
>
> There isn't an integration protocol at the moment.
>
> Any iterable source of indexable (nth-able) collections will work.
>
> So, Java or Clojure collections of vectors/arrays/arraylists/etc
>
>
>
Re: Possible data sources Thomas G. Kristensen 11/30/12 7:03 AM
Ah, good point. That approach makes it difficult to do more advanced queries with joins. Mapping the maps to EAV relations gave me what I was looking for:

(defn maps-eav [maps]
  (apply concat
         (for [map maps]
           (apply concat
                  (for [[a v] map]
                    (if (coll? v)
                      (for [sub-v v]
                        [map a sub-v])
                      [[map a v]]))))))

(d/q '[:find ?email
       :in $1 $2
       :where
       [$1 ?p1 :likes "Chocolate"]
       [$1 ?p1 :name ?name]
       [$2 ?p2 :person/name ?name]
       [$2 ?p2 :person/email ?email]]
     (maps-eav 
      [{:name "Charlie", :likes ["Chocolate"]}
       {:name "Willy Wonka", :likes ["Carrots"]}])
     (maps-eav
      [{:person/name "Charlie", :person/email "cha...@factory.co.uk"}
       {:person/name "Roald Dahl", :person/email "ro...@dahl.com"}]))

Thomas
Re: Possible data sources Jeb Beich 1/28/14 9:15 AM
How does this solution stack up to what's been suggested so far?

(d/q '[:find ?b :where [?c ?b]] 
     (map (juxt :c :b) [{:a 1 :b 2 :c 3} 

              {:a 11 :b 12 :c 13} 
              {:a 21 :b 22 :c 23} 
              {:a 31 :b 32 :c 33}]))