<commit_before>package org.neo4j.impl.batchinsert;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

import org.neo4j.api.core.Direction;
import org.neo4j.api.core.NeoService;
import org.neo4j.api.core.Node;
import org.neo4j.api.core.NotFoundException;
import org.neo4j.api.core.NotInTransactionException;
import org.neo4j.api.core.Relationship;
import org.neo4j.api.core.RelationshipType;
import org.neo4j.api.core.ReturnableEvaluator;
import org.neo4j.api.core.StopEvaluator;
import org.neo4j.api.core.Transaction;
import org.neo4j.api.core.Traverser;
import org.neo4j.api.core.Traverser.Order;
import org.neo4j.impl.cache.LruCache;
import org.neo4j.impl.nioneo.store.InvalidRecordException;

class NeoServiceBatchImpl implements NeoService
{
    final BatchInserterImpl batchInserter;
    
    private final LruCache<Long,NodeBatchImpl> nodes = 
        new LruCache<Long,NodeBatchImpl>( "NodeCache", 10000, null )
        {
            @Override
            public void elementCleaned( NodeBatchImpl node )
            {
                Map<String,Object> properties = node.getProperties();
                if ( properties != null )
                {
                    batchInserter.setNodeProperties( node.getId(), properties );
                }
            }
        };
        
    private final LruCache<Long,RelationshipBatchImpl> rels = 
        new LruCache<Long,RelationshipBatchImpl>( "RelCache", 100000, null )
        {
            @Override
            public void elementCleaned( RelationshipBatchImpl rel )
            {
                Map<String,Object> properties = rel.getProperties();
                if ( properties != null )
                {
                    batchInserter.setRelationshipProperties( rel.getId(), 
                        properties );
                }
            }
        };
    
    NeoServiceBatchImpl( BatchInserterImpl batchInserter )
    {
        this.batchInserter = batchInserter;
    }
    
    BatchInserterImpl getBatchInserter()
    {
        return batchInserter;
    }

    public Transaction beginTx()
    {
        return new FakeTransaction();
    }

    public Node createNode()
    {
        long id = batchInserter.createNode( null );
        NodeBatchImpl node = new NodeBatchImpl( id, this, emptyProps() );
        nodes.put( id, node );
        return node;
    }
    
    static Map<String,Object> emptyProps()
    {
        return new HashMap<String,Object>();
    }

    public boolean enableRemoteShell()
    {
        return false;
    }

    public boolean enableRemoteShell( Map<String,Serializable> initialProperties )
    {
        return false;
    }

    public Iterable<Node> getAllNodes()
    {
        throw new UnsupportedOperationException( "Batch inserter mode" );
    }

    public Node getNodeById( long id )
    {
        NodeBatchImpl node = nodes.get( id );
        if ( node == null )
        {
            try
            {
                node = new NodeBatchImpl( id, this, 
                    batchInserter.getNodeProperties( id ) );
                nodes.put( id, node );
            }
            catch ( InvalidRecordException e )
            {
                throw new NotFoundException( e );
            }
        }
        return node;
    }

    public Node getReferenceNode()
    {
        return getNodeById( 0 );
    }

    public Relationship getRelationshipById( long id )
    {
        RelationshipBatchImpl rel = rels.get( id );
        if ( rel == null )
        {
            try
            {
                SimpleRelationship simpleRel = 
                    batchInserter.getRelationshipById( id );
                Map<String,Object> props = 
                    batchInserter.getRelationshipProperties( id );
                rel = new RelationshipBatchImpl( simpleRel, this, props ); 
                rels.put( id, rel );
            }
            catch ( InvalidRecordException e )
            {
                throw new NotFoundException( e );
            }
        }
        return rel;
    }

    public Iterable<RelationshipType> getRelationshipTypes()
    {
        throw new UnsupportedOperationException( "Batch inserter mode" );
    }

    public void shutdown()
    {
        batchInserter.shutdown();
    }

    static class FakeTransaction implements Transaction
    {
        public void failure()
        {
            throw new NotInTransactionException( "Batch insert mode, " + 
                "failure is not an option." );
        }

        public void finish()
        {
        }

        public void success()
        {
        }
    }
    
    private static class NodeBatchImpl implements Node
    {
        private final NeoServiceBatchImpl neoService;
        
        private final long id;
        private final Map<String,Object> properties;
        
        NodeBatchImpl( long id, NeoServiceBatchImpl neoService, 
            Map<String,Object> properties )
        {
            this.id = id;
            this.neoService = neoService;
            this.properties = properties;
        }
        
        public Relationship createRelationshipTo( Node otherNode, 
            RelationshipType type )
        {
            long relId = neoService.getBatchInserter().createRelationship( id, 
                otherNode.getId(), type, null );
            RelationshipBatchImpl rel = new RelationshipBatchImpl( 
                new SimpleRelationship( (int)relId, (int) id, 
                    (int) otherNode.getId(), type ), neoService, emptyProps() );
            neoService.addRelationshipToCache( id, rel );
            return rel;
        }
        
        Map<String,Object> getProperties()
        {
            return properties;
        }

        public void delete()
        {
            throw new UnsupportedOperationException();
        }

