/**
   * Constructs a new map containing the elements in the specified map, that orders its elements
   * according to their {@linkplain Comparable natural ordering}.
   *
   * @param max Upper bound of this map.
   * @param map The elements that will comprise the new map.
   * @throws ClassCastException if the elements in <tt>map</tt> are not {@link Comparable}, or are
   *     not mutually comparable.
   * @throws NullPointerException if the specified map or any of its elements are {@code null}.
   */
  public GridBoundedConcurrentOrderedMap(int max, Map<? extends K, ? extends V> map) {
    super(map);

    A.ensure(max > 0, "max > 0");

    this.max = max;
  }
  /**
   * Constructs a new map containing the same elements and using the same ordering as the specified
   * sorted map.
   *
   * @param max Upper bound of this map.
   * @param map Sorted map whose elements will comprise the new map.
   * @throws NullPointerException if the specified sorted map or any of its elements are {@code
   *     null}.
   */
  public GridBoundedConcurrentOrderedMap(int max, SortedMap<K, V> map) {
    super(map);

    A.ensure(max > 0, "max > 0");

    this.max = max;
  }
  /**
   * Constructs a new, empty set that orders its elements according to the specified comparator.
   *
   * @param max Upper bound of this map.
   * @param comparator The comparator that will be used to order this map. If <tt>null</tt>, the
   *     {@linkplain Comparable natural ordering} of the elements will be used.
   */
  public GridBoundedConcurrentOrderedMap(int max, Comparator<? super K> comparator) {
    super(comparator);

    A.ensure(max > 0, "max > 0");

    this.max = max;
  }
  /**
   * 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();
    }
  }
Esempio n. 5
0
  /** {@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);
    }
  }
Esempio n. 6
0
  /** {@inheritDoc} */
  @Override
  public void perNodeBufferSize(int bufSize) {
    A.ensure(bufSize > 0, "bufSize > 0");

    this.bufSize = bufSize;
  }
  /**
   * Constructs a new, empty map that orders its elements according to their {@linkplain Comparable
   * natural ordering}.
   *
   * @param max Upper bound of this map.
   */
  public GridBoundedConcurrentOrderedMap(int max) {
    A.ensure(max > 0, "max > 0");

    this.max = max;
  }
  /**
   * 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();
        }
      }
    }
  }
  /** {@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);
  }
  /** {@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 int countDown(int val) throws GridException {
    A.ensure(val > 0, "val should be positive");

    return CU.outTx(new CountDownCallable(val), ctx);
  }