diff --git a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectory.java b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectory.java index a92b366b4c..0123678a3c 100644 --- a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectory.java +++ b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectory.java @@ -710,7 +710,8 @@ public Object getValue() { return value; } - protected static boolean areEqual(Object o1, Object o2) { + @SuppressWarnings("PMD.CompareObjectsWithEquals") // we use ref + protected static boolean areEqual(@Nullable Object o1, @Nullable Object o2) { if (o1 == null) { return o2 == null; } else { @@ -719,6 +720,12 @@ protected static boolean areEqual(Object o1, Object o2) { } } + // Handle ANY_VALUE specially - typeOf does not support ANY_VALUE + boolean isAnyValue = (o1 == ANY_VALUE || o2 == ANY_VALUE); + if (isAnyValue) { + return Objects.equals(o1, o2); + } + KeyType o1Type = KeyType.typeOf(o1); if (o1Type != KeyType.typeOf(o2)) { return false; @@ -740,6 +747,31 @@ protected static boolean areEqual(Object o1, Object o2) { } } + protected static int valueHashCode(@Nullable Object value) { + if (value == null) { + return 0; + } + + // Handle ANY_VALUE specially + if (value == ANY_VALUE) { + return System.identityHashCode(value); + } + + switch (KeyType.typeOf(value)) { + case BYTES: + return Arrays.hashCode((byte[]) value); + case LONG: + case STRING: + case FLOAT: + case DOUBLE: + case BOOLEAN: + case UUID: + return Objects.hashCode(value); + default: + throw new RecordCoreException("Unexpected key type " + KeyType.typeOf(value)); + } + } + /** * Returns the path that leads up to this directory (including this directory), and returns it as a string * that looks something like a filesystem path. diff --git a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePathImpl.java b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePathImpl.java index 7102da8936..73f8b86151 100644 --- a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePathImpl.java +++ b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePathImpl.java @@ -276,27 +276,21 @@ public boolean equals(Object obj) { } KeySpacePath that = (KeySpacePath) obj; - // Check that the KeySpaceDirectories of the two paths are "equal enough". - // Even this is probably overkill since the isCompatible check in KeySpaceDirectory - // will keep us from doing anything too bad. We could move this check into KeySpaceDirectory - // but comparing two directories by value would necessitate traversing the entire directory - // tree, so instead we will use a narrower definition of equality here. - boolean directoriesEqual = Objects.equals(this.getDirectory().getKeyType(), that.getDirectory().getKeyType()) && - Objects.equals(this.getDirectory().getName(), that.getDirectory().getName()) && - Objects.equals(this.getDirectory().getValue(), that.getDirectory().getValue()); + // Directories use reference equality, because the expected usage is that they go into a + // singleton KeySpace. + boolean directoriesEqual = this.getDirectory().equals(that.getDirectory()); + // the values might be byte[] return directoriesEqual && - Objects.equals(this.getValue(), that.getValue()) && - Objects.equals(this.getParent(), that.getParent()); + KeySpaceDirectory.areEqual(this.getValue(), that.getValue()) && + Objects.equals(this.getParent(), that.getParent()); } @Override public int hashCode() { return Objects.hash( - getDirectory().getKeyType(), - getDirectory().getName(), - getDirectory().getValue(), - getValue(), + getDirectory(), + KeySpaceDirectory.valueHashCode(getValue()), parent); } diff --git a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/PathValue.java b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/PathValue.java index 5de09bd396..2582654572 100644 --- a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/PathValue.java +++ b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/PathValue.java @@ -24,13 +24,14 @@ import javax.annotation.Nullable; import java.util.Arrays; +import java.util.Objects; /** * A class to represent the value stored at a particular element of a {@link KeySpacePath}. The resolvedValue * is the object that will appear in the {@link com.apple.foundationdb.tuple.Tuple} when * {@link KeySpacePath#toTuple(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)} is invoked. * The metadata is left null by {@link KeySpaceDirectory} but other implementations may make use of - * it (e.g. {@link DirectoryLayerDirectory}. + * it (e.g. {@link DirectoryLayerDirectory}). */ @API(API.Status.UNSTABLE) public class PathValue { @@ -69,4 +70,22 @@ public Object getResolvedValue() { public byte[] getMetadata() { return metadata == null ? null : Arrays.copyOf(metadata, metadata.length); } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof PathValue)) { + return false; + } + PathValue that = (PathValue) other; + return KeySpaceDirectory.areEqual(this.resolvedValue, that.resolvedValue) && + Arrays.equals(this.metadata, that.metadata); + } + + @Override + public int hashCode() { + return Objects.hash(KeySpaceDirectory.valueHashCode(resolvedValue), Arrays.hashCode(metadata)); + } } diff --git a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/ResolvedKeySpacePath.java b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/ResolvedKeySpacePath.java index f07629954e..0afd60db10 100644 --- a/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/ResolvedKeySpacePath.java +++ b/fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/ResolvedKeySpacePath.java @@ -229,13 +229,15 @@ public boolean equals(Object other) { } ResolvedKeySpacePath otherPath = (ResolvedKeySpacePath) other; - return this.inner.equals(otherPath.inner) - && Objects.equals(this.getResolvedValue(), otherPath.getResolvedValue()); + return Objects.equals(this.getResolvedPathValue(), otherPath.getResolvedPathValue()) && + Objects.equals(this.getParent(), otherPath.getParent()) && + this.inner.equals(otherPath.inner) && + Objects.equals(this.remainder, otherPath.remainder); } @Override public int hashCode() { - return Objects.hash(inner, getResolvedPathValue()); + return Objects.hash(inner, getResolvedPathValue(), remainder, getParent()); } @Override diff --git a/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectoryTest.java b/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectoryTest.java index 0263ca9bf4..675cad4cbe 100644 --- a/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectoryTest.java +++ b/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpaceDirectoryTest.java @@ -110,11 +110,16 @@ public KeyTypeValue(KeyType keyType, @Nullable Object value, @Nullable Object va assertTrue(keyType.isMatch(value)); assertTrue(keyType.isMatch(generator.get())); } + + @Override + public String toString() { + return "KeyTypeValue{" + keyType + '}'; + } } - private final Random random = new Random(); + private static final Random random = new Random(); - private final List valueOfEveryType = new ImmutableList.Builder() + private static final List valueOfEveryType = new ImmutableList.Builder() .add(new KeyTypeValue(KeyType.NULL, null, null, () -> null)) .add(new KeyTypeValue(KeyType.BYTES, new byte[] { 0x01, 0x02 }, new byte[] { 0x03, 0x04 }, () -> { int size = random.nextInt(10) + 1; @@ -1224,12 +1229,6 @@ public TestWrapper1(KeySpacePath inner) { } } - private static class TestWrapper2 extends KeySpacePathWrapper { - public TestWrapper2(KeySpacePath inner) { - super(inner); - } - } - @Test public void testListConstantValue() { // Create a root directory called "a" with subdirs of every type and a constant value @@ -1493,6 +1492,65 @@ public void testPathCompareByValue() { assertEquals(p1.hashCode(), sameAsP1.hashCode(), "they have the same hash code"); } + /** + * {@code KeySpaceDirectory}s are supposed to be inserted into a singleton {@link KeySpace}, thus we can use + * reference equality to do comparisons. This is particularly important for the efficiency of + * {@link KeySpacePathImpl#equals(Object)}, because we don't want it to have to re-compare all of the children of the + * directory as you go up through the parents. If using reference equality turns out to be problematic, + * we'll want to look at other solutions, such as ignoring the hierarchy, or something more tricky. + */ + @Test + void testKeySpaceDirectoryEqualsUsesReferenceEquality() { + // Create two directories with identical properties + KeySpaceDirectory dir1 = new KeySpaceDirectory("test", KeyType.STRING, "value"); + KeySpaceDirectory dir2 = new KeySpaceDirectory("test", KeyType.STRING, "value"); + + // KeySpaceDirectory.equals should use reference equality + assertEquals(dir1, dir1, "Directory should equal itself"); + assertNotEquals(dir1, dir2, "Directories with same properties should not be equal (reference equality)"); + + // Test with different properties + KeySpaceDirectory dir3 = new KeySpaceDirectory("different", KeyType.LONG, 42L); + assertNotEquals(dir1, dir3, "Directories with different properties should not be equal"); + + // Test with null + assertNotEquals(dir1, null, "Directory should not equal null, and calling with null shouldn't error"); + + // Test with different object type + assertNotEquals(dir1, "not a directory", "Directory should not equal a different type"); + } + + @Test + void testKeySpaceDirectoryHashCodeFollowsReferenceSemantics() { + // Create two directories with identical properties + KeySpaceDirectory dir1 = new KeySpaceDirectory("test", KeyType.STRING, "value"); + KeySpaceDirectory dir2 = new KeySpaceDirectory("test", KeyType.STRING, "value"); + + // Since equals uses reference equality, hashCode should be consistent with that + // (i.e., objects that are equal should have the same hashCode, but since these + // objects are not equal by reference, their hashCodes may differ) + + // The same object should always have the same hashCode + int hashCode1 = dir1.hashCode(); + assertEquals(hashCode1, dir1.hashCode(), "Same object should produce same hashCode"); + + // Different instances (even with same properties) may have different hashCodes + // We can't assert they're different, but we can verify the hashCode is stable + int hashCode2 = dir2.hashCode(); + assertEquals(hashCode2, dir2.hashCode(), "Same object should produce same hashCode"); + + // Test that hashCode is consistent across multiple calls + for (int i = 0; i < 10; i++) { + assertEquals(hashCode1, dir1.hashCode(), "hashCode should be stable across calls"); + assertEquals(hashCode2, dir2.hashCode(), "hashCode should be stable across calls"); + } + // two difference references may have the same hash code, but eventually we should find a different one, even + // though all properties are the same + for (int i = 0; i < 100; i++) { + assertNotEquals(hashCode1, new KeySpaceDirectory("test", KeyType.STRING, "value").hashCode()); + } + } + private List resolveBatch(FDBRecordContext context, String... names) { List> futures = new ArrayList<>(); for (String name : names) { diff --git a/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/PathValueTest.java b/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/PathValueTest.java new file mode 100644 index 0000000000..e23bd1e153 --- /dev/null +++ b/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/PathValueTest.java @@ -0,0 +1,96 @@ +/* + * PathValueTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2015-2025 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.foundationdb.record.provider.foundationdb.keyspace; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +/** + * Tests for {@link PathValue}. + */ +class PathValueTest { + + /** + * Test data for PathValue equality tests. + */ + static Stream equalPathValuePairs() { + return Stream.of( + Arguments.of("null values", null, null, null, null), + Arguments.of("same string values", "test", null, "test", null), + Arguments.of("same long values", 42L, null, 42L, null), + Arguments.of("same boolean values", true, null, true, null), + Arguments.of("same byte[] values", new byte[] {1, 2, 3}, null, new byte[] {1, 2, 3}, null), + Arguments.of("same metadata", "test", new byte[]{1, 2, 3}, "test", new byte[]{1, 2, 3}) + ); + } + + /** + * Test data for PathValue inequality tests. + */ + static Stream unequalPathValuePairs() { + return Stream.of( + Arguments.of("different string values", "test1", null, "test2", null), + Arguments.of("different long values", 42L, null, 100L, null), + Arguments.of("different boolean values", true, null, false, null), + Arguments.of("different types", "string", null, 42L, null), + Arguments.of("different metadata", "test", new byte[]{1, 2, 3}, "test", new byte[]{4, 5, 6}), + Arguments.of("one null metadata", "test", new byte[]{1, 2, 3}, "test", null), + Arguments.of("one null value", null, null, "test", null), + Arguments.of("different value with same metadata", "test1", new byte[]{1, 2, 3}, "test2", new byte[]{1, 2, 3}) + ); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("equalPathValuePairs") + void testEqualsAndHashCodeForEqualValues(String description, Object resolvedValue1, byte[] metadata1, + Object resolvedValue2, byte[] metadata2) { + PathValue value1 = new PathValue(resolvedValue1, metadata1); + PathValue value2 = new PathValue(resolvedValue2, metadata2); + + assertEquals(value1, value2, "PathValues should be equal: " + description); + assertEquals(value1.hashCode(), value2.hashCode(), "Equal PathValues should have equal hash codes: " + description); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("unequalPathValuePairs") + void testNotEqualsForUnequalValues(String description, Object resolvedValue1, byte[] metadata1, + Object resolvedValue2, byte[] metadata2) { + PathValue value1 = new PathValue(resolvedValue1, metadata1); + PathValue value2 = new PathValue(resolvedValue2, metadata2); + + assertNotEquals(value1, value2, "PathValues should not be equal: " + description); + } + + @Test + void testTrivialEquality() { + PathValue value1 = new PathValue("Foo", null); + + assertEquals(value1, value1, "Cover reference equality shortcut"); + assertNotEquals("Foo", value1, "Check it doesn't fail with non-PathValue"); + } +} diff --git a/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/ResolvedKeySpacePathTest.java b/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/ResolvedKeySpacePathTest.java new file mode 100644 index 0000000000..be82a58eb6 --- /dev/null +++ b/fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/ResolvedKeySpacePathTest.java @@ -0,0 +1,279 @@ +/* + * ResolvedKeySpacePathTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2015-2025 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.foundationdb.record.provider.foundationdb.keyspace; + +import com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType; +import com.apple.foundationdb.record.test.FDBDatabaseExtension; +import com.apple.foundationdb.tuple.Tuple; +import com.apple.test.BooleanSource; +import com.apple.test.ParameterizedTestUtils; +import com.apple.test.Tags; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.Map; +import java.util.UUID; +import java.util.function.Supplier; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * Tests for {@link ResolvedKeySpacePath}. + */ +@Tag(Tags.RequiresFDB) +class ResolvedKeySpacePathTest { + @RegisterExtension + final FDBDatabaseExtension dbExtension = new FDBDatabaseExtension(); + + /** + * Test value pairs for each KeyType. + */ + private static final Map TYPE_TEST_VALUES = Map.of( + KeyType.STRING, new TestValuePair(() -> "value1", () -> "value2"), + KeyType.LONG, new TestValuePair(() -> 100L, () -> 200L), + KeyType.BYTES, new TestValuePair(() -> new byte[]{1, 2, 3}, () -> new byte[]{4, 5, 6}), + KeyType.UUID, new TestValuePair(() -> new UUID(1, 1), () -> new UUID(2, 2)), + KeyType.BOOLEAN, new TestValuePair(() -> true, () -> false), + KeyType.NULL, new TestValuePair(() -> null, () -> null), + KeyType.FLOAT, new TestValuePair(() -> 1.5f, () -> 2.5f), + KeyType.DOUBLE, new TestValuePair(() -> 1.5d, () -> 2.5d) + ); + + @Nonnull + static Stream testEqualsHashCode() { + return ParameterizedTestUtils.cartesianProduct( + Arrays.stream(KeyType.values()), + ParameterizedTestUtils.booleans("constantDirectory"), + ParameterizedTestUtils.booleans("differenceInParent") + ); + } + + /** + * Test equals and hashCode contracts for depth 1 directories. + */ + @ParameterizedTest + @MethodSource("testEqualsHashCode") + void testEqualsHashCode(@Nonnull KeyType keyType, boolean constantDirectory, boolean differenceInParent) { + @Nonnull TestValuePair values = TYPE_TEST_VALUES.get(keyType); + + // Create a single KeySpace with the appropriate directory structure + KeySpaceDirectory rootDir = new KeySpaceDirectory("root", KeyType.STRING, "root"); + KeySpaceDirectory childDir = constantDirectory + ? new KeySpaceDirectory("test", keyType, values.getValue1()) + : new KeySpaceDirectory("test", keyType); + rootDir.addSubdirectory(childDir); + + // Optionally add a constant child for the differenceInParent test + if (differenceInParent) { + KeySpaceDirectory constantChild = new KeySpaceDirectory("constant", KeyType.STRING, "Constant"); + childDir.addSubdirectory(constantChild); + } + + KeySpace keySpace = new KeySpace(rootDir); + + // Create paths from the same KeySpace + KeySpacePath rootPath1 = keySpace.path("root"); + KeySpacePath rootPath2 = keySpace.path("root"); + + KeySpacePath childPath1; + KeySpacePath childPath2; + if (constantDirectory) { + childPath1 = rootPath1.add("test"); + childPath2 = rootPath2.add("test"); + } else { + childPath1 = rootPath1.add("test", values.getValue1()); + childPath2 = rootPath2.add("test", values.getValue1()); + } + + // Create ResolvedKeySpacePath instances + ResolvedKeySpacePath resolvedRoot1 = new ResolvedKeySpacePath(null, rootPath1, new PathValue("root", null), null); + ResolvedKeySpacePath resolvedRoot2 = new ResolvedKeySpacePath(null, rootPath2, new PathValue("root", null), null); + + ResolvedKeySpacePath path1 = new ResolvedKeySpacePath(resolvedRoot1, childPath1, new PathValue(values.getValue1(), null), null); + ResolvedKeySpacePath path2 = new ResolvedKeySpacePath(resolvedRoot2, childPath2, new PathValue(values.getValue1(), null), null); + + if (differenceInParent) { + KeySpacePath constantChildPath1 = childPath1.add("constant"); + KeySpacePath constantChildPath2 = childPath2.add("constant"); + path1 = new ResolvedKeySpacePath(path1, constantChildPath1, new PathValue("Constant", null), null); + path2 = new ResolvedKeySpacePath(path2, constantChildPath2, new PathValue("Constant", null), null); + } + + // Test equality contracts + assertEquals(path1, path2, "Identical paths should be equal"); + assertEquals(path2, path1, "Symmetry: path2.equals(path1)"); + assertEquals(path1.hashCode(), path2.hashCode(), "Equal objects must have equal hash codes"); + + // Test inequality when values differ (except NULL type which only has null values) + if (keyType != KeyType.NULL) { + if (constantDirectory) { + // For constant directories, we need a different directory (and thus different KeySpace) to test different values + // this doesn't really need to be parameterized by value type, since they will always be non-equal due + // to the directories being different + KeySpaceDirectory rootDir3 = new KeySpaceDirectory("root", KeyType.STRING, "root"); + KeySpaceDirectory childDir3 = new KeySpaceDirectory("test", keyType, values.getValue2()); + rootDir3.addSubdirectory(childDir3); + KeySpace keySpace3 = new KeySpace(rootDir3); + + KeySpacePath rootPath3 = keySpace3.path("root"); + KeySpacePath childPath3 = rootPath3.add("test"); + + ResolvedKeySpacePath resolvedRoot3 = new ResolvedKeySpacePath(null, rootPath3, new PathValue("root", null), null); + ResolvedKeySpacePath path3 = new ResolvedKeySpacePath(resolvedRoot3, childPath3, new PathValue(values.getValue2(), null), null); + + assertNotEquals(path1, path3, "Paths with different constant values should not be equal"); + } else { + // For non-constant directories, we can use the same directory with different values + KeySpacePath childPath3 = rootPath1.add("test", values.getValue2()); + ResolvedKeySpacePath path3 = new ResolvedKeySpacePath(resolvedRoot1, childPath3, new PathValue(values.getValue2(), null), null); + assertNotEquals(path1, path3, "Paths with different values should not be equal"); + } + + // Test different resolved value (same logical, different resolved) + KeySpacePath childPath4 = constantDirectory + ? rootPath1.add("test") + : rootPath1.add("test", values.getValue1()); + ResolvedKeySpacePath path4 = new ResolvedKeySpacePath(resolvedRoot1, childPath4, new PathValue(values.getValue2(), null), null); + assertNotEquals(path4, path1, "Paths with different resolved values should not be equal"); + + // Test different logical value (different logical, same resolved) + if (!constantDirectory) { + KeySpacePath childPath5 = rootPath1.add("test", values.getValue2()); + ResolvedKeySpacePath path5 = new ResolvedKeySpacePath(resolvedRoot1, childPath5, new PathValue(values.getValue1(), null), null); + assertNotEquals(path5, path1, "Paths with different logical values should not be equal"); + } + } else { + assertNull(values.getValue2()); + } + + // Test basic contracts + assertEquals(path1, path1, "Reflexivity"); + assertNotEquals(path1, null, "Null comparison"); + assertNotEquals(path1, "not a path", "Type safety"); + } + + /** + * Test that demonstrates the actual equals/hashCode behavior with different PathValue metadata. + */ + @ParameterizedTest + @BooleanSource("constantDirectory") + void testEqualsHashCodeWithDifferentMetadata(boolean constantDirectory) { + // Create two paths with same inner path and resolved value but different metadata + KeySpacePath innerPath = createKeySpacePath(createRootParent(), KeyType.STRING, "resolved", constantDirectory); + PathValue value1 = new PathValue("resolved", new byte[]{1, 2, 3}); + PathValue value2 = new PathValue("resolved", new byte[]{4, 5, 6}); + + ResolvedKeySpacePath path1 = new ResolvedKeySpacePath(null, innerPath, value1, null); + ResolvedKeySpacePath path2 = new ResolvedKeySpacePath(null, innerPath, value2, null); + + assertNotEquals(path1, path2, "Objects should be equal (same inner path and resolved value, metadata ignored)"); + assertNotEquals(path1.hashCode(), path2.hashCode(), + "Hash codes differ due to different PathValue metadata"); + } + + /** + * Test remainder field behavior in equals. + */ + @ParameterizedTest + @BooleanSource("constantDirectory") + void testRemainderComparedInEquals(boolean constantDirectory) { + KeySpacePath innerPath = createKeySpacePath(createRootParent(), KeyType.STRING, "resolved", constantDirectory); + PathValue value = new PathValue("resolved", null); + + ResolvedKeySpacePath path1 = new ResolvedKeySpacePath(null, innerPath, value, Tuple.from("remainder1")); + ResolvedKeySpacePath path2 = new ResolvedKeySpacePath(null, innerPath, value, Tuple.from("remainder2")); + ResolvedKeySpacePath path3 = new ResolvedKeySpacePath(null, innerPath, value, Tuple.from("remainder1")); + + assertNotEquals(path1, path2, "Paths with different remainders should not be equal"); + assertEquals(path1, path3, "Paths with the same remainder should be equal"); + assertEquals(path1.hashCode(), path3.hashCode(), "Paths with the same remainder should have same hashCode"); + ResolvedKeySpacePath nullRemainder = new ResolvedKeySpacePath(null, innerPath, value, null); + assertNotEquals(path1, nullRemainder, "Path without a remainder should not be the equal to one with a remainder"); + assertNotEquals(nullRemainder, null, "Make sure null is properly handled in equals"); + IntStream.range(0, 10_000).mapToObj(i -> new ResolvedKeySpacePath(null, innerPath, value, Tuple.from(i))) + .filter(path -> path.hashCode() != path1.hashCode()) + .findAny() + .orElseThrow(() -> new AssertionError("Paths with different remainders should sometimes have different hash codes")); + } + + @Nonnull + private KeySpacePath createKeySpacePath(@Nonnull ResolvedKeySpacePath parent, @Nonnull KeyType keyType, @Nullable Object value, + boolean constantDirectory) { + // Create child directory based on constantDirectory parameter + KeySpaceDirectory childDir; + if (constantDirectory) { + childDir = new KeySpaceDirectory("test", keyType, value); + } else { + childDir = new KeySpaceDirectory("test", keyType); + } + parent.getDirectory().addSubdirectory(childDir); + + if (constantDirectory) { + return parent.toPath().add("test"); + } else { + return parent.toPath().add("test", value); + } + } + + @Nonnull + private static ResolvedKeySpacePath createRootParent() { + final KeySpaceDirectory parentDir = new KeySpaceDirectory("root", KeyType.STRING, "root"); + KeySpacePath parent = new KeySpace(parentDir).path("root"); + return new ResolvedKeySpacePath(null, parent, new PathValue("root", null), null); + } + + /** + * Test value pair for each KeyType. + * We use {@link Supplier} here to make sure that if it is falling back to reference equality (e.g. byte[]), + * we want to catch if it doesn't consider those equal. + */ + private static class TestValuePair { + @Nonnull + private final Supplier value1Supplier; + @Nonnull + private final Supplier value2Supplier; + + TestValuePair(@Nonnull Supplier value1Supplier, @Nonnull Supplier value2Supplier) { + this.value1Supplier = value1Supplier; + this.value2Supplier = value2Supplier; + } + + @Nullable + Object getValue1() { + return value1Supplier.get(); + } + + @Nullable + Object getValue2() { + return value2Supplier.get(); + } + } +}