ttomcat-1778514358873.zip-extract/_dependencies/maven/com.h2database_h2-2.2.220/org/h2/engine/QueryStatisticsData.java

Path
ttomcat-1778514358873.zip-extract/_dependencies/maven/com.h2database_h2-2.2.220/org/h2/engine/QueryStatisticsData.java
Status
scanned
Type
file
Name
QueryStatisticsData.java
Extension
.java
Programming language
Java
Mime type
text/x-java
File type
Java source, ASCII text
Tag

      
    
Rootfs path

      
    
Size
5969 (5.8 KB)
MD5
7f6623ce60d0cee8f37571fa46e3a3a7
SHA1
e94899851ef9da1abca7b19bdc27f988e4338601
SHA256
56213bfb97b9a158a1773cb43ae13d6e725129a16d61ac10662437a956457d90
SHA512

      
    
SHA1_git
dd651f6149a6ff1b057da6882e47c6f7007067e1
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
QueryStatisticsData.java | 5.8 KB |

/* * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (https://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.engine; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; /** * Maintains query statistics. */ public class QueryStatisticsData { private static final Comparator<QueryEntry> QUERY_ENTRY_COMPARATOR = Comparator.comparingLong(q -> q.lastUpdateTime); private final HashMap<String, QueryEntry> map = new HashMap<>(); private int maxQueryEntries; public QueryStatisticsData(int maxQueryEntries) { this.maxQueryEntries = maxQueryEntries; } public synchronized void setMaxQueryEntries(int maxQueryEntries) { this.maxQueryEntries = maxQueryEntries; } public synchronized List<QueryEntry> getQueries() { // return a copy of the map so we don't have to // worry about external synchronization ArrayList<QueryEntry> list = new ArrayList<>(map.values()); // only return the newest 100 entries list.sort(QUERY_ENTRY_COMPARATOR); return list.subList(0, Math.min(list.size(), maxQueryEntries)); } /** * Update query statistics. * * @param sqlStatement the statement being executed * @param executionTimeNanos the time in nanoseconds the query/update took * to execute * @param rowCount the query or update row count */ public synchronized void update(String sqlStatement, long executionTimeNanos, long rowCount) { QueryEntry entry = map.get(sqlStatement); if (entry == null) { entry = new QueryEntry(sqlStatement); map.put(sqlStatement, entry); } entry.update(executionTimeNanos, rowCount); // Age-out the oldest entries if the map gets too big. // Test against 1.5 x max-size so we don't do this too often if (map.size() > maxQueryEntries * 1.5f) { // Sort the entries by age ArrayList<QueryEntry> list = new ArrayList<>(map.values()); list.sort(QUERY_ENTRY_COMPARATOR); // Create a set of the oldest 1/3 of the entries HashSet<QueryEntry> oldestSet = new HashSet<>(list.subList(0, list.size() / 3)); // Loop over the map using the set and remove // the oldest 1/3 of the entries. for (Iterator<Entry<String, QueryEntry>> it = map.entrySet().iterator(); it.hasNext();) { Entry<String, QueryEntry> mapEntry = it.next(); if (oldestSet.contains(mapEntry.getValue())) { it.remove(); } } } } /** * The collected statistics for one query. */ public static final class QueryEntry { /** * The SQL statement. */ public final String sqlStatement; /** * The number of times the statement was executed. */ public int count; /** * The last time the statistics for this entry were updated, * in milliseconds since 1970. */ public long lastUpdateTime; /** * The minimum execution time, in nanoseconds. */ public long executionTimeMinNanos; /** * The maximum execution time, in nanoseconds. */ public long executionTimeMaxNanos; /** * The total execution time. */ public long executionTimeCumulativeNanos; /** * The minimum number of rows. */ public long rowCountMin; /** * The maximum number of rows. */ public long rowCountMax; /** * The total number of rows. */ public long rowCountCumulative; /** * The mean execution time. */ public double executionTimeMeanNanos; /** * The mean number of rows. */ public double rowCountMean; // Using Welford's method, see also // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance // https://www.johndcook.com/blog/standard_deviation/ private double executionTimeM2Nanos; private double rowCountM2; public QueryEntry(String sql) { this.sqlStatement = sql; } /** * Update the statistics entry. * * @param timeNanos the execution time in nanos * @param rows the number of rows */ void update(long timeNanos, long rows) { count++; executionTimeMinNanos = Math.min(timeNanos, executionTimeMinNanos); executionTimeMaxNanos = Math.max(timeNanos, executionTimeMaxNanos); rowCountMin = Math.min(rows, rowCountMin); rowCountMax = Math.max(rows, rowCountMax); double rowDelta = rows - rowCountMean; rowCountMean += rowDelta / count; rowCountM2 += rowDelta * (rows - rowCountMean); double timeDelta = timeNanos - executionTimeMeanNanos; executionTimeMeanNanos += timeDelta / count; executionTimeM2Nanos += timeDelta * (timeNanos - executionTimeMeanNanos); executionTimeCumulativeNanos += timeNanos; rowCountCumulative += rows; lastUpdateTime = System.currentTimeMillis(); } public double getExecutionTimeStandardDeviation() { // population standard deviation return Math.sqrt(executionTimeM2Nanos / count); } public double getRowCountStandardDeviation() { // population standard deviation return Math.sqrt(rowCountM2 / count); } } }
Detected license expression

      
    
Detected license expression (SPDX)

      
    
Percentage of license text
1.97
Copyrights
- end_line: 2
  copyright: Copyright 2004-2023 H2 Group. Multiple-Licensed
  start_line: 2
Holders
- holder: H2 Group. Multiple-Licensed
  end_line: 2
  start_line: 2
Authors

      
    
License expression License clue details
(mpl-2.0 OR epl-1.0) AND proprietary-license {'score': 20.37, 'matcher': '3-seq', 'end_line': 3, 'rule_url': 'https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/rules/mpl-2.0_or_epl-1.0_and_proprietary-license_2.RULE', 'from_file': None, 'start_line': 2, 'matched_text': ' * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,\n * and the EPL 1.0 (https://h2database.com/html/license.html).', 'match_coverage': 20.37, 'matched_length': 11, 'rule_relevance': 100, 'rule_identifier': 'mpl-2.0_or_epl-1.0_and_proprietary-license_2.RULE', 'license_expression': '(mpl-2.0 OR epl-1.0) AND proprietary-license', 'license_expression_spdx': '(MPL-2.0 OR EPL-1.0) AND LicenseRef-scancode-proprietary-license'}
URL Start line End line
https://h2database.com/html/license.html 3 3
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance 144 144
https://www.johndcook.com/blog/standard_deviation/ 145 145
Package URL License Primary language
pkg:osgi/com.h2database.source@2.2.220