#ignite-964: change cache-put-get-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/bfc899e4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/bfc899e4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/bfc899e4 Branch: refs/heads/ignite-964 Commit: bfc899e42dcb652412e9a203dd46295ff6f5affa Parents: 242c21b Author: ivasilinets <ivasilin...@gridgain.com> Authored: Thu Jul 9 17:40:03 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Thu Jul 9 17:40:03 2015 +0300 ---------------------------------------------------------------------- examples/src/main/js/cache-api-example.js | 2 +- examples/src/main/js/cache-put-get-example.js | 132 +++++++++------------ 2 files changed, 59 insertions(+), 75 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bfc899e4/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 d17276a..1514941 100644 --- a/examples/src/main/js/cache-api-example.js +++ b/examples/src/main/js/cache-api-example.js @@ -55,7 +55,7 @@ function main() { cache.getAndPut(1, "1", onGetAndPut) }); - onGetAndPut = function(err, entry) { + function onGetAndPut(err, entry) { console.log(">>> Get and put finished [result=" + entry + "]"); // Put and do not return previous value. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bfc899e4/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 7a9b035..75da096 100644 --- a/examples/src/main/js/cache-put-get-example.js +++ b/examples/src/main/js/cache-put-get-example.js @@ -16,104 +16,88 @@ */ var apacheIgnite = require("apache-ignite"); - var Ignition = apacheIgnite.Ignition; var CacheEntry = apacheIgnite.CacheEntry; -Ignition.start(['127.0.0.1:9095'], null, onConnect); - -function onConnect(err, ignite) { - if (err) - throw err; - - ignite.getOrCreateCache("PutGetExampleCache", function(err, cache) { - putGet(cache); - - putAllGetAll(cache); - }); -} - -putGet = function(cache) { - console.log(">>> Cache put-get example started."); - - var keyCnt = 20; - - var putCnt = 0; - - var onGet = function(err, res) { - if (err) { - console.log("Error: " + err); - - throw new Error(err); - } - - console.log("Get val=" + res); +/** + * This example demonstrates very basic operations on cache, such as 'put' and 'get'. + * <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 = "PutGetExampleCache"; + + /** 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) { + ignite.getOrCreateCache(cacheName, function(err, cache) { putGetExample(ignite, cache); }); } - var onPut = function(err) { - if (err) { - console.log("Error: " + err); + /** Execute individual puts and gets. */ + putGetExample = function(ignite, cache) { + console.log(">>> Cache put-get example started."); - throw new Error(err); - } + var key = 1; + + // Store key in cache. + cache.put(key, "1", onPut); - if (putCnt < keyCnt - 1) { - putCnt++; + function onPut(err) { + console.log(">>> Stored values in cache."); - return; + cache.get(key, onGet); } - console.log(">>> Stored values in cache."); + function onGet(err, res) { + console.log("Get val=" + res); - for (var i = 0; i < keyCnt; i++) { - cache.get(i, onGet); + putAllGetAll(ignite, cache); } } - // Store keys in cache. - for (var i = 0; i < keyCnt; i++) { - cache.put(i, i.toString(), onPut); - } -} - -putAllGetAll = function(cache) { - console.log(">>> Starting putAll-getAll example."); - - var keyCnt = 20; + /** Execute bulk {@code putAll(...)} and {@code getAll(...)} operations. */ + function putAllGetAll(ignite, cache) { + console.log(">>> Starting putAll-getAll example."); - var batch = []; - var keys = []; + var keyCnt = 20; - for (var i = keyCnt; i < keyCnt + keyCnt; ++i) { - var key = i; + // Create batch. + var batch = []; + var keys = []; - var val = "bulk-" + i; + for (var i = keyCnt; i < keyCnt + keyCnt; ++i) { + var key = i; + var val = "bulk-" + i; - keys.push(key); - batch.push(new CacheEntry(key, val)); - } + keys.push(key); + batch.push(new CacheEntry(key, val)); + } - var onGetAll = function(err, entries) { - if (err) { - console.log("Error: " + err); + cache.putAll(batch, onPutAll); - throw new Error(err); - } + function onPutAll(err) { + console.log(">>> Stored values in cache."); - for (var e of entries) { - console.log("Got entry [key=" + e.key + ", val=" + e.value + ']'); + cache.getAll(keys, onGetAll); } - } - var onPutAll= function(err) { - if (err) { - console.log("Error: " + err); + function onGetAll(err, entries) { + for (var e of entries) { + console.log("Got entry [key=" + e.key + ", val=" + e.value + ']'); + } - throw new Error(err); + // Destroying cache. + ignite.destroyCache(cacheName, function(err) { + console.log(">>> End of cache put-get example."); + }); } - - cache.getAll(keys, onGetAll); } +} - cache.putAll(batch, onPutAll); -} \ No newline at end of file +main(); \ No newline at end of file