Skip to content

Instantly share code, notes, and snippets.

@a2ndrade
Last active December 17, 2015 14:29
Show Gist options
  • Save a2ndrade/5625075 to your computer and use it in GitHub Desktop.
Save a2ndrade/5625075 to your computer and use it in GitHub Desktop.
Datomic: DB Functions & Java Compilation Errors
import java.util.Map;
import java.util.UUID;
import datomic.Connection;
import datomic.Peer;
import datomic.Util;
public class JavaDbFunction {
/**
* The DB function is purposely useless since it's only used to
* demonstrate that Java compiler warnings prevent successful compilation
* of DB functions.
*/
public static void main(String[] args) {
String uri = "datomic:mem://" + UUID.randomUUID();
Peer.createDatabase(uri);
Connection conn = Peer.connect(uri);
// Compiler warnings (e.g. "rawtypes") prevent successful compilation. See
// http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/com/sun/tools/javac/util/MandatoryWarningHandler.java#112
Map iFailFn = Util.map("lang","java","params", Util.list("db", "k", "v"),
"code",
"import java.util.*;"+
"Map m = new HashMap();"+
"m.put(k,v);"+
"return list();");
conn.transact(Util.list(Util.map(":db/id", Peer.tempid("db.part/user"), ":db/ident",
":iFail", ":db/fn", Peer.function(iFailFn))));
try {
conn.transact(Util.list(Util.list(":iFail", "X", "Y"))).get();
throw new Error("Unreachable");
} catch (Exception e) {
e.printStackTrace(System.err);
}
// Need to force generic java.lang.Object to make compiler happy
Map iPassFn = Util.map("lang","java","params", Util.list("db", "k", "v"),
"code",
"import java.util.*;"+
"Map<Object,Object> m = new HashMap<Object,Object>();"+
"m.put(k,v);"+
"return list();");
conn.transact(Util.list(Util.map(":db/id", Peer.tempid("db.part/user"), ":db/ident",
":iPass", ":db/fn", Peer.function(iPassFn))));
try {
conn.transact(Util.list(Util.list(":iPass", "X", "Y"))).get();
} catch (Exception e) {
throw new Error("Unreachable");
}
System.out.println("Done.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment