-
Notifications
You must be signed in to change notification settings - Fork 34
Update facet id generation to match mixer #485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
n-h-diaz
wants to merge
4
commits into
datacommonsorg:master
Choose a base branch
from
n-h-diaz:facet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
pipeline/data/src/main/java/org/datacommons/ingestion/data/DataUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package org.datacommons.ingestion.data; | ||
|
|
||
| import com.google.common.base.Joiner; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| /** Util functions for the pipeline data model. */ | ||
| public class DataUtils { | ||
|
|
||
| // Standard FNV-1a 32-bit constants | ||
| private static final int FNV_32_INIT = 0x811c9dc5; | ||
| private static final int FNV_32_PRIME = 0x01000193; | ||
|
|
||
| /** | ||
| * Generates a consistent facet ID using the FNV-1a 32-bit hash algorithm. | ||
| * | ||
| * <p>This is designed to replicate the legacy Go facet ID generation implementation in Mixer's | ||
| * GetFacetID function. See | ||
| * https://github.com/datacommonsorg/mixer/blob/0618c1f3ef80703c98fc97f6c6c6e5cd3d7c13d3/internal/util/util.go#L497-L515 | ||
| * | ||
| * @param importName The name of the import this observation belongs to. | ||
| * @param measurementMethod The measurement method of the observation. | ||
| * @param observationPeriod The observation period of the observation. | ||
| * @param scalingFactor The scaling factor of the observation. | ||
| * @param unit The unit of the observation. | ||
| * @param isDcAggregate Whether the observation is a DC aggregate. | ||
| * @return A consistent facet ID string. | ||
| */ | ||
| public static String generateFacetId( | ||
| String importName, | ||
| String measurementMethod, | ||
| String observationPeriod, | ||
| String scalingFactor, | ||
| String unit, | ||
| boolean isDcAggregate) { | ||
| // Only include fields that are set in hash. | ||
| // This is so the hashes stay consistent if more fields are added. | ||
| String s = | ||
| Joiner.on("-").join(importName, measurementMethod, observationPeriod, scalingFactor, unit); | ||
| if (isDcAggregate) { | ||
| s += "-IsDcAggregate"; | ||
| } | ||
|
|
||
| int hash = fnv1a32(s); | ||
|
|
||
| // Go's fmt.Sprint on a uint32 treats it as unsigned. | ||
| // We must do the same in Java to avoid negative string values. | ||
| return Integer.toUnsignedString(hash); | ||
| } | ||
|
|
||
| /** | ||
| * Computes the 32-bit FNV-1a hash of a string. | ||
| * | ||
| * <p>Note: Java does not provide a built-in FNV-1a implementation, so we implement it manually | ||
| * here. | ||
| * | ||
| * @param data The input string to hash. | ||
| * @return The FNV-1a 32-bit hash as an integer. | ||
| */ | ||
| private static int fnv1a32(String data) { | ||
| int hash = FNV_32_INIT; | ||
| for (byte b : data.getBytes(StandardCharsets.UTF_8)) { | ||
| hash ^= (b & 0xff); // Bitwise XOR with the unsigned byte value | ||
| hash *= FNV_32_PRIME; | ||
| } | ||
| return hash; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
pipeline/data/src/test/java/org/datacommons/ingestion/data/DataUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package org.datacommons.ingestion.data; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
| import org.junit.runners.Parameterized.Parameters; | ||
|
|
||
| @RunWith(Parameterized.class) | ||
| public class DataUtilsTest { | ||
|
|
||
| private final String expectedId; | ||
| private final String importName; | ||
| private final String measurementMethod; | ||
| private final String observationPeriod; | ||
| private final String scalingFactor; | ||
| private final String unit; | ||
| private final boolean isDcAggregate; | ||
|
|
||
| public DataUtilsTest( | ||
| String expectedId, | ||
| String importName, | ||
| String measurementMethod, | ||
| String observationPeriod, | ||
| String scalingFactor, | ||
| String unit, | ||
| boolean isDcAggregate) { | ||
| this.expectedId = expectedId; | ||
| this.importName = importName; | ||
| this.measurementMethod = measurementMethod; | ||
| this.observationPeriod = observationPeriod; | ||
| this.scalingFactor = scalingFactor; | ||
| this.unit = unit; | ||
| this.isDcAggregate = isDcAggregate; | ||
| } | ||
|
|
||
| // This method provides the data for the test below | ||
| @Parameters(name = "Test {index}: expected {0} for {1}") | ||
| public static Collection<Object[]> data() { | ||
| return Arrays.asList( | ||
| new Object[][] { | ||
| // Format: expectedId, importName, measurementMethod, observationPeriod, scalingFactor, | ||
| // unit, isDcAggregate | ||
| {"3981252704", "WorldDevelopmentIndicators", "", "P1Y", "", "", false}, | ||
| { | ||
| "10983471", | ||
| "CensusACS5YearSurvey_SubjectTables_S2601A", | ||
| "CensusACS5yrSurveySubjectTable", | ||
| "", | ||
| "", | ||
| "", | ||
| false | ||
| }, | ||
| {"2825511676", "CDC_Mortality_UnderlyingCause", "", "", "", "", false}, | ||
| {"1226172227", "CensusACS1YearSurvey", "CensusACS1yrSurvey", "", "", "", false}, | ||
| {"2176550201", "USCensusPEP_Annual_Population", "CensusPEPSurvey", "P1Y", "", "", false}, | ||
| {"2645850372", "CensusACS5YearSurvey_AggCountry", "CensusACS5yrSurvey", "", "", "", true}, | ||
| { | ||
| "1541763368", | ||
| "USDecennialCensus_RedistrictingRelease", | ||
| "USDecennialCensus", | ||
| "", | ||
| "", | ||
| "", | ||
| false | ||
| }, | ||
| { | ||
| "4181918134", | ||
| "OECDRegionalDemography_Population", | ||
| "OECDRegionalStatistics", | ||
| "P1Y", | ||
| "", | ||
| "", | ||
| false | ||
| }, | ||
| { | ||
| "1964317807", | ||
| "CensusACS5YearSurvey_SubjectTables_S0101", | ||
| "CensusACS5yrSurveySubjectTable", | ||
| "", | ||
| "", | ||
| "", | ||
| false | ||
| }, | ||
| {"2517965213", "CensusPEP", "CensusPEPSurvey", "", "", "", false} | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGenerateFacetId() { | ||
| String facetId = | ||
| DataUtils.generateFacetId( | ||
| importName, measurementMethod, observationPeriod, scalingFactor, unit, isDcAggregate); | ||
|
|
||
| assertEquals(expectedId, facetId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.