Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #168 from zznate/closjure

Closjure
  • Loading branch information...
commit 7aec452d0313b00a7105e57e328a6ee0a974c3d8 2 parents 35cf2f1 + c8ab3b6
@zznate authored
View
9 pom.xml
@@ -15,8 +15,8 @@
<slf4j.version>1.7.2</slf4j.version>
<jackson-version>1.9.9</jackson-version>
<groovy-version>2.0.4</groovy-version>
- <vertx-version>1.3.1.final</vertx-version>
- <async-requests-enabled>false</async-requests-enabled>
+ <cloj-version>1.3.0</cloj-version>
+ <vertx-version>1.3.1.final</vertx-version>
</properties>
<dependencies>
@@ -129,6 +129,11 @@
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
</dependency>
+ <dependency>
+ <groupId>org.clojure</groupId>
+ <artifactId>clojure</artifactId>
+ <version>1.5.0</version>
+ </dependency>
</dependencies>
<build>
<plugins>
View
18 src/main/java/org/usergrid/vx/experimental/filter/FactoryProvider.java
@@ -1,16 +1,22 @@
package org.usergrid.vx.experimental.filter;
+import org.usergrid.vx.experimental.filter.clojure.ClojureFilterFactory;
import org.usergrid.vx.experimental.filter.groovy.GroovyFilterFactory;
import org.usergrid.vx.experimental.filter.javascript.JavaScriptFilterFactory;
public class FactoryProvider {
- public FilterFactory getFilterFactory(String spec) {
- switch (spec) {
- case "groovy": return new GroovyFilterFactory();
- case "javascript": return new JavaScriptFilterFactory();
- default: throw new IllegalArgumentException(spec + " is not yet supported for filters");
- }
+ public FilterFactory getFilterFactory(String spec) {
+ switch (spec) {
+ case "groovy":
+ return new GroovyFilterFactory();
+ case "javascript":
+ return new JavaScriptFilterFactory();
+ case "clojure":
+ return new ClojureFilterFactory();
+ default:
+ throw new IllegalArgumentException(spec + " is not yet supported for filters");
}
+ }
}
View
3  src/main/java/org/usergrid/vx/experimental/filter/FilterFactory.java
@@ -1,8 +1,5 @@
package org.usergrid.vx.experimental.filter;
-
public interface FilterFactory {
-
Filter createFilter(String script);
-
}
View
26 src/main/java/org/usergrid/vx/experimental/filter/clojure/ClojureFilter.java
@@ -0,0 +1,26 @@
+package org.usergrid.vx.experimental.filter.clojure;
+
+import java.util.Map;
+import org.usergrid.vx.experimental.filter.Filter;
+import clojure.lang.IPersistentMap;
+import clojure.lang.Var;
+
+public class ClojureFilter implements Filter{
+
+ private Var v;
+
+ public ClojureFilter(Var foo){
+ v=foo;
+ }
+
+ @Override
+ public Map filter(Map row) {
+ IPersistentMap m2 = clojure.lang.RT.map( );
+ for (Object pair : row.entrySet()){
+ Map.Entry e = (Map.Entry) pair;
+ m2= m2.assoc(e.getKey(), e.getValue());
+ }
+ return (Map) v.invoke(m2);
+ }
+
+}
View
33 src/main/java/org/usergrid/vx/experimental/filter/clojure/ClojureFilterFactory.java
@@ -0,0 +1,33 @@
+package org.usergrid.vx.experimental.filter.clojure;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import org.usergrid.vx.experimental.filter.Filter;
+import org.usergrid.vx.experimental.filter.FilterFactory;
+
+import clojure.lang.Compiler;
+import clojure.lang.RT;
+import clojure.lang.Var;
+
+public class ClojureFilterFactory implements FilterFactory {
+
+ @Override
+ public Filter createFilter(String script) {
+ try {
+ RT.load("clojure/core");
+ } catch (ClassNotFoundException | IOException e) {
+ e.printStackTrace();
+ }
+ Object result = Compiler.load(new StringReader(script));
+ if (result instanceof Filter) {
+ return (Filter) result;
+ } else if (result instanceof Var){
+ Var r = (Var) result;
+ return new ClojureFilter(r);
+ } else {
+ throw new IllegalArgumentException(script );
+ }
+ }
+
+}
View
24 src/test/java/org/usergrid/vx/experimental/FilterITest.java
@@ -37,6 +37,30 @@ public void executeJavaScriptFilter() throws Exception {
Assert.assertEquals(1, results.size());
}
+
+ @Test
+ public void executeClojureFilter() throws Exception {
+ IntraReq req = new IntraReq();
+ req.add(Operations.setKeyspaceOp("jsFilterks")); //0
+ req.add(Operations.createKsOp("jsFilterks", 1)); //1
+ req.add(Operations.createCfOp("filtercf")); //2
+ req.add(Operations.setColumnFamilyOp("filtercf")); //3
+ req.add(Operations.setAutotimestampOp(true)); //4
+ req.add(Operations.assumeOp("jsFilterks", "filtercf", "value", "UTF8Type"));//5
+ req.add(Operations.setOp("rowy", "col1", "20")); //6
+ req.add(Operations.setOp("rowy", "col2", "y")); //7
+ req.add(Operations.createFilterOp("valuey", "clojure",
+ "(ns user) (defn fil [a] (if (= (a \"value\") \"y\" ) a ))")); // 8
+ req.add(Operations.filterModeOp("valuey", true)); //9
+ req.add(Operations.sliceOp("rowy", "col1", "col3", 10)); //10
+ IntraClient2 ic2 = new IntraClient2("localhost", 8080);
+ IntraRes res = ic2.sendBlocking(req);
+ System.out.println(res.getException());
+ List<Map> results = (List<Map>) res.getOpsRes().get(10);
+ Assert.assertEquals("y", results.get(0).get("value"));
+ Assert.assertEquals(1, results.size());
+}
+
@Ignore
@Test
/* why wont this work */
View
71 src/test/java/org/usergrid/vx/experimental/SimpleClujTest.java
@@ -0,0 +1,71 @@
+package org.usergrid.vx.experimental;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+import org.usergrid.vx.experimental.filter.clojure.ClojureFilterFactory;
+
+import clojure.lang.IPersistentMap;
+import clojure.lang.RT;
+import clojure.lang.Var;
+import clojure.lang.Compiler;
+
+public class SimpleClujTest {
+
+ @Test
+ public void aTest() throws ClassNotFoundException, IOException {
+ //RT.init();
+ RT.load("clojure/core");
+ String str = "(ns user) (defn foo [a b] (str a \" \" b))";
+ Compiler.load(new StringReader(str));
+ Var foo = RT.var("user", "foo");
+ Object result = foo.invoke("Hi", "there");
+ Assert.assertEquals("Hi there", result);
+ }
+
+ @Test
+ public void bTest() throws ClassNotFoundException, IOException {
+ //RT.init();
+ RT.load("clojure/core");
+ //String str = "(ns user) (defn fil [a] ( map? a))";
+ //String str = "(ns user) (defn fil [a] ( a \"value\" ))";
+ //String str = "(ns user) (defn fil [a] (= (a \"value\") \"y\" ))";
+ //String str = "(ns user) (defn fil [a] (if (= (a \"value\"), \"y\" ) (a) () )";
+ /*
+ * (defn abs [x]
+ (if (< x 0)
+ (- x)
+ x))
+ */
+ String str = "(ns user) (defn fil [a] (if (= (a \"value\") \"y\" ) a ))";
+ Object r = Compiler.load(new StringReader(str));
+ System.out.println(r);
+ System.out.println(r.getClass());
+
+ Var foo = RT.var("user", "fil");
+ Map m = new HashMap();
+ m.put("name", "x");
+ m.put("value", "y");
+
+ IPersistentMap m2 = clojure.lang.RT.map( );
+ m2 = m2.assoc("name", "x");
+ m2 = m2.assoc("value", "y");
+ Object result = foo.invoke( m2 );
+ Assert.assertEquals(m, result);
+ }
+
+ @Test
+ public void cTest(){
+ Map m = new HashMap();
+ m.put("name", "x");
+ m.put("value", "y");
+ //how you like this one liner biotch?!
+ Assert.assertEquals(m, new ClojureFilterFactory().createFilter("(ns user) (defn fil [a] (if (= (a \"value\") \"y\" ) a ))").filter(m) );
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.