use of org.apache.ignite.internal.processors.datastructures.GridCacheQueueHeader in project ignite by apache.
the class CacheDataStructuresManager method queue0.
/**
* @param name Queue name.
* @param cap Capacity.
* @param colloc Collocated flag.
* @param create Create flag.
* @return Queue header.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
@Nullable
public <T> GridCacheQueueProxy<T> queue0(final String name, final int cap, boolean colloc, final boolean create) throws IgniteCheckedException {
cctx.gate().enter();
try {
GridCacheQueueHeaderKey key = new GridCacheQueueHeaderKey(name);
GridCacheQueueHeader hdr;
if (create) {
hdr = new GridCacheQueueHeader(IgniteUuid.randomUuid(), cap, colloc, 0, 0, null);
GridCacheQueueHeader old = queueHdrView.withNoRetries().getAndPutIfAbsent(key, hdr);
if (old != null) {
if (old.capacity() != cap || old.collocated() != colloc)
throw new IgniteCheckedException("Failed to create queue, queue with the same name but " + "different configuration already exists [name=" + name + ']');
hdr = old;
}
} else
hdr = queueHdrView.get(key);
if (hdr == null)
return null;
if (queueQryGuard.compareAndSet(false, true)) {
queueQryId = cctx.continuousQueries().executeInternalQuery(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
if (!busyLock.enterBusy())
return;
try {
for (CacheEntryEvent<?, ?> e : evts) {
GridCacheQueueHeaderKey key = (GridCacheQueueHeaderKey) e.getKey();
GridCacheQueueHeader hdr = (GridCacheQueueHeader) e.getValue();
for (final GridCacheQueueProxy queue : queuesMap.values()) {
if (queue.name().equals(key.queueName())) {
if (e.getEventType() == REMOVED) {
GridCacheQueueHeader oldHdr = (GridCacheQueueHeader) e.getOldValue();
assert oldHdr != null;
if (oldHdr.id().equals(queue.delegate().id())) {
queue.delegate().onRemoved(false);
queuesMap.remove(queue.delegate().id());
}
} else
queue.delegate().onHeaderChanged(hdr);
}
}
}
} finally {
busyLock.leaveBusy();
}
}
}, new QueueHeaderPredicate(), cctx.isLocal() || (cctx.isReplicated() && cctx.affinityNode()), true, false, false);
}
GridCacheQueueProxy queue = queuesMap.get(hdr.id());
if (queue == null) {
queue = new GridCacheQueueProxy(cctx, cctx.atomic() ? new GridAtomicCacheQueueImpl<>(name, hdr, cctx) : new GridTransactionalCacheQueueImpl<>(name, hdr, cctx));
GridCacheQueueProxy old = queuesMap.putIfAbsent(hdr.id(), queue);
if (old != null)
queue = old;
}
return queue;
} finally {
cctx.gate().leave();
}
}
Aggregations