private Object toRelationship(Object value, boolean virtual, Map<Long, Object> nodesCache) { Value internalValue = ((InternalEntity) value).asValue(); Relationship relationship = internalValue.asRelationship(); if (virtual) { VirtualNode start = (VirtualNode) nodesCache.getOrDefault(relationship.startNodeId(), new VirtualNode(relationship.startNodeId(), db)); VirtualNode end = (VirtualNode) nodesCache.getOrDefault(relationship.endNodeId(), new VirtualNode(relationship.endNodeId(), db)); VirtualRelationship virtualRelationship = new VirtualRelationship(relationship.id(), start, end, RelationshipType.withName(relationship.type()), relationship.asMap()); return virtualRelationship; } else return Util.map("entityType", internalValue.type().name(), "type", relationship.type(), "id", relationship.id(), "start", relationship.startNodeId(), "end", relationship.endNodeId(), "properties", relationship.asMap()); }
public long relationshipId(Object relationship) { return ((Relationship) relationship).id(); }
public long relationshipId(Object relationship) { return ((Relationship) relationship).id(); }
public Neo4jBoltRelationship(Relationship rel) { this.id = String.valueOf(rel.id()); this.types = Collections.singletonList(rel.type()); this.propertyContainer = new Neo4jBoltPropertyContainer(rel.asMap()); this.startNodeId = String.valueOf(rel.startNodeId()); this.endNodeId = String.valueOf(rel.endNodeId()); }
@Override public String toString() { return String.format( relationship.startNodeId() == start.id() ? "(%s)-[%s:%s]->(%s)" : "(%s)<-[%s:%s]-(%s)", start.id(), relationship.id(), relationship.type(), end.id() ); } }
@Override public PathInfo getPathInfo(String colKey) { PathInfo pathInfo = null; Value val; try { val = this.record.get(colKey); } catch (NoSuchRecordException e) { throw new RuntimeException("no result column: " + colKey); } String typName = val.type().name(); if ("PATH".equals(typName)) { Path p = val.asPath(); long startId = p.start().id(); long endId = p.end().id(); List<Long> relIds = new ArrayList<Long>(); Iterator<Relationship> it = p.relationships().iterator(); while(it.hasNext()) { Relationship rel = it.next(); relIds.add(Long.valueOf(rel.id())); } pathInfo = new PathInfo(startId, endId, relIds, p); } return pathInfo; }
@Override public PathInfo getPathInfo(String colKey) { PathInfo pathInfo = null; Value val; try { val = this.record.get(colKey); } catch (NoSuchRecordException e) { throw new RuntimeException("no result column: " + colKey); } String typName = val.type().name(); if ("PATH".equals(typName)) { Path p = val.asPath(); long startId = p.start().id(); long endId = p.end().id(); List<Long> relIds = new ArrayList<Long>(); Iterator<Relationship> it = p.relationships().iterator(); while(it.hasNext()) { Relationship rel = it.next(); relIds.add(Long.valueOf(rel.id())); } pathInfo = new PathInfo(startId, endId, relIds, p); } return pathInfo; }
public static ElementInfo fromRecordValue(Value val) { if (val instanceof ListValue) return ElementInfo.fromRecordValue(((ListValue)val).get(0)); ElementInfo ret = null; if (val != null) { String typName = val.type().name(); // NODE, RELATIONSHIP, NULL if ("NODE".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asNode().id(); ret.type = ElemType.NODE; } else if ("RELATIONSHIP".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asRelationship().id(); ret.type = ElemType.RELATION; } else if ("NULL".equals(typName)) ret = ElementInfo.nullElement(); } return ret; }
public static ElementInfo fromRecordValue(Value val) { if (val instanceof ListValue) return ElementInfo.fromRecordValue(((ListValue)val).get(0)); ElementInfo ret = null; if (val != null) { String typName = val.type().name(); // NODE, RELATIONSHIP, NULL if ("NODE".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asNode().id(); ret.type = ElemType.NODE; } else if ("RELATIONSHIP".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asRelationship().id(); ret.type = ElemType.RELATION; } else if ("NULL".equals(typName)) ret = ElementInfo.nullElement(); } return ret; }
private Entity getPropertiesObject(long id, int rowIndex, ElemType typ) { Record rec = this.records.get(rowIndex); List<Pair<String, Value>> flds = rec.fields(); for (Pair<String, Value> pair : flds) { if (typ == ElemType.NODE && pair.value() instanceof NodeValue) { Node nd = pair.value().asNode(); if (nd.id() == id) return nd; } else if (typ == ElemType.RELATION && pair.value() instanceof RelationshipValue) { Relationship rel = pair.value().asRelationship(); if (rel.id() == id) return rel; } } // element with id may not have been loaded return this.reloaded.getEntity(id, typ); }
private Entity getPropertiesObject(long id, int rowIndex, ElemType typ) { Record rec = this.records.get(rowIndex); List<Pair<String, Value>> flds = rec.fields(); for (Pair<String, Value> pair : flds) { if (typ == ElemType.NODE && pair.value() instanceof NodeValue) { Node nd = pair.value().asNode(); if (nd.id() == id) return nd; } else if (typ == ElemType.RELATION && pair.value() instanceof RelationshipValue) { Relationship rel = pair.value().asRelationship(); if (rel.id() == id) return rel; } } // element with id may not have been loaded return this.reloaded.getEntity(id, typ); }
@Test public void shouldCreateTheRelationshipInANewCurrentStatePreservingTheOldOne() { try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given Node entityA = initEntity(session); Node entityB = initEntity(session); String testType = "testType"; Long entityACurrentId = session.run(String.format("MATCH (e:Entity)-[:CURRENT]->(s:State) WHERE id(e) = %d RETURN s", entityA.id())).single().get("s").asNode().id(); // When String query = "MATCH (a:Entity), (b:Entity) WHERE id(a) = %d AND id(b) = %d WITH a, b CALL graph.versioner.relationship.create(a, b, '%s') YIELD relationship RETURN relationship"; Relationship relationship = session.run(String.format(query, entityA.id(), entityB.id(), testType)).single().get("relationship").asRelationship(); // Then String querySourceStates = "MATCH (:R)<-[r:%s]-(s1:State)-[:PREVIOUS]->(s2:State) WHERE id(r) = %d RETURN s1, s2"; StatementResult result = session.run(String.format(querySourceStates, testType, relationship.id())); assertThat(result) .hasSize(1) .allMatch(r -> r.get("s1").asNode().id() != r.get("s2").asNode().id() && r.get("s2").asNode().id() == entityACurrentId); } }