diff --git a/polydata-sqlite/build.gradle b/polydata-sqlite/build.gradle index c731585..81ffe92 100644 --- a/polydata-sqlite/build.gradle +++ b/polydata-sqlite/build.gradle @@ -6,7 +6,7 @@ dependencies { "com.fasterxml.jackson.core:jackson-core:$jacksonVersion", "org.xerial:sqlite-jdbc:$sqliteJdbcVersion", "org.flywaydb:flyway-core:$flywayCoreVersion", - "org.apache.commons:commons-text:$commonsTextVersion" - + "org.apache.commons:commons-text:$commonsTextVersion", + "commons-codec:commons-codec:$commonsCodecVersion" ) } diff --git a/polydata-sqlite/src/main/java/com/unidev/polydata4/sqlite/PolydataSqlite.java b/polydata-sqlite/src/main/java/com/unidev/polydata4/sqlite/PolydataSqlite.java index fbfe353..22c1a50 100644 --- a/polydata-sqlite/src/main/java/com/unidev/polydata4/sqlite/PolydataSqlite.java +++ b/polydata-sqlite/src/main/java/com/unidev/polydata4/sqlite/PolydataSqlite.java @@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.flywaydb.core.Flyway; import org.sqlite.SQLiteDataSource; @@ -166,7 +167,7 @@ public BasicPolyList insert(String dataset, InsertOptions insertOptions, Collect for (InsertRequest request : toInsert) { String id = request.getData()._id(); - int id_n = id.hashCode(); + long id_n = genHash(id); Set tags = buildTagIndex(request); String tagString = buildTagIndexString(tags); BasicPoly data = request.getData(); @@ -237,7 +238,7 @@ public BasicPolyList update(String dataset, Collection updateRequ BasicPoly data = request.getData(); data.put(INDEXES, tags); String id = data._id(); - int id_n = id.hashCode(); + long id_n = genHash(id); String jsonData = objectMapper.writeValueAsString(data); preparedStatement.setString(1, jsonData); preparedStatement.setString(2, tagString); @@ -275,7 +276,7 @@ public BasicPolyList read(String dataset, Set ids) { .prepareStatement("SELECT data FROM data WHERE _id_n IN ( " + q + ") ; "); int i = 1; for (String id : ids) { - preparedStatement.setLong(i++, id.hashCode()); + preparedStatement.setLong(i++, genHash(id)); } ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { @@ -300,7 +301,7 @@ public BasicPolyList remove(String dataset, Set ids) { .prepareStatement("DELETE FROM data WHERE _id_n IN (" + q + ") ; "); int i = 1; for (String id : ids) { - preparedStatement.setLong(i++, id.hashCode()); + preparedStatement.setLong(i++, genHash(id)); } long removedRows = preparedStatement.executeUpdate(); log.info("Removed {} rows", removedRows); @@ -514,4 +515,11 @@ private String buildTagIndexString(Set indexToPersist) { } return tagString; } + + long genHash(String value) { + return new HashCodeBuilder(17, 37) + .append(value) + .toHashCode() & 0xffffffffL; + } + } diff --git a/polydata-sqlite/src/test/java/com/unidev/polydata4/sqlite/PolydataSqliteTest.java b/polydata-sqlite/src/test/java/com/unidev/polydata4/sqlite/PolydataSqliteTest.java index 30c3208..99ccf1c 100644 --- a/polydata-sqlite/src/test/java/com/unidev/polydata4/sqlite/PolydataSqliteTest.java +++ b/polydata-sqlite/src/test/java/com/unidev/polydata4/sqlite/PolydataSqliteTest.java @@ -7,6 +7,8 @@ import java.io.File; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class PolydataSqliteTest { @TempDir @@ -26,4 +28,11 @@ void polyCreation() { polydata.create("test"); } + @Test + void genHash() { + long v1 = polydata.genHash("123"); + long v2 = polydata.genHash("123"); + assertEquals(v1, v2); + } + }