Skip to content

Commit

Permalink
Made the id generation stable across runtimes.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixGV committed Aug 17, 2023
1 parent d6ab13f commit cf8bea2
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.linkedin.avroutil1.compatibility.AvroCompatibilityHelper;
import com.linkedin.avroutil1.compatibility.AvroVersion;
import com.linkedin.avroutil1.compatibility.AvscGenerationConfig;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.CodeSource;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -114,7 +117,19 @@ public static String fixSeparatorsToMatchOS(String path) {
public static int getSchemaFingerprint(Schema schema) {
Integer schemaId = SCHEMA_IDS_CACHE.get(schema);
if (schemaId == null) {
schemaId = schema.toString().hashCode();
String schemaString = AvroCompatibilityHelper.toAvsc(schema, AvscGenerationConfig.CORRECT_ONELINE);
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(schemaString.getBytes());
int scratchPad = 0;
for (int i = 0; i < digest.length; i++) {
scratchPad = (scratchPad * 256 + (digest[i] & 0xFF));
}
schemaId = scratchPad;
} catch (NoSuchAlgorithmException e) {
schemaId = schemaString.hashCode();
}

SCHEMA_IDS_CACHE.put(schema, schemaId);
}

Expand Down

0 comments on commit cf8bea2

Please sign in to comment.