keith-turner commented on a change in pull request #320: ACCUMULO-4730 Created EntryLengthSummarizer URL: https://github.com/apache/accumulo/pull/320#discussion_r150937678
########## File path: core/src/main/java/org/apache/accumulo/core/client/summary/summarizers/EntryLengthSummarizer.java ########## @@ -0,0 +1,146 @@ +/* + * 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.accumulo.core.client.summary.summarizers; + +import java.math.RoundingMode; +import java.util.Map; +import java.util.function.BiFunction; + +import org.apache.accumulo.core.client.summary.Summarizer; +import org.apache.accumulo.core.client.summary.SummarizerConfiguration; +import org.apache.accumulo.core.data.Key; +import org.apache.accumulo.core.data.Value; + +import com.google.common.math.IntMath; + +/** + * Summarizer that computes summary information about field lengths. + * Specifically key length, row length, family length, qualifier length, visibility length, and value length. + * Incrementally computes minimum, maximum, count, sum, and log2 histogram of the lengths. + */ +public class EntryLengthSummarizer implements Summarizer { + + /* Helper function that calculates the various statistics that is used for the Collector methods.*/ + private static class LengthStats { + private long min = Long.MAX_VALUE; + private long max = Long.MIN_VALUE; + private long sum = 0; + private long[] counts = new long[32]; + + private void accept(int length) { + int idx; + + if (length < min) { + min = length; + } + + if (length > max) { + max = length; + } + + sum += length; + + if (length == 0) { + idx = 0; + } else { + idx = IntMath.log2(length, RoundingMode.HALF_UP); + } + + counts[idx]++; + } + + void summarize (String prefix, StatisticConsumer sc) { + sc.accept(prefix+".min", (min != Long.MAX_VALUE ? min:0)); + sc.accept(prefix+".max", (max != Long.MIN_VALUE ? max:0)); + sc.accept(prefix+".sum", sum); + + for (int i = 0; i < counts.length; i++) { + if (counts[i] > 0) { + sc.accept(prefix+".logHist."+i, counts[i]); + } + } + } + + } + + /* Helper functions for merging that is used by the Combiner. */ + private static void merge(String key, BiFunction<Long,Long,Long> mergeFunc, Map<String, Long> stats1, Map<String,Long> stats2) { + if (stats2.containsKey(key) && (stats1.containsKey(key) == false)) { Review comment: I think Java's `merge()` function on map handles the case where a key is not present. So I think the following code will insert the value from stats2 if stats1 did not have a value (w/o calling mergeFunc). ```java Long val2 = stats2.get(key); if(val2 != null) { stats1.merge(key, val2, mergeFunc); } ``` This also avoid calling `contains()` and then later `get()` on the map which is two lookups. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services