You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by iv...@apache.org on 2015/07/02 18:10:41 UTC

[18/18] incubator-ignite git commit: #ignite-964: wip

#ignite-964: wip


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/a8e42e97
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/a8e42e97
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/a8e42e97

Branch: refs/heads/ignite-964-1
Commit: a8e42e97a4184a0c46447adec72ea92fe19650df
Parents: 2570140
Author: ivasilinets <iv...@gridgain.com>
Authored: Thu Jul 2 19:09:35 2015 +0300
Committer: ivasilinets <iv...@gridgain.com>
Committed: Thu Jul 2 19:09:35 2015 +0300

----------------------------------------------------------------------
 modules/core/pom.xml                            |   6 +
 .../IgniteScriptingCommandHandler.java          |   7 +-
 .../handlers/scripting/JSONCacheObject.java     |  88 +++++++++++++
 .../rest/handlers/scripting/NodeJSIgnite.java   |  39 ++++++
 .../rest/handlers/scripting/NodeJsCache.java    |  61 +++++++++
 modules/nodejs/src/test/js/test-compute.js      |  30 +++--
 .../http/jetty/GridJettyRestHandler.java        |  19 +--
 .../protocols/http/jetty/JSONCacheObject.java   | 131 -------------------
 8 files changed, 223 insertions(+), 158 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/core/pom.xml
----------------------------------------------------------------------
diff --git a/modules/core/pom.xml b/modules/core/pom.xml
index c7cb953..09263ed 100644
--- a/modules/core/pom.xml
+++ b/modules/core/pom.xml
@@ -55,6 +55,12 @@
         </dependency>
 
         <dependency>
+            <groupId>javax.json</groupId>
+            <artifactId>javax.json-api</artifactId>
+            <version>1.0</version>
+        </dependency>
+
+        <dependency>
             <groupId>commons-cli</groupId>
             <artifactId>commons-cli</artifactId>
             <version>1.2</version>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
index 68908f4..795a9b0 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.processors.rest.handlers.scripting;
 
+
 import net.sf.json.*;
 import org.apache.ignite.*;
 import org.apache.ignite.cluster.*;
@@ -67,6 +68,9 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
             emitRes = new IgniteJsEmitResult();
 
             script.addBinding("__emitResult", emitRes);
+
+
+            script.addBinding("ignite", new NodeJSIgnite(ctx.grid()));
         }
         catch (IgniteCheckedException e) {
             ctx.log().error(e.getMessage());
@@ -175,7 +179,8 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
                     data[i] = results.get(i).getData();
                 }
 
-                return ctx.scripting().invokeJSFunction(reduceFunc, JSONSerializer.toJSON(data), null);
+                Object o = ctx.scripting().invokeJSFunction(reduceFunc, JSONSerializer.toJSON(data), null);
+                return o;
             }
             catch (IgniteCheckedException e) {
                 throw U.convertException(e);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java
new file mode 100644
index 0000000..6f0e2f8
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.rest.handlers.scripting;
+
+
+import javax.json.*;
+import java.util.*;
+
+/**
+ * Json cache object.
+ */
+public class JSONCacheObject extends HashMap<Object, Object> {
+    /**
+     * Empty constructor.
+     */
+    private JSONCacheObject() {
+    }
+
+    /**
+     * @param o JSON object.
+     */
+    public JSONCacheObject(Map o) {
+        for (Object key : o.keySet())
+            addField(toSimpleObject(key), toSimpleObject(o.get(key)));
+    }
+
+    /**
+     * @param key Field name.
+     * @param val Field value.
+     */
+    public void addField(Object key, Object val) {
+        put(key, val);
+    }
+
+    /**
+     * @param key Field name.
+     * @return Field value.
+     */
+    public Object getField(Object key) {
+        return get(key);
+    }
+
+    /**
+     * Convert JSON object to JSONCacheObject
+     *
+     * @param o Object to convert.
+     * @return Converted object.
+     */
+    public static Object toSimpleObject(Object o) {
+        if (o instanceof Map) {
+            Map o1 = (Map)o;
+
+            JSONCacheObject res = new JSONCacheObject();
+
+            for (Object key : o1.keySet())
+                res.addField(toSimpleObject(key), toSimpleObject(o1.get(key)));
+
+            return res;
+        }
+        else if (o instanceof List) {
+            List o1 = (List) o;
+
+            List<Object> val = new ArrayList<>();
+
+            for (Object v : o1)
+                val.add(toSimpleObject(v));
+
+            return val;
+        }
+
+        return o;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJSIgnite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJSIgnite.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJSIgnite.java
new file mode 100644
index 0000000..5084e60
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJSIgnite.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.rest.handlers.scripting;
+
+import org.apache.ignite.*;
+
+/**
+ * Node js ignite.
+ */
+public class NodeJSIgnite {
+    /** Ignite. */
+    private Ignite ignite;
+
+    /**
+     * @param ignite Ignite.
+     */
+    NodeJSIgnite(Ignite ignite) {
+        this.ignite = ignite;
+    }
+
+    public NodeJsCache cache(String cache) {
+        return new NodeJsCache(ignite.cache(cache));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
new file mode 100644
index 0000000..2941e7e
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.rest.handlers.scripting;
+
+import org.apache.ignite.*;
+
+import javax.json.*;
+
+/**
+ * Node js cache.
+ */
+public class NodeJsCache {
+    /** Ignite cache. */
+    private IgniteCache cache;
+
+    /**
+     * @param cache Ignite cache.
+     */
+    public NodeJsCache(IgniteCache cache) {
+        this.cache = cache;
+    }
+
+    /**
+     * @param key Key.
+     * @param val Value.
+     */
+    public void put(Object key, Object val) {
+        Object cacheKey = JSONCacheObject.toSimpleObject(key);
+        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+
+        cache.put(cacheKey, cacheVal);
+    }
+
+    /**
+     * @param key Key.
+     */
+    public Object get(Object key) {
+        Object cacheKey = JSONCacheObject.toSimpleObject(key);
+
+        cache.get(cacheKey);
+
+
+        JsonObject value = Json.createObjectBuilder().add("BBB", "a").build();
+        return value;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/nodejs/src/test/js/test-compute.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/js/test-compute.js b/modules/nodejs/src/test/js/test-compute.js
index b7d88f0..41773f6 100644
--- a/modules/nodejs/src/test/js/test-compute.js
+++ b/modules/nodejs/src/test/js/test-compute.js
@@ -143,6 +143,20 @@ function computeCacheExecute(error, ignite) {
     var map = function(nodes, args) {
         for (var i = 0; i < nodes.length; i++) {
             var f = function (args1) {
+                ignite.cache("mycache").put({"1": "1"}, 2);
+
+                var val = ignite.cache("mycache").get({"1": "1"});
+
+                println("GET 1,1 = " + val);
+
+                var val1 = ignite.cache("mycache").get(args1.get(0));
+
+                println("GET 1,2 = " + val1);
+                println("GET TYPE=" + (typeof val1));
+                println("GET TYPE=" + (Object.keys(val1)));
+
+                println("GET TYPE=" + (val1.get("age")));
+
                 var jsArgs = JSON.parse(args1);
                 println("!!!!jsArgs " + JSON.stringify(jsArgs[0]));
                 println("ARG0=" + args1.get(0));
@@ -160,21 +174,17 @@ function computeCacheExecute(error, ignite) {
     };
 
     var reduce = function(results) {
-        println("!!!!!!!!!!results=" + results);
-        var exp = {"age" : 12, "books" : ["1", "Book"]};
-
-        for (var i = 0; i < results.length; i++) {
-            var val = results[i];
-
-            println("Incorrect value [exp=" + JSON.stringify(exp) + ", val=" + JSON.stringify(val) + "]");
-        }
-
-        return sum;
+        return {"1": 1};
     };
 
     var callback = function(err, res) {
         assert(err == null, "Get error on compute task [err=" + err + "]");
 
+        console.log("RESULT:" + res + "; keys=" + Object.keys(res));
+        console.log("key:" + res["BB"]);
+        console.log("key:" + res.BB);
+
+
         ignite.cache("mycache").size(function(err, size){
             assert(size === res, "Incorrect size [size=" + size + ", res=" + res + "]");
             TestUtils.testDone();

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
index 1b6a170..0e659f9 100644
--- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
+++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
@@ -22,6 +22,7 @@ import net.sf.json.processors.*;
 import org.apache.ignite.*;
 import org.apache.ignite.internal.processors.rest.*;
 import org.apache.ignite.internal.processors.rest.client.message.*;
+import org.apache.ignite.internal.processors.rest.handlers.scripting.*;
 import org.apache.ignite.internal.processors.rest.request.*;
 import org.apache.ignite.internal.util.typedef.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
@@ -332,13 +333,6 @@ public class GridJettyRestHandler extends AbstractHandler {
                 res.add(new RestEntry(k, o.get(k)));
 
             cmdRes.setResponse(res);
-        } else if (cmd == CACHE_GET || cmd == CACHE_GET_AND_PUT ||
-            cmd == CACHE_GET_AND_PUT_IF_ABSENT || cmd == CACHE_GET_AND_REMOVE ||
-            cmd == CACHE_GET_AND_REPLACE) {
-            Object o = cmdRes.getResponse();
-
-            if (o instanceof JSONCacheObject)
-                cmdRes.setResponse(((JSONCacheObject)o).getFields());
         }
     }
 
@@ -818,15 +812,8 @@ public class GridJettyRestHandler extends AbstractHandler {
          * @param val Value.
          */
         public RestEntry(Object key, Object val) {
-            if (key instanceof JSONCacheObject)
-                this.key = ((JSONCacheObject)key).getFields();
-            else
-                this.key = key;
-
-            if (val instanceof JSONCacheObject)
-                this.value = ((JSONCacheObject)val).getFields();
-            else
-                this.value = val;
+            this.key = key;
+            this.value = val;
         }
 
         /**

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a8e42e97/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/JSONCacheObject.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/JSONCacheObject.java b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/JSONCacheObject.java
deleted file mode 100644
index 7ea30b3..0000000
--- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/JSONCacheObject.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.rest.protocols.http.jetty;
-
-import net.sf.json.*;
-
-import java.util.*;
-
-/**
- * Json cache object.
- */
-public class JSONCacheObject {
-    /** Fields map. */
-    private final Map<Object, Object> fields = new HashMap<>();
-
-    /**
-     * Empty constructor.
-     */
-    private JSONCacheObject() {
-    }
-
-    /**
-     * @param o JSON object.
-     */
-    public JSONCacheObject(JSONObject o) {
-        for (Object key : o.keySet())
-            addField(toSimpleObject(key), toSimpleObject(o.get(key)));
-    }
-
-    /**
-     * @param key Field name.
-     * @param val Field value.
-     */
-    public void addField(Object key, Object val) {
-        fields.put(key, val);
-    }
-
-    /**
-     * @param key Field name.
-     * @return Field value.
-     */
-    public Object getField(Object key) {
-        return fields.get(key);
-    }
-
-    /**
-     * @return Fields key set.
-     */
-    public Set<Object> keys() {
-        return fields.keySet();
-    }
-
-    /**
-     * @return Fields map.
-     */
-    public Map<Object, Object> getFields() {
-        return fields;
-    }
-
-    /**
-     * Convert JSON object to JSONCacheObject
-     *
-     * @param o Object to convert.
-     * @return Converted object.
-     */
-    private Object toSimpleObject(Object o) {
-        if (o instanceof JSONObject) {
-            JSONObject o1 = (JSONObject)o;
-
-            JSONCacheObject res = new JSONCacheObject();
-
-            for (Object key : o1.keySet())
-                res.addField(toSimpleObject(key), toSimpleObject(o1.get(key)));
-
-            return res;
-        }
-        else if (o instanceof JSONArray) {
-            JSONArray o1 = (JSONArray) o;
-
-            List<Object> val = new ArrayList<>();
-
-            for (Object v : o1)
-                val.add(toSimpleObject(v));
-
-            return val;
-        }
-
-        return o;
-    }
-
-    /** {@inheritDoc} */
-    @Override public int hashCode() {
-        return fields.hashCode();
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean equals(Object obj) {
-        if (obj == null || !(obj instanceof JSONCacheObject))
-            return false;
-
-        JSONCacheObject obj0 = (JSONCacheObject) obj;
-
-        if (fields.size() != obj0.fields.size())
-            return false;
-
-        for (Object key : obj0.keys()) {
-            if (!fields.containsKey(key))
-                return false;
-
-            if (!obj0.getField(key).equals(getField(key)))
-                return false;
-        }
-
-        return true;
-    }
-}