#ignite-964: change cache-api-example.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/242c21bb Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/242c21bb Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/242c21bb Branch: refs/heads/ignite-964 Commit: 242c21bb10ee0578055adeda1014a536e73ef88f Parents: 7763a37 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Thu Jul 9 17:08:06 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Thu Jul 9 17:08:06 2015 +0300 ---------------------------------------------------------------------- examples/config/js/example-js-cache.xml | 3 + .../examples/js/ExampleJsNodeStartup.java | 4 +- examples/src/main/js/cache-api-example.js | 103 ++++++++++++------- modules/nodejs/src/main/js/ignite.js | 4 +- 4 files changed, 74 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/242c21bb/examples/config/js/example-js-cache.xml ---------------------------------------------------------------------- diff --git a/examples/config/js/example-js-cache.xml b/examples/config/js/example-js-cache.xml index 2599e38..e8ffc8a 100644 --- a/examples/config/js/example-js-cache.xml +++ b/examples/config/js/example-js-cache.xml @@ -31,6 +31,9 @@ <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> <property name="gridName" value="ServerNode" /> + <!-- Set to true to enable distributed class loading for examples, default is false. --> + <property name="peerClassLoadingEnabled" value="true"/> + <property name="connectorConfiguration"> <bean class="org.apache.ignite.configuration.ConnectorConfiguration"> <property name="jettyPath" value="examples/config/js/rest-jetty.xml"/> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/242c21bb/examples/src/main/java/org/apache/ignite/examples/js/ExampleJsNodeStartup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/js/ExampleJsNodeStartup.java b/examples/src/main/java/org/apache/ignite/examples/js/ExampleJsNodeStartup.java index 6fa2e6c..0de6047 100644 --- a/examples/src/main/java/org/apache/ignite/examples/js/ExampleJsNodeStartup.java +++ b/examples/src/main/java/org/apache/ignite/examples/js/ExampleJsNodeStartup.java @@ -21,11 +21,11 @@ import org.apache.ignite.*; import org.apache.ignite.spi.discovery.tcp.internal.*; /** - * Starts up an empty node with example compute configuration. + * Starts up an empty node with example node js configuration. */ public class ExampleJsNodeStartup { /** - * Start up an empty node with example compute configuration. + * Start up an empty node with example node js configuration. * * @param args Command line arguments, none required. * @throws IgniteException If failed. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/242c21bb/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 13368d5..d17276a 100644 --- a/examples/src/main/js/cache-api-example.js +++ b/examples/src/main/js/cache-api-example.js @@ -18,50 +18,81 @@ var apacheIgnite = require("apache-ignite"); var Ignition = apacheIgnite.Ignition; -Ignition.start(['127.0.0.1:9095'], null, onConnect); +/** + * This example demonstrates some of the cache rich API capabilities. + * <p> + * Remote nodes should always be started with special configuration file which + * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/js/example-js-cache.xml'}. + * <p> + * Alternatively you can run {@link ExampleJsNodeStartup} in another JVM which will + * start node with {@code examples/config/js/example-js-cache.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:9095'], null, onConnect); + + function onConnect(err, ignite) { + console.log(">>> Cache API example started."); + + // Create cache on server with cacheName. + ignite.getOrCreateCache(cacheName, function(err, cache) { + atomicMapOperations(ignite, cache); + }); + } -function onConnect(err, ignite) { - console.log(">>> Cache API example started."); + /** + * 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."); - ignite.getOrCreateCache("ApiExampleCache", function(err, cache) { - atomicMapOperations(cache); + cache.removeAllFromCache(function(err) { + // Put and return previous value. + cache.getAndPut(1, "1", onGetAndPut) }); -} -/** - * Demonstrates cache operations similar to {@link ConcurrentMap} API. Note that - * cache API is a lot richer than the JDK {@link ConcurrentMap}. - */ -atomicMapOperations = function(cache) { - console.log(">>> Cache atomic map operation examples."); + onGetAndPut = function(err, entry) { + console.log(">>> Get and put finished [result=" + entry + "]"); - cache.removeAllFromCache(function(err) { - cache.getAndPut(1, "1", onGetAndPut.bind(null, cache)) - }); -} + // Put and do not return previous value. + // Performs better when previous value is not needed. + cache.put(2, "2", onPut); + } -function onGetAndPut(cache, err, entry) { - cache.put(2, "2", onPut.bind(null, cache)); -} + onPut = function(err) { + console.log(">>> Put finished."); -function onPut(cache, err) { - cache.putIfAbsent(4, "44", onPutIfAbsent.bind(null, cache, true)); -} + // Put-if-absent. + cache.putIfAbsent(4, "44", onPutIfAbsent); + } -function onPutIfAbsent(cache, expRes, err, res) { - if (expRes) { - cache.putIfAbsent(4, "44", onPutIfAbsent.bind(null, cache, false)); - } - else { - cache.replaceValue(4, "55", "44", onReplaceValue.bind(null, cache, true)); + onPutIfAbsent = function(err, res) { + console.log(">>> Put if absent finished [result=" + res + "]"); + + // Replace. + cache.replaceValue(4, "55", "44", onReplaceValue); + } + + onReplaceValue = function(err, res) { + console.log(">>> Replace value finished [result=" + res + "]"); + + // Replace not correct value. + cache.replaceValue(4, "555", "44", onEnd); + } + + onEnd = function(err) { + console.log(">>> Replace finished."); + + // Destroying cache. + ignite.destroyCache(cacheName, function(err) { + console.log(">>> End of Cache API example."); + }); + } } } -function onReplaceValue(cache, expRes, err, res) { - if (expRes) { - cache.replaceValue(4, "555", "44", onReplaceValue.bind(null, cache, false)); - } - else { - console.log("End of the example.") - } -} \ No newline at end of file +main(); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/242c21bb/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 5dfb15b..a4a1dd9 100644 --- a/modules/nodejs/src/main/js/ignite.js +++ b/modules/nodejs/src/main/js/ignite.js @@ -59,7 +59,7 @@ Ignite.prototype.cache = function(cacheName) { * @param callback Callback with cache. */ Ignite.prototype.getOrCreateCache = function(cacheName, callback) { - var onCreateCallback = function(err) { + var onCreateCallback = function(callback, err, res) { if (err !== null) { callback.call(null, err, null); @@ -70,7 +70,7 @@ Ignite.prototype.getOrCreateCache = function(cacheName, callback) { } this._server.runCommand(new Command("getorcreatecache").addParam("cacheName", cacheName), - onCreateCallback.bind(this)); + onCreateCallback.bind(this, callback)); } /**