@Override public void invalidateCache(CacheAccessBackDoor cacheAccess) { cacheAccess.removeRelationshipFromCache(getKey()); /* * If isRecovered() then beforeUpdate is the correct one UNLESS this is the second time this command * is executed, where it might have been actually written out to disk so the fields are already -1. So * we still need to check. * If !isRecovered() then beforeUpdate is the same as record, so we are still ok. * We don't check for !inUse() though because that is implicit in the call of this method. * The above is a hand waiving proof that the conditions that lead to the patchDeletedRelationshipNodes() * in the if below are the same as in RelationshipCommand.execute() so it should be safe. */ if (beforeUpdate.getFirstNode() != -1 || beforeUpdate.getSecondNode() != -1) { cacheAccess.patchDeletedRelationshipNodes( getKey(), beforeUpdate.getFirstNode(), beforeUpdate.getFirstNextRel(), beforeUpdate.getSecondNode(), beforeUpdate.getSecondNextRel()); } if (record.getFirstNode() != -1 || record.getSecondNode() != -1) { cacheAccess.removeNodeFromCache(record.getFirstNode()); cacheAccess.removeNodeFromCache(record.getSecondNode()); } }
public Iterable<SimpleRelationship> getSimpleRelationships(long nodeId) { NodeRecord nodeRecord = getNodeRecord(nodeId); long nextRel = nodeRecord.getNextRel(); List<SimpleRelationship> rels = new ArrayList<SimpleRelationship>(); while (nextRel != Record.NO_NEXT_RELATIONSHIP.intValue()) { RelationshipRecord relRecord = getRelationshipRecord(nextRel); RelationshipType type = new RelationshipTypeImpl(typeHolder.getName(relRecord.getType())); rels.add( new SimpleRelationship( relRecord.getId(), relRecord.getFirstNode(), relRecord.getSecondNode(), type)); long firstNode = relRecord.getFirstNode(); long secondNode = relRecord.getSecondNode(); if (firstNode == nodeId) { nextRel = relRecord.getFirstNextRel(); } else if (secondNode == nodeId) { nextRel = relRecord.getSecondNextRel(); } else { throw new InvalidRecordException( "Node[" + nodeId + "] not part of firstNode[" + firstNode + "] or secondNode[" + secondNode + "]"); } } return rels; }
@Override public BatchRelationship getRelationshipById(long relId) { RelationshipRecord record = getRelationshipRecord(relId); RelationshipType type = new RelationshipTypeImpl(typeHolder.getName(record.getType())); return new BatchRelationship( record.getId(), record.getFirstNode(), record.getSecondNode(), type); }
@Override public Iterable<Long> getRelationshipIds(long nodeId) { NodeRecord nodeRecord = getNodeRecord(nodeId); long nextRel = nodeRecord.getNextRel(); List<Long> ids = new ArrayList<Long>(); while (nextRel != Record.NO_NEXT_RELATIONSHIP.intValue()) { RelationshipRecord relRecord = getRelationshipRecord(nextRel); ids.add(relRecord.getId()); long firstNode = relRecord.getFirstNode(); long secondNode = relRecord.getSecondNode(); if (firstNode == nodeId) { nextRel = relRecord.getFirstNextRel(); } else if (secondNode == nodeId) { nextRel = relRecord.getSecondNextRel(); } else { throw new InvalidRecordException( "Node[" + nodeId + "] not part of firstNode[" + firstNode + "] or secondNode[" + secondNode + "]"); } } return ids; }
@Override public BatchRelationship apply(RelationshipRecord relRecord) { RelationshipType type = new RelationshipTypeImpl(relationshipTypeTokens.nameOf(relRecord.getType())); return new BatchRelationship( relRecord.getId(), relRecord.getFirstNode(), relRecord.getSecondNode(), type); }
private void receiveRelationships( Iterable<RelationshipRecord> rels, ArrayMap<Integer, RelIdArray> newRelationshipMap, List<RelationshipImpl> relsList, DirectionWrapper dir, boolean hasLoops) { for (RelationshipRecord rel : rels) { long relId = rel.getId(); RelationshipImpl relImpl = relCache.get(relId); RelationshipType type = null; int typeId; if (relImpl == null) { typeId = rel.getType(); type = getRelationshipTypeById(typeId); assert type != null; relImpl = newRelationshipImpl( relId, rel.getFirstNode(), rel.getSecondNode(), type, typeId, false); relsList.add(relImpl); } else { typeId = relImpl.getTypeId(); type = getRelationshipTypeById(typeId); } RelIdArray relationshipSet = newRelationshipMap.get(typeId); if (relationshipSet == null) { relationshipSet = hasLoops ? new RelIdArrayWithLoops(typeId) : new RelIdArray(typeId); newRelationshipMap.put(typeId, relationshipSet); } relationshipSet.add(relId, dir); } }
protected Relationship getRelationshipByIdOrNull(long relId) { RelationshipImpl relationship = relCache.get(relId); if (relationship != null) { return new RelationshipProxy(relId, relationshipLookups); } ReentrantLock loadLock = lockId(relId); try { relationship = relCache.get(relId); if (relationship != null) { return new RelationshipProxy(relId, relationshipLookups); } RelationshipRecord data = persistenceManager.loadLightRelationship(relId); if (data == null) { return null; } int typeId = data.getType(); RelationshipType type = getRelationshipTypeById(typeId); if (type == null) { throw new NotFoundException( "Relationship[" + data.getId() + "] exist but relationship type[" + typeId + "] not found."); } final long startNodeId = data.getFirstNode(); final long endNodeId = data.getSecondNode(); relationship = newRelationshipImpl(relId, startNodeId, endNodeId, type, typeId, false); relCache.put(relationship); return new RelationshipProxy(relId, relationshipLookups); } finally { loadLock.unlock(); } }
private void setCorrectNextRel(NodeRecord node, RelationshipRecord rel, long nextRel) { if (node.getId() == rel.getFirstNode()) { rel.setFirstNextRel(nextRel); } if (node.getId() == rel.getSecondNode()) { rel.setSecondNextRel(nextRel); } }
@Override public BatchRelationship getRelationshipById(long relId) { RelationshipRecord record = getRelationshipRecord(relId).forReadingData(); RelationshipType type = new RelationshipTypeImpl(relationshipTypeTokens.nameOf(record.getType())); return new BatchRelationship( record.getId(), record.getFirstNode(), record.getSecondNode(), type); }
private static RelationshipConnection relChain(RelationshipRecord rel, long nodeId) { if (rel.getFirstNode() == nodeId) { return RelationshipConnection.START_NEXT; } if (rel.getSecondNode() == nodeId) { return RelationshipConnection.END_NEXT; } throw new RuntimeException(nodeId + " neither start not end node in " + rel); }
private void connect( long nodeId, long firstRelId, RelationshipRecord rel, RecordAccess<Long, RelationshipRecord, Void> relRecords) { long newCount = 1; if (firstRelId != Record.NO_NEXT_RELATIONSHIP.intValue()) { locker.getWriteLock(firstRelId); RelationshipRecord firstRel = relRecords.getOrLoad(firstRelId, null).forChangingLinkage(); boolean changed = false; if (firstRel.getFirstNode() == nodeId) { newCount = firstRel.getFirstPrevRel() + 1; firstRel.setFirstPrevRel(rel.getId()); firstRel.setFirstInFirstChain(false); changed = true; } if (firstRel.getSecondNode() == nodeId) { newCount = firstRel.getSecondPrevRel() + 1; firstRel.setSecondPrevRel(rel.getId()); firstRel.setFirstInSecondChain(false); changed = true; } if (!changed) { throw new InvalidRecordException(nodeId + " doesn't match " + firstRel); } } // Set the relationship count if (rel.getFirstNode() == nodeId) { rel.setFirstPrevRel(newCount); rel.setFirstInFirstChain(true); } if (rel.getSecondNode() == nodeId) { rel.setSecondPrevRel(newCount); rel.setFirstInSecondChain(true); } }
private void connect(NodeRecord node, RelationshipRecord rel) { if (node.getNextRel() != Record.NO_NEXT_RELATIONSHIP.intValue()) { RelationshipRecord nextRel = getRelationshipStore().getRecord(node.getNextRel()); boolean changed = false; if (nextRel.getFirstNode() == node.getId()) { nextRel.setFirstPrevRel(rel.getId()); changed = true; } if (nextRel.getSecondNode() == node.getId()) { nextRel.setSecondPrevRel(rel.getId()); changed = true; } if (!changed) { throw new InvalidRecordException(node + " dont match " + nextRel); } getRelationshipStore().updateRecord(nextRel); } }
public RelationshipImpl getRelationshipForProxy(long relId, LockType lock) { if (lock != null) { lock.acquire(getTransactionState(), new RelationshipProxy(relId, relationshipLookups)); } RelationshipImpl relationship = relCache.get(relId); if (relationship != null) { return relationship; } ReentrantLock loadLock = lockId(relId); try { relationship = relCache.get(relId); if (relationship != null) { return relationship; } RelationshipRecord data = persistenceManager.loadLightRelationship(relId); if (data == null) { throw new NotFoundException(format("Relationship %d not found", relId)); } int typeId = data.getType(); RelationshipType type = getRelationshipTypeById(typeId); if (type == null) { throw new NotFoundException( "Relationship[" + data.getId() + "] exist but relationship type[" + typeId + "] not found."); } relationship = newRelationshipImpl( relId, data.getFirstNode(), data.getSecondNode(), type, typeId, false); // relCache.put( relId, relationship ); relCache.put(relationship); return relationship; } finally { loadLock.unlock(); } }
@Override long compareNode(RelationshipRecord rel) { return rel.getFirstNode(); }