package com.scylladb.tools;

import java.util.ArrayList;
import java.util.List;

public class Metrics {
    public long statementsSent;
    public long statementsFailed;
    public long statementsProcessed;
    public long batchesProcessed;
    public long keysProcessed;
    public long preparationsDone;
    public long preparationsFailed;
    public long typesCreated;

    private List<Metrics> children;

    public Metrics() {
    }

    public Metrics(Metrics m) {
        this.statementsSent = m.statementsSent;
        this.statementsFailed = m.statementsFailed;
        this.statementsProcessed = m.statementsProcessed;
        this.batchesProcessed = m.batchesProcessed;
        this.keysProcessed = m.keysProcessed;
        this.preparationsDone = m.preparationsDone;
        this.preparationsFailed = m.preparationsFailed;
        this.typesCreated = m.typesCreated;
    }

    public void add(Metrics m) {
        this.statementsSent += m.statementsSent;
        this.statementsFailed += m.statementsFailed;
        this.statementsProcessed += m.statementsProcessed;
        this.batchesProcessed += m.batchesProcessed;
        this.keysProcessed += m.keysProcessed;
        this.preparationsDone += m.preparationsDone;
        this.preparationsFailed += m.preparationsFailed;
        this.typesCreated += m.typesCreated;
    }

    public synchronized Metrics newChild() {
        Metrics m = new Metrics();
        if (children == null) {
            children = new ArrayList<>();
        }
        children.add(m);
        return m;
    }

    public Metrics sum() {
        if (children == null) {
            return this;
        }
        Metrics m = new Metrics(this);
        synchronized (this) {
            for (Metrics o : children) {
                m.add(o);
            }
        }
        return m;
    }
}
@@ -74,16 +74,110 @@
 .List;%0A%0A
+/**%0A * Non thread-safe metrics amalgation of sstable loader stats.%0A *%0A * @author calle%0A *%0A */%0A
 public c
@@ -1438,228 +1438,711 @@
     
-public synchronized Metrics newChild() %7B%0A        Metrics m = new Metrics();%0A        if (children == null) %7B%0A            children = new ArrayList%3C%3E();%0A        %7D%0A        children.add(m);%0A        return m;%0A    %7D%0A%0A    public
+/**%0A     * Allow the concept of %22children%22 to enable sub-processes (threads) to have%0A     * their own metrics than can be updated in a singlular path, yet be%0A     * summarized (potentially inexact) by us%0A     */%0A    public synchronized Metrics newChild() %7B%0A        Metrics m = new Metrics();%0A        if (children == null) %7B%0A            children = new ArrayList%3C%3E();%0A        %7D%0A        children.add(m);%0A        return m;%0A    %7D%0A%0A    /**%0A     * Generate a new metrics instance with the (inexact) sum of me and all my%0A     * children. We only sync the children array really, all values are picked%0A     * up as perceived by us, potentially slightly askew.%0A     *%0A     * @return%0A     */%0A    public synchronized
  Met
@@ -2264,42 +2264,8 @@
 s);%0A
-        synchronized (this) %7B%0A    
     
@@ -2309,20 +2309,16 @@
         
-    
 m.add(o)
@@ -2323,22 +2323,8 @@
 o);%0A
-            %7D%0A
     

Add metrics comments + more coarse sync