You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by iv...@apache.org on 2015/07/13 13:50:54 UTC
incubator-ignite git commit: #ignite-961-promise: add promises to
nodejs.
Repository: incubator-ignite
Updated Branches:
refs/heads/ignite-961-promise [created] 2013a7adb
#ignite-961-promise: add promises to nodejs.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/2013a7ad
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/2013a7ad
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/2013a7ad
Branch: refs/heads/ignite-961-promise
Commit: 2013a7adbaa3a489d5a7c0eafafaa5f07280bfda
Parents: b1e557c
Author: ivasilinets <iv...@gridgain.com>
Authored: Mon Jul 13 14:50:39 2015 +0300
Committer: ivasilinets <iv...@gridgain.com>
Committed: Mon Jul 13 14:50:39 2015 +0300
----------------------------------------------------------------------
.../main/js/cache-not-promise-api-example.js | 81 +++++++++
.../src/main/js/cache-promise-api-example.js | 75 +++++++++
.../src/main/js/cache-promise-query-example.js | 108 ++++++++++++
modules/nodejs/src/main/js/cache-promise.js | 164 +++++++++++++++++++
modules/nodejs/src/main/js/ignite.js | 36 ++++
modules/nodejs/src/main/js/server.js | 2 +
.../ignite/internal/NodeJsSqlQuerySelfTest.java | 10 ++
modules/nodejs/src/test/js/test-query.js | 48 ++++++
8 files changed, 524 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/examples/src/main/js/cache-not-promise-api-example.js
----------------------------------------------------------------------
diff --git a/examples/src/main/js/cache-not-promise-api-example.js b/examples/src/main/js/cache-not-promise-api-example.js
new file mode 100644
index 0000000..519e4d7
--- /dev/null
+++ b/examples/src/main/js/cache-not-promise-api-example.js
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var apacheIgnite = require("apache-ignite");
+var Ignition = apacheIgnite.Ignition;
+
+/**
+ * This example demonstrates some of the cache rich API capabilities.
+ * <p>
+ * Start Ignite node with {@code examples/config/example-ignite.xml} configuration before running example.
+ * <p>
+ * Alternatively you can run ExampleJsNodeStartup which will
+ * start node with {@code examples/config/example-ignite.xml} configuration.
+ */
+function main() {
+ /** Cache name. */
+ var cacheName = "ApiExampleCache";
+
+ /** Connect to node that started with {@code examples/config/js/example-js-cache.xml} configuration. */
+ Ignition.start(['127.0.0.1:8000..9000'], null, onConnect);
+
+ function onConnect(err, ignite) {
+ if (err !== null)
+ throw "Start remote node with config examples/config/example-ignite.xml.";
+
+ console.log(">>> Cache API example started.");
+
+ // Create cache on server with cacheName.
+ ignite.getOrCreateCache(cacheName, function(err, promiseCache) {
+ atomicMapOperations(ignite, promiseCache);
+ });
+ }
+
+ /**
+ * Demonstrates cache operations similar to {@link ConcurrentMap} API. Note that
+ * cache API is a lot richer than the JDK {@link ConcurrentMap}.
+ */
+ atomicMapOperations = function(ignite, cache) {
+ console.log(">>> Cache atomic map operation examples.");
+
+ cache.put(1, "1", onPut1);
+
+ function onPut1(err,res) {
+ cache.get(1, onGet1);
+ }
+
+ function onGet1(err, res) {
+ if (res === "1") {
+ return cache.put(1, "2", onEnd);
+ }
+ else {
+ return cache.put(1, "3", onEnd);
+ }
+ }
+
+ function onEnd() {
+ console.log(">>> Replace finished.");
+
+ // Destroying cache.
+ ignite.destroyCache(cacheName, function(err) {
+ console.log(">>> End of Cache API example.");
+ });
+ };
+ }
+}
+
+main();
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/examples/src/main/js/cache-promise-api-example.js
----------------------------------------------------------------------
diff --git a/examples/src/main/js/cache-promise-api-example.js b/examples/src/main/js/cache-promise-api-example.js
new file mode 100644
index 0000000..b73b5c1
--- /dev/null
+++ b/examples/src/main/js/cache-promise-api-example.js
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var apacheIgnite = require("apache-ignite");
+var Ignition = apacheIgnite.Ignition;
+
+/**
+ * This example demonstrates some of the cache rich API capabilities.
+ * <p>
+ * Start Ignite node with {@code examples/config/example-ignite.xml} configuration before running example.
+ * <p>
+ * Alternatively you can run ExampleJsNodeStartup which will
+ * start node with {@code examples/config/example-ignite.xml} configuration.
+ */
+function main() {
+ /** Cache name. */
+ var cacheName = "ApiExampleCache";
+
+ /** Connect to node that started with {@code examples/config/js/example-js-cache.xml} configuration. */
+ Ignition.start(['127.0.0.1:8000..9000'], null, onConnect);
+
+ function onConnect(err, ignite) {
+ if (err !== null)
+ throw "Start remote node with config examples/config/example-ignite.xml.";
+
+ console.log(">>> Cache API example started.");
+
+ // Create cache on server with cacheName.
+ ignite.getOrCreatePromiseCache(cacheName, function(err, promiseCache) {
+ atomicMapOperations(ignite, promiseCache);
+ });
+ }
+
+ /**
+ * Demonstrates cache operations similar to {@link ConcurrentMap} API. Note that
+ * cache API is a lot richer than the JDK {@link ConcurrentMap}.
+ */
+ atomicMapOperations = function(ignite, cache) {
+ console.log(">>> Cache atomic map operation examples.");
+
+ cache.put(1, "1").then(function(res) {
+ return cache.get(1);
+ }).then(function(res) {
+ if (res === "1") {
+ return cache.put(1, "2");
+ }
+ else {
+ return cache.put(1, "3");
+ }
+ }).then(function() {
+ console.log(">>> Replace finished.");
+
+ // Destroying cache.
+ ignite.destroyCache(cacheName, function(err) {
+ console.log(">>> End of Cache API example.");
+ });
+ });
+ }
+}
+
+main();
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/examples/src/main/js/cache-promise-query-example.js
----------------------------------------------------------------------
diff --git a/examples/src/main/js/cache-promise-query-example.js b/examples/src/main/js/cache-promise-query-example.js
new file mode 100644
index 0000000..c31b8fb
--- /dev/null
+++ b/examples/src/main/js/cache-promise-query-example.js
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var apacheIgnite = require("apache-ignite");
+
+var Ignition = apacheIgnite.Ignition;
+var SqlQuery = apacheIgnite.SqlQuery;
+var SqlFieldsQuery = apacheIgnite.SqlFieldsQuery;
+var CacheEntry = apacheIgnite.CacheEntry;
+
+
+/**
+ * Cache queries example. This example demonstrates SQL queries over cache.
+ * <p>
+ * Start Ignite node with {@code examples/config/example-ignite.xml} configuration before running example.
+ * <p>
+ * Alternatively you can run ExampleJsNodeStartup which will
+ * start node with {@code examples/config/example-ignite.xml} configuration.
+ */
+main = function() {
+ /** Cache name. */
+ var cacheName = "CacheQueryExample";
+
+ /** Connect to node that started with {@code examples/config/js/example-js-cache.xml} configuration. */
+ Ignition.start(['127.0.0.1:8000..9000'], null, onConnect);
+
+ function onConnect(err, ignite) {
+ if (err !== null)
+ throw "Start remote node with config examples/config/example-ignite.xml.";
+
+ console.log(">>> Cache query example started.");
+
+ var entries = initializeEntries();
+
+ ignite.getOrCreatePromiseCache(cacheName, function(err, cache) {
+ cacheQuery(ignite, cache, entries);
+ });
+ }
+
+ function cacheQuery(ignite, cache, entries) {
+ cache.putAll(entries, onCachePut);
+
+ function onCachePut(err) {
+ console.log(">>> Create cache for people.")
+
+ //SQL clause which selects salaries based on range.
+ var qry = new SqlQuery("salary > ? and salary <= ?");
+ qry.setReturnType("Object");
+
+ // Set page size for query.
+ qry.setPageSize(2);
+
+ //Set salary range.
+ qry.setArguments([0, 2000]);
+
+ var fullRes = [];
+
+ ignite.cache(cacheName).query(qry).then(onQuery);
+
+ function onQuery(cursor) {
+ fullRes.concat(cursor.page());
+
+ if (cursor.isFinished()) {
+ console.log("Result=" + fullRes);
+
+ return;
+ }
+
+ var nextPromise = cursor.next();
+
+ nextPromise.then(onQuery);
+ }
+ }
+ }
+
+ // Initialize cache for people.
+ function initializeEntries() {
+ var key1 = "1";
+ var value1 = {"firstName" : "John", "lastName" : "Doe", "salary" : 2000};
+ var key2 = "2";
+ var value2 = {"firstName" : "Jane", "lastName" : "Doe", "salary" : 1000};
+ var key3 = "3";
+ var value3 = {"firstName" : "John", "lastName" : "Smith", "salary" : 1000};
+ var key4 = "4";
+ var value4 = {"firstName" : "Jane", "lastName" : "Smith", "salary" : 2000};
+ var key5 = "5";
+ var value5 = {"firstName" : "Ann", "lastName" : "Smith", "salary" : 3000};
+
+ return [new CacheEntry(key1, value1), new CacheEntry(key2, value2),
+ new CacheEntry(key3, value3), new CacheEntry(key4, value4)];
+ }
+}
+
+main();
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/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
new file mode 100644
index 0000000..1c0b489
--- /dev/null
+++ b/modules/nodejs/src/main/js/cache-promise.js
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var Promise = require("bluebird");
+var Server = require("./server").Server;
+var Command = require("./server").Command;
+var SqlFieldsQuery = require("./sql-fields-query").SqlFieldsQuery
+var SqlQuery = require("./sql-query").SqlQuery
+
+/**
+ * Creates an instance of CachePromise
+ *
+ * @constructor
+ * @this {CachePromise}
+ * @param {Server} server Server class
+ * @param {string} cacheName Cache name
+ */
+function CachePromise(server, cacheName) {
+ this._server = server;
+ this._cacheName = 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})));
+};
+
+/**
+ * Put cache value
+ *
+ * @this {Cache}
+ * @param {string} key Key
+ * @param {string} value Value
+ */
+CachePromise.prototype.put = function(key, value) {
+ return this.__createPromise(this._createCommand("put").setPostData(JSON.stringify({"key": key, "val" : value})));
+}
+
+/**
+ * Execute sql query
+ *
+ * @param {SqlQuery|SqlFieldsQuery} qry Query
+ */
+CachePromise.prototype.query = function(qry) {
+ if (qry.returnType() == null) {
+ qry.end("No type for sql query.");
+
+ return;
+ }
+
+ var command = this._createQueryCommand("qryexecute", qry).addParam("type", qry.returnType()).
+ setPostData(JSON.stringify({"arg" : qry.arguments()}));
+
+ function onResult(resolve, reject, err, res) {
+ if(err != null) {
+ reject(err);
+ }
+ else {
+ console.log("res:" + JSON.stringify(res));
+ resolve(new Cursor(qry, res));
+ }
+ }
+
+ function makePromise(server, resolve, reject) {
+ server.runCommand(command, onResult.bind(null, resolve, reject));
+ }
+
+ return new Promise(makePromise.bind(null, this._server));
+}
+
+Cursor = function(qry, res) {
+ this._qry = qry;
+ this._res = res;
+}
+
+Cursor.prototype.next = function() {
+ if (this._res["last"]) {
+ throw "Do not have more pages";
+ }
+
+ var command = this._createCommand("qryfetch");
+
+ command.addParam("qryId", _res.queryId).addParam("psz", _qry.pageSize());
+
+ function onResult(resolve, reject, err, res) {
+ if(err != null) {
+ reject(err);
+ }
+ else {
+ resolve(new Cursor(qry, res));
+ }
+ }
+
+ function makePromise(server, resolve, reject) {
+ server.runCommand(command, onResult.bind(null, resolve, reject));
+ }
+
+ this._server.runCommand(command, onQueryExecute.bind(this, _qry));
+
+ return new Promise(makePromise);
+}
+
+Cursor.prototype.page = function() {
+ return this._res["items"];
+}
+
+Cursor.prototype.isFinished = function() {
+ return this._res["last"];
+}
+
+CachePromise.prototype.__createPromise = function(cmd) {
+ function onResult(resolve, reject, err, res) {
+ if(err != null) {
+ reject(err);
+ }
+ else {
+ resolve(res);
+ }
+ }
+
+ function makePromise(server, resolve, reject) {
+ server.runCommand(cmd, onResult.bind(null, resolve, reject));
+ }
+
+ return new Promise(makePromise.bind(null, this._server));
+}
+
+
+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
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/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 a4a1dd9..edc0301 100644
--- a/modules/nodejs/src/main/js/ignite.js
+++ b/modules/nodejs/src/main/js/ignite.js
@@ -16,6 +16,7 @@
*/
var Cache = require("./cache").Cache;
+var CachePromise = require("./cache-promise").CachePromise;
var Compute = require("./compute").Compute;
var ClusterNode = require("./cluster-node").ClusterNode;
var Server = require("./server").Server;
@@ -52,6 +53,18 @@ Ignite.prototype.cache = function(cacheName) {
}
/**
+ * Get an instance of cache
+ *
+ * @this {Ignite}
+ * @param {string} Cache name
+ * @returns {Cache} Cache
+ */
+Ignite.prototype.cachePromise = function(cacheName) {
+ return new CachePromise(this._server, cacheName);
+}
+
+
+/**
* Get or create an instance of cache
*
* @this {Ignite}
@@ -73,6 +86,29 @@ Ignite.prototype.getOrCreateCache = function(cacheName, callback) {
onCreateCallback.bind(this, callback));
}
+
+/**
+ * Get or create an instance of cache
+ *
+ * @this {Ignite}
+ * @param {string} Cache name
+ * @param callback Callback with cache.
+ */
+Ignite.prototype.getOrCreatePromiseCache = function(cacheName, callback) {
+ var onCreateCallback = function(callback, err, res) {
+ if (err !== null) {
+ callback.call(null, err, null);
+
+ return;
+ }
+
+ callback.call(null, null, new CachePromise(this._server, cacheName))
+ }
+
+ this._server.runCommand(new Command("getorcreatecache").addParam("cacheName", cacheName),
+ onCreateCallback.bind(this, callback));
+}
+
/**
* Stops dynamically started cache
*
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/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 5d7430a..51118e9 100644
--- a/modules/nodejs/src/main/js/server.js
+++ b/modules/nodejs/src/main/js/server.js
@@ -61,6 +61,8 @@ Server.prototype.host = function() {
* @param {onGet} Called on finish
*/
Server.prototype.runCommand = function(cmd, callback) {
+ console.log("cmd=" + cmd._method());
+ console.log("callback=" + callback)
var requestQry = "cmd=" + cmd.name() + cmd.paramsString();
var http = require('http');
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
index 9024b93..e83a32e 100644
--- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
+++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
@@ -70,6 +70,16 @@ public class NodeJsSqlQuerySelfTest extends NodeJsAbstractTest {
runJsScript("testSqlQueryWithParams");
}
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testSqlPromiseQuery() throws Exception {
+ initCache();
+
+ runJsScript("testSqlPromiseQuery");
+ }
+
/**
* Init cache.
*/
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2013a7ad/modules/nodejs/src/test/js/test-query.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/js/test-query.js b/modules/nodejs/src/test/js/test-query.js
index a86c057..fd01945 100644
--- a/modules/nodejs/src/test/js/test-query.js
+++ b/modules/nodejs/src/test/js/test-query.js
@@ -64,6 +64,54 @@ testSqlQuery = function() {
TestUtils.startIgniteNode(put);
}
+testSqlPromiseQuery = function() {
+ function sqlQuery(ignite, error) {
+ assert(error == null, "error on sql query [err=" + error + "]");
+
+ var qry = new SqlQuery("select * from String");
+ qry.setReturnType("String");
+
+ var fullRes = [];
+
+ function onQuery(cursor) {
+ console.log("RESULT PAGE=" + cursor.page())
+ fullRes = fullRes.concat(cursor.page());
+
+ if (cursor.isFinished()) {
+ console.log("Result=" + JSON.stringify(fullRes));
+
+ assert(fullRes.length === 1, "Result length is not correct" +
+ "[expected=1, val = " + fullRes.length + "]");
+
+ assert(fullRes[0]["key"] === "key0", "Result value for key is not correct "+
+ "[expected=key0, real=" + fullRes[0]["key"] + "]");
+
+ assert(fullRes[0]["value"] === "val0", "Result value for key is not correct "+
+ "[expected=val0, real=" + fullRes[0]["value"] + "]");
+
+ TestUtils.testDone();
+
+ return;
+ }
+
+ var nextPromise = cursor.next();
+
+ nextPromise.then(onQuery);
+
+ }
+
+ ignite.cachePromise("mycache").query(qry).then(onQuery);
+ }
+
+ function put(error, ignite) {
+ assert(error == null, "error on put [err=" + error + "]");
+
+ ignite.cache("mycache").put("key0", "val0", sqlQuery.bind(null, ignite))
+ }
+
+ TestUtils.startIgniteNode(put);
+}
+
testSqlFieldsQuery = function() {
function sqlFieldsQuery(error, ignite) {
assert(error == null, "error on sqlfields query [err=" + error + "]");