Search in sources :

Example 31 with RelationshipRecord

use of org.neo4j.kernel.impl.nioneo.store.RelationshipRecord in project graphdb by neo4j-attic.

the class WriteTransaction method getMoreRelationships.

public Iterable<RelationshipData> getMoreRelationships(long nodeId, RelationshipChainPosition position) {
    long nextRel = position.getNextRecord();
    List<RelationshipData> rels = new ArrayList<RelationshipData>();
    for (int i = 0; i < getRelGrabSize() && nextRel != Record.NO_NEXT_RELATIONSHIP.intValue(); i++) {
        RelationshipRecord relRecord = getRelationshipStore().getChainRecord(nextRel);
        if (relRecord == null) {
            // return what we got so far
            position.setNextRecord(Record.NO_NEXT_RELATIONSHIP.intValue());
            return rels;
        }
        long firstNode = relRecord.getFirstNode();
        long secondNode = relRecord.getSecondNode();
        if (// && !relRecord.isCreated() )
        relRecord.inUse()) {
            rels.add(new RelationshipData(relRecord.getId(), firstNode, secondNode, relRecord.getType()));
        } else {
            i--;
        }
        if (firstNode == nodeId) {
            nextRel = relRecord.getFirstNextRel();
        } else if (secondNode == nodeId) {
            nextRel = relRecord.getSecondNextRel();
        } else {
            throw new InvalidRecordException("Node[" + nodeId + "] is neither firstNode[" + firstNode + "] nor secondNode[" + secondNode + "] for Relationship[" + relRecord.getId() + "]");
        }
    }
    position.setNextRecord(nextRel);
    return rels;
}
Also used : RelationshipData(org.neo4j.kernel.impl.nioneo.store.RelationshipData) ArrayList(java.util.ArrayList) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord) InvalidRecordException(org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)

Example 32 with RelationshipRecord

use of org.neo4j.kernel.impl.nioneo.store.RelationshipRecord in project graphdb by neo4j-attic.

the class WriteTransaction method doPrepare.

@Override
protected void doPrepare() throws XAException {
    if (committed) {
        throw new XAException("Cannot prepare committed transaction[" + getIdentifier() + "]");
    }
    if (prepared) {
        throw new XAException("Cannot prepare prepared transaction[" + getIdentifier() + "]");
    }
    // generate records then write to logical log via addCommand method
    prepared = true;
    for (RelationshipTypeRecord record : relTypeRecords.values()) {
        Command.RelationshipTypeCommand command = new Command.RelationshipTypeCommand(neoStore.getRelationshipTypeStore(), record);
        relTypeCommands.add(command);
        addCommand(command);
    }
    for (NodeRecord record : nodeRecords.values()) {
        if (!record.inUse() && record.getNextRel() != Record.NO_NEXT_RELATIONSHIP.intValue()) {
            throw new InvalidRecordException("Node record " + record + " still has relationships");
        }
        Command.NodeCommand command = new Command.NodeCommand(neoStore.getNodeStore(), record);
        nodeCommands.add(command);
        if (!record.inUse()) {
            removeNodeFromCache(record.getId());
        }
        addCommand(command);
    }
    for (RelationshipRecord record : relRecords.values()) {
        Command.RelationshipCommand command = new Command.RelationshipCommand(neoStore.getRelationshipStore(), record);
        relCommands.add(command);
        if (!record.inUse()) {
            removeRelationshipFromCache(record.getId());
        }
        addCommand(command);
    }
    for (PropertyIndexRecord record : propIndexRecords.values()) {
        Command.PropertyIndexCommand command = new Command.PropertyIndexCommand(neoStore.getPropertyStore().getIndexStore(), record);
        propIndexCommands.add(command);
        addCommand(command);
    }
    for (PropertyRecord record : propertyRecords.values()) {
        Command.PropertyCommand command = new Command.PropertyCommand(neoStore.getPropertyStore(), record);
        propCommands.add(command);
        addCommand(command);
    }
}
Also used : XAException(javax.transaction.xa.XAException) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord) PropertyIndexRecord(org.neo4j.kernel.impl.nioneo.store.PropertyIndexRecord) RelationshipTypeRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipTypeRecord) PropertyCommand(org.neo4j.kernel.impl.nioneo.xa.Command.PropertyCommand) NodeRecord(org.neo4j.kernel.impl.nioneo.store.NodeRecord) PropertyRecord(org.neo4j.kernel.impl.nioneo.store.PropertyRecord) PropertyCommand(org.neo4j.kernel.impl.nioneo.xa.Command.PropertyCommand) XaCommand(org.neo4j.kernel.impl.transaction.xaframework.XaCommand) PropertyCommand(org.neo4j.kernel.impl.nioneo.xa.Command.PropertyCommand) InvalidRecordException(org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)

Example 33 with RelationshipRecord

use of org.neo4j.kernel.impl.nioneo.store.RelationshipRecord in project graphdb by neo4j-attic.

the class WriteTransaction method relGetProperties.

