GH-2289 - Don’t use the fromId with obverse to stop traversing relationships.

The previous check used the obverse combined with the fromId to check if traversal can be stopped or not. This is wrong, we would need to combine obverse with targetId, which we may not have at the moment.

This change simplifies the check and only looks at visited relationships from source to somewhere else to avoid additional rounds.

This fixes #2289.
This commit is contained in:
Michael Simons
2021-06-17 12:58:15 +02:00
parent f81fc92149
commit bc1c23b3ef
8 changed files with 337 additions and 6 deletions

View File

@@ -490,7 +490,6 @@ public final class Neo4jTemplate implements Neo4jOperations, BeanFactoryAware {
rawValue);
RelationshipDescription relationshipDescription = relationshipContext.getRelationship();
RelationshipDescription relationshipDescriptionObverse = relationshipDescription.getRelationshipObverse();
Neo4jPersistentProperty idProperty;
if (!relationshipDescription.hasInternalIdProperty()) {
@@ -501,7 +500,7 @@ public final class Neo4jTemplate implements Neo4jOperations, BeanFactoryAware {
}
// break recursive procession and deletion of previously created relationships
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescriptionObverse, relatedValuesToStore);
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescription, relatedValuesToStore);
if (processState == ProcessState.PROCESSED_ALL_RELATIONSHIPS || processState == ProcessState.PROCESSED_BOTH) {
return;
}

View File

@@ -606,7 +606,6 @@ public final class ReactiveNeo4jTemplate implements ReactiveNeo4jOperations, Bea
rawValue);
RelationshipDescription relationshipDescription = relationshipContext.getRelationship();
RelationshipDescription relationshipDescriptionObverse = relationshipDescription.getRelationshipObverse();
Neo4jPersistentProperty idProperty;
if (!relationshipDescription.hasInternalIdProperty()) {
@@ -617,7 +616,7 @@ public final class ReactiveNeo4jTemplate implements ReactiveNeo4jOperations, Bea
}
// break recursive procession and deletion of previously created relationships
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescriptionObverse, relatedValuesToStore);
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescription, relatedValuesToStore);
if (processState == ProcessState.PROCESSED_ALL_RELATIONSHIPS || processState == ProcessState.PROCESSED_BOTH) {
return;
}

View File

