use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class GridCacheMultiNodeDataStructureTest method sample.
/**
*
* @param g Grid.
* @param cacheName Cache name.
*/
private static void sample(Ignite g, String cacheName) {
IgniteAtomicLong atomicLong = g.atomicLong("keygen", 0, true);
IgniteAtomicSequence seq = g.atomicSequence("keygen", 0, true);
seq.incrementAndGet();
seq.incrementAndGet();
seq.incrementAndGet();
seq.incrementAndGet();
atomicLong.incrementAndGet();
atomicLong.incrementAndGet();
atomicLong.incrementAndGet();
X.println(cacheName + ": Seq: " + seq.get() + " atomicLong " + atomicLong.get());
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class GridRedisGetCommandHandler method makeResponse.
/** {@inheritDoc} */
@Override
public ByteBuffer makeResponse(final GridRestResponse restRes, List<String> params) {
if (restRes.getResponse() == null) {
// check if an atomic long with the key exists (related to incr/decr).
IgniteAtomicLong l = ctx.grid().atomicLong(params.get(0), 0, false);
long val;
try {
val = l.get();
} catch (Exception ignored) {
return GridRedisProtocolParser.nil();
}
return GridRedisProtocolParser.toBulkString(val);
}
if (restRes.getResponse() instanceof String)
return GridRedisProtocolParser.toBulkString(restRes.getResponse());
else
return GridRedisProtocolParser.toTypeError("Operation against a key holding the wrong kind of value");
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class IgniteDataStructureUniqueNameTest method testUniqueName.
/**
* @param singleGrid If {@code true} uses single grid.
* @throws Exception If failed.
*/
private void testUniqueName(final boolean singleGrid) throws Exception {
final String name = IgniteUuid.randomUuid().toString();
final int DS_TYPES = 9;
final int THREADS = DS_TYPES * 3;
for (int iter = 0; iter < 20; iter++) {
log.info("Iteration: " + iter);
List<IgniteInternalFuture<Object>> futs = new ArrayList<>(THREADS);
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
for (int i = 0; i < THREADS; i++) {
final int idx = i;
IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
Thread.currentThread().setName("test thread-" + idx);
barrier.await();
Ignite ignite = singleGrid ? ignite(0) : ignite(idx % gridCount());
Object res;
switch(idx % DS_TYPES) {
case 0:
log.info("Create atomic long, grid: " + ignite.name());
res = ignite.atomicLong(name, 0, true);
break;
case 1:
log.info("Create atomic sequence, grid: " + ignite.name());
res = ignite.atomicSequence(name, 0, true);
break;
case 2:
log.info("Create atomic stamped, grid: " + ignite.name());
res = ignite.atomicStamped(name, 0, true, true);
break;
case 3:
log.info("Create atomic latch, grid: " + ignite.name());
res = ignite.countDownLatch(name, 0, true, true);
break;
case 4:
log.info("Create atomic reference, grid: " + ignite.name());
res = ignite.atomicReference(name, null, true);
break;
case 5:
log.info("Create queue, grid: " + ignite.name());
res = ignite.queue(name, 0, config(false));
break;
case 6:
log.info("Create set, grid: " + ignite.name());
res = ignite.set(name, config(false));
break;
case 7:
log.info("Create atomic semaphore, grid: " + ignite.name());
res = ignite.semaphore(name, 0, false, true);
break;
case 8:
log.info("Create atomic reentrant lock, grid: " + ignite.name());
res = ignite.reentrantLock(name, true, true, true);
break;
default:
fail();
return null;
}
log.info("Thread created: " + res);
return res;
} catch (IgniteException e) {
log.info("Failed: " + e);
return e;
}
}
});
futs.add(fut);
}
Closeable dataStructure = null;
int createdCnt = 0;
for (IgniteInternalFuture<Object> fut : futs) {
Object res = fut.get();
if (res instanceof IgniteException || res instanceof IgniteCheckedException)
continue;
assertTrue("Unexpected object: " + res, res instanceof IgniteAtomicLong || res instanceof IgniteAtomicSequence || res instanceof IgniteAtomicReference || res instanceof IgniteAtomicStamped || res instanceof IgniteCountDownLatch || res instanceof IgniteQueue || res instanceof IgniteSet || res instanceof IgniteSemaphore || res instanceof IgniteLock);
log.info("Data structure created: " + dataStructure);
createdCnt++;
if (dataStructure != null)
assertEquals(dataStructure.getClass(), res.getClass());
else
dataStructure = (Closeable) res;
}
assertNotNull(dataStructure);
assertEquals(3, createdCnt);
dataStructure.close();
}
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class IgniteAtomicLongApiAbstractSelfTest method testAddAndGet.
/**
* @throws Exception If failed.
*/
public void testAddAndGet() throws Exception {
info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
Ignite ignite = grid(0);
IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
long delta = RND.nextLong();
long curAtomicVal = atomic.get();
assert curAtomicVal + delta == atomic.addAndGet(delta);
assert curAtomicVal + delta == atomic.get();
}
use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.
the class IgniteAtomicLongApiAbstractSelfTest method testGetAndDecrement.
/**
* @throws Exception If failed.
*/
public void testGetAndDecrement() 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 == atomic.getAndDecrement();
assert curAtomicVal - 1 == atomic.get();
}
Aggregations