Repository: incubator-ignite Updated Branches: refs/heads/ignite-964 5a8982fee -> a7bb9ee4a
#ignite-964: aff replace method for nodejs cache. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/2496ac3c Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/2496ac3c Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/2496ac3c Branch: refs/heads/ignite-964 Commit: 2496ac3c9548a96ebb1bbd655fea969ef4bab93c Parents: 5a8982f Author: ivasilinets <ivasilin...@gridgain.com> Authored: Wed Jul 1 16:41:22 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Wed Jul 1 16:41:22 2015 +0300 ---------------------------------------------------------------------- modules/nodejs/src/main/js/cache.js | 26 +++++++++ .../ignite/internal/NodeJsCacheApiSelfTest.java | 21 +++++++ modules/nodejs/src/test/js/test-cache-api.js | 59 ++++++++++++++++++++ .../http/jetty/GridJettyRestHandler.java | 2 +- 4 files changed, 107 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2496ac3c/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 c96b709..bb48dfd 100644 --- a/modules/nodejs/src/main/js/cache.js +++ b/modules/nodejs/src/main/js/cache.js @@ -217,6 +217,32 @@ Cache.prototype.getAndPut = function(key, val, callback) { } /** + * replace cache value + * + * @this {Cache} + * @param {string} key Key + * @param {string} value Value + * @param {onGet} callback Called on finish + */ +Cache.prototype.replace = function(key, val, callback) { + this._server.runCommand(this._createCommand("rep"). + setPostData(JSON.stringify({"key" : key, "val" : val})), callback); +} + +/** + * Get and put cache value + * + * @this {Cache} + * @param {string} key Key + * @param {string} value Value + * @param {onGet} callback Called on finish + */ +Cache.prototype.getAndReplace = function(key, val, callback) { + this._server.runCommand(this._createCommand("getandreplace"). + setPostData(JSON.stringify({"key" : key, "val" : val})), callback); +} + +/** * Stores given key-value pair in cache only if cache had no previous mapping for it. * * @this {Cache} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2496ac3c/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 47e4f09..d5bf7ae 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 @@ -175,4 +175,25 @@ public class NodeJsCacheApiSelfTest extends NodeJsAbstractTest { public void testRemoveAllFromCache() throws Exception { runJsScript("testRemoveAllFromCache"); } + + /** + * @throws Exception If failed. + */ + public void testReplace() throws Exception { + runJsScript("testReplace"); + } + + /** + * @throws Exception If failed. + */ + public void testIncorrectReplaceObject() throws Exception { + runJsScript("testIncorrectReplaceObject"); + } + + /** + * @throws Exception If failed. + */ + public void testReplaceObject() throws Exception { + runJsScript("testReplaceObject"); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2496ac3c/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 4766c9c..cca7e10 100644 --- a/modules/nodejs/src/test/js/test-cache-api.js +++ b/modules/nodejs/src/test/js/test-cache-api.js @@ -160,6 +160,65 @@ testRemoveAllFromCache = function() { startTest("mycache", {trace: [put, removeAllFromCache, getNone], entry: ["key", "6"]}); } +testReplace = function() { + function replace(cache, entry, next) { + cache.replace(entry[0], "7", onReplace.bind(null, cache)); + + function onReplace(cache, err, res) { + assert(err === null, "Get error on get and put [err=" + err + "]"); + assert(res === true, "Incorrect result for replace [expected=true, val" + res + "]"); + + cache.get(entry[0], function(err, res) { + assert(!err); + assert("7" === res, "Get incorrect value on get [exp=7, val=" + res + "]"); + TestUtils.testDone(); + }); + } + } + + startTest("mycache", {trace: [put, replace], entry: ["key", "6"]}); +} + +testReplaceObject = function() { + function replace(cache, entry, next) { + var newKey = {"key" :"7"}; + cache.replace(entry[0], newKey, onReplace.bind(null, cache)); + + function onReplace(cache, err, res) { + assert(err === null, "Get error on get and put [err=" + err + "]"); + assert(res === true, "Incorrect result for replace [expected=true, val" + res + "]"); + + cache.get(entry[0], function(err, res) { + assert(!err); + assert(TestUtils.compareObject(newKey, res), "Get incorrect value on get."); + TestUtils.testDone(); + }); + } + } + + var key = {"name" : "Paul"}; + var val = {"age" : 12, "books" : ["1", "Book"]}; + + startTest("mycache", {trace: [put, replace], entry: [key, val]}); +} + +testIncorrectReplaceObject = function() { + function replace(cache, entry, next) { + cache.replace(entry[0], "7", onReplace.bind(null, cache)); + + function onReplace(cache, err, res) { + assert(err !== null, "Do not get error"); + assert(err.indexOf("Failed to update keys") > -1, "Incorrect error message: " + err); + TestUtils.testDone(); + } + } + + var key = {"name" : "Paul"}; + var val = {"age" : 12, "books" : ["1", "Book"]}; + + startTest("mycache", {trace: [put, replace], entry: [key, val]}); +} + function objectEntries() { entries = []; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2496ac3c/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 be44916..b96b066 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 @@ -427,7 +427,7 @@ public class GridJettyRestHandler extends AbstractHandler { else if (cmd == CACHE_GET || cmd == CACHE_PUT || cmd == CACHE_REMOVE || cmd == CACHE_CONTAINS_KEY || cmd == CACHE_GET_AND_PUT || cmd == CACHE_GET_AND_PUT_IF_ABSENT || cmd == CACHE_GET_AND_REMOVE || - cmd == CACHE_PUT_IF_ABSENT || cmd == CACHE_REMOVE_VALUE) { + cmd == CACHE_PUT_IF_ABSENT || cmd == CACHE_REMOVE_VALUE || cmd == CACHE_REPLACE) { JSONCacheObject cacheObj = new JSONCacheObject(o); restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName);