package com.codahale.metrics;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;

import static java.lang.Math.min;

/**
 * A {@link Reservoir} implementation backed by a sliding window that stores the
 * last {@code N} measurements.
 */
public class SlidingWindowReservoir implements Reservoir {
    private final AtomicLong index;
    private final AtomicLong count;
    private final AtomicLongArray measurements;

    /**
     * Creates a new {@link SlidingWindowReservoir} which stores the last
     * {@code size} measurements.
     *
     * @param size the number of measurements to store
     */
    public SlidingWindowReservoir(int size) {
        this.index = new AtomicLong();
        this.count = new AtomicLong();
        this.measurements = new AtomicLongArray(size);
    }

    @Override
    public int size() {
        // use count, not index, to prevent phantom reads
        return (int) min(count.get(), measurements.length());
    }

    @Override
    public void update(long value) {
        // entry barrier
        final long n = index.getAndIncrement();
        final int idx = (int) (n % measurements.length());

        // critical section
        measurements.set(idx, value);

        // exit barrier
        while (true) {
            // Only return once other writers (i.e., those who have already incremented index) have
            // incremented count.
            if (count.compareAndSet(n, n + 1)) {
                return;
            }
        }
    }

    @Override
    public Snapshot getSnapshot() {
        final long[] values = new long[size()];
        for (int i = 0; i < values.length; i++) {
            values[i] = measurements.get(i);
        }
        return new Snapshot(values);
    }
}

@@ -874,66 +874,8 @@
 ) %7B%0A
-        // use count, not index, to prevent phantom reads%0A
     
@@ -1005,21 +1005,42 @@
  // 
-entry barrier
+first, get and increment the index
 %0A   
@@ -1054,17 +1054,23 @@
 nal 
-long
+int
  n = 
+(int) (
 inde
@@ -1092,42 +1092,8 @@
 nt()
-;%0A        final int idx = (int) (n
  %25 m
@@ -1115,17 +1115,16 @@
 gth());%0A
-%0A
         
@@ -1130,24 +1130,35 @@
  // 
-critical section
+second, set the measurement
 %0A   
@@ -1183,11 +1183,9 @@
 set(
-idx
+n
 , va
@@ -1190,17 +1190,16 @@
 value);%0A
-%0A
         
@@ -1205,331 +1205,205 @@
  // 
-exit barrier%0A        while (true) %7B%0A            // Only return once other writers (i.e., those who have already incremented index) have%0A            // incremented count.%0A            if (count.compareAndSet(n, n + 1)) %7B%0A                return;%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public Snapshot getSnapshot() %7B
+third, increment the count of written measurements%0A        count.incrementAndGet();%0A    %7D%0A%0A    @Override%0A    public Snapshot getSnapshot() %7B%0A        // use size, not index, to prevent phantom reads
 %0A   

Revert "Fix the exit barrier in SlidingWindowReservoir."