Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sboikov committed Jan 28, 2015
1 parent 67a50f9 commit 7e3c81b
Show file tree
Hide file tree
Showing 53 changed files with 1,156 additions and 1,154 deletions.
Expand Up @@ -33,9 +33,6 @@
* start GridGain node with {@code examples/config/example-cache.xml} configuration.
*/
public final class CacheAtomicLongExample {
/** Cache name. */
private static final String CACHE_NAME = "partitioned_tx";

/** Number of retries */
private static final int RETRIES = 20;

Expand All @@ -48,20 +45,20 @@ public final class CacheAtomicLongExample {
public static void main(String[] args) throws IgniteCheckedException {
try (Ignite g = Ignition.start("examples/config/example-cache.xml")) {
System.out.println();
System.out.println(">>> Cache atomic long example started.");
System.out.println(">>> Atomic long example started.");

// Make name for atomic long (by which it will be known in the grid).
String atomicName = UUID.randomUUID().toString();

// Initialize atomic long in grid.
final IgniteAtomicLong atomicLong = g.cache(CACHE_NAME).dataStructures().atomicLong(atomicName, 0, true);
final IgniteAtomicLong atomicLong = g.atomicLong(atomicName, 0, true);

System.out.println();
System.out.println("Atomic long initial value : " + atomicLong.get() + '.');

// Try increment atomic long from all grid nodes.
// Note that this node is also part of the grid.
g.compute(g.cluster().forCache(CACHE_NAME)).call(new IgniteCallable<Object>() {
g.compute().broadcast(new IgniteCallable<Object>() {
@Override public Object call() throws Exception {
for (int i = 0; i < RETRIES; i++)
System.out.println("AtomicLong value has been incremented: " + atomicLong.incrementAndGet());
Expand Down
Expand Up @@ -33,9 +33,6 @@
* start GridGain node with {@code examples/config/example-cache.xml} configuration.
*/
public final class CacheAtomicReferenceExample {
/** Cache name. */
private static final String CACHE_NAME = "partitioned_tx";

/**
* Executes example.
*
Expand All @@ -45,7 +42,7 @@ public final class CacheAtomicReferenceExample {
public static void main(String[] args) throws IgniteCheckedException {
try (Ignite g = Ignition.start("examples/config/example-cache.xml")) {
System.out.println();
System.out.println(">>> Cache atomic reference example started.");
System.out.println(">>> Atomic reference example started.");

// Make name of atomic reference.
final String refName = UUID.randomUUID().toString();
Expand All @@ -54,13 +51,12 @@ public static void main(String[] args) throws IgniteCheckedException {
String val = UUID.randomUUID().toString();

// Initialize atomic reference in grid.
IgniteAtomicReference<String> ref = g.cache(CACHE_NAME).dataStructures().
atomicReference(refName, val, true);
IgniteAtomicReference<String> ref = g.atomicReference(refName, val, true);

System.out.println("Atomic reference initial value : " + ref.get() + '.');

// Make closure for checking atomic reference value on grid.
Runnable c = new ReferenceClosure(CACHE_NAME, refName);
Runnable c = new ReferenceClosure(refName);

// Check atomic reference on all grid nodes.
g.compute().run(c);
Expand Down Expand Up @@ -94,26 +90,20 @@ public static void main(String[] args) throws IgniteCheckedException {
* Obtains atomic reference.
*/
private static class ReferenceClosure implements IgniteRunnable {
/** Cache name. */
private final String cacheName;

/** Reference name. */
private final String refName;

/**
* @param cacheName Cache name.
* @param refName Reference name.
*/
ReferenceClosure(String cacheName, String refName) {
this.cacheName = cacheName;
ReferenceClosure(String refName) {
this.refName = refName;
}

/** {@inheritDoc} */
@Override public void run() {
try {
IgniteAtomicReference<String> ref = Ignition.ignite().cache(cacheName).dataStructures().
atomicReference(refName, null, true);
IgniteAtomicReference<String> ref = Ignition.ignite().atomicReference(refName, null, true);

System.out.println("Atomic reference value is " + ref.get() + '.');
}
Expand Down
Expand Up @@ -33,9 +33,6 @@
* start GridGain node with {@code examples/config/example-cache.xml} configuration.
*/
public final class CacheAtomicStampedExample {
/** Cache name. */
private static final String CACHE_NAME = "partitioned_tx";

/**
* Executes example.
*
Expand All @@ -45,7 +42,7 @@ public final class CacheAtomicStampedExample {
public static void main(String[] args) throws IgniteCheckedException {
try (Ignite g = Ignition.start("examples/config/example-cache.xml")) {
System.out.println();
System.out.println(">>> Cache atomic stamped example started.");
System.out.println(">>> Atomic stamped example started.");

// Make name of atomic stamped.
String stampedName = UUID.randomUUID().toString();
Expand All @@ -57,13 +54,12 @@ public static void main(String[] args) throws IgniteCheckedException {
String stamp = UUID.randomUUID().toString();

// Initialize atomic stamped in cache.
IgniteAtomicStamped<String, String> stamped = g.cache(CACHE_NAME).dataStructures().
atomicStamped(stampedName, val, stamp, true);
IgniteAtomicStamped<String, String> stamped = g.atomicStamped(stampedName, val, stamp, true);

System.out.println("Atomic stamped initial [value=" + stamped.value() + ", stamp=" + stamped.stamp() + ']');

// Make closure for checking atomic stamped on grid.
Runnable c = new StampedUpdateClosure(CACHE_NAME, stampedName);
Runnable c = new StampedUpdateClosure(stampedName);

// Check atomic stamped on all grid nodes.
g.compute().run(c);
Expand Down Expand Up @@ -100,25 +96,20 @@ public static void main(String[] args) throws IgniteCheckedException {
* Performs update of on an atomic stamped variable in cache.
*/
private static class StampedUpdateClosure implements IgniteRunnable {
/** Cache name. */
private final String cacheName;

/** Atomic stamped variable name. */
private final String stampedName;

/**
* @param cacheName Cache name.
* @param stampedName Atomic stamped variable name.
*/
StampedUpdateClosure(String cacheName, String stampedName) {
this.cacheName = cacheName;
StampedUpdateClosure(String stampedName) {
this.stampedName = stampedName;
}

/** {@inheritDoc} */
@Override public void run() {
try {
IgniteAtomicStamped<String, String> stamped = Ignition.ignite().cache(cacheName).dataStructures().
IgniteAtomicStamped<String, String> stamped = Ignition.ignite().
atomicStamped(stampedName, null, null, true);

System.out.println("Atomic stamped [value=" + stamped.value() + ", stamp=" + stamped.stamp() + ']');
Expand Down
Expand Up @@ -33,9 +33,6 @@
* start GridGain node with {@code examples/config/example-cache.xml} configuration.
*/
public class CacheCountDownLatchExample {
/** Cache name. */
private static final String CACHE_NAME = "partitioned_tx";

/** Number of latch initial count */
private static final int INITIAL_COUNT = 10;

Expand All @@ -54,14 +51,13 @@ public static void main(String[] args) throws Exception {
final String latchName = UUID.randomUUID().toString();

// Initialize count down latch in grid.
IgniteCountDownLatch latch = g.cache(CACHE_NAME).dataStructures().
countDownLatch(latchName, INITIAL_COUNT, false, true);
IgniteCountDownLatch latch = g.countDownLatch(latchName, INITIAL_COUNT, false, true);

System.out.println("Latch initial value: " + latch.count());

// Start waiting on the latch on all grid nodes.
for (int i = 0; i < INITIAL_COUNT; i++)
g.compute().run(new LatchClosure(CACHE_NAME, latchName));
g.compute().run(new LatchClosure(latchName));

// Wait for latch to go down which essentially means that all remote closures completed.
latch.await();
Expand All @@ -78,26 +74,20 @@ public static void main(String[] args) throws Exception {
* Closure which simply waits on the latch on all nodes.
*/
private static class LatchClosure implements IgniteRunnable {
/** Cache name. */
private final String cacheName;

/** Latch name. */
private final String latchName;

/**
* @param cacheName Cache name.
* @param latchName Latch name.
*/
LatchClosure(String cacheName, String latchName) {
this.cacheName = cacheName;
LatchClosure(String latchName) {
this.latchName = latchName;
}

/** {@inheritDoc} */
@Override public void run() {
try {
IgniteCountDownLatch latch = Ignition.ignite().cache(cacheName).dataStructures().
countDownLatch(latchName, 1, false, true);
IgniteCountDownLatch latch = Ignition.ignite().countDownLatch(latchName, 1, false, true);

int newCnt = latch.countDown();

Expand Down
Expand Up @@ -142,8 +142,8 @@ private static void clearAndRemoveQueue(Ignite g) throws IgniteCheckedException

System.out.println("Queue size after clearing: " + queue.size());

// Remove queue from cache.
g.cache(CACHE_NAME).dataStructures().removeQueue(queue.name());
// Remove queue.
queue.close();

// Try to work with removed queue.
try {
Expand Down
Expand Up @@ -87,7 +87,7 @@ public static void main(String[] args) throws Exception {
System.out.println(">>> Current cache size: " + cache.size() + " (expected: 0).");

// Create atomic long.
IgniteAtomicLong l = cache.dataStructures().atomicLong("atomicLong", 10, true);
IgniteAtomicLong l = g.atomicLong("atomicLong", 10, true);

// Increment atomic long by 5 using Memcache client.
if (client.incr("atomicLong", 5, 0) == 15)
Expand Down
Expand Up @@ -210,11 +210,11 @@ public void testDelete() throws Exception {
public void testIncrement() throws Exception {
Assert.assertEquals(5, client.incr("incrKey", 3, 2));

assertEquals(5, cache().dataStructures().atomicLong("incrKey", 0, true).get());
assertEquals(5, grid(0).atomicLong("incrKey", 0, true).get());

Assert.assertEquals(15, client.incr("incrKey", 10, 0));

assertEquals(15, cache().dataStructures().atomicLong("incrKey", 0, true).get());
assertEquals(15, grid(0).atomicLong("incrKey", 0, true).get());
}

/**
Expand All @@ -223,11 +223,11 @@ public void testIncrement() throws Exception {
public void testDecrement() throws Exception {
Assert.assertEquals(5, client.decr("decrKey", 10, 15));

assertEquals(5, cache().dataStructures().atomicLong("decrKey", 0, true).get());
assertEquals(5, grid(0).atomicLong("decrKey", 0, true).get());

Assert.assertEquals(2, client.decr("decrKey", 3, 0));

assertEquals(2, cache().dataStructures().atomicLong("decrKey", 0, true).get());
assertEquals(2, grid(0).atomicLong("decrKey", 0, true).get());
}

/**
Expand Down
Expand Up @@ -118,9 +118,8 @@ private String cachePattern(String res, boolean success) {
* @param success Success flag.
* @return Regex pattern for JSON.
*/
private String cacheIntegerPattern(int res, boolean success) {
return "\\{\\\"affinityNodeId\\\":\\\"\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}\\\"\\," +
"\\\"error\\\":\\\"\\\"\\," +
private String integerPattern(int res, boolean success) {
return "\\{\\\"error\\\":\\\"\\\"\\," +
"\\\"response\\\":" + res + "\\," +
"\\\"sessionToken\\\":\\\"\\\"," +
"\\\"successStatus\\\":" + (success ? 0 : 1) + "\\}";
Expand Down Expand Up @@ -493,18 +492,18 @@ public void testIncrement() throws Exception {
assertNotNull(ret);
assertTrue(!ret.isEmpty());

jsonEquals(ret, cacheIntegerPattern(5, true));
jsonEquals(ret, integerPattern(5, true));

assertEquals(5, cache().dataStructures().atomicLong("incrKey", 0, true).get());
assertEquals(5, grid(0).atomicLong("incrKey", 0, true).get());

ret = content(F.asMap("cmd", "incr", "key", "incrKey", "delta", "10"));

assertNotNull(ret);
assertTrue(!ret.isEmpty());

jsonEquals(ret, cacheIntegerPattern(15, true));
jsonEquals(ret, integerPattern(15, true));

assertEquals(15, cache().dataStructures().atomicLong("incrKey", 0, true).get());
assertEquals(15, grid(0).atomicLong("incrKey", 0, true).get());
}

/**
Expand All @@ -516,18 +515,18 @@ public void testDecrement() throws Exception {
assertNotNull(ret);
assertTrue(!ret.isEmpty());

jsonEquals(ret, cacheIntegerPattern(5, true));
jsonEquals(ret, integerPattern(5, true));

assertEquals(5, cache().dataStructures().atomicLong("decrKey", 0, true).get());
assertEquals(5, grid(0).atomicLong("decrKey", 0, true).get());

ret = content(F.asMap("cmd", "decr", "key", "decrKey", "delta", "3"));

assertNotNull(ret);
assertTrue(!ret.isEmpty());

jsonEquals(ret, cacheIntegerPattern(2, true));
jsonEquals(ret, integerPattern(2, true));

assertEquals(2, cache().dataStructures().atomicLong("decrKey", 0, true).get());
assertEquals(2, grid(0).atomicLong("decrKey", 0, true).get());
}

/**
Expand Down
Expand Up @@ -243,38 +243,46 @@ public void testMetrics() throws Exception {
* @throws Exception If failed.
*/
public void testIncrement() throws Exception {
assertEquals(15L, client().cacheIncrement(null, "key", 10L, 5L));
assertEquals(15L, grid().cache(null).dataStructures().atomicLong("key", 0, true).get());
assertEquals(18L, client().cacheIncrement(null, "key", 20L, 3L));
assertEquals(18L, grid().cache(null).dataStructures().atomicLong("key", 0, true).get());
assertEquals(20L, client().cacheIncrement(null, "key", null, 2L));
assertEquals(20L, grid().cache(null).dataStructures().atomicLong("key", 0, true).get());

assertEquals(15L, client().cacheIncrement(CACHE_NAME, "key", 10L, 5L));
assertEquals(15L, grid().cache(CACHE_NAME).dataStructures().atomicLong("key", 0, true).get());
assertEquals(18L, client().cacheIncrement(CACHE_NAME, "key", 20L, 3L));
assertEquals(18L, grid().cache(CACHE_NAME).dataStructures().atomicLong("key", 0, true).get());
assertEquals(20L, client().cacheIncrement(CACHE_NAME, "key", null, 2L));
assertEquals(20L, grid().cache(CACHE_NAME).dataStructures().atomicLong("key", 0, true).get());
assertEquals(15L, client().increment("key", 10L, 5L));
assertEquals(15L, grid().atomicLong("key", 0, true).get());

assertEquals(18L, client().increment("key", 20L, 3L));
assertEquals(18L, grid().atomicLong("key", 0, true).get());

assertEquals(20L, client().increment("key", null, 2L));
assertEquals(20L, grid().atomicLong("key", 0, true).get());

assertEquals(15L, client().increment("key1", 10L, 5L));
assertEquals(15L, grid().atomicLong("key1", 0, true).get());

assertEquals(18L, client().increment("key1", 20L, 3L));
assertEquals(18L, grid().atomicLong("key1", 0, true).get());

assertEquals(20L, client().increment("key1", null, 2L));
assertEquals(20L, grid().atomicLong("key1", 0, true).get());
}

/**
* @throws Exception If failed.
*/
public void testDecrement() throws Exception {
assertEquals(15L, client().cacheDecrement(null, "key", 20L, 5L));
assertEquals(15L, grid().cache(null).dataStructures().atomicLong("key", 0, true).get());
assertEquals(12L, client().cacheDecrement(null, "key", 20L, 3L));
assertEquals(12L, grid().cache(null).dataStructures().atomicLong("key", 0, true).get());
assertEquals(10L, client().cacheDecrement(null, "key", null, 2L));
assertEquals(10L, grid().cache(null).dataStructures().atomicLong("key", 0, true).get());

assertEquals(15L, client().cacheDecrement(CACHE_NAME, "key", 20L, 5L));
assertEquals(15L, grid().cache(CACHE_NAME).dataStructures().atomicLong("key", 0, true).get());
assertEquals(12L, client().cacheDecrement(CACHE_NAME, "key", 20L, 3L));
assertEquals(12L, grid().cache(CACHE_NAME).dataStructures().atomicLong("key", 0, true).get());
assertEquals(10L, client().cacheDecrement(CACHE_NAME, "key", null, 2L));
assertEquals(10L, grid().cache(CACHE_NAME).dataStructures().atomicLong("key", 0, true).get());
assertEquals(15L, client().decrement("key", 20L, 5L));
assertEquals(15L, grid().atomicLong("key", 0, true).get());

assertEquals(12L, client().decrement("key", 20L, 3L));
assertEquals(12L, grid().atomicLong("key", 0, true).get());

assertEquals(10L, client().decrement("key", null, 2L));
assertEquals(10L, grid().atomicLong("key", 0, true).get());

assertEquals(15L, client().decrement("key1", 20L, 5L));
assertEquals(15L, grid().atomicLong("key1", 0, true).get());

assertEquals(12L, client().decrement("key1", 20L, 3L));
assertEquals(12L, grid().atomicLong("key1", 0, true).get());

assertEquals(10L, client().decrement("key1", null, 2L));
assertEquals(10L, grid().atomicLong("key1", 0, true).get());
}

/**
Expand Down

0 comments on commit 7e3c81b

Please sign in to comment.