Skip to content

Commit

Permalink
#IGNITE-106: Change cache Api in examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivasilinets committed Feb 2, 2015
1 parent 88514f7 commit 72b94af
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 94 deletions.
Expand Up @@ -53,13 +53,13 @@ public static void main(String[] args) throws IgniteCheckedException {
System.out.println(); System.out.println();
System.out.println(">>> Cache affinity example started."); System.out.println(">>> Cache affinity example started.");


GridCache<Integer, String> cache = g.cache(CACHE_NAME); IgniteCache<Integer, String> cache = g.jcache(CACHE_NAME);


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
cache.globalClearAll(0); cache.clear();


for (int i = 0; i < KEY_CNT; i++) for (int i = 0; i < KEY_CNT; i++)
cache.putx(i, Integer.toString(i)); cache.put(i, Integer.toString(i));


// Co-locates jobs with data using GridCompute.affinityRun(...) method. // Co-locates jobs with data using GridCompute.affinityRun(...) method.
visitUsingAffinityRun(); visitUsingAffinityRun();
Expand All @@ -78,7 +78,7 @@ public static void main(String[] args) throws IgniteCheckedException {
private static void visitUsingAffinityRun() throws IgniteCheckedException { private static void visitUsingAffinityRun() throws IgniteCheckedException {
Ignite g = Ignition.ignite(); Ignite g = Ignition.ignite();


final GridCache<Integer, String> cache = g.cache(CACHE_NAME); final IgniteCache<Integer, String> cache = g.jcache(CACHE_NAME);


for (int i = 0; i < KEY_CNT; i++) { for (int i = 0; i < KEY_CNT; i++) {
final int key = i; final int key = i;
Expand All @@ -90,7 +90,7 @@ private static void visitUsingAffinityRun() throws IgniteCheckedException {
@Override public void run() { @Override public void run() {
// Peek is a local memory lookup, however, value should never be 'null' // Peek is a local memory lookup, however, value should never be 'null'
// as we are co-located with node that has a given key. // as we are co-located with node that has a given key.
System.out.println("Co-located using affinityRun [key= " + key + ", value=" + cache.peek(key) + ']'); System.out.println("Co-located using affinityRun [key= " + key + ", value=" + cache.localPeek(key) + ']');
} }
}); });
} }
Expand Down Expand Up @@ -123,13 +123,13 @@ private static void visitUsingMapKeysToNodes() throws IgniteCheckedException {
// Bring computations to the nodes where the data resides (i.e. collocation). // Bring computations to the nodes where the data resides (i.e. collocation).
g.compute(g.cluster().forNode(node)).run(new IgniteRunnable() { g.compute(g.cluster().forNode(node)).run(new IgniteRunnable() {
@Override public void run() { @Override public void run() {
GridCache<Integer, String> cache = g.cache(CACHE_NAME); IgniteCache<Integer, String> cache = g.jcache(CACHE_NAME);


// Peek is a local memory lookup, however, value should never be 'null' // Peek is a local memory lookup, however, value should never be 'null'
// as we are co-located with node that has a given key. // as we are co-located with node that has a given key.
for (Integer key : mappedKeys) for (Integer key : mappedKeys)
System.out.println("Co-located using mapKeysToNodes [key= " + key + System.out.println("Co-located using mapKeysToNodes [key= " + key +
", value=" + cache.peek(key) + ']'); ", value=" + cache.localPeek(key) + ']');
} }
}); });
} }
Expand Down
Expand Up @@ -49,13 +49,10 @@ public static void main(String[] args) throws IgniteCheckedException {
System.out.println(">>> Cache API example started."); System.out.println(">>> Cache API example started.");


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
g.cache(CACHE_NAME).globalClearAll(0); g.jcache(CACHE_NAME).clear();


// Demonstrate atomic map operations. // Demonstrate atomic map operations.
atomicMapOperations(); atomicMapOperations();

// Demonstrate various ways to iterate over locally cached values.
localIterators();
} }
} }


Expand Down Expand Up @@ -119,52 +116,4 @@ private static void atomicMapOperations() throws IgniteCheckedException {
b2 = cache.replace(7, "7", "777"); b2 = cache.replace(7, "7", "777");
assert b1 & !b2; assert b1 & !b2;
} }

