<commit_before>/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.admin.nioneo;

import org.neo4j.kernel.impl.nioneo.store.NodeRecord;
import org.neo4j.kernel.impl.nioneo.store.NodeStoreAccess;
import org.neo4j.kernel.impl.nioneo.store.Record;
import org.neo4j.kernel.impl.nioneo.store.RelationshipRecord;
import org.neo4j.kernel.impl.nioneo.store.RelationshipStoreAccess;
import org.neo4j.shell.AppCommandParser;
import org.neo4j.shell.Output;
import org.neo4j.shell.Session;
import org.neo4j.shell.ShellException;

public class FixRelChain extends NioneoApp
{

    public String execute( AppCommandParser parser, Session session, Output out ) throws ShellException
    {
        String arg = parser.arguments().get( 0 );
        int nodeId = Integer.parseInt( arg );
        return checkChain( getServer(), out, nodeId );
    }

    static String checkChain( NioneoServer server, Output out, long nodeId ) throws ShellException
    {
        NodeStoreAccess nodeStore = server.getNodeStore();
        NodeRecord nodeRecord = nodeStore.getRecord( nodeId );
        RelationshipStoreAccess relStore = server.getRelStore();
        long nextRelId = nodeRecord.getNextRel();
        while ( nextRelId != Record.NO_PREV_RELATIONSHIP.intValue() )
        {
            RelationshipRecord record = relStore.getRecord( nextRelId );
            if ( record.getFirstNode() == nodeId )
            {
                NodeRecord otherNode = nodeStore.forceGetRecord( record.getSecondNode() );
                if ( !otherNode.inUse() )
                {
                    relDelete( server, record );
                    System.out.println( record );
                }
                nextRelId = record.getFirstNextRel();
            }
            else if ( record.getSecondNode() == nodeId )
            {
                NodeRecord otherNode = nodeStore.forceGetRecord( record.getFirstNode() );
                if ( !otherNode.inUse() )
                {
                    relDelete( server, record );
                    System.out.println( record );
                }
                nextRelId = record.getSecondNextRel();
            }
        }
        return null;
    }

    static void relDelete( NioneoServer server, RelationshipRecord rel )
    {
        RelationshipStoreAccess relStore = server.getRelStore();
        if ( rel.getFirstPrevRel() != Record.NO_NEXT_RELATIONSHIP.intValue() )<commit_msg>Fixed infinte loop.<commit_after>/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.admin.nioneo;

import org.neo4j.kernel.impl.nioneo.store.NodeRecord;
import org.neo4j.kernel.impl.nioneo.store.NodeStoreAccess;
import org.neo4j.kernel.impl.nioneo.store.Record;
import org.neo4j.kernel.impl.nioneo.store.RelationshipRecord;
import org.neo4j.kernel.impl.nioneo.store.RelationshipStoreAccess;
import org.neo4j.shell.AppCommandParser;
import org.neo4j.shell.Output;
import org.neo4j.shell.Session;
import org.neo4j.shell.ShellException;

public class FixRelChain extends NioneoApp
{

    public String execute( AppCommandParser parser, Session session, Output out ) throws ShellException
    {
        String arg = parser.arguments().get( 0 );
        int nodeId = Integer.parseInt( arg );
        return checkChain( getServer(), out, nodeId );
    }

    static String checkChain( NioneoServer server, Output out, long nodeId ) throws ShellException
    {
        NodeStoreAccess nodeStore = server.getNodeStore();
        NodeRecord nodeRecord = nodeStore.getRecord( nodeId );
        RelationshipStoreAccess relStore = server.getRelStore();
        long nextRelId = nodeRecord.getNextRel();
        while ( nextRelId != Record.NO_PREV_RELATIONSHIP.intValue() )
        {
            RelationshipRecord record = relStore.getRecord( nextRelId );
            if ( record.getFirstNode() == nodeId )
            {
                NodeRecord otherNode = nodeStore.forceGetRecord( record.getSecondNode() );
                if ( !otherNode.inUse() )
                {
                    relDelete( server, record );
                    System.out.println( record );
                }
                nextRelId = record.getFirstNextRel();
            }
            else if ( record.getSecondNode() == nodeId )
            {
                NodeRecord otherNode = nodeStore.forceGetRecord( record.getFirstNode() );
                if ( !otherNode.inUse() )
                {
                    relDelete( server, record );
                    System.out.println( record );
                }
                nextRelId = record.getSecondNextRel();
            }
            else
            {
                nextRelId = Record.NO_NEXT_RELATIONSHIP.intValue();
            }
        }
        return null;
    }

    static void relDelete( NioneoServer server, RelationshipRecord rel )
    {
        RelationshipStoreAccess relStore = server.getRelStore();
        if ( rel.getFirstPrevRel() != Record.NO_NEXT_RELATIONSHIP.intValue() )
{'fraction_non_alphanumeric': 0.08847515002308047, 'fraction_numerical': 0.006770272349592245, 'mean_word_length': 2.4871244635193133, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7482954', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1734, 'n_words': 503}