From 0426d4a4ee2beac64a275494c9da52da6bb8a0b3 Mon Sep 17 00:00:00 2001 From: Laurent Goujon Date: Wed, 13 Jan 2021 10:03:27 -0800 Subject: [PATCH] Fix Java hashCode implementation (#7860) Summary: Classes ColumnFamilyHandle and CapturingWriteBatchHandler.Event have byte array fields as part of their identity, but they do not use the arrays' content to compute the instance's hash, and instead rely on the arrays' identity, causing instances to have different hashcodes although they are equal. The PR addresses it by using the arrays' content to compute the hash, like the equals method does. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7860 Reviewed By: jay-zhuang Differential Revision: D25901327 Pulled By: akankshamahajan15 fbshipit-source-id: 347e7b3d2ba7befe7faa956b033e6421b9d0c235 --- java/src/main/java/org/rocksdb/ColumnFamilyHandle.java | 4 +++- .../java/org/rocksdb/util/CapturingWriteBatchHandler.java | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/java/src/main/java/org/rocksdb/ColumnFamilyHandle.java b/java/src/main/java/org/rocksdb/ColumnFamilyHandle.java index 0f1ed3e00..1ac0a35bb 100644 --- a/java/src/main/java/org/rocksdb/ColumnFamilyHandle.java +++ b/java/src/main/java/org/rocksdb/ColumnFamilyHandle.java @@ -115,7 +115,9 @@ public class ColumnFamilyHandle extends RocksObject { @Override public int hashCode() { try { - return Objects.hash(getName(), getID(), rocksDB_.nativeHandle_); + int result = Objects.hash(getID(), rocksDB_.nativeHandle_); + result = 31 * result + Arrays.hashCode(getName()); + return result; } catch (RocksDBException e) { throw new RuntimeException("Cannot calculate hash code of column family handle", e); } diff --git a/java/src/test/java/org/rocksdb/util/CapturingWriteBatchHandler.java b/java/src/test/java/org/rocksdb/util/CapturingWriteBatchHandler.java index 890819471..f80e69c1c 100644 --- a/java/src/test/java/org/rocksdb/util/CapturingWriteBatchHandler.java +++ b/java/src/test/java/org/rocksdb/util/CapturingWriteBatchHandler.java @@ -156,8 +156,10 @@ public class CapturingWriteBatchHandler extends WriteBatch.Handler { @Override public int hashCode() { - - return Objects.hash(action, columnFamilyId, key, value); + int result = Objects.hash(action, columnFamilyId); + result = 31 * result + Arrays.hashCode(key); + result = 31 * result + Arrays.hashCode(value); + return result; } }