rdblue commented on code in PR #5348:
URL: https://github.com/apache/iceberg/pull/5348#discussion_r947064887


##########
api/src/main/java/org/apache/iceberg/metrics/FixedReservoirHistogram.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.metrics;
+
+import java.util.Arrays;
+import java.util.Random;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+/** A {@link Histogram} implementation with reservoir sampling. */
+public class FixedReservoirHistogram implements Histogram {
+  private final Random rand;
+  private final long[] measurements;
+  private int count;
+
+  public FixedReservoirHistogram(int reservoirSize) {
+    this.rand = new Random();
+    this.measurements = new long[reservoirSize];
+    this.count = 0;
+  }
+
+  @Override
+  public synchronized int count() {
+    return count;
+  }
+
+  @Override
+  public synchronized void update(long value) {
+    count += 1;
+    int index = count <= measurements.length ? count - 1 : rand.nextInt(count);
+    if (index < measurements.length) {
+      measurements[index] = value;
+    }
+  }
+
+  /**
+   * Naive algorithm for calculating variance:
+   * https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
+   */
+  @Override
+  public Statistics statistics() {
+    int size = measurements.length;
+    if (count < (long) measurements.length) {
+      size = count;
+    }
+
+    long[] values = new long[size];
+    if (size == 0) {
+      return new UniformWeightStatistics(values, 0.0, 0.0);

Review Comment:
   Minor: This could be a constant.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to