Example usage for com.google.common.util.concurrent AtomicDouble AtomicDouble

List of usage examples for com.google.common.util.concurrent AtomicDouble AtomicDouble

Introduction

In this page you can find the example usage for com.google.common.util.concurrent AtomicDouble AtomicDouble.

Prototype

public AtomicDouble() 

Source Link

Document

Creates a new AtomicDouble with initial value 0.0 .

Usage

From source file:com.facebook.stats.QuantileDigest.java

public synchronized List<Bucket> getHistogram(List<Long> bucketUpperBounds) {
    checkArgument(Ordering.natural().isOrdered(bucketUpperBounds),
            "buckets must be sorted in increasing order");

    final ImmutableList.Builder<Bucket> builder = ImmutableList.builder();
    final PeekingIterator<Long> iterator = Iterators.peekingIterator(bucketUpperBounds.iterator());

    final AtomicDouble sum = new AtomicDouble();
    final AtomicDouble lastSum = new AtomicDouble();

    // for computing weighed average of values in bucket
    final AtomicDouble bucketWeightedSum = new AtomicDouble();

    final double normalizationFactor = weight(TimeUnit.MILLISECONDS.toSeconds(clock.getMillis()));

    postOrderTraversal(root, new Callback() {
        public boolean process(Node node) {

            while (iterator.hasNext() && iterator.peek() <= node.getUpperBound()) {
                double bucketCount = sum.get() - lastSum.get();

                Bucket bucket = new Bucket(bucketCount / normalizationFactor,
                        bucketWeightedSum.get() / bucketCount);

                builder.add(bucket);/*from ww w  .j  ava 2  s. c om*/
                lastSum.set(sum.get());
                bucketWeightedSum.set(0);
                iterator.next();
            }

            bucketWeightedSum.addAndGet(node.getMiddle() * node.weightedCount);
            sum.addAndGet(node.weightedCount);
            return iterator.hasNext();
        }
    });

    while (iterator.hasNext()) {
        double bucketCount = sum.get() - lastSum.get();
        Bucket bucket = new Bucket(bucketCount / normalizationFactor, bucketWeightedSum.get() / bucketCount);

        builder.add(bucket);

        iterator.next();
    }

    return builder.build();
}

From source file:com.linkedin.pinot.core.query.aggregation.function.quantile.digest.QuantileDigest.java

public List<Bucket> getHistogram(List<Long> bucketUpperBounds) {
    checkArgument(Ordering.natural().isOrdered(bucketUpperBounds),
            "buckets must be sorted in increasing order");

    final ImmutableList.Builder<Bucket> builder = ImmutableList.builder();
    final PeekingIterator<Long> iterator = Iterators.peekingIterator(bucketUpperBounds.iterator());

    final AtomicDouble sum = new AtomicDouble();
    final AtomicDouble lastSum = new AtomicDouble();

    // for computing weighed average of values in bucket
    final AtomicDouble bucketWeightedSum = new AtomicDouble();

    final double normalizationFactor = weight(TimeUnit.NANOSECONDS.toSeconds(ticker.read()));

    postOrderTraversal(root, new Callback() {
        @Override/*from  w  ww .  j  a  v a  2 s  .  c o m*/
        public boolean process(Node node) {

            while (iterator.hasNext() && iterator.peek() <= node.getUpperBound()) {
                double bucketCount = sum.get() - lastSum.get();

                Bucket bucket = new Bucket(bucketCount / normalizationFactor,
                        bucketWeightedSum.get() / bucketCount);

                builder.add(bucket);
                lastSum.set(sum.get());
                bucketWeightedSum.set(0);
                iterator.next();
            }

            bucketWeightedSum.addAndGet(node.getMiddle() * node.weightedCount);
            sum.addAndGet(node.weightedCount);
            return iterator.hasNext();
        }
    });

    while (iterator.hasNext()) {
        double bucketCount = sum.get() - lastSum.get();
        Bucket bucket = new Bucket(bucketCount / normalizationFactor, bucketWeightedSum.get() / bucketCount);

        builder.add(bucket);

        iterator.next();
    }

    return builder.build();
}

From source file:com.facebook.stats.QuantileDigest.java

@VisibleForTesting
synchronized void validate() {
    final AtomicDouble sumOfWeights = new AtomicDouble();
    final AtomicInteger actualNodeCount = new AtomicInteger();
    final AtomicInteger actualNonZeroNodeCount = new AtomicInteger();

    if (root != null) {
        validateStructure(root);/*from  w  ww. j a  va 2  s.  c  om*/

        postOrderTraversal(root, new Callback() {
            @Override
            public boolean process(Node node) {
                sumOfWeights.addAndGet(node.weightedCount);
                actualNodeCount.incrementAndGet();

                if (node.weightedCount > ZERO_WEIGHT_THRESHOLD) {
                    actualNonZeroNodeCount.incrementAndGet();
                }

                return true;
            }
        });
    }

    checkState(Math.abs(sumOfWeights.get() - weightedCount) < ZERO_WEIGHT_THRESHOLD,
            "Computed weight (%s) doesn't match summary (%s)", sumOfWeights.get(), weightedCount);

    checkState(actualNodeCount.get() == totalNodeCount, "Actual node count (%s) doesn't match summary (%s)",
            actualNodeCount.get(), totalNodeCount);

    checkState(actualNonZeroNodeCount.get() == nonZeroNodeCount,
            "Actual non-zero node count (%s) doesn't match summary (%s)", actualNonZeroNodeCount.get(),
            nonZeroNodeCount);
}

From source file:com.linkedin.pinot.core.query.aggregation.function.quantile.digest.QuantileDigest.java

@VisibleForTesting
void validate() {
    final AtomicDouble sumOfWeights = new AtomicDouble();
    final AtomicInteger actualNodeCount = new AtomicInteger();
    final AtomicInteger actualNonZeroNodeCount = new AtomicInteger();

    if (root != null) {
        validateStructure(root);/* w ww . j  a  v  a 2  s  .  c o  m*/

        postOrderTraversal(root, new Callback() {
            @Override
            public boolean process(Node node) {
                sumOfWeights.addAndGet(node.weightedCount);
                actualNodeCount.incrementAndGet();

                if (node.weightedCount >= ZERO_WEIGHT_THRESHOLD) {
                    actualNonZeroNodeCount.incrementAndGet();
                }

                return true;
            }
        });
    }

    checkState(Math.abs(sumOfWeights.get() - weightedCount) < ZERO_WEIGHT_THRESHOLD,
            "Computed weight (%s) doesn't match summary (%s)", sumOfWeights.get(), weightedCount);

    checkState(actualNodeCount.get() == totalNodeCount, "Actual node count (%s) doesn't match summary (%s)",
            actualNodeCount.get(), totalNodeCount);

    checkState(actualNonZeroNodeCount.get() == nonZeroNodeCount,
            "Actual non-zero node count (%s) doesn't match summary (%s)", actualNonZeroNodeCount.get(),
            nonZeroNodeCount);
}