Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

implemented JavaScript reduce function compiler #67

Merged
merged 1 commit into from

2 participants

@pegli

simple stuff, but I needed the reduce function compiler so I thought I'd pass it along.

@mschoch mschoch merged commit 3ff11b4 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 10, 2012
  1. @pegli
This page is out of date. Refresh to see the latest.
View
94 TouchDB-Android-JavaScript/src/com/couchbase/touchdb/javascript/TDJavaScriptViewCompiler.java
@@ -21,21 +21,44 @@
public class TDJavaScriptViewCompiler implements TDViewCompiler {
- @Override
- public TDViewMapBlock compileMapFunction(String mapSource, String language) {
+ @Override
+ public TDViewMapBlock compileMapFunction(String mapSource, String language) {
if (language.equals("javascript")) {
return new TDViewMapBlockRhino(mapSource);
}
throw new IllegalArgumentException(language + " is not supported");
- }
+ }
+
+ @Override
+ public TDViewReduceBlock compileReduceFunction(String reduceSource, String language) {
+ if (language.equals("javascript")) {
+ return new TDViewReduceBlockRhino(reduceSource);
+ }
+ throw new IllegalArgumentException(language + " is not supported");
+ }
- @Override
- public TDViewReduceBlock compileReduceFunction(String reduceSource,
- String language) {
- // TODO Auto-generated method stub
- return null;
+}
+
+/**
+ * Wrap Factory for Rhino Script Engine
+ */
+class CustomWrapFactory extends WrapFactory {
+
+ public CustomWrapFactory() {
+ setJavaPrimitiveWrap(false); // RingoJS does that..., claims its annoying...
}
+ @Override
+ public Scriptable wrapAsJavaObject(Context cx, Scriptable scope, Object javaObject, Class staticType) {
+ if (javaObject instanceof Map) {
+ return new NativeMap(scope, (Map) javaObject);
+ }
+ else if(javaObject instanceof List) {
+ return new NativeList(scope, (List<Object>)javaObject);
+ }
+
+ return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
+ }
}
class TDViewMapBlockRhino implements TDViewMapBlock {
@@ -56,7 +79,7 @@ public TDViewMapBlockRhino(String src) {
}
}
- @Override
+ @Override
public void map(Map<String, Object> document, TDViewMapEmitBlock emitter) {
Context ctx = Context.enter();
try {
@@ -101,26 +124,49 @@ public void map(Map<String, Object> document, TDViewMapEmitBlock emitter) {
}
}
+
+}
- /**
- * Wrap Factory for Rhino Script Engine
- */
- public static class CustomWrapFactory extends WrapFactory {
+class TDViewReduceBlockRhino implements TDViewReduceBlock {
+
+ private static WrapFactory wrapFactory = new CustomWrapFactory();
+ private Scriptable globalScope;
+ private String src;
- public CustomWrapFactory() {
- setJavaPrimitiveWrap(false); // RingoJS does that..., claims its annoying...
+ public TDViewReduceBlockRhino(String src) {
+ this.src = src;
+ Context ctx = Context.enter();
+ try {
+ ctx.setOptimizationLevel(-1);
+ ctx.setWrapFactory(wrapFactory);
+ globalScope = ctx.initStandardObjects(null, true);
+ } finally {
+ Context.exit();
}
+ }
- public Scriptable wrapAsJavaObject(Context cx, Scriptable scope, Object javaObject, Class staticType) {
- if (javaObject instanceof Map) {
- return new NativeMap(scope, (Map) javaObject);
- }
- else if(javaObject instanceof List) {
- return new NativeList(scope, (List<Object>)javaObject);
- }
+ @Override
+ public Object reduce(List<Object> keys, List<Object> values, boolean rereduce) {
+ Context ctx = Context.enter();
+ try {
+ ctx.setOptimizationLevel(-1);
+ ctx.setWrapFactory(wrapFactory);
+
+ //register the reduce function
+ String reduceSrc = "var reduce = " + src + ";";
+ ctx.evaluateString(globalScope, reduceSrc, "reduce", 1, null);
+
+ //find the reduce function and execute it
+ Function reduceFun = (Function)globalScope.get("reduce", globalScope);
+ Object[] functionArgs = { keys, values, rereduce };
+ Object result = reduceFun.call(ctx, globalScope, globalScope, functionArgs);
+
+ return result;
- return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
+ } finally {
+ Context.exit();
}
- }
+ }
+
}
Something went wrong with that request. Please try again.