Repository: incubator-ignite Updated Branches: refs/heads/ignite-961-promise e97945747 -> 9ccabbaa4
#ignite-961-promise: add all methods to cache-promise. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/9ccabbaa Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/9ccabbaa Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/9ccabbaa Branch: refs/heads/ignite-961-promise Commit: 9ccabbaa47bdcbb76175363e7cac60a77fff86e4 Parents: e979457 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Mon Jul 13 17:23:29 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Mon Jul 13 17:23:29 2015 +0300 ---------------------------------------------------------------------- modules/nodejs/src/main/js/cache-promise.js | 267 ++++++++++++++++++++--- 1 file changed, 235 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/9ccabbaa/modules/nodejs/src/main/js/cache-promise.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/cache-promise.js b/modules/nodejs/src/main/js/cache-promise.js index 8c4d329..1e34233 100644 --- a/modules/nodejs/src/main/js/cache-promise.js +++ b/modules/nodejs/src/main/js/cache-promise.js @@ -35,14 +35,24 @@ function CachePromise(server, cacheName) { } /** + * Get cache name. + * + * @this{Cache} + * @returns {string} Cache name. + */ +CachePromise.prototype.name = function() { + return this._cacheName; +} + +/** * Get cache value * * @this {Cache} * @param {string} key Key */ CachePromise.prototype.get = function(key) { - console.log("GET key=" + key); - return this.__createPromise(this._createCommand("get").setPostData(JSON.stringify({"key": key}))); + return this.__createPromise(this._createCommand("get"). + setPostData(JSON.stringify({"key": key}))); }; /** @@ -53,7 +63,202 @@ CachePromise.prototype.get = function(key) { * @param {string} value Value */ CachePromise.prototype.put = function(key, value) { - return this.__createPromise(this._createCommand("put").setPostData(JSON.stringify({"key": key, "val" : value}))); + return this.__createPromise(this._createCommand("put"). + setPostData(JSON.stringify({"key": key, "val" : value}))); +} + +/** + * Put if absent + * + * @this {Cache} + * @param {string} key Key + * @param {string} value Value + */ +CachePromise.prototype.putIfAbsent = function(key, value) { + return this.__createPromise(this._createCommand("putifabsent"). + setPostData(JSON.stringify({"key": key, "val" : value}))); +} + +/** + * Remove cache key + * + * @this {Cache} + * @param key Key + */ +CachePromise.prototype.remove = function(key, callback) { + return this.__createPromise(this._createCommand("rmv"). + setPostData(JSON.stringify({"key": key}))); +} + +/** + * Remove cache key + * + * @this {Cache} + * @param key Key + * @param value Value + */ +CachePromise.prototype.removeValue = function(key, value, callback) { + return this.__createPromise(this._createCommand("rmvvalue"). + setPostData(JSON.stringify({"key": key, "val" : value}))); +} + +/** + * Get and remove cache key + * + * @this {Cache} + * @param {string} key Key + */ +CachePromise.prototype.getAndRemove = function(key, callback) { + return this.__createPromise(this._createCommand("getandrmv"). + setPostData(JSON.stringify({"key": key}))); +} + +/** + * Remove cache keys + * + * @this {Cache} + * @param {string[]} keys Keys to remove + */ +CachePromise.prototype.removeAll = function(keys, callback) { + return this.__createPromise(this._createCommand("rmvall"). + setPostData(JSON.stringify({"keys" : keys}))); +} + +/** + * Remove all cache keys + * + * @this {Cache} + */ +CachePromise.prototype.removeAllFromCache = function(callback) { + return this.__createPromise(this._createCommand("rmvall")); +} + +/** + * Put keys to cache + * + * @this {Cache} + * @param {CacheEntry[]} List of entries to put in the cache + */ +CachePromise.prototype.putAll = function(entries) { + return this.__createPromise(this._createCommand("putall").setPostData( + JSON.stringify({"entries" : entries}))); +} + +/** + * Get keys from the cache + * + * @this {Cache} + * @param {Object[]} keys Keys + */ +CachePromise.prototype.getAll = function(keys, callback) { + var cmd = this._createCommand("getall").setPostData(JSON.stringify({"keys" : keys})); + + return new Promise(function(resolve, reject) { + server.runCommand(cmd, function(err, res) { + if(err != null) { + reject(err); + } + else { + var result = []; + + for (var key of res) { + result.push(new CacheEntry(key["key"], key["value"])); + } + + resolve(result); + } + }); + }); +} + +/** + * Determines if the cache contains an entry for the specified key. + * + * @this {Cache} + * @param {Object} key Key + */ +CachePromise.prototype.containsKey = function(key) { + return this.__createPromise(this._createCommand("containskey"). + setPostData(JSON.stringify({"key" : key}))); +} + +/** + * Determines if the cache contains all keys. + * + * @this {Cache} + * @param {Object[]} keys Keys + */ +CachePromise.prototype.containsKeys = function(keys, callback) { + return this.__createPromise(this._createCommand("containskeys"). + setPostData(JSON.stringify({"keys" : keys}))); +} + +/** + * Get and put cache value + * + * @this {Cache} + * @param {string} key Key + * @param {string} value Value + */ +CachePromise.prototype.getAndPut = function(key, val) { + return this.__createPromise(this._createCommand("getandput"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); +} + +/** + * replace cache value + * + * @this {Cache} + * @param key Key + * @param value Value + */ +CachePromise.prototype.replace = function(key, val, callback) { + return this.__createPromise(this._createCommand("rep"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); +} + +/** + * replace cache value + * + * @this {Cache} + * @param key Key + * @param value Value + * @param oldVal Old value + */ +CachePromise.prototype.replaceValue = function(key, val, oldVal) { + return this.__createPromise(this._createCommand("repVal"). + setPostData(JSON.stringify({"key" : key, "val" : val, "oldVal" : oldVal}))); +} + +/** + * Get and put cache value + * + * @this {Cache} + * @param {string} key Key + * @param {string} value Value + */ +CachePromise.prototype.getAndReplace = function(key, val) { + return this.__createPromise(this._createCommand("getandreplace"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); +} + +/** + * Stores given key-value pair in cache only if cache had no previous mapping for it. + * + * @this {Cache} + * @param {string} key Key + * @param {string} value Value + */ +CachePromise.prototype.getAndPutIfAbsent = function(key, val) { + return this.__createPromise(this._createCommand("getandputifabsent"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); +} + +/** + * @this {Cache} + */ +CachePromise.prototype.size = function(callback) { + return this.__createPromise(this._createCommand("cachesize")); } /** @@ -85,6 +290,33 @@ CachePromise.prototype.query = function(qry) { }); } +CachePromise.prototype.__createPromise = function(cmd) { + return new Promise(function(resolve, reject) { + server.runCommand(cmd, function(err, res) { + if(err != null) { + reject(err); + } + else { + resolve(res); + } + }); + }); +} + +CachePromise.prototype._createCommand = function(name) { + var command = new Command(name); + + return command.addParam("cacheName", this._cacheName); +} + +CachePromise.prototype._createQueryCommand = function(name, qry) { + var command = this._createCommand(name); + + command.addParam("qry", qry.query()); + + return command.addParam("psz", qry.pageSize()); +} + Cursor = function(qry, res) { this._qry = qry; this._res = res; @@ -120,33 +352,4 @@ Cursor.prototype.isFinished = function() { return this._res["last"]; } -CachePromise.prototype.__createPromise = function(cmd) { - return new Promise(function(resolve, reject) { - server.runCommand(cmd, function(err, res) { - if(err != null) { - reject(err); - } - else { - resolve(res); - } - }); - }); -} - - -CachePromise.prototype._createCommand = function(name) { - var command = new Command(name); - - return command.addParam("cacheName", this._cacheName); -} - - -CachePromise.prototype._createQueryCommand = function(name, qry) { - var command = this._createCommand(name); - - command.addParam("qry", qry.query()); - - return command.addParam("psz", qry.pageSize()); -} - exports.CachePromise = CachePromise \ No newline at end of file