Skip to content

Fixed #11 Implement ReplicationFilterCompiler for JavaScript #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

package com.couchbase.lite.javascript;

import com.couchbase.lite.ReplicationFilter;
import com.couchbase.lite.ReplicationFilterCompiler;

/**
* Created by hideki on 10/28/15.
*/
public class JavaScriptReplicationFilterCompiler implements ReplicationFilterCompiler {
@Override
public ReplicationFilter compileFilterFunction(String source, String language) {
if (language != null && language.equalsIgnoreCase("javascript")) {
return new ReplicationFilterBlockRhino(source);
}
throw new IllegalArgumentException(language + " is not supported");
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite.javascript;

import com.couchbase.lite.Mapper;
Expand All @@ -7,15 +20,15 @@
public class JavaScriptViewCompiler implements ViewCompiler {
@Override
public Mapper compileMap(String source, String language) {
if (language.equals("javascript")) {
if (language != null && language.equalsIgnoreCase("javascript")) {
return new ViewMapBlockRhino(source);
}
throw new IllegalArgumentException(language + " is not supported");
}

@Override
public Reducer compileReduce(String source, String language) {
if (language.equals("javascript")) {
if (language != null && language.equalsIgnoreCase("javascript")) {
return new ViewReduceBlockRhino(source);
}
throw new IllegalArgumentException(language + " is not supported");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

package com.couchbase.lite.javascript;

import com.couchbase.lite.ReplicationFilter;
import com.couchbase.lite.SavedRevision;
import com.couchbase.lite.javascript.scopes.GlobalScope;
import com.couchbase.lite.javascript.wrapper.CustomWrapFactory;
import com.couchbase.lite.util.Log;

import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.WrapFactory;

import java.util.Map;

/**
* Created by hideki on 10/28/15.
*/
public class ReplicationFilterBlockRhino implements ReplicationFilter {
public static String TAG = "ReplicationFilterBlockRhino";

private static WrapFactory wrapFactory = new CustomWrapFactory();
private Scriptable scope;
private GlobalScope globalScope;
private Function filterFunction;

public ReplicationFilterBlockRhino(String src){
org.mozilla.javascript.Context ctx = org.mozilla.javascript.Context.enter();
try {
ctx.setOptimizationLevel(-1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the -1 meaning?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Optimization

-1

Interpretive mode is always used. The compilation time is minimized at the expense of runtime performance. No class files are generated, which may improve memory usage depending on your system. Another benefit of the interpreted mode is that the interpreter performs tail-call elimination of recursive functions. Also, you must use this optimization level if your code uses Continuation objects.

If the optimization package is not available, then optimization acts as if it is always -1.

ctx.setWrapFactory(wrapFactory);
globalScope = new GlobalScope();
scope = ctx.initStandardObjects(globalScope, true);
filterFunction = ctx.compileFunction(scope, src, "filter", 0, null);
} finally {
org.mozilla.javascript.Context.exit();
}
}

@Override
public boolean filter(SavedRevision revision, Map<String, Object> params) {
org.mozilla.javascript.Context ctx = org.mozilla.javascript.Context.enter();
try {
ctx.setOptimizationLevel(-1);
ctx.setWrapFactory(wrapFactory);

Scriptable localScope = ctx.newObject(scope);
localScope.setPrototype(scope);
localScope.setParentScope(null);

Object jsDocument = org.mozilla.javascript.Context.javaToJS(revision.getProperties(), localScope);
Object jsParams = org.mozilla.javascript.Context.javaToJS(params, localScope);

try {
Object result = filterFunction.call(ctx, localScope, null, new Object[]{jsDocument, jsParams});
return ((Boolean)result).booleanValue();
} catch (org.mozilla.javascript.RhinoException e) {
// Error in the JavaScript view - CouchDB swallows the error and tries the next document
Log.e(TAG, "Error in filterFunction.call()", e);
return false;
}
} finally {
org.mozilla.javascript.Context.exit();
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/couchbase/lite/javascript/ViewMapBlockRhino.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite.javascript;

import com.couchbase.lite.Emitter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite.javascript;

import com.couchbase.lite.Reducer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite.javascript.scopes;

import com.couchbase.lite.util.Log;

import org.mozilla.javascript.ScriptableObject;

class GlobalScope extends ScriptableObject {
public class GlobalScope extends ScriptableObject {
public GlobalScope() {
super();
String[] names = {"log"};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite.javascript.scopes;

import com.couchbase.lite.Emitter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite.javascript.scopes;

import com.couchbase.lite.CouchbaseLiteException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite.javascript.wrapper;

import org.mozilla.javascript.Context;
Expand All @@ -14,7 +27,6 @@ public class CustomWrapFactory extends WrapFactory {

public CustomWrapFactory() {
setJavaPrimitiveWrap(false);

}

@Override
Expand Down
113 changes: 113 additions & 0 deletions src/test/java/com/couchbase/lite/ReplicationFilterTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright (c) 2015 Couchbase, Inc All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

package com.couchbase.lite;

import com.couchbase.lite.internal.RevisionInternal;
import com.couchbase.lite.javascript.JavaScriptReplicationFilterCompiler;

import junit.framework.TestCase;

import java.util.HashMap;
import java.util.Map;

/**
* Created by hideki on 11/9/15.
*/
public class ReplicationFilterTestCase extends TestCase {
private JavaScriptReplicationFilterCompiler replicationFilterCompiler;

@Override
public void setUp() throws Exception {
super.setUp();
replicationFilterCompiler = new JavaScriptReplicationFilterCompiler();
}

public void testSimpleFilterTrue() {
Map<String, Object> props = new HashMap<String, Object>();
props.put("_id", "foo");
props.put("_rev", "1-1111");
props.put("_deleted", false);
props.put("type", "order");
props.put("SeatNumber", 10);
RevisionInternal revisionInternal = new RevisionInternal(props);
SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal);
ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order') {return true;}else{return false}}", "javascript");
assertTrue(replicationFilter.filter(savedRevision, null));
}

public void testSimpleFilterFalse() {
Map<String, Object> props = new HashMap<String, Object>();
props.put("_id", "foo");
props.put("_rev", "1-1111");
props.put("_deleted", false);
props.put("type", "user");
props.put("Name", "tom");
RevisionInternal revisionInternal = new RevisionInternal(props);
SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal);
ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order') {return true;}else{return false}}", "javascript");
assertFalse(replicationFilter.filter(savedRevision, null));
}


public void testSimpleFilterWithReqTrue() {
Map<String, Object> props = new HashMap<String, Object>();
props.put("_id", "foo");
props.put("_rev", "1-1111");
props.put("_deleted", false);
props.put("type", "order");
props.put("SeatNumber", 10);
RevisionInternal revisionInternal = new RevisionInternal(props);
SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal);
ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order' && req.abc == 1) {return true;}else{return false}}", "javascript");
Map req = new HashMap();
req.put("abc", 1);
assertTrue(replicationFilter.filter(savedRevision, req));
}

public void testSimpleFilterWithReqFalse() {
Map<String, Object> props = new HashMap<String, Object>();
props.put("_id", "foo");
props.put("_rev", "1-1111");
props.put("_deleted", false);
props.put("type", "user");
props.put("Name", "tom");
RevisionInternal revisionInternal = new RevisionInternal(props);
SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal);
ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order' && req.abc == 1) {return true;}else{return false}}", "javascript");
Map req = new HashMap();
req.put("abc", 2);
assertFalse(replicationFilter.filter(savedRevision, req));
}
public void testSimpleFilterWithReqFalse2() {
Map<String, Object> props = new HashMap<String, Object>();
props.put("_id", "foo");
props.put("_rev", "1-1111");
props.put("_deleted", false);
props.put("type", "user");
props.put("Name", "tom");
RevisionInternal revisionInternal = new RevisionInternal(props);
SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal);
ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order' && req.abc == 1) {return true;}else{return false}}", "javascript");
assertFalse(replicationFilter.filter(savedRevision, null));
}
public void testInvalidLanguage() {
try {
ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order' && req.abc == 1) {return true;}else{return false}}", "C++");
fail("IllegalArgumentException should be thrown");
}
catch (IllegalArgumentException e){
}
}
}