/**
* Demonstrates various iteration methods over locally cached values.
*/
private static void localIterators() {
System.out.println();
System.out.println(">>> Local iterator examples.");

GridCache<Integer, String> cache = Ignition.ignite().cache(CACHE_NAME);

// Iterate over whole cache.
for (CacheEntry<Integer, String> e : cache)
System.out.println("Basic cache iteration [key=" + e.getKey() + ", val=" + e.getValue() + ']');

// Iterate over cache projection for all keys below 5.
CacheProjection<Integer, String> keysBelow5 = cache.projection(
new IgnitePredicate<CacheEntry<Integer, String>>() {
@Override public boolean apply(CacheEntry<Integer, String> e) {
return e.getKey() < 5;
}
}
);

for (CacheEntry<Integer, String> e : keysBelow5)
System.out.println("Cache projection iteration [key=" + e.getKey() + ", val=" + e.getValue() + ']');

// Iterate over each element using 'forEach' construct.
cache.forEach(new IgniteInClosure<CacheEntry<Integer, String>>() {
@Override public void apply(CacheEntry<Integer, String> e) {
System.out.println("forEach iteration [key=" + e.getKey() + ", val=" + e.getValue() + ']');
}
});

// Search cache for element with value "1" using 'forAll' construct.
cache.forAll(new IgnitePredicate<CacheEntry<Integer, String>>() {
@Override public boolean apply(CacheEntry<Integer, String> e) {
String v = e.peek();

if ("1".equals(v)) {
System.out.println("Found cache value '1' using forEach iteration.");

return false; // Stop iteration.
}

return true; // Continue iteration.
}
});
}
} }
Expand Up @@ -56,7 +56,7 @@ public static void main(String[] args) throws Exception {
System.out.println(">>> Cache data loader example started."); System.out.println(">>> Cache data loader example started.");


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
g.cache(CACHE_NAME).globalClearAll(0); g.jcache(CACHE_NAME).clear();


System.out.println(); System.out.println();
System.out.println(">>> Cache clear finished."); System.out.println(">>> Cache clear finished.");
Expand Down
Expand Up @@ -51,10 +51,10 @@ public static void main(String[] args) throws IgniteCheckedException, Interrupte
System.out.println(); System.out.println();
System.out.println(">>> Cache events example started."); System.out.println(">>> Cache events example started.");


final GridCache<Integer, String> cache = g.cache(CACHE_NAME); final IgniteCache<Integer, String> cache = g.jcache(CACHE_NAME);


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
cache.globalClearAll(0); cache.clear();


// This optional local callback is called for each event notification // This optional local callback is called for each event notification
// that passed remote predicate listener. // that passed remote predicate listener.
Expand All @@ -75,7 +75,7 @@ public static void main(String[] args) throws IgniteCheckedException, Interrupte


int key = evt.key(); int key = evt.key();


return key >= 10 && cache.affinity().isPrimary(g.cluster().localNode(), key); return key >= 10 && g.affinity(CACHE_NAME).isPrimary(g.cluster().localNode(), key);
} }
}; };


Expand All @@ -86,7 +86,7 @@ public static void main(String[] args) throws IgniteCheckedException, Interrupte


// Generate cache events. // Generate cache events.
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
cache.putx(i, Integer.toString(i)); cache.put(i, Integer.toString(i));


// Wait for a while while callback is notified about remaining puts. // Wait for a while while callback is notified about remaining puts.
Thread.sleep(2000); Thread.sleep(2000);
Expand Down
Expand Up @@ -65,7 +65,7 @@ public static void main(String[] args) throws Exception {
System.out.println(">>> Cache popular numbers example started."); System.out.println(">>> Cache popular numbers example started.");


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
g.cache(CACHE_NAME).globalClearAll(0); g.jcache(CACHE_NAME).clear();


ClusterGroup prj = g.cluster().forCache(CACHE_NAME); ClusterGroup prj = g.cluster().forCache(CACHE_NAME);


Expand Down
Expand Up @@ -44,7 +44,7 @@ public class CachePutGetExample {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
try (Ignite g = Ignition.start("examples/config/example-cache.xml")) { try (Ignite g = Ignition.start("examples/config/example-cache.xml")) {
// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
g.cache(CACHE_NAME).globalClearAll(0); g.jcache(CACHE_NAME).clear();


// Individual puts and gets. // Individual puts and gets.
putGet(); putGet();
Expand All @@ -65,13 +65,13 @@ private static void putGet() throws IgniteCheckedException {


Ignite g = Ignition.ignite(); Ignite g = Ignition.ignite();


final GridCache<Integer, String> cache = g.cache(CACHE_NAME); final IgniteCache<Integer, String> cache = g.jcache(CACHE_NAME);


final int keyCnt = 20; final int keyCnt = 20;


// Store keys in cache. // Store keys in cache.
for (int i = 0; i < keyCnt; i++) for (int i = 0; i < keyCnt; i++)
cache.putx(i, Integer.toString(i)); cache.put(i, Integer.toString(i));


System.out.println(">>> Stored values in cache."); System.out.println(">>> Stored values in cache.");


Expand All @@ -90,7 +90,7 @@ private static void putAllGetAll() throws IgniteCheckedException {


Ignite g = Ignition.ignite(); Ignite g = Ignition.ignite();


final GridCache<Integer, String> cache = g.cache(CACHE_NAME); final IgniteCache<Integer, String> cache = g.jcache(CACHE_NAME);


final int keyCnt = 20; final int keyCnt = 20;


Expand Down
Expand Up @@ -80,7 +80,7 @@ public static void main(String[] args) throws Exception {
System.out.println(">>> Cache query example started."); System.out.println(">>> Cache query example started.");


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
g.cache(CACHE_NAME).globalClearAll(0); g.jcache(CACHE_NAME).clear();


// Populate cache. // Populate cache.
initialize(); initialize();
Expand Down
Expand Up @@ -52,13 +52,13 @@ public static void main(String[] args) throws IgniteCheckedException {
System.out.println(">>> Cache transaction example started."); System.out.println(">>> Cache transaction example started.");


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
g.cache(CACHE_NAME).globalClearAll(0); g.jcache(CACHE_NAME).clear();


GridCache<Integer, Account> cache = g.cache(CACHE_NAME); IgniteCache<Integer, Account> cache = g.jcache(CACHE_NAME);


// Initialize. // Initialize.
cache.putx(1, new Account(1, 100)); cache.put(1, new Account(1, 100));
cache.putx(2, new Account(1, 200)); cache.put(2, new Account(1, 200));


System.out.println(); System.out.println();
System.out.println(">>> Accounts before deposit: "); System.out.println(">>> Accounts before deposit: ");
Expand Down Expand Up @@ -87,18 +87,18 @@ public static void main(String[] args) throws IgniteCheckedException {
*/ */
private static void deposit(int acctId, double amount) throws IgniteCheckedException { private static void deposit(int acctId, double amount) throws IgniteCheckedException {
// Clone every object we get from cache, so we can freely update it. // Clone every object we get from cache, so we can freely update it.
CacheProjection<Integer, Account> cache = Ignition.ignite().<Integer, Account>cache(CACHE_NAME).flagsOn(CLONE); IgniteCache<Integer, Account> cache = Ignition.ignite().jcache(CACHE_NAME);


try (IgniteTx tx = cache.txStart(PESSIMISTIC, REPEATABLE_READ)) { try (IgniteTx tx = Ignition.ignite().transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
Account acct = cache.get(acctId); assert cache.get(acctId) != null;


assert acct != null; Account acct = new Account(cache.get(acctId).id, cache.get(acctId).balance);


// Deposit into account. // Deposit into account.
acct.update(amount); acct.update(amount);


// Store updated account in cache. // Store updated account in cache.
cache.putx(acctId, acct); cache.put(acctId, acct);


tx.commit(); tx.commit();
} }
Expand Down
Expand Up @@ -70,8 +70,8 @@ public static void main(String[] args) throws Exception {
System.out.println(">>> Cache star schema example started."); System.out.println(">>> Cache star schema example started.");


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
g.cache(PARTITIONED_CACHE_NAME).globalClearAll(0); g.jcache(PARTITIONED_CACHE_NAME).clear();
g.cache(REPLICATED_CACHE_NAME).globalClearAll(0); g.jcache(REPLICATED_CACHE_NAME).clear();


try { try {
populateDimensions(); populateDimensions();
Expand All @@ -92,7 +92,7 @@ public static void main(String[] args) throws Exception {
* @throws IgniteCheckedException If failed. * @throws IgniteCheckedException If failed.
*/ */
private static void populateDimensions() throws IgniteCheckedException { private static void populateDimensions() throws IgniteCheckedException {
GridCache<Integer, Object> cache = Ignition.ignite().cache(REPLICATED_CACHE_NAME); IgniteCache<Integer, Object> cache = Ignition.ignite().jcache(REPLICATED_CACHE_NAME);


DimStore store1 = new DimStore(idGen++, "Store1", "12345", "321 Chilly Dr, NY"); DimStore store1 = new DimStore(idGen++, "Store1", "12345", "321 Chilly Dr, NY");
DimStore store2 = new DimStore(idGen++, "Store2", "54321", "123 Windy Dr, San Francisco"); DimStore store2 = new DimStore(idGen++, "Store2", "54321", "123 Windy Dr, San Francisco");
Expand Down
Expand Up @@ -49,19 +49,17 @@ public static void main(String[] args) throws Exception {
System.out.println(); System.out.println();
System.out.println(">>> Cache store example started."); System.out.println(">>> Cache store example started.");


GridCache<Long, Person> cache = g.cache(null); IgniteCache<Long, Person> cache = g.jcache(null);


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
cache.globalClearAll(0); cache.clear();


try (IgniteTx tx = cache.txStart()) { try (IgniteTx tx = g.transactions().txStart()) {
Person val = cache.get(id); Person val = cache.get(id);


System.out.println("Read value: " + val); System.out.println("Read value: " + val);


val = cache.put(id, person(id, "Isaac", "Newton")); cache.put(id, person(id, "Isaac", "Newton"));

System.out.println("Overwrote old value: " + val);


val = cache.get(id); val = cache.get(id);


Expand Down
Expand Up @@ -51,10 +51,10 @@ public static void main(String[] args) throws Exception {
System.out.println(); System.out.println();
System.out.println(">>> Cache store load data example started."); System.out.println(">>> Cache store load data example started.");


final GridCache<String, Integer> cache = g.cache(null); final IgniteCache<String, Integer> cache = g.jcache(null);


// Clean up caches on all nodes before run. // Clean up caches on all nodes before run.
cache.globalClearAll(0); cache.clear();


long start = System.currentTimeMillis(); long start = System.currentTimeMillis();


Expand Down
Expand Up @@ -18,7 +18,6 @@
package org.apache.ignite.examples.datagrid.store.dummy; package org.apache.ignite.examples.datagrid.store.dummy;


import org.apache.ignite.*; import org.apache.ignite.*;
import org.apache.ignite.cache.GridCache;
import org.apache.ignite.cache.store.*; import org.apache.ignite.cache.store.*;
import org.apache.ignite.examples.datagrid.store.*; import org.apache.ignite.examples.datagrid.store.*;
import org.apache.ignite.lang.*; import org.apache.ignite.lang.*;
Expand Down Expand Up @@ -80,8 +79,6 @@ public class CacheDummyPersonStore extends CacheStoreAdapter<Long, Person> {


System.out.println(">>> Store loadCache for entry count: " + cnt); System.out.println(">>> Store loadCache for entry count: " + cnt);


GridCache<Long, Person> cache = ignite.cache(cacheName);

for (int i = 0; i < cnt; i++) { for (int i = 0; i < cnt; i++) {
// Generate dummy person on the fly. // Generate dummy person on the fly.
Person p = new Person(i, "first-" + i, "last-" + 1); Person p = new Person(i, "first-" + i, "last-" + 1);
Expand All @@ -90,7 +87,7 @@ public class CacheDummyPersonStore extends CacheStoreAdapter<Long, Person> {
// but we check if local node is primary or backup anyway just to demonstrate that we can. // but we check if local node is primary or backup anyway just to demonstrate that we can.
// Ideally, partition ID of a key would be stored in the database and only keys // Ideally, partition ID of a key would be stored in the database and only keys
// for partitions that belong on this node would be loaded from database. // for partitions that belong on this node would be loaded from database.
if (cache.affinity().isPrimaryOrBackup(ignite.cluster().localNode(), p.getId())) { if (ignite.affinity(cacheName).isPrimaryOrBackup(ignite.cluster().localNode(), p.getId())) {
// Update dummy database. // Update dummy database.
// In real life data would be loaded from database. // In real life data would be loaded from database.
dummyDB.put(p.getId(), p); dummyDB.put(p.getId(), p);
Expand Down

0 comments on commit 72b94af

Please sign in to comment.