#ignite-964: NodeJs ignite.getOrCreateCache is async function.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/ae2d0c2f Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/ae2d0c2f Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/ae2d0c2f Branch: refs/heads/ignite-964 Commit: ae2d0c2ff32dd2ba56f915876a35c99feac930f7 Parents: 43ab939 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Wed Jul 8 20:07:10 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Wed Jul 8 20:07:10 2015 +0300 ---------------------------------------------------------------------- examples/src/main/js/cache-api-example.js | 6 +- examples/src/main/js/cache-put-get-example.js | 8 +-- examples/src/main/js/cache-query-example.js | 4 +- modules/nodejs/src/main/js/apache-ignite.js | 1 - modules/nodejs/src/main/js/cache.js | 75 +++++++++------------- modules/nodejs/src/main/js/ignite.js | 19 ++++-- modules/nodejs/src/test/js/test-cache-api.js | 39 +++++++---- 7 files changed, 83 insertions(+), 69 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ae2d0c2f/examples/src/main/js/cache-api-example.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/cache-api-example.js b/examples/src/main/js/cache-api-example.js index 24c31d2..13368d5 100644 --- a/examples/src/main/js/cache-api-example.js +++ b/examples/src/main/js/cache-api-example.js @@ -23,9 +23,9 @@ Ignition.start(['127.0.0.1:9095'], null, onConnect); function onConnect(err, ignite) { console.log(">>> Cache API example started."); - var cache = ignite.getOrCreateCache("ApiExampleCache"); - - atomicMapOperations(cache); + ignite.getOrCreateCache("ApiExampleCache", function(err, cache) { + atomicMapOperations(cache); + }); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ae2d0c2f/examples/src/main/js/cache-put-get-example.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/cache-put-get-example.js b/examples/src/main/js/cache-put-get-example.js index 92e0797..7a9b035 100644 --- a/examples/src/main/js/cache-put-get-example.js +++ b/examples/src/main/js/cache-put-get-example.js @@ -26,11 +26,11 @@ function onConnect(err, ignite) { if (err) throw err; - var cache = ignite.getOrCreateCache("PutGetExampleCache"); + ignite.getOrCreateCache("PutGetExampleCache", function(err, cache) { + putGet(cache); - putGet(cache); - - putAllGetAll(cache); + putAllGetAll(cache); + }); } putGet = function(cache) { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ae2d0c2f/examples/src/main/js/cache-query-example.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/cache-query-example.js b/examples/src/main/js/cache-query-example.js index f31f0d5..1b774a0 100644 --- a/examples/src/main/js/cache-query-example.js +++ b/examples/src/main/js/cache-query-example.js @@ -31,7 +31,9 @@ function onConnect(err, ignite) { var entries = [new Entry("key0", "val0"), new Entry("key1", "val1")]; - ignite.getOrCreateCache(cacheName).putAll(entries, onCachePut.bind(null, ignite)); + ignite.getOrCreateCache(cacheName, function(err, cache) { + cache.putAll(entries, onCachePut.bind(null, ignite)); + }); } function onCachePut(ignite, err) { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ae2d0c2f/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 2379b36..053b88a 100644 --- a/modules/nodejs/src/main/js/apache-ignite.js +++ b/modules/nodejs/src/main/js/apache-ignite.js @@ -19,7 +19,6 @@ module.exports = { Cache : require('./cache.js').Cache, CacheEntry : require('./cache.js').CacheEntry, Ignition : require('./ignition.js').Ignition, - Server : require('./server.js').Server, Ignite : require('./ignite.js').Ignite, Compute : require('./compute.js').Compute, SqlQuery : require('./sql-query.js').SqlQuery, http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ae2d0c2f/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 67a8b6c..893a945 100644 --- a/modules/nodejs/src/main/js/cache.js +++ b/modules/nodejs/src/main/js/cache.js @@ -27,12 +27,20 @@ var SqlQuery = require("./sql-query").SqlQuery * @this {Cache} * @param {Server} server Server class * @param {string} cacheName Cache name - * @param {boolean} create True if cache is not exist. */ -function Cache(server, cacheName, create) { +function Cache(server, cacheName) { this._server = server; this._cacheName = cacheName; - this._create = create; +} + +/** + * Get cache name. + * + * @this{Cache} + * @returns {string} Cache name. + */ +Cache.prototype.name = function() { + return this._cacheName; } /** @@ -43,7 +51,7 @@ function Cache(server, cacheName, create) { * @param {onGet} callback Called on finish */ Cache.prototype.get = function(key, callback) { - this._runCacheCommand(this._createCommand("get"). + this._server.runCommand(this._createCommand("get"). setPostData(JSON.stringify({"key": key})), callback); }; @@ -57,7 +65,7 @@ Cache.prototype.get = function(key, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.put = function(key, value, callback) { - this._runCacheCommand(this._createCommand("put"). + this._server.runCommand(this._createCommand("put"). setPostData(JSON.stringify({"key": key, "val" : value})), callback); } @@ -71,7 +79,7 @@ Cache.prototype.put = function(key, value, callback) { * @param {onGet} callback Called on finish */ Cache.prototype.putIfAbsent = function(key, value, callback) { - this._runCacheCommand(this._createCommand("putifabsent"). + this._server.runCommand(this._createCommand("putifabsent"). setPostData(JSON.stringify({"key": key, "val" : value})), callback); } @@ -84,7 +92,7 @@ Cache.prototype.putIfAbsent = function(key, value, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.remove = function(key, callback) { - this._runCacheCommand(this._createCommand("rmv"). + this._server.runCommand(this._createCommand("rmv"). setPostData(JSON.stringify({"key": key})), callback); } @@ -98,7 +106,7 @@ Cache.prototype.remove = function(key, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.removeValue = function(key, value, callback) { - this._runCacheCommand(this._createCommand("rmvvalue"). + this._server.runCommand(this._createCommand("rmvvalue"). setPostData(JSON.stringify({"key": key, "val" : value})), callback); } @@ -111,7 +119,7 @@ Cache.prototype.removeValue = function(key, value, callback) { * @param {onGet} callback Called on finish with previous value */ Cache.prototype.getAndRemove = function(key, callback) { - this._runCacheCommand(this._createCommand("getandrmv"). + this._server.runCommand(this._createCommand("getandrmv"). setPostData(JSON.stringify({"key": key})), callback); } @@ -124,7 +132,7 @@ Cache.prototype.getAndRemove = function(key, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.removeAll = function(keys, callback) { - this._runCacheCommand(this._createCommand("rmvall"). + this._server.runCommand(this._createCommand("rmvall"). setPostData(JSON.stringify({"keys" : keys})), callback); } @@ -136,7 +144,7 @@ Cache.prototype.removeAll = function(keys, callback) { * @param {noValue} callback Called on finish */ Cache.prototype.removeAllFromCache = function(callback) { - this._runCacheCommand(this._createCommand("rmvall"), + this._server.runCommand(this._createCommand("rmvall"), callback); } @@ -148,7 +156,7 @@ Cache.prototype.removeAllFromCache = function(callback) { * @param {noValue} callback Called on finish */ Cache.prototype.putAll = function(entries, callback) { - this._runCacheCommand(this._createCommand("putall").setPostData( + this._server.runCommand(this._createCommand("putall").setPostData( JSON.stringify({"entries" : entries})), callback); } @@ -176,7 +184,7 @@ Cache.prototype.getAll = function(keys, callback) { callback.call(null, null, result); } - this._runCacheCommand(this._createCommand("getall").setPostData( + this._server.runCommand(this._createCommand("getall").setPostData( JSON.stringify({"keys" : keys})), onGetAll.bind(null, callback)); } @@ -189,7 +197,7 @@ Cache.prototype.getAll = function(keys, callback) { * @param {Cache~onGetAll} callback Called on finish with boolean result */ Cache.prototype.containsKey = function(key, callback) { - this._runCacheCommand(this._createCommand("containskey"). + this._server.runCommand(this._createCommand("containskey"). setPostData(JSON.stringify({"key" : key})), callback); } @@ -201,7 +209,7 @@ Cache.prototype.containsKey = function(key, callback) { * @param {Cache~onGetAll} callback Called on finish with boolean result */ Cache.prototype.containsKeys = function(keys, callback) { - this._runCacheCommand(this._createCommand("containskeys"). + this._server.runCommand(this._createCommand("containskeys"). setPostData(JSON.stringify({"keys" : keys})), callback); } @@ -214,7 +222,7 @@ Cache.prototype.containsKeys = function(keys, callback) { * @param {onGet} callback Called on finish */ Cache.prototype.getAndPut = function(key, val, callback) { - this._runCacheCommand(this._createCommand("getandput"). + this._server.runCommand(this._createCommand("getandput"). setPostData(JSON.stringify({"key" : key, "val" : val})), callback); } @@ -227,7 +235,7 @@ Cache.prototype.getAndPut = function(key, val, callback) { * @param {onGet} callback Called on finish */ Cache.prototype.replace = function(key, val, callback) { - this._runCacheCommand(this._createCommand("rep"). + this._server.runCommand(this._createCommand("rep"). setPostData(JSON.stringify({"key" : key, "val" : val})), callback); } @@ -241,7 +249,7 @@ Cache.prototype.replace = function(key, val, callback) { * @param {onGet} callback Called on finish */ Cache.prototype.replaceValue = function(key, val, oldVal, callback) { - this._runCacheCommand(this._createCommand("repVal"). + this._server.runCommand(this._createCommand("repVal"). setPostData(JSON.stringify({"key" : key, "val" : val, "oldVal" : oldVal})), callback); } @@ -254,7 +262,7 @@ Cache.prototype.replaceValue = function(key, val, oldVal, callback) { * @param {onGet} callback Called on finish */ Cache.prototype.getAndReplace = function(key, val, callback) { - this._runCacheCommand(this._createCommand("getandreplace"). + this._server.runCommand(this._createCommand("getandreplace"). setPostData(JSON.stringify({"key" : key, "val" : val})), callback); } @@ -267,7 +275,7 @@ Cache.prototype.getAndReplace = function(key, val, callback) { * @param {onGet} callback Called on finish */ Cache.prototype.getAndPutIfAbsent = function(key, val, callback) { - this._runCacheCommand(this._createCommand("getandputifabsent"). + this._server.runCommand(this._createCommand("getandputifabsent"). setPostData(JSON.stringify({"key" : key, "val" : val})), callback); } @@ -276,7 +284,7 @@ Cache.prototype.getAndPutIfAbsent = function(key, val, callback) { * @param {onGet} callback Called on finish */ Cache.prototype.size = function(callback) { - this._runCacheCommand(this._createCommand("cachesize"), callback); + this._server.runCommand(this._createCommand("cachesize"), callback); } /** @@ -319,7 +327,7 @@ Cache.prototype._sqlFieldsQuery = function(qry, onQueryExecute) { command.setPostData(JSON.stringify({"arg" : qry.arguments()})); - this._runCacheCommand(command, onQueryExecute.bind(this, qry)); + this._server.runCommand(command, onQueryExecute.bind(this, qry)); } Cache.prototype._sqlQuery = function(qry, onQueryExecute) { @@ -335,7 +343,7 @@ Cache.prototype._sqlQuery = function(qry, onQueryExecute) { command.setPostData(JSON.stringify({"arg" : qry.arguments()})); - this._runCacheCommand(command, onQueryExecute.bind(this, qry)); + this._server.runCommand(command, onQueryExecute.bind(this, qry)); } Cache.prototype._createCommand = function(name) { @@ -352,27 +360,6 @@ Cache.prototype._createQueryCommand = function(name, qry) { return command.addParam("psz", qry.pageSize()); } -Cache.prototype._runCacheCommand = function(command, callback) { - if (this._create) { - var onCreateCallback = function(command, callback, err) { - if (err !== null) { - callback.call(null, err, null); - - return; - } - - this._create = false; - - this._server.runCommand(command, callback); - } - - this._server.runCommand(this._createCommand("getorcreatecache"), - onCreateCallback.bind(this, command, callback)); - } - else { - this._server.runCommand(command, callback); - } -} /** * @this{CacheEntry} * @param key Key http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ae2d0c2f/modules/nodejs/src/main/js/ignite.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/ignite.js b/modules/nodejs/src/main/js/ignite.js index fea34f9..cbe8552 100644 --- a/modules/nodejs/src/main/js/ignite.js +++ b/modules/nodejs/src/main/js/ignite.js @@ -48,7 +48,7 @@ Ignite.prototype.server = function() { * @returns {Cache} Cache */ Ignite.prototype.cache = function(cacheName) { - return new Cache(this._server, cacheName, false); + return new Cache(this._server, cacheName); } /** @@ -56,10 +56,21 @@ Ignite.prototype.cache = function(cacheName) { * * @this {Ignite} * @param {string} Cache name - * @returns {Cache} Cache + * @param callback Callback with cache. */ -Ignite.prototype.getOrCreateCache = function(cacheName) { - return new Cache(this._server, cacheName, true); +Ignite.prototype.getOrCreateCache = function(cacheName, callback) { + var onCreateCallback = function(err) { + if (err !== null) { + callback.call(null, err, null); + + return; + } + + callback.call(null, null, new Cache(this._server, cacheName)) + } + + this._server.runCommand(new Command("getorcreatecache").addParam("cacheName", cacheName), + onCreateCallback.bind(this)); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ae2d0c2f/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 c599a86..9855fa3 100644 --- a/modules/nodejs/src/test/js/test-cache-api.js +++ b/modules/nodejs/src/test/js/test-cache-api.js @@ -311,23 +311,38 @@ function startTest(createCache, cacheName, testDescription) { } function onStart(createCache, cacheName, testDescription, error, ignite) { - var cache; if (createCache) { - cache = ignite.getOrCreateCache(cacheName); + ignite.getOrCreateCache(cacheName, function(err, cache) { + assert(err === null, err); + + function callNext(error) { + assert(!error); + var next = testDescription.trace.shift(); + if (next) + next.call(null, cache, testDescription.entry, callNext); + else + TestUtils.testDone(); + } + + callNext(); + }); } else { - cache = ignite.cache(cacheName); - } - callNext(); + var cache = ignite.cache(cacheName); + + function callNext(error) { + assert(!error); + var next = testDescription.trace.shift(); + if (next) + next.call(null, cache, testDescription.entry, callNext); + else + TestUtils.testDone(); + } - function callNext(error) { - assert(!error); - var next = testDescription.trace.shift(); - if (next) - next.call(null, cache, testDescription.entry, callNext); - else - TestUtils.testDone(); + callNext(); } + + } function put(cache, entry, next) {