#ignite-964: nodejs cache works with json objects and strings.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/b963d033 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/b963d033 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/b963d033 Branch: refs/heads/ignite-964 Commit: b963d033922fa712ddb11fd3542468af802ea91f Parents: 80bd452 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Wed Jul 1 01:14:44 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Wed Jul 1 01:14:44 2015 +0300 ---------------------------------------------------------------------- modules/nodejs/src/main/js/apache-ignite.js | 1 + modules/nodejs/src/main/js/cache.js | 71 +++++++++++-- .../ignite/internal/NodeJsCacheApiSelfTest.java | 7 ++ modules/nodejs/src/test/js/test-cache-api.js | 100 ++++++++++++++----- modules/nodejs/src/test/js/test-utils.js | 11 +- .../http/jetty/GridJettyRestHandler.java | 91 ++++++++++++++--- .../protocols/http/jetty/JSONCacheObject.java | 8 ++ 7 files changed, 240 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b963d033/modules/nodejs/src/main/js/apache-ignite.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/apache-ignite.js b/modules/nodejs/src/main/js/apache-ignite.js index af86916..82aa5ca 100644 --- a/modules/nodejs/src/main/js/apache-ignite.js +++ b/modules/nodejs/src/main/js/apache-ignite.js @@ -17,6 +17,7 @@ module.exports = { Cache : require('./cache.js').Cache, + Entry : require('./cache.js').Entry, Ignition : require('./ignition.js').Ignition, Server : require('./server.js').Server, Ignite : require('./ignite.js').Ignite, http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b963d033/modules/nodejs/src/main/js/cache.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/cache.js b/modules/nodejs/src/main/js/cache.js index e0ed505..3eaadb4 100644 --- a/modules/nodejs/src/main/js/cache.js +++ b/modules/nodejs/src/main/js/cache.js @@ -41,7 +41,9 @@ function Cache(server, cacheName) { * @param {onGet} callback Called on finish */ Cache.prototype.get = function(key, callback) { - this._server.runCommand(this._createCommand("get").addParam("key", key), callback); + this._server.runCommand(this._createCommand("get"). + setPostData(JSON.stringify({"key": key})), + callback); }; /** @@ -53,7 +55,8 @@ Cache.prototype.get = function(key, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.put = function(key, value, callback) { - this._server.runCommand(this._createCommand("put").addParam("key", key).addParam("val", value), + this._server.runCommand(this._createCommand("put"). + setPostData(JSON.stringify({"key": key, "val" : value})), callback); } @@ -65,7 +68,9 @@ Cache.prototype.put = function(key, value, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.remove = function(key, callback) { - this._server.runCommand(this._createCommand("rmv").addParam("key", key), callback); + this._server.runCommand(this._createCommand("rmv"). + setPostData(JSON.stringify({"key": key})), + callback); } /** @@ -76,18 +81,21 @@ Cache.prototype.remove = function(key, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.removeAll = function(keys, callback) { - this._server.runCommand(this._createCommand("rmvall").addParams("k", keys), callback); + this._server.runCommand(this._createCommand("rmvall"). + setPostData(JSON.stringify({"keys" : keys})), + callback); } /** * Put keys to cache * * @this {Cache} - * @param {Object.<string, string>} map collection of entries to put in the cache + * @param {Entry[]} List of entries to put in the cache * @param {noValue} callback Called on finish */ -Cache.prototype.putAll = function(map, callback) { - this._server.runCommand(this._createCommand("putall").setPostData(JSON.stringify(map)), callback); +Cache.prototype.putAll = function(entries, callback) { + this._server.runCommand(this._createCommand("putall").setPostData( + JSON.stringify({"entries" : entries})), callback); } /** @@ -106,7 +114,25 @@ Cache.prototype.putAll = function(map, callback) { * @param {Cache~onGetAll} callback Called on finish */ Cache.prototype.getAll = function(keys, callback) { - this._server.runCommand(this._createCommand("getall").addParams("k", keys), callback); + function onGetAll(callback, err, res) { + if (err) { + callback.call(null, err, null); + + return; + } + + var result = []; + + for (var key of res) { + result.push(new Entry(key["key"], key["value"])); + } + + callback.call(null, null, result); + } + + this._server.runCommand(this._createCommand("getall").setPostData( + JSON.stringify({"keys" : keys})), + onGetAll.bind(null, callback)); } /** @@ -182,4 +208,31 @@ Cache.prototype._createQueryCommand = function(name, qry) { return command.addParam("psz", qry.pageSize()); } -exports.Cache = Cache \ No newline at end of file +/** + * @this{Entry} + * @param key Key + * @param val Value + */ +function Entry(key, val) { + this._key = key; + this._val = val; +} + +/** + * @this{Entry} + * @returns Key + */ +Entry.prototype.key = function() { + return this._key; +} + +/** + * @this{Entry} + * @returns Value + */ +Entry.prototype.val = function() { + return this._val; +} + +exports.Cache = Cache +exports.Entry = Entry \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b963d033/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java index 1a9293c..d8f3859 100644 --- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java +++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java @@ -91,4 +91,11 @@ public class NodeJsCacheApiSelfTest extends NodeJsAbstractTest { public void testPutAllObjectGetAll() throws Exception { runJsScript("testPutAllObjectGetAll"); } + + /** + * @throws Exception If failed. + */ + public void testRemoveAllObjectGetAll() throws Exception { + runJsScript("testRemoveAllObjectGetAll"); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b963d033/modules/nodejs/src/test/js/test-cache-api.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/js/test-cache-api.js b/modules/nodejs/src/test/js/test-cache-api.js index 6e660b1..fa3f6cc 100644 --- a/modules/nodejs/src/test/js/test-cache-api.js +++ b/modules/nodejs/src/test/js/test-cache-api.js @@ -17,45 +17,71 @@ var TestUtils = require("./test-utils").TestUtils; +var Ignite = require(TestUtils.scriptPath()); +var Entry = Ignite.Entry; + var assert = require("assert"); testPutGet = function() { - startTest("mycache", {trace: [put, getExist], entry: "6"}); + startTest("mycache", {trace: [put, getExist], entry: ["key" , "6"]}); } testRemove = function() { - startTest("mycache", {trace: [put, getExist, remove, getNonExist], entry: "6"}); + startTest("mycache", {trace: [put, getExist, remove, getNonExist], entry: ["key" , "6"]}); } testRemoveNoKey = function() { - startTest("mycache", {trace: [remove, getNonExist], entry: "6"}); + startTest("mycache", {trace: [remove, getNonExist], entry: ["key" , "6"]}); } testPutAllGetAll = function() { - startTest("mycache", {trace: [putAll, getAll], entry: {"key1": "val1", "key2" : "val2"}}); + entries = []; + + entries.push(new Entry("key1", "val1")); + entries.push(new Entry("key2", "val2")); + + startTest("mycache", {trace: [putAll, getAll], entry: entries}); } testPutAllObjectGetAll = function() { - var params = {} + entries = []; + + var key1 = {"name" : "Ann"}; + var key2 = {"name" : "Paul"}; + var val1 = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = {"age" : 13, "books" : ["1", "Book"]}; + + entries.push(new Entry(key1, val1)); + entries.push(new Entry(key2, val2)); + + startTest("mycache", {trace: [putAll, getAll], entry: entries}); +} + +testRemoveAllObjectGetAll = function() { + entries = []; + var key1 = {"name" : "Ann"}; var key2 = {"name" : "Paul"}; var val1 = {"age" : 12, "books" : ["1", "Book"]}; var val2 = {"age" : 13, "books" : ["1", "Book"]}; - params["k1"] = key1; - params["k2"] = key2; - params["val1"] = val1; - params["val2"] = val2; + entries.push(new Entry(key1, val1)); + entries.push(new Entry(key2, val2)); - startTest("mycache", {trace: [putAll, getAll], entry: params}); + startTest("mycache", {trace: [putAll, getAll, removeAll, getNone], entry: entries}); } testRemoveAll = function() { - startTest("mycache", {trace: [putAll, getAll, removeAll, getNone], entry: {"key1": "val1", "key2" : "val2"}}); + entries = []; + + entries.push(new Entry("key1", "val1")); + entries.push(new Entry("key2", "val2")); + + startTest("mycache", {trace: [putAll, getAll, removeAll, getNone], entry: entries}); } testIncorrectCacheName = function() { - startTest("mycache1", {trace: [incorrectPut], entry: "6"}); + startTest("mycache1", {trace: [incorrectPut], entry: ["key", "6"]}); } function startTest(cacheName, testDescription) { @@ -77,25 +103,27 @@ function onStart(cacheName, testDescription, error, ignite) { } function put(cache, entry, next) { - cache.put("key", entry, next); + cache.put(entry[0], entry[1], next); } function getExist(cache, entry, next) { - cache.get("key", onGet); + var key = Object.keys(entry)[0]; + + cache.get(entry[0], onGet); function onGet(error, value) { assert(!error); - assert(value === entry); + assert(value === entry[1]); next(); } } function remove(cache, entry, next) { - cache.remove("key", next); + cache.remove(entry[0], next); } function getNonExist(cache, entry, next) { - cache.get("key", onGet); + cache.get(entry[0], onGet); function onGet(error, value) { assert(!error); @@ -109,21 +137,47 @@ function putAll(cache, entries, next) { } function getAll(cache, entries, next) { - cache.getAll(Object.keys(entries), onGetAll); + var keys = [] + + for (var entry of entries) { + keys.push(entry.key()); + } + + cache.getAll(keys, onGetAll.bind(null, keys)); + var expected = entries; - function onGetAll(error, values) { + function onGetAll(keys, error, values) { assert(!error, error); - var keys = Object.keys(expected); + assert(values.length === keys.length, "Values length is incorrect " + + "[expected=" + keys.length + ", real=" + values.length + "]"); for (var i = 0; i < keys.length; ++i) { var key = keys[i]; - assert(!!values[key], "Cannot find key. [key=" + key + "]."); + var foundVal = null; + + for (var j = 0; j < values.length; ++j) { + if (TestUtils.compareObject(key, values[j].key())) { + foundVal = values[j]; + } + } + + var foundExp = null; - TestUtils.compareObject(expected[key], values[key]); + for (var j = 0; j < expected.length; ++j) { + if (TestUtils.compareObject(key, expected[j].key())) { + foundExp = expected[j]; + } + } + + assert(foundVal !== null, "Cannot find key. [key=" + key + "]."); + assert(foundExp !== null, "Cannot find key. [key=" + key + "]."); + + assert(TestUtils.compareObject(foundExp, foundVal), "Incorrect value"); } + next(); } } @@ -144,7 +198,7 @@ function getNone(cache, entries, next) { } function incorrectPut(cache, entry, next) { - cache.put("key", entry, callback); + cache.put(entry[0], entry[1], callback); function callback(error) { assert(!!error, "Do not get error for not exist cache"); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b963d033/modules/nodejs/src/test/js/test-utils.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/js/test-utils.js b/modules/nodejs/src/test/js/test-utils.js index 1ec5ab7..8beb3dc 100644 --- a/modules/nodejs/src/test/js/test-utils.js +++ b/modules/nodejs/src/test/js/test-utils.js @@ -53,15 +53,20 @@ TestUtils.sep = function() { TestUtils.compareObject = function(o1, o2) { if (typeof o1 !== 'object') { - assert(o1 === o2, "Incorrect value. [expected=" + o1 + ", val= " + o2 + "]."); + return o1 === o2; } else { - assert(Object.keys(o1).length === Object.keys(o2).length, "Incorrect key set") + if (Object.keys(o1).length !== Object.keys(o2).length) + return false; for (var keyObj of Object.keys(o2)) { - TestUtils.compareObject(o1[keyObj], o2[keyObj]); + if (!TestUtils.compareObject(o1[keyObj], o2[keyObj])) { + return false; + } } } + + return true; } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b963d033/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 f7b1db8..55fe156 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 @@ -291,6 +291,8 @@ public class GridJettyRestHandler extends AbstractHandler { JSON json; try { + createResponse(cmd, cmdRes); + json = JSONSerializer.toJSON(cmdRes, cfg); } catch (JSONException e) { @@ -313,6 +315,54 @@ public class GridJettyRestHandler extends AbstractHandler { } } + private void createResponse(GridRestCommand cmd, GridRestResponse cmdRes) { + if (cmd == CACHE_GET_ALL) { + if (cmdRes.getResponse() == null) { + return; + } + + Map o = (Map)cmdRes.getResponse(); + + List<RestEntry> res = new ArrayList<>(); + + for (Object k : o.keySet()) + res.add(new RestEntry(k, o.get(k))); + + cmdRes.setResponse(res); + } + } + + public static class RestEntry { + private Object key; + private Object value; + public RestEntry(Object key, Object value) { + if (key instanceof JSONCacheObject) + this.key = ((JSONCacheObject)key).getFields(); + else + this.key = key; + + if (value instanceof JSONCacheObject) + this.value = ((JSONCacheObject)value).getFields(); + else + this.value = value; + } + public Object getKey() { + return key; + } + + public void setKey(Object key) { + this.key = key; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + } + /** * Creates REST request. * @@ -357,35 +407,48 @@ public class GridJettyRestHandler extends AbstractHandler { String cacheName = (String)params.get("cacheName"); if (req.getHeader("JSONObject") != null) { + JSONObject o = parseRequest(req); + + Map<Object, Object> map = U.newHashMap(o.keySet().size()); + if (cmd == CACHE_PUT_ALL) { - JSONObject o =parseRequest(req); + List entries = (List)o.get("entries"); - int i = 1; + for (Object entry : entries) { + JSONCacheObject cacheEntry = new JSONCacheObject((JSONObject)entry); - Map<Object, Object> map = U.newHashMap(o.keySet().size()); + Object key = cacheEntry.getField("_key"); + Object val = cacheEntry.getField("_val"); - while (o.get("k" + i) != null) { - Object key = o.get("k" + i); + map.put(key, val); + } - Object val = o.get("val" + i); + restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName); - if (key instanceof JSONObject) - key = new JSONCacheObject((JSONObject)key); + restReq0.values(map); + } + else if (cmd == CACHE_GET_ALL || cmd == CACHE_REMOVE_ALL) { + JSONCacheObject cacheObj = new JSONCacheObject(o); - if (val instanceof JSONObject) - val = new JSONCacheObject((JSONObject)val); + List keys = (List)cacheObj.getField("keys"); - map.put(key, val); - i++; - } + for (Object key : keys) + map.put(key, null); restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName); restReq0.values(map); } + else if (cmd == CACHE_GET || cmd == CACHE_PUT || cmd == CACHE_REMOVE) { + JSONCacheObject cacheObj = new JSONCacheObject(o); + + restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName); + + restReq0.key(cacheObj.getField("key")); + restReq0.value(cacheObj.getField("val")); + } } else { - restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName); restReq0.key(params.get("key")); restReq0.value(params.get("val")); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b963d033/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 index 9bcd419..67f2430 100644 --- 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 @@ -32,6 +32,14 @@ public class JSONCacheObject { } + public void setFields(Map<Object, Object> fields) { + this.fields = fields; + } + + public Map<Object, Object> getFields() { + return fields; + } + public JSONCacheObject(JSONObject o) { for (Object key : o.keySet()) addField(toSimpleObject(key), toSimpleObject(o.get(key)));