Search in sources :

Example 6 with RelationshipStore

use of org.neo4j.kernel.impl.store.RelationshipStore in project neo4j by neo4j.

the class ConsistencyCheckServiceIntegrationTest method prepareDbWithDeletedRelationshipPartOfTheChain.

private void prepareDbWithDeletedRelationshipPartOfTheChain() {
    GraphDatabaseAPI db = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(testDirectory.graphDbDir()).setConfig(GraphDatabaseSettings.record_format, getRecordFormatName()).newGraphDatabase();
    try {
        RelationshipType relationshipType = RelationshipType.withName("testRelationshipType");
        try (Transaction tx = db.beginTx()) {
            Node node1 = set(db.createNode());
            Node node2 = set(db.createNode(), property("key", "value"));
            node1.createRelationshipTo(node2, relationshipType);
            node1.createRelationshipTo(node2, relationshipType);
            node1.createRelationshipTo(node2, relationshipType);
            node1.createRelationshipTo(node2, relationshipType);
            node1.createRelationshipTo(node2, relationshipType);
            node1.createRelationshipTo(node2, relationshipType);
            tx.success();
        }
        RecordStorageEngine recordStorageEngine = db.getDependencyResolver().resolveDependency(RecordStorageEngine.class);
        NeoStores neoStores = recordStorageEngine.testAccessNeoStores();
        RelationshipStore relationshipStore = neoStores.getRelationshipStore();
        RelationshipRecord relationshipRecord = new RelationshipRecord(-1);
        RelationshipRecord record = relationshipStore.getRecord(4, relationshipRecord, RecordLoad.FORCE);
        record.setInUse(false);
        relationshipStore.updateRecord(relationshipRecord);
    } finally {
        db.shutdown();
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) RecordStorageEngine(org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine) Node(org.neo4j.graphdb.Node) NeoStores(org.neo4j.kernel.impl.store.NeoStores) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) RelationshipType(org.neo4j.graphdb.RelationshipType) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord)

Example 7 with RelationshipStore

use of org.neo4j.kernel.impl.store.RelationshipStore in project neo4j by neo4j.

the class DetectAllRelationshipInconsistenciesIT method shouldDetectSabotagedRelationshipWhereEverItIs.

@Test
public void shouldDetectSabotagedRelationshipWhereEverItIs() throws Exception {
    // GIVEN a database which lots of relationships
    GraphDatabaseAPI db = getGraphDatabaseAPI();
    Sabotage sabotage;
    try {
        Node[] nodes = new Node[1_000];
        Relationship[] relationships = new Relationship[10_000];
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < nodes.length; i++) {
                nodes[i] = db.createNode(label("Foo"));
            }
            for (int i = 0; i < 10_000; i++) {
                relationships[i] = random.among(nodes).createRelationshipTo(random.among(nodes), MyRelTypes.TEST);
            }
            tx.success();
        }
        // WHEN sabotaging a random relationship
        DependencyResolver resolver = db.getDependencyResolver();
        PageCache pageCache = resolver.resolveDependency(PageCache.class);
        StoreFactory storeFactory = newStoreFactory(pageCache);
        try (NeoStores neoStores = storeFactory.openNeoStores(false, StoreType.RELATIONSHIP)) {
            RelationshipStore relationshipStore = neoStores.getRelationshipStore();
            Relationship sabotagedRelationships = random.among(relationships);
            sabotage = sabotage(relationshipStore, sabotagedRelationships.getId());
        }
    } finally {
        db.shutdown();
    }
    // THEN the checker should find it, where ever it is in the store
    db = getGraphDatabaseAPI();
    try {
        DependencyResolver resolver = db.getDependencyResolver();
        PageCache pageCache = resolver.resolveDependency(PageCache.class);
        StoreFactory storeFactory = newStoreFactory(pageCache);
        try (NeoStores neoStores = storeFactory.openAllNeoStores()) {
            StoreAccess storeAccess = new StoreAccess(neoStores).initialize();
            DirectStoreAccess directStoreAccess = new DirectStoreAccess(storeAccess, db.getDependencyResolver().resolveDependency(LabelScanStore.class), db.getDependencyResolver().resolveDependency(SchemaIndexProvider.class));
            int threads = random.intBetween(2, 10);
            FullCheck checker = new FullCheck(getTuningConfiguration(), ProgressMonitorFactory.NONE, Statistics.NONE, threads);
            AssertableLogProvider logProvider = new AssertableLogProvider(true);
            ConsistencySummaryStatistics summary = checker.execute(directStoreAccess, logProvider.getLog(FullCheck.class));
            int relationshipInconsistencies = summary.getInconsistencyCountForRecordType(RecordType.RELATIONSHIP);
            assertTrue("Couldn't detect sabotaged relationship " + sabotage, relationshipInconsistencies > 0);
            logProvider.assertContainsLogCallContaining(sabotage.after.toString());
        }
    } finally {
        db.shutdown();
    }
}
Also used : LabelScanStore(org.neo4j.kernel.api.labelscan.LabelScanStore) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) DirectStoreAccess(org.neo4j.kernel.api.direct.DirectStoreAccess) StoreAccess(org.neo4j.kernel.impl.store.StoreAccess) Node(org.neo4j.graphdb.Node) DirectStoreAccess(org.neo4j.kernel.api.direct.DirectStoreAccess) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) DependencyResolver(org.neo4j.graphdb.DependencyResolver) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) NeoStores(org.neo4j.kernel.impl.store.NeoStores) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) PageCache(org.neo4j.io.pagecache.PageCache) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) ConsistencySummaryStatistics(org.neo4j.consistency.report.ConsistencySummaryStatistics) Test(org.junit.Test)