        public long getId()<commit_msg>Fixed cache bug caching on node id instead of rel id.<commit_after>package org.neo4j.impl.batchinsert;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

import org.neo4j.api.core.Direction;
import org.neo4j.api.core.NeoService;
import org.neo4j.api.core.Node;
import org.neo4j.api.core.NotFoundException;
import org.neo4j.api.core.NotInTransactionException;
import org.neo4j.api.core.Relationship;
import org.neo4j.api.core.RelationshipType;
import org.neo4j.api.core.ReturnableEvaluator;
import org.neo4j.api.core.StopEvaluator;
import org.neo4j.api.core.Transaction;
import org.neo4j.api.core.Traverser;
import org.neo4j.api.core.Traverser.Order;
import org.neo4j.impl.cache.LruCache;
import org.neo4j.impl.nioneo.store.InvalidRecordException;

class NeoServiceBatchImpl implements NeoService
{
    final BatchInserterImpl batchInserter;
    
    private final LruCache<Long,NodeBatchImpl> nodes = 
        new LruCache<Long,NodeBatchImpl>( "NodeCache", 10000, null )
        {
            @Override
            public void elementCleaned( NodeBatchImpl node )
            {
                Map<String,Object> properties = node.getProperties();
                if ( properties != null )
                {
                    batchInserter.setNodeProperties( node.getId(), properties );
                }
            }
        };
        
    private final LruCache<Long,RelationshipBatchImpl> rels = 
        new LruCache<Long,RelationshipBatchImpl>( "RelCache", 100000, null )
        {
            @Override
            public void elementCleaned( RelationshipBatchImpl rel )
            {
                Map<String,Object> properties = rel.getProperties();
                if ( properties != null )
                {
                    batchInserter.setRelationshipProperties( rel.getId(), 
                        properties );
                }
            }
        };
    
    NeoServiceBatchImpl( BatchInserterImpl batchInserter )
    {
        this.batchInserter = batchInserter;
    }
    
    BatchInserterImpl getBatchInserter()
    {
        return batchInserter;
    }

    public Transaction beginTx()
    {
        return new FakeTransaction();
    }

    public Node createNode()
    {
        long id = batchInserter.createNode( null );
        NodeBatchImpl node = new NodeBatchImpl( id, this, emptyProps() );
        nodes.put( id, node );
        return node;
    }
    
    static Map<String,Object> emptyProps()
    {
        return new HashMap<String,Object>();
    }

    public boolean enableRemoteShell()
    {
        return false;
    }

    public boolean enableRemoteShell( Map<String,Serializable> initialProperties )
    {
        return false;
    }

    public Iterable<Node> getAllNodes()
    {
        throw new UnsupportedOperationException( "Batch inserter mode" );
    }

    public Node getNodeById( long id )
    {
        NodeBatchImpl node = nodes.get( id );
        if ( node == null )
        {
            try
            {
                node = new NodeBatchImpl( id, this, 
                    batchInserter.getNodeProperties( id ) );
                nodes.put( id, node );
            }
            catch ( InvalidRecordException e )
            {
                throw new NotFoundException( e );
            }
        }
        return node;
    }

    public Node getReferenceNode()
    {
        return getNodeById( 0 );
    }

    public Relationship getRelationshipById( long id )
    {
        RelationshipBatchImpl rel = rels.get( id );
        if ( rel == null )
        {
            try
            {
                SimpleRelationship simpleRel = 
                    batchInserter.getRelationshipById( id );
                Map<String,Object> props = 
                    batchInserter.getRelationshipProperties( id );
                rel = new RelationshipBatchImpl( simpleRel, this, props ); 
                rels.put( id, rel );
            }
            catch ( InvalidRecordException e )
            {
                throw new NotFoundException( e );
            }
        }
        return rel;
    }

    public Iterable<RelationshipType> getRelationshipTypes()
    {
        throw new UnsupportedOperationException( "Batch inserter mode" );
    }

    public void shutdown()
    {
        batchInserter.shutdown();
    }

    static class FakeTransaction implements Transaction
    {
        public void failure()
        {
            throw new NotInTransactionException( "Batch insert mode, " + 
                "failure is not an option." );
        }

        public void finish()
        {
        }

        public void success()
        {
        }
    }
    
    private static class NodeBatchImpl implements Node
    {
        private final NeoServiceBatchImpl neoService;
        
        private final long id;
        private final Map<String,Object> properties;
        
        NodeBatchImpl( long id, NeoServiceBatchImpl neoService, 
            Map<String,Object> properties )
        {
            this.id = id;
            this.neoService = neoService;
            this.properties = properties;
        }
        
        public Relationship createRelationshipTo( Node otherNode, 
            RelationshipType type )
        {
            long relId = neoService.getBatchInserter().createRelationship( id, 
                otherNode.getId(), type, null );
            RelationshipBatchImpl rel = new RelationshipBatchImpl( 
                new SimpleRelationship( (int)relId, (int) id, 
                    (int) otherNode.getId(), type ), neoService, emptyProps() );
            neoService.addRelationshipToCache( relId, rel );
            return rel;
        }
        
        Map<String,Object> getProperties()
        {
            return properties;
        }

        public void delete()
        {
            throw new UnsupportedOperationException();
        }

        public long getId()
{'fraction_non_alphanumeric': 0.0818600754084625, 'fraction_numerical': 0.004524507750314202, 'mean_word_length': 1.777105630525826, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2651136', 'n_tokens_mistral': 3271, 'n_tokens_neox': 3113, 'n_words': 705}