ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ivasilin...@apache.org
Subject [06/39] incubator-ignite git commit: #ignite-964: introduce command object in server.js
Date Wed, 01 Jul 2015 10:13:42 GMT
#ignite-964: introduce command object in server.js


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

Branch: refs/heads/ignite-964
Commit: 8744687687f5994c1a881432a7c0600a6b7368c4
Parents: 2c1ecf2
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Tue Jun 30 02:02:44 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Tue Jun 30 02:02:44 2015 +0300

----------------------------------------------------------------------
 modules/nodejs/src/main/js/cache.js   | 100 ++++++++---------------------
 modules/nodejs/src/main/js/compute.js |  14 ++--
 modules/nodejs/src/main/js/ignite.js  |   7 +-
 modules/nodejs/src/main/js/server.js  |  85 ++++++++++++++++--------
 4 files changed, 98 insertions(+), 108 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/87446876/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 9f72589..1ece92d 100644
--- a/modules/nodejs/src/main/js/cache.js
+++ b/modules/nodejs/src/main/js/cache.js
@@ -16,6 +16,7 @@
  */
 
 var Server = require("./server").Server;
+var Command = require("./server").Command;
 var SqlFieldsQuery = require("./sql-fields-query").SqlFieldsQuery
 var SqlQuery = require("./sql-query").SqlQuery
 
