mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Change query metric hash from MD5 of sql + name + loc to Checksum of sql
This commit is contained in:
@@ -20,7 +20,7 @@ public interface SpiQueryPlan {
|
||||
/**
|
||||
* The hash for the query plan.
|
||||
*/
|
||||
String getHash();
|
||||
long getHash();
|
||||
|
||||
/**
|
||||
* The SQL for the query plan.
|
||||
|
||||
@@ -197,7 +197,7 @@ class DumpMetricsJson implements ServerMetricsAsJson {
|
||||
metricStart(metric);
|
||||
appendTiming(metric);
|
||||
if (withHash) {
|
||||
appendExtra("hash", metric.getHash());
|
||||
keyVal("hash", metric.getHash());
|
||||
}
|
||||
if (isIncludeDetail(metric)) {
|
||||
appendExtra("loc", metric.getLocation());
|
||||
@@ -218,13 +218,14 @@ class DumpMetricsJson implements ServerMetricsAsJson {
|
||||
}
|
||||
|
||||
private void appendTiming(MetaTimedMetric timedMetric) throws IOException {
|
||||
key("count");
|
||||
val(timedMetric.getCount());
|
||||
key("total");
|
||||
val(timedMetric.getTotal());
|
||||
key("mean");
|
||||
val(timedMetric.getMean());
|
||||
key("max");
|
||||
val(timedMetric.getMax());
|
||||
keyVal("count", timedMetric.getCount());
|
||||
keyVal("total", timedMetric.getTotal());
|
||||
keyVal("mean", timedMetric.getMean());
|
||||
keyVal("max", timedMetric.getMax());
|
||||
}
|
||||
|
||||
private void keyVal(String key, long value) throws IOException {
|
||||
key(key);
|
||||
val(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.ProfileLocation;
|
||||
import io.ebeaninternal.server.util.Md5;
|
||||
import io.ebeaninternal.server.util.Checksum;
|
||||
|
||||
class DQueryPlanMeta {
|
||||
|
||||
@@ -10,7 +10,7 @@ class DQueryPlanMeta {
|
||||
private final ProfileLocation profileLocation;
|
||||
private final String name;
|
||||
private final String sql;
|
||||
private final String hash;
|
||||
private final long hash;
|
||||
|
||||
DQueryPlanMeta(Class<?> type, String label, ProfileLocation profileLocation, String sql) {
|
||||
this.type = type;
|
||||
@@ -22,22 +22,14 @@ class DQueryPlanMeta {
|
||||
name += "_" + label;
|
||||
}
|
||||
this.name = name;
|
||||
this.hash = initHash();
|
||||
}
|
||||
|
||||
private String initHash() {
|
||||
StringBuilder sb = new StringBuilder(sql).append("|").append(name);
|
||||
if (profileLocation != null) {
|
||||
sb.append("|").append(profileLocation.location());
|
||||
}
|
||||
return Md5.hash(sb.toString());
|
||||
this.hash = Checksum.checksum(sql);
|
||||
}
|
||||
|
||||
public Class<?> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getHash() {
|
||||
public long getHash() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ class DQueryPlanMetric implements QueryPlanMetric {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHash() {
|
||||
public long getHash() {
|
||||
return meta.getHash();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,12 +13,12 @@ import io.ebeaninternal.api.SpiQueryBindCapture;
|
||||
import io.ebeaninternal.api.SpiQueryPlan;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequest;
|
||||
import io.ebeaninternal.server.core.timezone.DataTimeZone;
|
||||
import io.ebeaninternal.server.util.Checksum;
|
||||
import io.ebeaninternal.server.util.Str;
|
||||
import io.ebeaninternal.server.query.CQueryPlanStats.Snapshot;
|
||||
import io.ebeaninternal.server.type.DataBind;
|
||||
import io.ebeaninternal.server.type.DataBindCapture;
|
||||
import io.ebeaninternal.server.type.RsetDataReader;
|
||||
import io.ebeaninternal.server.util.Md5;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -68,7 +68,7 @@ public class CQueryPlan implements SpiQueryPlan {
|
||||
private final boolean rawSql;
|
||||
|
||||
private final String sql;
|
||||
private final String hash;
|
||||
private final long hash;
|
||||
|
||||
private final String logWhereSql;
|
||||
|
||||
@@ -118,7 +118,7 @@ public class CQueryPlan implements SpiQueryPlan {
|
||||
this.stats = new CQueryPlanStats(this);
|
||||
this.dependentTables = sqlTree.dependentTables();
|
||||
this.bindCapture = initBindCapture(query);
|
||||
this.hash = md5Hash();
|
||||
this.hash = Checksum.checksum(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,7 +143,7 @@ public class CQueryPlan implements SpiQueryPlan {
|
||||
this.stats = new CQueryPlanStats(this);
|
||||
this.dependentTables = sqlTree.dependentTables();
|
||||
this.bindCapture = initBindCaptureRaw(sql, query);
|
||||
this.hash = md5Hash();
|
||||
this.hash = Checksum.checksum(sql);
|
||||
}
|
||||
|
||||
private String deriveName(String label, SpiQuery.Type type, String simpleName) {
|
||||
@@ -193,7 +193,7 @@ public class CQueryPlan implements SpiQueryPlan {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHash() {
|
||||
public long getHash() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
@@ -276,21 +276,6 @@ public class CQueryPlan implements SpiQueryPlan {
|
||||
return rawSql ? planKey.getPartialKey() + "_" + hash : planKey.getPartialKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the MD5 hash of the sql.
|
||||
*/
|
||||
private String md5Hash() {
|
||||
StringBuilder sb = new StringBuilder(sql)
|
||||
.append("|").append(name)
|
||||
.append("|").append(location);
|
||||
try {
|
||||
return Md5.hash(sb.toString());
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to MD5 hash the query", e);
|
||||
return "error";
|
||||
}
|
||||
}
|
||||
|
||||
SqlTree getSqlTree() {
|
||||
return sqlTree;
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ public final class CQueryPlanStats {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHash() {
|
||||
public long getHash() {
|
||||
return queryPlan.getHash();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,12 +16,11 @@ class DQueryPlanOutput implements MetaQueryPlan, SpiDbQueryPlan {
|
||||
private final String sql;
|
||||
private final String bind;
|
||||
private final String plan;
|
||||
|
||||
private String hash;
|
||||
private final long hash;
|
||||
private long queryTimeMicros;
|
||||
private long captureCount;
|
||||
|
||||
DQueryPlanOutput(Class<?> beanType, String label, String hash, String sql, ProfileLocation profileLocation, String bind, String plan) {
|
||||
DQueryPlanOutput(Class<?> beanType, String label, long hash, String sql, ProfileLocation profileLocation, String bind, String plan) {
|
||||
this.beanType = beanType;
|
||||
this.label = label;
|
||||
this.hash = hash;
|
||||
@@ -32,7 +31,7 @@ class DQueryPlanOutput implements MetaQueryPlan, SpiDbQueryPlan {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHash() {
|
||||
public long getHash() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package io.ebeaninternal.server.util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public final class Md5 {
|
||||
|
||||
/**
|
||||
* Return the MD5 hash of the underlying sql.
|
||||
*/
|
||||
public static String hash(String content) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
return digestToHex(md.digest(content.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("MD5 hashing failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the digest into a hex value.
|
||||
*/
|
||||
private static String digestToHex(byte[] digest) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte aDigest : digest) {
|
||||
sb.append(Integer.toString((aDigest & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user