Created
May 10, 2012 05:22
-
-
Save stuarthalloway/2651196 to your computer and use it in GitHub Desktop.
Datomic queries against Java collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Datomic example code | |
package datomic.examples; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Set; | |
import static datomic.Peer.*; | |
import static datomic.Util.*; | |
public class BindingExamples { | |
public static void printRelation(Collection stuff) { | |
for (Iterator iterator = stuff.iterator(); iterator.hasNext();) { | |
List tuple = (List) iterator.next(); | |
for (Iterator itt = tuple.iterator(); itt.hasNext();) { | |
Object next = itt.next(); | |
System.out.print(next); | |
System.out.print(" "); | |
} | |
System.out.println(""); | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println("\nScalar binding..."); | |
// ?answer binds a scalar | |
printRelation(q("[:find ?answer :in ?answer]", 42)); | |
System.out.println("\nBinding 2 variables..."); | |
// bind as many variables as you want | |
printRelation(q("[:find ?last ?first :in ?last ?first]", | |
"Doe", | |
"John")); | |
System.out.println("\nTuple Binding..."); | |
// [?last ?first?] binds a tuple | |
printRelation(q("[:find ?last ?first :in [?last ?first]]", | |
list("Doe", "John"))); | |
System.out.println("\nCollection Binding..."); | |
// [?first ...] binds a collection | |
printRelation(q("[:find ?first :in [?first ...]]", | |
list("John", "Jane", "Phineas"))); | |
System.out.println("\nRelation Binding..."); | |
// [[?first ?last]] binds a relation | |
printRelation(q("[:find ?first :in [[?first ?last]]]", | |
list(list("John", "Doe"), | |
list("Jane", "Doe")))); | |
Object firstName = read(":firstName"); | |
Object lastName = read(":lastName"); | |
Object email = read(":email"); | |
Object height = read(":height"); | |
System.out.println("\nDatabase binding..."); | |
// a database binding name starts with $ instead of ? | |
// any relation with 4-tuples E/A/V/T can act as a database | |
// so in Datomic, you can mock a database with a list of lists | |
printRelation(q("[:find ?first" + | |
" :in $db " + | |
" :where [$db _ :firstName ?first]]", | |
list(list(1, firstName, "John", 42)))); | |
// Similar to previous. You can omit the $db and the :in when | |
// querying against a single database. | |
System.out.println("\nDefault database binding..."); | |
printRelation(q("[:find ?first" + | |
" :where [_ :firstName ?first]]", | |
list(list(1, firstName, "John", 42), | |
list(1, lastName, "Doe", 42)))); | |
// cross-database in-memory join, two database bindings | |
System.out.println("\nIn-memory, cross database join, two database bindings..."); | |
printRelation(q("[:find ?first ?height" + | |
" :in $db1 $db2" + | |
" :where [$db1 ?e1 :firstName ?first]" + | |
" [$db1 ?e1 :email ?email]" + | |
" [$db2 ?e2 :email ?email]" + | |
" [$db2 ?e2 :height ?height]]" , | |
// database 1 | |
list(list(1, firstName, "John"), | |
list(1, email, "[email protected]"), | |
list(2, firstName, "Jane"), | |
list(2, email, "[email protected]")), | |
// database 2 | |
list(list(100, email, "[email protected]"), | |
list(100, height, 73), | |
list(101, email, "[email protected]"), | |
list(101, height, 71) | |
))); | |
// use query for every collection manipulation tasks | |
// compare to http://stackoverflow.com/questions/3717939/iterating-and-processing-an-arraylist | |
System.out.println("\nQuery with filtering predicate..."); | |
printRelation(q("[:find ?car ?speed" + | |
" :in [[?car ?speed]]" + | |
" :where [(> ?speed 100)]]", | |
list(list("Stock", 225), | |
list("Spud", 80), | |
list("Rocket", 400), | |
list("Stock", 225), | |
list("Clunker", 40)))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment