Created
April 8, 2012 11:58
-
-
Save datablend/2336854 to your computer and use it in GitHub Desktop.
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
| package be.datablend; | |
| import datomic.Connection; | |
| import datomic.Database; | |
| import datomic.Peer; | |
| import datomic.Util; | |
| import java.io.FileNotFoundException; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.concurrent.ExecutionException; | |
| /** | |
| * User: Davy Suvee (Datablend.be) | |
| */ | |
| public class DatomicTest { | |
| private Connection conn; | |
| public void createData() throws ExecutionException, InterruptedException { | |
| List tx = new ArrayList(); | |
| // Add entity | |
| tx.add(Util.map(":db/id", Peer.tempid(":db.part/user"), | |
| ":person/firstname", "Davy Test", | |
| ":person/lastname", "Suvee")); | |
| tx.add(Util.map(":db/id", Peer.tempid(":db.part/user"), | |
| ":person/firstname", "John", | |
| ":person/lastname", "Doe Test")); | |
| conn.transact(tx).get(); | |
| } | |
| public void query() { | |
| System.out.println(Peer.q("[:find ?firstname ?lastname :in $ :where [(fulltext $ :person/firstname \"Test\") [[?person ?value]] ]" + | |
| "[?person :person/firstname ?firstname]" + | |
| "[?person :person/lastname ?lastname] ]", conn.db())); | |
| String rules = "[ [ (findname ?person ?search) [(fulltext $ :person/firstname ?search) [[?person ?name]]] ] [ (findname ?person ?search) [(fulltext $ :person/lastname ?search) [[?person ?name]]] ] ]"; | |
| System.out.println(Peer.q("[:find ?firstname ?lastname :in $ % :where (findname ?person \"Test\") " + | |
| "[?person :person/firstname ?firstname] " + | |
| "[?person :person/lastname ?lastname] ]" , conn.db(), rules)); | |
| } | |
| public void setup() throws ExecutionException, InterruptedException, FileNotFoundException { | |
| // Setup the database | |
| String uri = "datomic:mem://test"; | |
| Peer.createDatabase(uri); | |
| conn = Peer.connect(uri); | |
| // Create schema | |
| List tx = new ArrayList(); | |
| tx.add(Util.map(":db/id", Peer.tempid(":db.part/db"), | |
| ":db/ident", ":person/firstname", | |
| ":db/valueType", ":db.type/string", | |
| ":db/cardinality", ":db.cardinality/one", | |
| ":db/doc", "A person's first name", | |
| ":db/fulltext", true, | |
| ":db.install/_attribute", ":db.part/db")); | |
| tx.add(Util.map(":db/id", Peer.tempid(":db.part/db"), | |
| ":db/ident", ":person/lastname", | |
| ":db/valueType", ":db.type/string", | |
| ":db/cardinality", ":db.cardinality/one", | |
| ":db/doc", "A person's last name", | |
| ":db/fulltext", true, | |
| ":db.install/_attribute", ":db.part/db")); | |
| conn.transact(tx).get(); | |
| } | |
| public static void main(String[] args) throws FileNotFoundException, ExecutionException, InterruptedException { | |
| DatomicTest datomic = new DatomicTest(); | |
| datomic.setup(); | |
| datomic.createData(); | |
| datomic.query(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment