use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class IgniteAtomicLongApiAbstractSelfTest method testIncrementAndGet.
/**
* @throws Exception If failed.
*/
public void testIncrementAndGet() throws Exception {
info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
Ignite ignite = grid(0);
IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
long curAtomicVal = atomic.get();
assert curAtomicVal + 1 == atomic.incrementAndGet();
assert curAtomicVal + 1 == atomic.get();
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class IgniteDataStructureUniqueNameTest method testCreateRemove.
/**
* @throws Exception If failed.
*/
public void testCreateRemove() throws Exception {
final String name = IgniteUuid.randomUuid().toString();
final Ignite ignite = ignite(0);
assertNull(ignite.atomicLong(name, 0, false));
IgniteAtomicReference<Integer> ref = ignite.atomicReference(name, 0, true);
assertNotNull(ref);
assertSame(ref, ignite.atomicReference(name, 0, true));
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.atomicLong(name, 0, false);
return null;
}
}, IgniteException.class, null);
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.atomicLong(name, 0, true);
return null;
}
}, IgniteException.class, null);
ref.close();
IgniteAtomicLong atomicLong = ignite.atomicLong(name, 0, true);
assertNotNull(atomicLong);
assertSame(atomicLong, ignite.atomicLong(name, 0, true));
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.atomicReference(name, 0, false);
return null;
}
}, IgniteException.class, null);
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.queue(name, 0, config(false));
return null;
}
}, IgniteException.class, null);
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.queue(name, 0, null);
return null;
}
}, IgniteException.class, null);
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.set(name, config(false));
return null;
}
}, IgniteException.class, null);
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.set(name, null);
return null;
}
}, IgniteException.class, null);
atomicLong.close();
IgniteQueue<Integer> q = ignite.queue(name, 0, config(false));
assertNotNull(q);
assertSame(q, ignite.queue(name, 0, config(false)));
assertSame(q, ignite.queue(name, 0, null));
q.close();
assertNull(ignite.set(name, null));
IgniteSet<Integer> set = ignite.set(name, config(false));
assertNotNull(set);
assertSame(set, ignite.set(name, config(false)));
assertSame(set, ignite.set(name, null));
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.atomicReference(name, 0, false);
return null;
}
}, IgniteException.class, null);
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.queue(name, 0, config(false));
return null;
}
}, IgniteException.class, null);
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
ignite.queue(name, 0, null);
return null;
}
}, IgniteException.class, null);
set.close();
ref = ignite.atomicReference(name, 0, true);
assertNotNull(ref);
assertSame(ref, ignite.atomicReference(name, 0, true));
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class GridCachePartitionedNodeRestartTxSelfTest method checkAtomic.
/**
* Test AtomicLong.
* @param name Name of atomic.
* @throws Exception If failed.
*/
private void checkAtomic(String name) throws Exception {
for (int i = INIT_GRID_NUM; i < 20; i++) {
startGrid(i);
assert PARTITIONED == grid(i).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode();
IgniteAtomicLong atomic = grid(i).atomicLong(name, 0, true);
long val = atomic.get();
assertEquals("Atomic check failed for node: " + i, (long) i, val);
atomic.incrementAndGet();
stopGrid(i);
}
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class IgniteAtomicLongExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Atomic long example started.");
// Make name for atomic long (by which it will be known in the cluster).
String atomicName = UUID.randomUUID().toString();
// Initialize atomic long.
final IgniteAtomicLong atomicLong = ignite.atomicLong(atomicName, 0, true);
System.out.println();
System.out.println("Atomic long initial value : " + atomicLong.get() + '.');
// Try increment atomic long from all nodes.
// Note that this node is also part of the ignite cluster.
ignite.compute().broadcast(new IgniteCallable<Object>() {
@Override
public Object call() {
for (int i = 0; i < RETRIES; i++) System.out.println("AtomicLong value has been incremented: " + atomicLong.incrementAndGet());
return null;
}
});
System.out.println();
System.out.println("Atomic long value after successful CAS: " + atomicLong.get());
}
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class MemcacheRestExample method main.
/**
* @param args Command line arguments.
* @throws Exception In case of error.
*/
public static void main(String[] args) throws Exception {
MemcachedClient client = null;
try (Ignite ignite = Ignition.start(MemcacheRestExampleNodeStartup.configuration())) {
System.out.println();
System.out.println(">>> Memcache REST example started.");
IgniteCache<String, Object> cache = ignite.cache("default");
client = startMemcachedClient(host, port);
// Put string value to cache using Memcache binary protocol.
if (client.add("strKey", 0, "strVal").get())
System.out.println(">>> Successfully put string value using Memcache client.");
// Check that string value is actually in cache using traditional
// Ignite API and Memcache binary protocol.
System.out.println(">>> Getting value for 'strKey' using Ignite cache API: " + cache.get("strKey"));
System.out.println(">>> Getting value for 'strKey' using Memcache client: " + client.get("strKey"));
// Remove string value from cache using Memcache binary protocol.
if (client.delete("strKey").get())
System.out.println(">>> Successfully removed string value using Memcache client.");
// Check that cache is empty.
System.out.println(">>> Current cache size: " + cache.size() + " (expected: 0).");
// Put integer value to cache using Memcache binary protocol.
if (client.add("intKey", 0, 100).get())
System.out.println(">>> Successfully put integer value using Memcache client.");
// Check that integer value is actually in cache using traditional
// Ignite API and Memcache binary protocol.
System.out.println(">>> Getting value for 'intKey' using Ignite cache API: " + cache.get("intKey"));
System.out.println(">>> Getting value for 'intKey' using Memcache client: " + client.get("intKey"));
// Remove string value from cache using Memcache binary protocol.
if (client.delete("intKey").get())
System.out.println(">>> Successfully removed integer value using Memcache client.");
// Check that cache is empty.
System.out.println(">>> Current cache size: " + cache.size() + " (expected: 0).");
// Create atomic long and close it after test is done.
try (IgniteAtomicLong l = ignite.atomicLong("atomicLong", 10, true)) {
// Increment atomic long by 5 using Memcache client.
if (client.incr("atomicLong", 5, 0) == 15)
System.out.println(">>> Successfully incremented atomic long by 5.");
// Increment atomic long using Ignite API and check that value is correct.
System.out.println(">>> New atomic long value: " + l.incrementAndGet() + " (expected: 16).");
// Decrement atomic long by 3 using Memcache client.
if (client.decr("atomicLong", 3, 0) == 13)
System.out.println(">>> Successfully decremented atomic long by 3.");
// Decrement atomic long using Ignite API and check that value is correct.
System.out.println(">>> New atomic long value: " + l.decrementAndGet() + " (expected: 12).");
}
} finally {
if (client != null)
client.shutdown();
}
}
Aggregations