Skip to content

Commit

Permalink
Change field name from md5 to sha256
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 committed May 4, 2023
1 parent 76790bd commit 2b6cf50
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class DatasourceManifest {
private static final ParseField URL_FIELD = new ParseField("url");
private static final ParseField DB_NAME_FIELD = new ParseField("db_name");
private static final ParseField MD5_HASH_FIELD = new ParseField("md5_hash");
private static final ParseField SHA256_HASH_FIELD = new ParseField("sha256_hash");
private static final ParseField VALID_FOR_IN_DAYS_FIELD = new ParseField("valid_for_in_days");
private static final ParseField UPDATED_AT_FIELD = new ParseField("updated_at");
private static final ParseField PROVIDER_FIELD = new ParseField("provider");
Expand All @@ -56,10 +56,10 @@ public class DatasourceManifest {
*/
private String dbName;
/**
* @param md5Hash MD5 hash value of a database file
* @return MD5 hash value of a database file
* @param sha256Hash SHA256 hash value of a database file
* @return SHA256 hash value of a database file
*/
private String md5Hash;
private String sha256Hash;
/**
* @param validForInDays A duration in which the database file is valid to use
* @return A duration in which the database file is valid to use
Expand All @@ -85,17 +85,17 @@ public class DatasourceManifest {
args -> {
String url = (String) args[0];
String dbName = (String) args[1];
String md5hash = (String) args[2];
String sha256Hash = (String) args[2];
Long validForInDays = (Long) args[3];
Long updatedAt = (Long) args[4];
String provider = (String) args[5];
return new DatasourceManifest(url, dbName, md5hash, validForInDays, updatedAt, provider);
return new DatasourceManifest(url, dbName, sha256Hash, validForInDays, updatedAt, provider);
}
);
static {
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), URL_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DB_NAME_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), MD5_HASH_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), SHA256_HASH_FIELD);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VALID_FOR_IN_DAYS_FIELD);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), UPDATED_AT_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PROVIDER_FIELD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public boolean isExpired() {
*/
public void setDatabase(final DatasourceManifest datasourceManifest, final List<String> fields) {
this.database.setProvider(datasourceManifest.getProvider());
this.database.setMd5Hash(datasourceManifest.getMd5Hash());
this.database.setSha256Hash(datasourceManifest.getSha256Hash());
this.database.setUpdatedAt(Instant.ofEpochMilli(datasourceManifest.getUpdatedAt()));
this.database.setValidForInDays(datasourceManifest.getValidForInDays());
this.database.setFields(fields);
Expand Down Expand Up @@ -389,7 +389,7 @@ public boolean isCompatible(final List<String> fields) {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class Database implements ToXContent {
private static final ParseField PROVIDER_FIELD = new ParseField("provider");
private static final ParseField MD5_HASH_FIELD = new ParseField("md5_hash");
private static final ParseField SHA256_HASH_FIELD = new ParseField("sha256_hash");
private static final ParseField UPDATED_AT_FIELD = new ParseField("updated_at");
private static final ParseField UPDATED_AT_FIELD_READABLE = new ParseField("updated_at_field");
private static final ParseField FIELDS_FIELD = new ParseField("fields");
Expand All @@ -401,10 +401,10 @@ public static class Database implements ToXContent {
*/
private String provider;
/**
* @param md5Hash MD5 hash value of a database file
* @return MD5 hash value of a database file
* @param sha256Hash SHA256 hash value of a database file
* @return SHA256 hash value of a database file
*/
private String md5Hash;
private String sha256Hash;
/**
* @param updatedAt A date when the database was updated
* @return A date when the database was updated
Expand All @@ -426,16 +426,16 @@ public static class Database implements ToXContent {
true,
args -> {
String provider = (String) args[0];
String md5Hash = (String) args[1];
String sha256Hash = (String) args[1];
Instant updatedAt = args[2] == null ? null : Instant.ofEpochMilli((Long) args[2]);
Long validForInDays = (Long) args[3];
List<String> fields = (List<String>) args[4];
return new Database(provider, md5Hash, updatedAt, validForInDays, fields);
return new Database(provider, sha256Hash, updatedAt, validForInDays, fields);
}
);
static {
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PROVIDER_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), MD5_HASH_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), SHA256_HASH_FIELD);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), UPDATED_AT_FIELD);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VALID_FOR_IN_DAYS_FIELD);
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), FIELDS_FIELD);
Expand All @@ -447,8 +447,8 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa
if (provider != null) {
builder.field(PROVIDER_FIELD.getPreferredName(), provider);
}
if (md5Hash != null) {
builder.field(MD5_HASH_FIELD.getPreferredName(), md5Hash);
if (sha256Hash != null) {
builder.field(SHA256_HASH_FIELD.getPreferredName(), sha256Hash);
}
if (updatedAt != null) {
builder.timeField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private String setupIndex(final DatasourceManifest manifest, final Datasource da
*
* Update is needed when all following conditions are met
* 1. updatedAt value in datasource is equal or before updateAt value in manifest
* 2. MD5 hash value in datasource is different with MD5 hash value in manifest
* 2. SHA256 hash value in datasource is different with SHA256 hash value in manifest
*
* @param datasource
* @param manifest
Expand All @@ -214,7 +214,7 @@ private boolean shouldUpdate(final Datasource datasource, final DatasourceManife
return false;
}

if (manifest.getMd5Hash().equals(datasource.getDatabase().getMd5Hash())) {
if (manifest.getSha256Hash().equals(datasource.getDatabase().getSha256Hash())) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public Datasource randomDatasource() {
.setFields(Arrays.asList(GeospatialTestHelper.randomLowerCaseString(), GeospatialTestHelper.randomLowerCaseString()));
datasource.getDatabase().setProvider(GeospatialTestHelper.randomLowerCaseString());
datasource.getDatabase().setUpdatedAt(now);
datasource.getDatabase().setMd5Hash(GeospatialTestHelper.randomLowerCaseString());
datasource.getDatabase().setSha256Hash(GeospatialTestHelper.randomLowerCaseString());
datasource.getDatabase().setValidForInDays(Randomness.get().nextInt(30) + 1l);
datasource.getUpdateStats().setLastSkippedAt(now);
datasource.getUpdateStats().setLastSucceededAt(now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testGetDatabaseReader() throws Exception {
DatasourceManifest manifest = new DatasourceManifest(
zipFile.toURI().toURL().toExternalForm(),
"sample_valid.csv",
"fake_md5",
"fake_sha256",
1l,
Instant.now().toEpochMilli(),
"tester"
Expand All @@ -128,7 +128,7 @@ public void testGetDatabaseReaderNoFile() throws Exception {
DatasourceManifest manifest = new DatasourceManifest(
zipFile.toURI().toURL().toExternalForm(),
"no_file.csv",
"fake_md5",
"fake_sha256",
1l,
Instant.now().toEpochMilli(),
"tester"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testParser() throws Exception {
datasource.getDatabase().setFields(Arrays.asList("field1", "field2"));
datasource.getDatabase().setProvider("test_provider");
datasource.getDatabase().setUpdatedAt(Instant.now().truncatedTo(ChronoUnit.MILLIS));
datasource.getDatabase().setMd5Hash(GeospatialTestHelper.randomLowerCaseString());
datasource.getDatabase().setSha256Hash(GeospatialTestHelper.randomLowerCaseString());
datasource.getDatabase().setValidForInDays(1l);
datasource.getUpdateStats().setLastProcessingTimeInMillis(Randomness.get().nextLong());
datasource.getUpdateStats().setLastSucceededAt(Instant.now().truncatedTo(ChronoUnit.MILLIS));
Expand All @@ -56,7 +56,7 @@ public void testCurrentIndexName() {
Datasource datasource = new Datasource();
datasource.setName(id);
datasource.getDatabase().setProvider("provider");
datasource.getDatabase().setMd5Hash("md5Hash");
datasource.getDatabase().setSha256Hash("sha256Hash");
datasource.getDatabase().setUpdatedAt(now);
datasource.getDatabase().setValidForInDays(10l);
datasource.getDatabase().setFields(new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testUpdateDatasourceSkip() throws Exception {
datasource.setState(DatasourceState.AVAILABLE);
datasource.getUpdateStats().setLastSkippedAt(null);
datasource.getDatabase().setUpdatedAt(Instant.ofEpochMilli(manifest.getUpdatedAt()));
datasource.getDatabase().setMd5Hash(manifest.getMd5Hash());
datasource.getDatabase().setSha256Hash(manifest.getSha256Hash());
datasource.setEndpoint(manifestFile.toURI().toURL().toExternalForm());

// Run
Expand All @@ -69,7 +69,7 @@ public void testUpdateDatasourceInvalidFile() throws Exception {
Datasource datasource = new Datasource();
datasource.setState(DatasourceState.AVAILABLE);
datasource.getDatabase().setUpdatedAt(Instant.ofEpochMilli(manifest.getUpdatedAt() - 1));
datasource.getDatabase().setMd5Hash(manifest.getMd5Hash().substring(1));
datasource.getDatabase().setSha256Hash(manifest.getSha256Hash().substring(1));
datasource.getDatabase().setFields(Arrays.asList("country_name"));
datasource.setEndpoint(manifestFile.toURI().toURL().toExternalForm());

Expand All @@ -87,7 +87,7 @@ public void testUpdateDatasourceIncompatibleFields() throws Exception {
Datasource datasource = new Datasource();
datasource.setState(DatasourceState.AVAILABLE);
datasource.getDatabase().setUpdatedAt(Instant.ofEpochMilli(manifest.getUpdatedAt() - 1));
datasource.getDatabase().setMd5Hash(manifest.getMd5Hash().substring(1));
datasource.getDatabase().setSha256Hash(manifest.getSha256Hash().substring(1));
datasource.getDatabase().setFields(Arrays.asList("country_name", "additional_field"));
datasource.setEndpoint(manifestFile.toURI().toURL().toExternalForm());

Expand All @@ -105,7 +105,7 @@ public void testUpdateDatasource() throws Exception {
Datasource datasource = new Datasource();
datasource.setState(DatasourceState.AVAILABLE);
datasource.getDatabase().setUpdatedAt(Instant.ofEpochMilli(manifest.getUpdatedAt() - 1));
datasource.getDatabase().setMd5Hash(manifest.getMd5Hash().substring(1));
datasource.getDatabase().setSha256Hash(manifest.getSha256Hash().substring(1));
datasource.getDatabase().setFields(Arrays.asList("country_name"));
datasource.setEndpoint(manifestFile.toURI().toURL().toExternalForm());
datasource.getUpdateStats().setLastSucceededAt(null);
Expand All @@ -116,7 +116,7 @@ public void testUpdateDatasource() throws Exception {

// Verify
assertEquals(manifest.getProvider(), datasource.getDatabase().getProvider());
assertEquals(manifest.getMd5Hash(), datasource.getDatabase().getMd5Hash());
assertEquals(manifest.getSha256Hash(), datasource.getDatabase().getSha256Hash());
assertEquals(Instant.ofEpochMilli(manifest.getUpdatedAt()), datasource.getDatabase().getUpdatedAt());
assertEquals(manifest.getValidForInDays(), datasource.getDatabase().getValidForInDays());
assertNotNull(datasource.getUpdateStats().getLastSucceededAt());
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/ip2geo/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"url": "https://test.com/db.zip",
"db_name": "sample_valid.csv",
"md5_hash": "safasdfaskkkesadfasdf",
"sha256_hash": "safasdfaskkkesadfasdf",
"valid_for_in_days": 30,
"updated_at": 3134012341236,
"provider": "sample_provider"
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/ip2geo/manifest_invalid_url.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"url": "invalid://test.com/db.zip",
"db_name": "sample_valid.csv",
"md5_hash": "safasdfaskkkesadfasdf",
"sha256_hash": "safasdfaskkkesadfasdf",
"valid_for_in_days": 30,
"updated_at": 3134012341236,
"provider": "sample_provider"
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/ip2geo/manifest_template.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"url": "URL",
"db_name": "sample_valid.csv",
"md5_hash": "safasdfaskkkesadfasdf",
"sha256_hash": "safasdfaskkkesadfasdf",
"valid_for_in_days": 30,
"updated_at": 3134012341236,
"provider": "maxmind"
Expand Down

0 comments on commit 2b6cf50

Please sign in to comment.