mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Change from MD5 to Checksum (Adler32) for json content dirty detection
This commit is contained in:
@@ -7,7 +7,7 @@ import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.text.TextException;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
import io.ebeaninternal.server.util.Md5;
|
||||
import io.ebeaninternal.server.util.Checksum;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.SQLException;
|
||||
@@ -66,10 +66,14 @@ public class BeanPropertyJsonMapper extends BeanProperty {
|
||||
|
||||
private static class Md5MutableHash implements MutableHash {
|
||||
|
||||
private final String md5;
|
||||
private final String hash;
|
||||
|
||||
Md5MutableHash(String json) {
|
||||
md5 = Md5.hash(json);
|
||||
this.hash = hash(json);
|
||||
}
|
||||
|
||||
private String hash(String json) {
|
||||
return String.valueOf(Checksum.checksum(json));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,7 +83,7 @@ public class BeanPropertyJsonMapper extends BeanProperty {
|
||||
|
||||
@Override
|
||||
public boolean isEqualToJson(String json) {
|
||||
return Md5.hash(json).equals(md5);
|
||||
return hash(json).equals(hash);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.ebeaninternal.server.util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.Adler32;
|
||||
|
||||
/**
|
||||
* Compute a checksum for String content. Use when we desire cheaper option than MD5.
|
||||
*/
|
||||
public final class Checksum {
|
||||
|
||||
/**
|
||||
* Return the checksum for the given String input.
|
||||
*/
|
||||
public static long checksum(String input) {
|
||||
Adler32 adler32 = new Adler32();
|
||||
final byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
|
||||
adler32.update(bytes, 0, bytes.length);
|
||||
return adler32.getValue();
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,6 @@ public final class Md5 {
|
||||
* 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));
|
||||
|
||||
Reference in New Issue
Block a user