@@ -138,7 +138,6 @@ final class DefaultRelationshipDescription extends Association<Neo4jPersistentPr
@Override
public int hashCode() {
return Objects.hash(type, type, fieldName, target, source, direction);
return Objects.hash(fieldName, type, target, source, direction);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2011-2021 the original author or 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
*
* https://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 org.springframework.data.neo4j.integration.issues.gh2289;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.AbstractNeo4jConfig;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.test.Neo4jExtension;
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author Michael J. Simons
*/
@Neo4jIntegrationTest
class GH2289IT {
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
@BeforeAll
protected static void setupData() {
try (Session session = neo4jConnectionSupport.getDriver().session();
Transaction transaction = session.beginTransaction();
) {
transaction.run("MATCH (n) detach delete n");
transaction.commit();
}
}
@RepeatedTest(23)
void testNewRelation(@Autowired SkuRepository skuRepo) {
Sku a = skuRepo.save(new Sku(0L, "A"));
Sku b = skuRepo.save(new Sku(1L, "B"));
Sku c = skuRepo.save(new Sku(2L, "C"));
Sku d = skuRepo.save(new Sku(3L, "D"));
a.rangeRelationTo(b, 1, 1, RelationType.MULTIPLICATIVE);
a.rangeRelationTo(c, 1, 1, RelationType.MULTIPLICATIVE);
a.rangeRelationTo(d, 1, 1, RelationType.MULTIPLICATIVE);
a = skuRepo.save(a);
Assertions.assertThat(a.getRangeRelationsOut()).hasSize(3);
b = skuRepo.findById(b.getId()).get();
Assertions.assertThat(b.getRangeRelationsIn()).hasSize(1);
b.rangeRelationTo(c, 1, 1, RelationType.MULTIPLICATIVE);
b = skuRepo.save(b);
Assertions.assertThat(b.getRangeRelationsIn()).hasSize(1);
Assertions.assertThat(b.getRangeRelationsOut()).hasSize(1);
}
@Repository
public interface SkuRepository extends Neo4jRepository<Sku, Long> {
}
@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(considerNestedRepositories = true)
static class Config extends AbstractNeo4jConfig {
@Bean
public Driver driver() {
return neo4jConnectionSupport.getDriver();
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2011-2021 the original author or 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
*
* https://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 org.springframework.data.neo4j.integration.issues.gh2289;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;
/**
* @author Michael J. Simons
*/
@Data // lombok
@RelationshipProperties
public class RangeRelation {
@Id @GeneratedValue private Long id;
@Property private double minDelta;
@Property private double maxDelta;
@Property private RelationType relationType;
@TargetNode private Sku targetSku;
public RangeRelation(Sku targetSku, double minDelta, double maxDelta, RelationType relationType) {
this.targetSku = targetSku;
this.minDelta = minDelta;
this.maxDelta = maxDelta;
this.relationType = relationType;
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2011-2021 the original author or 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
*
* https://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 org.springframework.data.neo4j.integration.issues.gh2289;
import reactor.test.StepVerifier;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Tag;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig;
import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository;
import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories;
import org.springframework.data.neo4j.test.Neo4jExtension;
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author Michael J. Simons
*/
@Neo4jIntegrationTest
@Tag(Neo4jExtension.NEEDS_REACTIVE_SUPPORT)
class ReactiveGH2289IT {
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
@BeforeAll
protected static void setupData() {
try (Session session = neo4jConnectionSupport.getDriver().session();
Transaction transaction = session.beginTransaction();
) {
transaction.run("MATCH (n) detach delete n");
transaction.commit();
}
}
@RepeatedTest(23)
void testNewRelation(@Autowired SkuRepository skuRepo) {
AtomicLong bId = new AtomicLong();
AtomicReference<Sku> cRef = new AtomicReference<>();
skuRepo.save(new Sku(0L, "A"))
.zipWith(skuRepo.save(new Sku(1L, "B")))
.zipWith(skuRepo.save(new Sku(2L, "C")))
.zipWith(skuRepo.save(new Sku(3L, "D"))).flatMap(t -> {
Sku a = t.getT1().getT1().getT1();
Sku b = t.getT1().getT1().getT2();
Sku c = t.getT1().getT2();
Sku d = t.getT2();
bId.set(b.getId());
cRef.set(c);
a.rangeRelationTo(b, 1, 1, RelationType.MULTIPLICATIVE);
a.rangeRelationTo(c, 1, 1, RelationType.MULTIPLICATIVE);
a.rangeRelationTo(d, 1, 1, RelationType.MULTIPLICATIVE);
return skuRepo.save(a);
}).as(StepVerifier::create)
.expectNextMatches(a -> a.getRangeRelationsOut().size() == 3)
.verifyComplete();
skuRepo.findById(bId.get())
.doOnNext(b -> Assertions.assertThat(b.getRangeRelationsIn()).hasSize(1))
.flatMap(b -> {
b.rangeRelationTo(cRef.get(), 1, 1, RelationType.MULTIPLICATIVE);
return skuRepo.save(b);
})
.as(StepVerifier::create)
.expectNextMatches(a -> a.getRangeRelationsIn().size() == 1 && a.getRangeRelationsOut().size() == 1)
.verifyComplete();
}
@Repository
public interface SkuRepository extends ReactiveNeo4jRepository<Sku, Long> {
}
@Configuration
@EnableTransactionManagement
@EnableReactiveNeo4jRepositories(considerNestedRepositories = true)
static class Config extends AbstractReactiveNeo4jConfig {
@Bean
public Driver driver() {
return neo4jConnectionSupport.getDriver();
}
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2011-2021 the original author or 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
*
* https://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 org.springframework.data.neo4j.integration.issues.gh2289;
/**
* @author Michael J. Simons
*/
public enum RelationType {
MULTIPLICATIVE
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2011-2021 the original author or 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
*
* https://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 org.springframework.data.neo4j.integration.issues.gh2289;
import lombok.Getter;
import lombok.Setter;
import java.util.HashSet;
import java.util.Set;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.Relationship;
/**
* @author Michael J. Simons
*/
@Node("SKU")
@Getter // lombok
@Setter
public class Sku {
@Id @GeneratedValue
private Long id;
@Property("number")
private Long number;
@Property("name")
private String name;
@Relationship(type = "RANGE_RELATION_TO", direction = Relationship.Direction.OUTGOING)
private Set<RangeRelation> rangeRelationsOut = new HashSet<>();
@Relationship(type = "RANGE_RELATION_TO", direction = Relationship.Direction.INCOMING)
private Set<RangeRelation> rangeRelationsIn = new HashSet<>();
public Sku(Long number, String name) {
this.number = number;
this.name = name;
}
public RangeRelation rangeRelationTo(Sku sku, double minDelta, double maxDelta, RelationType relationType) {
RangeRelation relationOut = new RangeRelation(sku, minDelta, maxDelta, relationType);
RangeRelation relationIn = new RangeRelation(this, minDelta, maxDelta, relationType);
rangeRelationsOut.add(relationOut);
sku.rangeRelationsIn.add(relationIn);
return relationOut;
}
}