Skip to content

Commit cf31bda

Browse files
author
hideki
committed
Experimental ReplicationFilter and ReplicationFilterCompiler with JavaScript
1 parent eb1eb96 commit cf31bda

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.couchbase.lite.javascript;
2+
3+
import com.couchbase.lite.Emitter;
4+
import com.couchbase.lite.ReplicationFilter;
5+
import com.couchbase.lite.SavedRevision;
6+
import com.couchbase.lite.javascript.scopes.MapGlobalScope;
7+
import com.couchbase.lite.javascript.wrapper.CustomWrapFactory;
8+
import com.couchbase.lite.util.Log;
9+
10+
import org.mozilla.javascript.Function;
11+
import org.mozilla.javascript.Scriptable;
12+
import org.mozilla.javascript.WrapFactory;
13+
14+
import java.util.Map;
15+
16+
/**
17+
* Created by hideki on 10/28/15.
18+
*/
19+
public class JavaScriptReplicationFilter implements ReplicationFilter {
20+
public static String TAG = "JavaScriptReplicationFilter";
21+
22+
private static WrapFactory wrapFactory = new CustomWrapFactory();
23+
private Scriptable globalScope;
24+
private MapGlobalScope mapGlobalScope;
25+
private Function mapFunction;
26+
27+
28+
public JavaScriptReplicationFilter(String src){
29+
Log.e(TAG, "JavaScriptReplicationFilter(String) src="+src);
30+
org.mozilla.javascript.Context ctx = org.mozilla.javascript.Context.enter();
31+
32+
try {
33+
ctx.setOptimizationLevel(-1);
34+
ctx.setWrapFactory(wrapFactory);
35+
mapGlobalScope = new MapGlobalScope();
36+
globalScope = ctx.initStandardObjects(mapGlobalScope, true);
37+
mapFunction = ctx.compileFunction(globalScope, src, "filter", 0, null);
38+
} finally {
39+
org.mozilla.javascript.Context.exit();
40+
}
41+
}
42+
43+
public void map(Map<String, Object> document, Emitter emitter) {
44+
45+
mapGlobalScope.setEmitter(emitter);
46+
47+
org.mozilla.javascript.Context ctx = org.mozilla.javascript.Context.enter();
48+
try {
49+
ctx.setOptimizationLevel(-1);
50+
ctx.setWrapFactory(wrapFactory);
51+
52+
Scriptable localScope = ctx.newObject(globalScope);
53+
localScope.setPrototype(globalScope);
54+
localScope.setParentScope(null);
55+
56+
Object jsDocument = org.mozilla.javascript.Context.javaToJS(document, localScope);
57+
58+
try {
59+
mapFunction.call(ctx, localScope, null, new Object[]{jsDocument});
60+
} catch (org.mozilla.javascript.RhinoException e) {
61+
// Error in the JavaScript view - CouchDB swallows the error and tries the next document
62+
return;
63+
}
64+
} finally {
65+
org.mozilla.javascript.Context.exit();
66+
}
67+
}
68+
@Override
69+
public boolean filter(SavedRevision revision, Map<String, Object> params) {
70+
Log.e(TAG, "filter(SavedRevision, Map<String, Object>)");
71+
//mapGlobalScope.setEmitter(emitter);
72+
73+
org.mozilla.javascript.Context ctx = org.mozilla.javascript.Context.enter();
74+
try {
75+
ctx.setOptimizationLevel(-1);
76+
ctx.setWrapFactory(wrapFactory);
77+
78+
Scriptable localScope = ctx.newObject(globalScope);
79+
localScope.setPrototype(globalScope);
80+
localScope.setParentScope(null);
81+
82+
Object jsDocument = org.mozilla.javascript.Context.javaToJS(revision.getProperties(), localScope);
83+
Log.e(TAG, "jsDocument="+jsDocument);
84+
85+
try {
86+
Object result = mapFunction.call(ctx, localScope, null, new Object[]{jsDocument});
87+
Log.e(TAG, "result="+result);
88+
return ((Boolean)result).booleanValue();
89+
} catch (org.mozilla.javascript.RhinoException e) {
90+
// Error in the JavaScript view - CouchDB swallows the error and tries the next document
91+
Log.e(TAG, "Error in mapFunction.call()", e);
92+
return false;
93+
}
94+
} finally {
95+
org.mozilla.javascript.Context.exit();
96+
}
97+
}
98+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.couchbase.lite.javascript;
2+
3+
import com.couchbase.lite.ReplicationFilter;
4+
import com.couchbase.lite.ReplicationFilterCompiler;
5+
import com.couchbase.lite.util.Log;
6+
7+
/**
8+
* Created by hideki on 10/28/15.
9+
*/
10+
public class JavaScriptReplicationFilterCompiler implements ReplicationFilterCompiler {
11+
public static String TAG = "JSReplicationFilterCompiler";
12+
@Override
13+
public ReplicationFilter compileFilterFunction(String source, String language) {
14+
Log.e(TAG, "compileFilterFunction(String, String)");
15+
return new JavaScriptReplicationFilter(source);
16+
}
17+
}

0 commit comments

Comments
 (0)