@@ -30,7 +31,6 @@ var SqlQuery = require("./sql-query").SqlQuery
 function Cache(server, cacheName) {
     this._server = server;
     this._cacheName = cacheName;
-    this._cacheNameParam = Server.pair("cacheName", this._cacheName);
 }
 
 /**
@@ -41,7 +41,7 @@ function Cache(server, cacheName) {
  * @param {onGet} callback Called on finish
  */
 Cache.prototype.get = function(key, callback) {
-    this._server.runCommand("get", [this._cacheNameParam, Server.pair("key", key)], callback);
+    this._server.runCommand(this._createCommand("get").addParam("key", key), callback);
 };
 
 /**
@@ -53,7 +53,7 @@ Cache.prototype.get = function(key, callback) {
  * @param {noValue} callback Called on finish
  */
 Cache.prototype.put = function(key, value, callback) {
-    this._server.runCommand("put", [this._cacheNameParam, Server.pair("key", key), Server.pair("val",
value)],
+    this._server.runCommand(this._createCommand("put").addParam("key", key).addParam("val",
value),
         callback);
 }
 
@@ -65,7 +65,7 @@ Cache.prototype.put = function(key, value, callback) {
  * @param {noValue} callback Called on finish
  */
 Cache.prototype.remove = function(key, callback) {
-    this._server.runCommand("rmv", [this._cacheNameParam, Server.pair("key", key)], callback);
+    this._server.runCommand(this._createCommand("rmv").addParam("key", key), callback);
 }
 
 /**
@@ -76,11 +76,7 @@ Cache.prototype.remove = function(key, callback) {
  * @param {noValue} callback Called on finish
  */
 Cache.prototype.removeAll = function(keys, callback) {
-    var params = [this._cacheNameParam];
-
-    params = params.concat(Cache.concatParams("k", keys));
-
-    this._server.runCommand("rmvall", params, callback);
+    this._server.runCommand(this._createCommand("rmvall").addParams("k", keys), callback);
 }
 
 /**
@@ -99,12 +95,7 @@ Cache.prototype.putAll = function(map, callback) {
         values.push(map[key]);
     }
 
-    var params = Cache.concatParams("k", keys);
-    params = params.concat(Cache.concatParams("v", values));
-
-    params.push(this._cacheNameParam);
-
-    this._server.runCommand("putall", params, callback);
+    this._server.runCommand(this._createCommand("putall").addParams("k", keys).addParams("v",
values), callback);
 }
 
 /**
@@ -115,9 +106,7 @@ Cache.prototype.putAll = function(map, callback) {
  * @param {noValue} callback Called on finish
  */
 Cache.prototype.postPutAll = function(map, callback) {
-    var params = [this._cacheNameParam];
-
-    this._server.runCommand("putall2", params, callback, JSON.stringify(map));
+    this._server.runCommand(this._createCommand("putall2").setPostData(JSON.stringify(map)),
callback);
 }
 
 /**
@@ -136,11 +125,7 @@ Cache.prototype.postPutAll = function(map, callback) {
  * @param {Cache~onGetAll} callback Called on finish
  */
 Cache.prototype.getAll = function(keys, callback) {
-    var params = Cache.concatParams("k", keys);
-
-    params.push(this._cacheNameParam);
-
-    this._server.runCommand("getall", params, callback);
+    this._server.runCommand(this._createCommand("getall").addParams("k", keys), callback);
 }
 
 /**
@@ -167,11 +152,9 @@ Cache.prototype.query = function(qry) {
             qry.end();
         }
         else {
-            this._server.runCommand("qryfetch", [
-                Server.pair("cacheName", this._cacheName),
-                Server.pair("qryId", res.queryId),
-                Server.pair("psz", qry.pageSize())],
-                onQueryExecute.bind(this, qry));
+            var command = this._createCommand("qryfetch");
+            command.addParam("qryId", res.queryId).addParam("psz", qry.pageSize());
+            this._server.runCommand(command, onQueryExecute.bind(this, qry));
         }
     }
 
@@ -184,63 +167,36 @@ Cache.prototype.query = function(qry) {
 }
 
 Cache.prototype._sqlFieldsQuery = function(qry, onQueryExecute) {
-    var params = [Server.pair("cacheName", this._cacheName),
-        Server.pair("qry", qry.query()),
-        Server.pair("psz", qry.pageSize())];
-
-    params = params.concat(this._sqlArguments(qry.arguments()));
-
-    this._server.runCommand("qryfieldsexecute", params,
-        onQueryExecute.bind(this, qry));
-}
-
-Cache.prototype._sqlArguments = function(args) {
-    var res = [];
-    console.log("ARGS=" + args);
-
-    for (var i = 1; i <= args.length; i++) {
-        res.push(Server.pair("arg" + i, args[i - 1]));
-    }
+    var command = this._createQueryCommand("qryfieldsexecute", qry);
+    command.addParams("arg", qry.arguments());
 
-    return res;
+    this._server.runCommand(command, onQueryExecute.bind(this, qry));
 }
 
 Cache.prototype._sqlQuery = function(qry, onQueryExecute) {
-    var params = [Server.pair("cacheName", this._cacheName),
-        Server.pair("qry", qry.query()),
-        Server.pair("psz", qry.pageSize())]
 
-    params = params.concat(this._sqlArguments(qry.arguments()));
-
-    if (qry.returnType() != null) {
-        params.push(Server.pair("type", qry.returnType()));
-    }
-    else {
+    if (qry.returnType() == null) {
         qry.error("No type for sql query.");
         qry.end();
-
         return;
     }
 
-    this._server.runCommand("qryexecute", params,
-        onQueryExecute.bind(this, qry));
-}
+    var command = this._createQueryCommand("qryexecute", qry);
+    command.addParams("arg", qry.arguments());
+    command.addParam("type", qry.returnType());
 
-/**
- * Concatenate all parameters
- *
- * @param {string} pref Prefix
- * @param {string[]} keys Keys
- * @returns List of parameters.
- */
-Cache.concatParams = function(pref, keys) {
-    var temp = []
+    this._server.runCommand(command, onQueryExecute.bind(this, qry));
+}
 
-    for (var i = 1; i <= keys.length; ++i) {
-        temp.push(Server.pair(pref + i, keys[i-1]));
-    }
+Cache.prototype._createCommand = function(name) {
+    var command = new Command(name);
+    return command.addParam("cacheName", this._cacheName);
+}
 
-    return temp;
+Cache.prototype._createQueryCommand = function(name, qry) {
+    var command = this._createCommand(name);
+    command.addParam("qry", qry.query());
+    return command.addParam("psz", qry.pageSize());
 }
 
 exports.Cache = Cache
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/87446876/modules/nodejs/src/main/js/compute.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/main/js/compute.js b/modules/nodejs/src/main/js/compute.js
index 5789528..830ba85 100644
--- a/modules/nodejs/src/main/js/compute.js
+++ b/modules/nodejs/src/main/js/compute.js
@@ -16,6 +16,7 @@
  */
 
 var Server = require("./server").Server;
+var Command = require("./server").Command;
 
 /**
  * @constructor
@@ -33,8 +34,7 @@ function Compute(server) {
  * @param {onGet} callback Callback
  */
 Compute.prototype.runScript = function(runnable, args, callback) {
-    this._server.runCommand("runscript", [Server.pair("func", runnable),
-        Server.pair("arg", args)], callback);
+    this._server.runCommand(new Command("runscript").addParam("func", runnable).addParam("arg",
args), callback);
 }
 
 /**
@@ -45,13 +45,13 @@ Compute.prototype.runScript = function(runnable, args, callback) {
  * @param {onGet} callback Callback
  */
 Compute.prototype.execute = function(map, reduce, arg, callback) {
-    var params = [];
+    var command = new Command("excmapreduce");
 
-    params.push(Server.pair("map", map));
-    params.push(Server.pair("reduce", reduce));
-    params.push(Server.pair("arg", arg));
+    command.addParam("map", map);
+    command.addParam("reduce", reduce);
+    command.addParam("arg", arg);
 
-    this._server.runCommand("excmapreduce", params, callback);
+    this._server.runCommand(command, callback);
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/87446876/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 d7203e4..dde259e 100644
--- a/modules/nodejs/src/main/js/ignite.js
+++ b/modules/nodejs/src/main/js/ignite.js
@@ -19,6 +19,7 @@ var Cache = require("./cache").Cache;
 var Compute = require("./compute").Compute;
 var ClusterNode = require("./cluster-node").ClusterNode;
 var Server = require("./server").Server;
+var Command = require("./server").Command;
 
 /**
  * Create an instance of Ignite
@@ -67,7 +68,7 @@ Ignite.prototype.compute = function() {
  * @param {onGet} callback Result in callback contains string with Ignite version.
  */
 Ignite.prototype.version = function(callback) {
-    this._server.runCommand("version", [], callback);
+    this._server.runCommand(new Command("version"), callback);
 }
 
 /**
@@ -77,7 +78,7 @@ Ignite.prototype.version = function(callback) {
  * @param {onGet} callback Result in callback contains string with Ignite name.
  */
 Ignite.prototype.name = function(callback) {
-    this._server.runCommand("name", [], callback);
+    this._server.runCommand(new Command("name"), callback);
 }
 
 /**
@@ -107,7 +108,7 @@ Ignite.prototype.cluster = function(callback) {
         callback.call(null, null, nodes);
     }
 
-    this._server.runCommand("top", [Server.pair("attr", "true"), Server.pair("mtr", "false")],
+    this._server.runCommand(new Command("top").addParam("attr", "true").addParam("mtr", "false"),
         onTop.bind(null, callback));
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/87446876/modules/nodejs/src/main/js/server.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/main/js/server.js b/modules/nodejs/src/main/js/server.js
index b3586e3..2714ae5 100644
--- a/modules/nodejs/src/main/js/server.js
+++ b/modules/nodejs/src/main/js/server.js
@@ -61,27 +61,23 @@ Server.prototype.host = function() {
  * @param params Parameters for command.
  * @param {onGet} Called on finish
  */
-Server.prototype.runCommand = function(cmdName, params, callback, postData) {
-    var paramsString = "";
-
-    for (var p of params) {
-        paramsString += "&" + p.key + "=" + p.value;
-    }
+Server.prototype.runCommand = function(cmd, callback) {
 
-    var requestQry = "cmd=" + cmdName + paramsString;
+    var requestQry = "cmd=" + cmd.name() + cmd.paramsString();
 
     var http = require('http');
 
     var options = {
         host: this._host,
         port: this._port,
-        method : postData ? "POST" : "GET",
+        method : cmd._method(),
         path: "/ignite?" + requestQry,
         headers: this._signature()
     };
 
-    if (postData)
-        options.headers['Content-Length'] = postData.length;
+    if (cmd._isPost()) {
+        options.headers['Content-Length'] = cmd.postData().length;
+    }
 
     function streamCallback(response) {
         var fullResponseString = '';
@@ -129,9 +125,9 @@ Server.prototype.runCommand = function(cmdName, params, callback, postData)
{
 
     request.on('error', callback);
 
-    if (postData)
-        request.write(postData);
-
+    if (cmd._isPost()) {
+        request.write(cmd.postData());
+    }
     request.end();
 }
 
@@ -142,18 +138,7 @@ Server.prototype.runCommand = function(cmdName, params, callback, postData)
{
  * @param {onGet} callback Called on finish
  */
 Server.prototype.checkConnection = function(callback) {
-    this.runCommand("version", [], callback);
-}
-
-/**
- * Returns pair for runCommand
- *
- * @param {string} key Key
- * @param {string} value Value
- * @returns Pair of strings
- */
-Server.pair = function(key, value) {
-    return {key: Server._escape(key), value: Server._escape(value)}
+    this.runCommand(new Command("version"), callback);
 }
 
 /**
@@ -194,4 +179,52 @@ Server._escape = function(f) {
     return qs.escape(f.toString());
 }
 
-exports.Server = Server;
\ No newline at end of file
+function Command(name) {
+    this._name = name;
+    this._params = [];
+}
+
+Command.prototype.addParam = function(key, value) {
+    this._params.push({key: key, value: value});
+    return this;
+}
+
+Command.prototype.addParams = function(prefix, params) {
+    for (var i = 1; i <= params.length; ++i) {
+        this.addParam(prefix + i, params[i - 1]);
+    }
+    return this;
+}
+
+Command.prototype.setPostData = function(postData) {
+    this._postData = postData;
+    return this;
+}
+
+Command.prototype.postData = function() {
+    return this._postData;
+}
+
+Command.prototype._method = function() {
+    return this._isPost()? "POST" : "GET";
+}
+
+Command.prototype._isPost = function() {
+    return !!this._postData;
+}
+
+Command.prototype.name = function() {
+    return this._name;
+}
+
+Command.prototype.paramsString = function() {
+    var paramsString = "";
+
+    for (var p of this._params) {
+        paramsString += "&" + Server._escape(p.key) + "=" + Server._escape(p.value);
+    }
+    return paramsString;
+}
+
+exports.Server = Server;
+exports.Command = Command;
\ No newline at end of file


Mime
View raw message