Skip to content
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

improved hash generation #20

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions polydata-sqlite/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> tags = buildTagIndex(request);
String tagString = buildTagIndexString(tags);
BasicPoly data = request.getData();
Expand Down Expand Up @@ -237,7 +238,7 @@ public BasicPolyList update(String dataset, Collection<InsertRequest> 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);
Expand Down Expand Up @@ -275,7 +276,7 @@ public BasicPolyList read(String dataset, Set<String> 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()) {
Expand All @@ -300,7 +301,7 @@ public BasicPolyList remove(String dataset, Set<String> 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);
Expand Down Expand Up @@ -514,4 +515,11 @@ private String buildTagIndexString(Set<String> indexToPersist) {
}
return tagString;
}

long genHash(String value) {
return new HashCodeBuilder(17, 37)
.append(value)
.toHashCode() & 0xffffffffL;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.io.File;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class PolydataSqliteTest {

@TempDir
Expand All @@ -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);
}

}