Example 8 with RelationshipStore

use of org.neo4j.kernel.impl.store.RelationshipStore in project neo4j by neo4j.

the class StoreMigrator method legacyRelationshipsAsInput.

private InputIterable<InputRelationship> legacyRelationshipsAsInput(NeoStores legacyStore, boolean requiresPropertyMigration, RecordCursors cursors) {
    RelationshipStore store = legacyStore.getRelationshipStore();
    final BiConsumer<InputRelationship, RelationshipRecord> propertyDecorator = propertyDecorator(requiresPropertyMigration, cursors);
    return new StoreScanAsInputIterable<InputRelationship, RelationshipRecord>(store) {

        @Override
        protected InputRelationship inputEntityOf(RelationshipRecord record) {
            InputRelationship result = new InputRelationship("legacy store", record.getId(), record.getId() * RelationshipRecordFormat.RECORD_SIZE, InputEntity.NO_PROPERTIES, record.getNextProp(), record.getFirstNode(), record.getSecondNode(), null, record.getType());
            propertyDecorator.accept(result, record);
            return result;
        }
    };
}
Also used : RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) InputRelationship(org.neo4j.unsafe.impl.batchimport.input.InputRelationship)

Example 9 with RelationshipStore

use of org.neo4j.kernel.impl.store.RelationshipStore in project neo4j by neo4j.

the class ParallelBatchImporter method doImport.

@Override
public void doImport(Input input) throws IOException {
    log.info("Import starting");
    // Things that we need to close later. The reason they're not in the try-with-resource statement
    // is that we need to close, and set to null, at specific points preferably. So use good ol' finally block.
    NodeRelationshipCache nodeRelationshipCache = null;
    NodeLabelsCache nodeLabelsCache = null;
    long startTime = currentTimeMillis();
    CountingStoreUpdateMonitor storeUpdateMonitor = new CountingStoreUpdateMonitor();
    try (BatchingNeoStores neoStore = getBatchingNeoStores();
        CountsAccessor.Updater countsUpdater = neoStore.getCountsStore().reset(neoStore.getLastCommittedTransactionId());
        InputCache inputCache = new InputCache(fileSystem, storeDir, recordFormats, config)) {
        Collector badCollector = input.badCollector();
        // Some temporary caches and indexes in the import
        IoMonitor writeMonitor = new IoMonitor(neoStore.getIoTracer());
        IdMapper idMapper = input.idMapper();
        IdGenerator idGenerator = input.idGenerator();
        nodeRelationshipCache = new NodeRelationshipCache(AUTO, config.denseNodeThreshold());
        StatsProvider memoryUsageStats = new MemoryUsageStatsProvider(nodeRelationshipCache, idMapper);
        InputIterable<InputNode> nodes = input.nodes();
        InputIterable<InputRelationship> relationships = input.relationships();
        InputIterable<InputNode> cachedNodes = cachedForSure(nodes, inputCache.nodes(MAIN, true));
        InputIterable<InputRelationship> cachedRelationships = cachedForSure(relationships, inputCache.relationships(MAIN, true));
        RelationshipStore relationshipStore = neoStore.getRelationshipStore();
        // Stage 1 -- nodes, properties, labels
        NodeStage nodeStage = new NodeStage(config, writeMonitor, nodes, idMapper, idGenerator, neoStore, inputCache, neoStore.getLabelScanStore(), storeUpdateMonitor, nodeRelationshipCache, memoryUsageStats);
        executeStage(nodeStage);
        if (idMapper.needsPreparation()) {
            executeStage(new IdMapperPreparationStage(config, idMapper, cachedNodes, badCollector, memoryUsageStats));
            PrimitiveLongIterator duplicateNodeIds = badCollector.leftOverDuplicateNodesIds();
            if (duplicateNodeIds.hasNext()) {
                executeStage(new DeleteDuplicateNodesStage(config, duplicateNodeIds, neoStore));
            }
        }
        // Stage 2 -- calculate dense node threshold
        CalculateDenseNodesStage calculateDenseNodesStage = new CalculateDenseNodesStage(withBatchSize(config, config.batchSize() * 10), relationships, nodeRelationshipCache, idMapper, badCollector, inputCache, neoStore);
        executeStage(calculateDenseNodesStage);
        importRelationships(nodeRelationshipCache, storeUpdateMonitor, neoStore, writeMonitor, idMapper, cachedRelationships, inputCache, calculateDenseNodesStage.getRelationshipTypes(Long.MAX_VALUE), calculateDenseNodesStage.getRelationshipTypes(100));
        // Release this potentially really big piece of cached data
        long peakMemoryUsage = totalMemoryUsageOf(idMapper, nodeRelationshipCache);
        long highNodeId = nodeRelationshipCache.getHighNodeId();
        idMapper.close();
        idMapper = null;
        nodeRelationshipCache.close();
        nodeRelationshipCache = null;
        new RelationshipGroupDefragmenter(config, executionMonitor).run(max(max(peakMemoryUsage, highNodeId * 4), mebiBytes(1)), neoStore, highNodeId);
        // Stage 6 -- count nodes per label and labels per node
        nodeLabelsCache = new NodeLabelsCache(AUTO, neoStore.getLabelRepository().getHighId());
        memoryUsageStats = new MemoryUsageStatsProvider(nodeLabelsCache);
        executeStage(new NodeCountsStage(config, nodeLabelsCache, neoStore.getNodeStore(), neoStore.getLabelRepository().getHighId(), countsUpdater, memoryUsageStats));
        // Stage 7 -- count label-[type]->label
        executeStage(new RelationshipCountsStage(config, nodeLabelsCache, relationshipStore, neoStore.getLabelRepository().getHighId(), neoStore.getRelationshipTypeRepository().getHighId(), countsUpdater, AUTO));
        // We're done, do some final logging about it
        long totalTimeMillis = currentTimeMillis() - startTime;
        executionMonitor.done(totalTimeMillis, format("%n") + storeUpdateMonitor.toString() + format("%n") + "Peak memory usage: " + bytes(peakMemoryUsage));
        log.info("Import completed, took " + Format.duration(totalTimeMillis) + ". " + storeUpdateMonitor);
    } catch (Throwable t) {
        log.error("Error during import", t);
        throw Exceptions.launderedException(IOException.class, t);
    } finally {
        if (nodeRelationshipCache != null) {
            nodeRelationshipCache.close();
        }
        if (nodeLabelsCache != null) {
            nodeLabelsCache.close();
        }
    }
}
Also used : NodeLabelsCache(org.neo4j.unsafe.impl.batchimport.cache.NodeLabelsCache) IdMapper(org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper) InputRelationship(org.neo4j.unsafe.impl.batchimport.input.InputRelationship) CountsAccessor(org.neo4j.kernel.impl.api.CountsAccessor) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) Collector(org.neo4j.unsafe.impl.batchimport.input.Collector) InputCache(org.neo4j.unsafe.impl.batchimport.input.InputCache) NodeRelationshipCache(org.neo4j.unsafe.impl.batchimport.cache.NodeRelationshipCache) InputNode(org.neo4j.unsafe.impl.batchimport.input.InputNode) BatchingNeoStores(org.neo4j.unsafe.impl.batchimport.store.BatchingNeoStores) IdGenerator(org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdGenerator) IOException(java.io.IOException) StatsProvider(org.neo4j.unsafe.impl.batchimport.stats.StatsProvider) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) IoMonitor(org.neo4j.unsafe.impl.batchimport.store.io.IoMonitor)

