/**
   * @param keys Keys.
   * @return If near entries for given keys are locked.
   */
  public boolean isAllLockedNearOnly(Iterable<? extends K> keys) {
    A.notNull(keys, "keys");

    for (K key : keys) if (!isLockedNearOnly(key)) return false;

    return true;
  }
  /** {@inheritDoc} */
  @Override
  public GridCacheQuery<T> timeout(long timeout) {
    A.ensure(timeout >= 0, "timeout >= 0");

    this.timeout = timeout;

    return this;
  }
  /** {@inheritDoc} */
  @Override
  public GridCacheQuery<T> pageSize(int pageSize) {
    A.ensure(pageSize > 0, "pageSize > 0");

    this.pageSize = pageSize;

    return this;
  }
  /**
   * Set local batch size for this sequences.
   *
   * @param size Sequence batch size. Must be more then 0.
   */
  @Override
  public void batchSize(int size) {
    A.ensure(size > 0, " Batch size can't be less then 0: " + size);

    lock.lock();

    try {
      batchSize = size;
    } finally {
      lock.unlock();
    }
  }
  /** {@inheritDoc} */
  @Override
  public void timeInterval(long timeInterval) {
    A.ensure(timeInterval >= 0, "timeInterval >= 0");

    if (!guard.enterBusy())
      throw new IllegalStateException("Continuous query can't be changed after it was executed.");

    try {
      this.timeInterval = timeInterval;
    } finally {
      guard.leaveBusy();
    }
  }
  /** {@inheritDoc} */
  @Override
  public void bufferSize(int bufSize) {
    A.ensure(bufSize > 0, "bufSize > 0");

    if (!guard.enterBusy())
      throw new IllegalStateException("Continuous query can't be changed after it was executed.");

    try {
      this.bufSize = bufSize;
    } finally {
      guard.leaveBusy();
    }
  }
  /** {@inheritDoc} */
  @Override
  public void autoFlushFrequency(long autoFlushFreq) {
    A.ensure(autoFlushFreq >= 0, "autoFlushFreq >= 0");

    long old = this.autoFlushFreq;

    if (autoFlushFreq != old) {
      this.autoFlushFreq = autoFlushFreq;

      if (autoFlushFreq != 0 && old == 0) flushQ.add(this);
      else if (autoFlushFreq == 0) flushQ.remove(this);
    }
  }
  /** {@inheritDoc} */
  @Override
  public void callback(GridBiPredicate<UUID, Collection<Map.Entry<K, V>>> cb) {
    A.notNull(cb, "cb");

    if (!guard.enterBusy())
      throw new IllegalStateException("Continuous query can't be changed after it was executed.");

    try {
      this.cb = cb;
    } finally {
      guard.leaveBusy();
    }
  }
  /** {@inheritDoc} */
  @Override
  public GridFuture<?> addData(Collection<? extends Map.Entry<K, V>> entries) {
    A.notEmpty(entries, "entries");

    enterBusy();

    try {
      GridFutureAdapter<Object> resFut = new GridFutureAdapter<>(ctx);

      activeFuts.add(resFut);

      resFut.listenAsync(rmvActiveFut);

      Collection<K> keys = new GridConcurrentHashSet<>(entries.size(), 1.0f, 16);

      for (Map.Entry<K, V> entry : entries) keys.add(entry.getKey());

      load0(entries, resFut, keys, 0);

      return resFut;
    } finally {
      leaveBusy();
    }
  }
  /** {@inheritDoc} */
  @Override
  public int countDown(int val) throws GridException {
    A.ensure(val > 0, "val should be positive");

    return CU.outTx(new CountDownCallable(val), ctx);
  }
  /** {@inheritDoc} */
  @Override
  public GridFuture<?> addData(K key, V val) throws GridException, IllegalStateException {
    A.notNull(key, "key");

    return addData(new Entry0<>(key, val));
  }
  /** {@inheritDoc} */
  @Override
  public GridFuture<?> addData(Map.Entry<K, V> entry) throws GridException, IllegalStateException {
    A.notNull(entry, "entry");

    return addData(F.asList(entry));
  }
  /** {@inheritDoc} */
  @Override
  public GridFuture<?> addData(Map<K, V> entries) throws IllegalStateException {
    A.notNull(entries, "entries");

    return addData(entries.entrySet());
  }
  /** {@inheritDoc} */
  @Override
  public long addAndGet(long l) throws GridException {
    A.ensure(l > 0, " Parameter mustn't be less then 1: " + l);

    return internalUpdate(l, null, true);
  }
  /** {@inheritDoc} */
  @Override
  public void perNodeBufferSize(int bufSize) {
    A.ensure(bufSize > 0, "bufSize > 0");

    this.bufSize = bufSize;
  }
  /** {@inheritDoc} */
  @Override
  public void updater(GridDataLoadCacheUpdater<K, V> updater) {
    A.notNull(updater, "updater");

    this.updater = updater;
  }
  /** {@inheritDoc} */
  @Override
  public GridFuture<Long> getAndAddAsync(long l) throws GridException {
    A.ensure(l > 0, " Parameter mustn't be less then 1: " + l);

    return internalUpdateAsync(l, null, false);
  }
  /**
   * Asynchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Future indicating sequence value.
   * @throws GridException If update failed.
   */
  private GridFuture<Long> internalUpdateAsync(
      long l, @Nullable Callable<Long> updateCall, boolean updated) throws GridException {
    checkRemoved();

    A.ensure(l > 0, " Parameter mustn't be less then 1: " + l);

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return ctx.closures().callLocalSafe(updateCall, true);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }