-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/com/couchbase/lite/javascript/JavaScriptReplicationFilterCompiler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/couchbase/lite/javascript/ReplicationFilterBlockRhino.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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
13
src/main/java/com/couchbase/lite/javascript/ViewMapBlockRhino.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/couchbase/lite/javascript/ViewReduceBlockRhino.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 14 additions & 1 deletion
15
src/main/java/com/couchbase/lite/javascript/scopes/GlobalScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/couchbase/lite/javascript/scopes/MapGlobalScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/couchbase/lite/javascript/scopes/ReduceGlobalScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/test/java/com/couchbase/lite/ReplicationFilterTestCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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