Example 10 with RelationshipStore

use of org.neo4j.kernel.impl.store.RelationshipStore in project neo4j by neo4j.

the class StoreIteratorRelationshipCursorTest method getRelationshipStore.

private RelationshipStore getRelationshipStore(RelationshipRecord relationshipRecord, RecordCursor recordCursor) {
    RelationshipStore relationshipStore = mock(RelationshipStore.class);
    when(recordCursor.acquire(anyLong(), any())).thenReturn(recordCursor);
    when(relationshipStore.newRecord()).thenReturn(relationshipRecord);
    when(relationshipStore.newRecordCursor(relationshipRecord)).thenReturn(recordCursor);
    return relationshipStore;
}
Also used : RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore)

Aggregations

RelationshipStore (org.neo4j.kernel.impl.store.RelationshipStore)16 Test (org.junit.Test)6 RelationshipRecord (org.neo4j.kernel.impl.store.record.RelationshipRecord)6 NeoStores (org.neo4j.kernel.impl.store.NeoStores)5 NodeStore (org.neo4j.kernel.impl.store.NodeStore)3 RecordCursor (org.neo4j.kernel.impl.store.RecordCursor)3 StoreFactory (org.neo4j.kernel.impl.store.StoreFactory)3 Node (org.neo4j.graphdb.Node)2 Transaction (org.neo4j.graphdb.Transaction)2 CountsComputer (org.neo4j.kernel.impl.store.CountsComputer)2 RelationshipGroupRecord (org.neo4j.kernel.impl.store.record.RelationshipGroupRecord)2 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)2 Lifespan (org.neo4j.kernel.lifecycle.Lifespan)2 InputRelationship (org.neo4j.unsafe.impl.batchimport.input.InputRelationship)2 File (java.io.File)1 IOException (java.io.IOException)1 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)1 ConsistencySummaryStatistics (org.neo4j.consistency.report.ConsistencySummaryStatistics)1 DependencyResolver (org.neo4j.graphdb.DependencyResolver)1 Relationship (org.neo4j.graphdb.Relationship)1