@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); }
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; }
public void stop() { if (useAdaptiveCache) { cacheManager.stop(); cacheManager.unregisterCache(nodeCache); cacheManager.unregisterCache(relCache); } relTypeHolder.clear(); }
public Relationship createRelationship( Node startNodeProxy, NodeImpl startNode, Node endNode, RelationshipType type) { if (startNode == null || endNode == null || type == null) { throw new IllegalArgumentException( "Null parameter, startNode=" + startNode + ", endNode=" + endNode + ", type=" + type); } if (!relTypeHolder.isValidRelationshipType(type)) { relTypeHolder.addValidRelationshipType(type.name(), true); } long startNodeId = startNode.getId(); long endNodeId = endNode.getId(); NodeImpl secondNode = getLightNode(endNodeId); if (secondNode == null) { setRollbackOnly(); throw new NotFoundException("Second node[" + endNode.getId() + "] deleted"); } long id = idGenerator.nextId(Relationship.class); int typeId = getRelationshipTypeIdFor(type); RelationshipImpl rel = newRelationshipImpl(id, startNodeId, endNodeId, type, typeId, true); RelationshipProxy proxy = new RelationshipProxy(id, relationshipLookups); TransactionState transactionState = getTransactionState(); transactionState.acquireWriteLock(proxy); boolean success = false; TransactionState tx = transactionState; try { transactionState.acquireWriteLock(startNodeProxy); transactionState.acquireWriteLock(endNode); persistenceManager.relationshipCreate(id, typeId, startNodeId, endNodeId); if (startNodeId == endNodeId) { tx.getOrCreateCowRelationshipAddMap(startNode, typeId).add(id, DirectionWrapper.BOTH); } else { tx.getOrCreateCowRelationshipAddMap(startNode, typeId).add(id, DirectionWrapper.OUTGOING); tx.getOrCreateCowRelationshipAddMap(secondNode, typeId).add(id, DirectionWrapper.INCOMING); } // relCache.put( rel.getId(), rel ); relCache.put(rel); success = true; return proxy; } finally { if (!success) { setRollbackOnly(); } } }
private int createNewRelationshipType(String name) { RelationshipTypeStore typeStore = getRelationshipTypeStore(); int id = (int) typeStore.nextId(); RelationshipTypeRecord record = new RelationshipTypeRecord(id); record.setInUse(true); record.setCreated(); int nameId = typeStore.nextNameId(); record.setNameId(nameId); Collection<DynamicRecord> nameRecords = typeStore.allocateNameRecords(nameId, encodeString(name)); for (DynamicRecord typeRecord : nameRecords) { record.addNameRecord(typeRecord); } typeStore.updateRecord(record); typeHolder.addRelationshipType(name, id); return id; }
@Override public long createRelationship( long node1, long node2, RelationshipType type, Map<String, Object> properties) { NodeRecord firstNode = getNodeRecord(node1); NodeRecord secondNode = getNodeRecord(node2); int typeId = typeHolder.getTypeId(type.name()); if (typeId == -1) { typeId = createNewRelationshipType(type.name()); } long id = getRelationshipStore().nextId(); RelationshipRecord record = new RelationshipRecord(id, node1, node2, typeId); record.setInUse(true); record.setCreated(); connectRelationship(firstNode, secondNode, record); getNodeStore().updateRecord(firstNode); getNodeStore().updateRecord(secondNode); record.setNextProp(createPropertyChain(properties)); getRelationshipStore().updateRecord(record); return id; }
void addRelationshipType(RelationshipTypeData type) { relTypeHolder.addRawRelationshipType(type); }
Iterable<RelationshipType> getRelationshipTypes() { return relTypeHolder.getRelationshipTypes(); }
void addRawRelationshipTypes(RelationshipTypeData[] relTypes) { relTypeHolder.addRawRelationshipTypes(relTypes); }
int getRelationshipTypeIdFor(RelationshipType type) { return relTypeHolder.getIdFor(type); }
public void removeRelationshipTypeFromCache(int id) { relTypeHolder.removeRelType(id); }
RelationshipType getRelationshipTypeById(int id) { return relTypeHolder.getRelationshipType(id); }
public Relationship createRelationship(Node startNode, Node endNode, RelationshipType type) { if (startNode == null || endNode == null || type == null) { throw new IllegalArgumentException( "Null parameter, startNode=" + startNode + ", endNode=" + endNode + ", type=" + type); } if (!relTypeHolder.isValidRelationshipType(type)) { relTypeHolder.addValidRelationshipType(type.name(), true); } int startNodeId = (int) startNode.getId(); NodeImpl firstNode = getLightNode(startNodeId); if (firstNode == null) { setRollbackOnly(); throw new NotFoundException("First node[" + startNode.getId() + "] deleted"); } int endNodeId = (int) endNode.getId(); NodeImpl secondNode = getLightNode(endNodeId); if (secondNode == null) { setRollbackOnly(); throw new NotFoundException("Second node[" + endNode.getId() + "] deleted"); } int id = idGenerator.nextId(Relationship.class); RelationshipImpl rel = new RelationshipImpl(id, startNodeId, endNodeId, type, true, this); boolean firstNodeTaken = false; boolean secondNodeTaken = false; acquireLock(rel, LockType.WRITE); boolean success = false; try { acquireLock(firstNode, LockType.WRITE); firstNodeTaken = true; acquireLock(secondNode, LockType.WRITE); secondNodeTaken = true; int typeId = getRelationshipTypeIdFor(type); persistenceManager.relationshipCreate(id, typeId, startNodeId, endNodeId); firstNode.addRelationship(type, id); secondNode.addRelationship(type, id); relCache.put((int) rel.getId(), rel); success = true; return new RelationshipProxy(id, this); } finally { boolean releaseFailed = false; if (firstNodeTaken) { try { releaseLock(firstNode, LockType.WRITE); } catch (Exception e) { releaseFailed = true; e.printStackTrace(); log.severe("Failed to release lock"); } } if (secondNodeTaken) { try { releaseLock(secondNode, LockType.WRITE); } catch (Exception e) { releaseFailed = true; e.printStackTrace(); log.severe("Failed to release lock"); } } releaseLock(rel, LockType.WRITE); if (!success) { setRollbackOnly(); } if (releaseFailed) { throw new LockException( "Unable to release locks [" + startNode + "," + endNode + "] in relationship create->" + rel); } } }