use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class GridDhtPartitionSupplier method clearContext.
/**
* Clear context.
*
* @param sc Supply context.
* @param log Logger.
*/
private static void clearContext(final SupplyContext sc, final IgniteLogger log) {
if (sc != null) {
final Iterator it = sc.entryIt;
if (it != null && it instanceof GridCloseableIterator && !((GridCloseableIterator) it).isClosed()) {
try {
((GridCloseableIterator) it).close();
} catch (IgniteCheckedException e) {
U.error(log, "Iterator close failed.", e);
}
}
final GridDhtLocalPartition loc = sc.loc;
if (loc != null) {
assert loc.reservations() > 0;
loc.release();
}
}
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class GridUnsafeMap method iterator.
/**
* {@inheritDoc}
*/
@Override
public <T> GridCloseableIterator<T> iterator(final CX2<T2<Long, Integer>, T2<Long, Integer>, T> c) {
return new GridCloseableIteratorAdapter<T>() {
private GridCloseableIterator<T> curIt;
private int idx;
{
try {
advance();
} catch (IgniteCheckedException e) {
// Should never happen.
e.printStackTrace();
}
}
private void advance() throws IgniteCheckedException {
curIt = null;
while (idx < segs.length) {
curIt = segs[idx++].iterator(c);
if (curIt.hasNext())
return;
else
curIt.close();
}
curIt = null;
}
@Override
protected T onNext() throws IgniteCheckedException {
if (curIt == null)
throw new NoSuchElementException();
T t = curIt.next();
if (!curIt.hasNext()) {
curIt.close();
advance();
}
return t;
}
@Override
protected boolean onHasNext() {
return curIt != null;
}
@Override
protected void onRemove() {
throw new UnsupportedOperationException();
}
@Override
protected void onClose() throws IgniteCheckedException {
if (curIt != null)
curIt.close();
}
};
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class GridUnsafeMap method iterator.
/**
* {@inheritDoc}
*/
@Override
public GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> iterator() {
return new GridCloseableIteratorAdapter<IgniteBiTuple<byte[], byte[]>>() {
private GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> curIt;
private int idx;
{
try {
advance();
} catch (IgniteCheckedException e) {
// Should never happen.
e.printStackTrace();
}
}
private void advance() throws IgniteCheckedException {
curIt = null;
while (idx < segs.length) {
curIt = segs[idx++].iterator();
if (curIt.hasNext())
return;
else
curIt.close();
}
curIt = null;
}
@Override
protected IgniteBiTuple<byte[], byte[]> onNext() throws IgniteCheckedException {
if (curIt == null)
throw new NoSuchElementException();
IgniteBiTuple<byte[], byte[]> t = curIt.next();
if (!curIt.hasNext()) {
curIt.close();
advance();
}
return t;
}
@Override
protected boolean onHasNext() {
return curIt != null;
}
@Override
protected void onRemove() {
throw new UnsupportedOperationException();
}
@Override
protected void onClose() throws IgniteCheckedException {
if (curIt != null)
curIt.close();
}
};
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class GridOffHeapPartitionedMapAbstractSelfTest method testPartitionIteratorMultithreaded.
/**
* @throws Exception If failed.
*/
@Test
public void testPartitionIteratorMultithreaded() throws Exception {
initCap = 10;
map = newMap();
final AtomicInteger rehashes = new AtomicInteger();
final AtomicInteger releases = new AtomicInteger();
map.eventListener(new GridOffHeapEventListener() {
@Override
public void onEvent(GridOffHeapEvent evt) {
switch(evt) {
case REHASH:
rehashes.incrementAndGet();
break;
case RELEASE:
releases.incrementAndGet();
break;
// No-op.
default:
}
}
});
final int max = 64;
int threads = 5;
final Map<String, String> m = new ConcurrentHashMap<>(max);
multithreaded(new Callable() {
@Override
public Object call() throws Exception {
for (int p = 0; p < parts; p++) {
for (int i = 0; i < max; i++) {
String key = string();
String val = string();
// info("Storing [i=" + i + ", key=" + key + ", val=" + val + ']');
m.put(key, val);
assertTrue(map.put(p, hash(key), key.getBytes(), val.getBytes()));
assertTrue(map.contains(p, hash(key), key.getBytes()));
assertNotNull(map.get(p, hash(key), key.getBytes()));
assertEquals(new String(map.get(p, hash(key), key.getBytes())), val);
try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator(p)) {
while (it.hasNext()) {
IgniteBiTuple<byte[], byte[]> t = it.next();
String k = new String(t.get1());
String v = new String(t.get2());
// info("Entry [k=" + k + ", v=" + v + ']');
assertEquals(m.get(k), v);
}
}
}
}
return null;
}
}, threads);
assertEquals(max * threads * parts, map.size());
info("Stats [size=" + map.size() + ", rehashes=" + rehashes + ", releases=" + releases + ']');
assertTrue(rehashes.get() > 0);
assertEquals(rehashes.get(), releases.get());
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class IgniteH2Indexing method queryLocalSql.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryLocalSql(String schemaName, String cacheName, final SqlQuery qry, final IndexingQueryFilter filter, final boolean keepBinary) throws IgniteCheckedException {
String type = qry.getType();
String sqlQry = qry.getSql();
String alias = qry.getAlias();
Object[] params = qry.getArgs();
GridQueryCancel cancel = new GridQueryCancel();
final GridCloseableIterator<IgniteBiTuple<K, V>> i = queryLocalSql(schemaName, cacheName, sqlQry, alias, F.asList(params), type, filter, cancel);
return new QueryCursorImpl<>(new Iterable<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return new ClIter<Cache.Entry<K, V>>() {
@Override
public void close() throws Exception {
i.close();
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public Cache.Entry<K, V> next() {
IgniteBiTuple<K, V> t = i.next();
K key = (K) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get1(), keepBinary, false);
V val = (V) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get2(), keepBinary, false);
return new CacheEntryImpl<>(key, val);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}, cancel);
}
Aggregations