public ArrayMap<Integer, PropertyData> relGetProperties(long relId, boolean light) {
    ArrayMap<Integer, PropertyData> propertyMap = new ArrayMap<Integer, PropertyData>(9, false, true);
    RelationshipRecord relRecord = getRelationshipRecord(relId);
    if (relRecord != null && relRecord.isCreated()) {
        return propertyMap;
    }
    if (relRecord != null) {
        if (!relRecord.inUse() && !light) {
            throw new IllegalStateException("Relationship[" + relId + "] has been deleted in this tx");
        }
    }
    relRecord = getRelationshipStore().getRecord(relId);
    if (!relRecord.inUse()) {
        throw new InvalidRecordException("Relationship[" + relId + "] not in use");
    }
    long nextProp = relRecord.getNextProp();
    while (nextProp != Record.NO_NEXT_PROPERTY.intValue()) {
        PropertyRecord propRecord = getPropertyStore().getLightRecord(nextProp);
        propertyMap.put(propRecord.getKeyIndexId(), new PropertyData(propRecord.getId(), propertyGetValueOrNull(propRecord)));
        nextProp = propRecord.getNextProp();
    }
    return propertyMap;
}
Also used : PropertyData(org.neo4j.kernel.impl.nioneo.store.PropertyData) PropertyRecord(org.neo4j.kernel.impl.nioneo.store.PropertyRecord) ArrayMap(org.neo4j.kernel.impl.util.ArrayMap) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord) InvalidRecordException(org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)

Example 34 with RelationshipRecord

use of org.neo4j.kernel.impl.nioneo.store.RelationshipRecord in project graphdb by neo4j-attic.

the class WriteTransaction method doRollback.

public void doRollback() throws XAException {
    if (committed) {
        throw new XAException("Cannot rollback partialy commited " + "transaction[" + getIdentifier() + "]. Recover and " + "commit");
    }
    try {
        for (RelationshipTypeRecord record : relTypeRecords.values()) {
            if (record.isCreated()) {
                getRelationshipTypeStore().freeId(record.getId());
                for (DynamicRecord dynamicRecord : record.getTypeRecords()) {
                    if (dynamicRecord.isCreated()) {
                        getRelationshipTypeStore().freeBlockId((int) dynamicRecord.getId());
                    }
                }
            }
            removeRelationshipTypeFromCache(record.getId());
        }
        for (NodeRecord record : nodeRecords.values()) {
            if (record.isCreated()) {
                getNodeStore().freeId(record.getId());
            }
            removeNodeFromCache(record.getId());
        }
        for (RelationshipRecord record : relRecords.values()) {
            if (record.isCreated()) {
                getRelationshipStore().freeId(record.getId());
            }
            removeRelationshipFromCache(record.getId());
        }
        for (PropertyIndexRecord record : propIndexRecords.values()) {
            if (record.isCreated()) {
                getPropertyStore().getIndexStore().freeId(record.getId());
                for (DynamicRecord dynamicRecord : record.getKeyRecords()) {
                    if (dynamicRecord.isCreated()) {
                        getPropertyStore().getIndexStore().freeBlockId((int) dynamicRecord.getId());
                    }
                }
            }
        }
        for (PropertyRecord record : propertyRecords.values()) {
            if (record.getNodeId() != -1) {
                removeNodeFromCache(record.getNodeId());
            } else if (record.getRelId() != -1) {
                removeRelationshipFromCache(record.getRelId());
            }
            if (record.isCreated()) {
                getPropertyStore().freeId(record.getId());
                for (DynamicRecord dynamicRecord : record.getValueRecords()) {
                    if (dynamicRecord.isCreated()) {
                        if (dynamicRecord.getType() == PropertyType.STRING.intValue()) {
                            getPropertyStore().freeStringBlockId(dynamicRecord.getId());
                        } else if (dynamicRecord.getType() == PropertyType.ARRAY.intValue()) {
                            getPropertyStore().freeArrayBlockId(dynamicRecord.getId());
                        } else {
                            throw new InvalidRecordException("Unknown type on " + dynamicRecord);
                        }
                    }
                }
            }
        }
    } finally {
        nodeRecords.clear();
        propertyRecords.clear();
        relRecords.clear();
        relTypeRecords.clear();
        propIndexRecords.clear();
        nodeCommands.clear();
        propCommands.clear();
        propIndexCommands.clear();
        relCommands.clear();
        relTypeCommands.clear();
        if (!isRecovered()) {
            lockReleaser.rollback();
        }
    }
}
Also used : DynamicRecord(org.neo4j.kernel.impl.nioneo.store.DynamicRecord) NodeRecord(org.neo4j.kernel.impl.nioneo.store.NodeRecord) XAException(javax.transaction.xa.XAException) PropertyRecord(org.neo4j.kernel.impl.nioneo.store.PropertyRecord) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord) PropertyIndexRecord(org.neo4j.kernel.impl.nioneo.store.PropertyIndexRecord) RelationshipTypeRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipTypeRecord) InvalidRecordException(org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)

Example 35 with RelationshipRecord

