nfsantos commented on code in PR #1247:
URL: https://github.com/apache/jackrabbit-oak/pull/1247#discussion_r1440191945


##########
oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/analysis/modules/BinarySize.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.jackrabbit.oak.index.indexer.document.flatfile.analysis.modules;
+
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import 
org.apache.jackrabbit.oak.index.indexer.document.flatfile.analysis.stream.NodeData;
+import 
org.apache.jackrabbit.oak.index.indexer.document.flatfile.analysis.stream.NodeProperty;
+import 
org.apache.jackrabbit.oak.index.indexer.document.flatfile.analysis.stream.NodeProperty.ValueType;
+
+/**
+ * Collects the total binary size (references to the datastore) per path.
+ */
+public class BinarySize implements StatsCollector {
+
+    private final Storage storage = new Storage();
+    private final int resolution;
+    private final boolean embedded;
+    private final String unit;
+    private final int divideBy;
+    private final Random random;
+
+    public BinarySize(boolean embedded, long seed) {
+        this.embedded = embedded;
+        if (embedded) {
+            unit = "MB";
+            divideBy = 1_000_000;
+        } else {
+            unit = "GB";
+            divideBy = 1_000_000_000;
+        }
+        this.resolution = divideBy / 10;
+        this.random = new Random(seed); //NOSONAR
+    }
+
+    public void add(NodeData node) {
+        long size = 0;
+        for(NodeProperty p : node.getProperties()) {
+            if (p.getType() == ValueType.BINARY) {
+                for (String v : p.getValues()) {
+                    if (!v.startsWith(":blobId:")) {
+                        continue;
+                    }
+                    v = v.substring(":blobId:".length());
+                    if (v.startsWith("0x")) {
+                        // embedded
+                        if (embedded) {
+                            int hashIndex = v.lastIndexOf('#');
+                            if (hashIndex >= 0) {
+                                v = v.substring(0, hashIndex);
+                            }
+                            size = (v.length() - 2) / 2;
+                        }
+                    } else {
+                        // reference
+                        if (!embedded) {
+                            int hashIndex = v.lastIndexOf('#');
+                            String length = v.substring(hashIndex + 1);
+                            size += Long.parseLong(length);
+                        }
+                    }
+                }
+            }
+        }
+        if (size == 0) {
+            return;
+        }
+        storage.add("/", size);
+        StringBuilder buff = new StringBuilder();
+        for (int i = 0; i < node.getPathElements().size(); i++) {
+            String pe = node.getPathElements().get(i);
+            buff.append('/').append(pe);
+            String key = buff.toString();
+            if (pe.equals("jcr:content")) {
+                break;
+            }
+            if (i < 2) {
+                storage.add(key, size);
+            } else {
+                long s2 = size / resolution * resolution;
+                if (s2 > 0) {
+                    storage.add(key, size);
+                } else {
+                    if (random.nextInt(resolution) < size) {
+                        storage.add(key, resolution);
+                    }
+                }
+            }
+        }
+    }
+
+    public List<String> getRecords() {
+        List<String> result = new ArrayList<>();
+        for(Entry<String, Long> e : storage.entrySet()) {
+            long v = e.getValue();
+            if (v > divideBy) {
+                result.add(e.getKey() + ": " + (v / divideBy));
+            }
+        }
+        return result;
+    }
+
+    public String toString() {
+        StringBuilder buff = new StringBuilder();
+        buff.append("BinarySize");
+        buff.append(embedded ? " embedded" : " references");
+        buff.append(" in " + unit);
+        buff.append(" (resolution: " + resolution + ")\n");
+        buff.append(getRecords().stream().map(s -> s + 
"\n").collect(Collectors.joining()));

Review Comment:
   This method does not seem to be performance critical, but just for 
coherence, since a StringBuilder is used with the intent to reduce allocations, 
it would be better in this line to append directly the individual records to 
the String builder instead of using Collectors.joining() to create a separate 
String.
   
   BTW, in recent versions of Java (as in Java 11 or later), String 
concatenation with + (when not done in loops) is often faster than using 
StringBuilder because of [JEP 280: Indify String 
Concatenation](https://openjdk.org/jeps/280). Probably does not apply here 
because there is an implicit loop.



-- 
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: dev-unsubscr...@jackrabbit.apache.org

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

Reply via email to