use of org.neo4j.kernel.impl.nioneo.store.RelationshipRecord in project neo4j-mobile-android by neo4j-contrib.

the class LegacyRelationshipStoreReader method readRelationshipStore.

public Iterable<RelationshipRecord> readRelationshipStore() throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(RECORD_LENGTH);
    return new Iterable<RelationshipRecord>() {

        @Override
        public Iterator<RelationshipRecord> iterator() {
            return new PrefetchingIterator<RelationshipRecord>() {

                long id = 0;

                @Override
                protected RelationshipRecord fetchNextOrNull() {
                    RelationshipRecord record = null;
                    while (record == null && id <= maxId) {
                        buffer.clear();
                        try {
                            fileChannel.read(buffer);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        buffer.flip();
                        long inUseByte = buffer.get();
                        boolean inUse = (inUseByte & 0x1) == Record.IN_USE.intValue();
                        if (inUse) {
                            long firstNode = LegacyStore.getUnsignedInt(buffer);
                            long firstNodeMod = (inUseByte & 0xEL) << 31;
                            long secondNode = LegacyStore.getUnsignedInt(buffer);
                            // [ xxx,    ][    ,    ][    ,    ][    ,    ] second node high order bits,     0x70000000
                            // [    ,xxx ][    ,    ][    ,    ][    ,    ] first prev rel high order bits,  0xE000000
                            // [    ,   x][xx  ,    ][    ,    ][    ,    ] first next rel high order bits,  0x1C00000
                            // [    ,    ][  xx,x   ][    ,    ][    ,    ] second prev rel high order bits, 0x380000
                            // [    ,    ][    , xxx][    ,    ][    ,    ] second next rel high order bits, 0x70000
                            // [    ,    ][    ,    ][xxxx,xxxx][xxxx,xxxx] type
                            long typeInt = buffer.getInt();
                            long secondNodeMod = (typeInt & 0x70000000L) << 4;
                            int type = (int) (typeInt & 0xFFFF);
                            record = new RelationshipRecord(id, LegacyStore.longFromIntAndMod(firstNode, firstNodeMod), LegacyStore.longFromIntAndMod(secondNode, secondNodeMod), type);
                            record.setInUse(inUse);
                            long firstPrevRel = LegacyStore.getUnsignedInt(buffer);
                            long firstPrevRelMod = (typeInt & 0xE000000L) << 7;
                            record.setFirstPrevRel(LegacyStore.longFromIntAndMod(firstPrevRel, firstPrevRelMod));
                            long firstNextRel = LegacyStore.getUnsignedInt(buffer);
                            long firstNextRelMod = (typeInt & 0x1C00000L) << 10;
                            record.setFirstNextRel(LegacyStore.longFromIntAndMod(firstNextRel, firstNextRelMod));
                            long secondPrevRel = LegacyStore.getUnsignedInt(buffer);
                            long secondPrevRelMod = (typeInt & 0x380000L) << 13;
                            record.setSecondPrevRel(LegacyStore.longFromIntAndMod(secondPrevRel, secondPrevRelMod));
                            long secondNextRel = LegacyStore.getUnsignedInt(buffer);
                            long secondNextRelMod = (typeInt & 0x70000L) << 16;
                            record.setSecondNextRel(LegacyStore.longFromIntAndMod(secondNextRel, secondNextRelMod));
                            long nextProp = LegacyStore.getUnsignedInt(buffer);
                            long nextPropMod = (inUseByte & 0xF0L) << 28;
                            record.setNextProp(LegacyStore.longFromIntAndMod(nextProp, nextPropMod));
                        } else {
                            record = new RelationshipRecord(id, -1, -1, -1);
                            record.setInUse(false);
                        }
                        id++;
                    }
                    return record;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)42 InvalidRecordException (org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)20 NodeRecord (org.neo4j.kernel.impl.nioneo.store.NodeRecord)12 PropertyRecord (org.neo4j.kernel.impl.nioneo.store.PropertyRecord)12 ArrayList (java.util.ArrayList)7 RelationshipType (org.neo4j.graphdb.RelationshipType)7 DynamicRecord (org.neo4j.kernel.impl.nioneo.store.DynamicRecord)6 XAException (javax.transaction.xa.XAException)4 Relationship (org.neo4j.graphdb.Relationship)4 PropertyData (org.neo4j.kernel.impl.nioneo.store.PropertyData)4 PropertyIndexRecord (org.neo4j.kernel.impl.nioneo.store.PropertyIndexRecord)4 RelationshipTypeRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipTypeRecord)4 PropertyBlock (org.neo4j.kernel.impl.nioneo.store.PropertyBlock)3 RelationshipData (org.neo4j.kernel.impl.nioneo.store.RelationshipData)3 XaCommand (org.neo4j.kernel.impl.transaction.xaframework.XaCommand)3 ArrayMap (org.neo4j.kernel.impl.util.ArrayMap)3 IOException (java.io.IOException)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 PropertyCommand (org.neo4j.kernel.impl.nioneo.xa.Command.PropertyCommand)2