Neo4j浏览器冻结了整个系统

时间:2017-10-10 13:14:35

标签: java spring spring-boot neo4j

我在Spring Boot项目中使用了嵌入式Neo4j实例(我分别使用Spring JPA和Neo4j而我没有使用Spring-Boot-Neo4j的东西)我希望可视化我使用从here下载的Neo4j浏览器构建的图表,添加了一个自定义init.coffee,允许它在节点内显示图像

这是我用来构建图表的代码。 " buildGraph"当我的一个RestControllers

收到请求时执行函数

(我将所有业务逻辑实现包含在内的原因是因为它可能有助于检测以错误方式创建/处理的关系/节点等)

@Component
public class GraphBuilder
{
    private String dbPath;
    private int numberOFConnectionsForKeyIndividuals;
    private String neo4jBoltAddress;

    @Autowired
    PersonService personService;

    private final GraphDatabaseService graphDb;

    public GraphBuilder(@Value("${dbPath}") String dbPath,
                        @Value("${neo4jBoltAddress}") String neo4jBoltAddress,
                        @Value("${numberOFConnectionsForKeyIndividuals}") int numberOFConnectionsForKeyIndividuals)
    {
        GraphDatabaseSettings.BoltConnector bolt = GraphDatabaseSettings.boltConnector( "0" );

        this.dbPath = dbPath;
        this.neo4jBoltAddress = neo4jBoltAddress;
        this.numberOFConnectionsForKeyIndividuals = numberOFConnectionsForKeyIndividuals;

        graphDb = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder( new File(dbPath) )
                .setConfig( bolt.type, "BOLT" )
                .setConfig( bolt.enabled, "true" )
                .setConfig( bolt.address, neo4jBoltAddress )
                .newGraphDatabase();

        registerShutdownHook( graphDb );
    }

    public void buildGraph()
    {

        List<Long> AsIDs = new ArrayList<>();

        Map<Long,Long> personIdToNodeMap = new HashMap<>();
        Map<String,List<Long>> nameToId = new HashMap<>();
        Map<Long,List<Association>> associationsMap = new HashMap<>();

        try ( Transaction tx = graphDb.beginTx() )
        {
            Schema schema = graphDb.schema();

            for(Person person : personService.findAllPersons())
            {
                Node personNode = graphDb.createNode(new Label() {
                    @Override
                    public String name() {
                        return "Person";
                    }
                });

                //mapping persons to their respective nodes
                personIdToNodeMap.put(person.getPersonId(),personNode.getId());

                //mapping names to the ids of the persons
                if(nameToId.get(person.getName()) == null)
                {
                    nameToId.put(person.getName(), new ArrayList<>());
                }
                nameToId.get(person.getName()).add(person.getPersonId());

                personNode.setProperty("Name", person.getName());

                for(int a = 0 ; a < person.getAliases().size() ; a++)
                {
                    personNode.setProperty("Alias " + a+1, person.getAliases().get(a).getAlias());
                }

                personNode.setProperty("Additional Name Information", person.getAdditionalNameInformation() != null ? person.getAdditionalNameInformation() : "");
                personNode.setProperty("Id", person.getPersonId());
                personNode.setProperty("Date of Birth", person.getDob() != null ? person.getDob() : "");

                for(int f = 0 ; f < person.getFacebook().size() ; f++)
                {
                    personNode.setProperty("Facebook " + f+1, person.getFacebook().get(f).getFacebookPage() + " (" + person.getFacebook().get(f).getAdditionalFacebookPageInformation() + ")");
                }

                personNode.setProperty("Additional Information", person.getInfo() != null ? person.getInfo() : "");
                personNode.setProperty("image_url","http://localhost:8888/files/"+person.getPictureFilePath());
                personNode.setProperty("Node Type", "Person");

                if(person.getAssociations().size() > numberOFConnectionsForKeyIndividuals)
                {
                    personNode.setProperty("Key_Individual","Yes");
                }

                for(A A : person.getAs())
                {
                    Node ANode = graphDb.createNode(new Label() {
                        @Override
                        public String name() {
                            return "A";
                        }
                    });

                    ANode.setProperty("A", A.getA());
                    //TODO elaborate more on the A with additional properties
                    ANode.setProperty("Node Type", "A");
                    personNode.createRelationshipTo( ANode, EdgeTypes.HAS_DONE );
                    ANode.setProperty("image_url","http://localhost:8888/images/A.png");


                    AsIDs.add(ANode.getId());
                }

                for(Association association : person.getAssociations())
                {
                    if(associationsMap.get(person.getPersonId()) == null)
                    {
                        associationsMap.put(person.getPersonId(), new ArrayList<>());
                    }
                    associationsMap.get(person.getPersonId()).add(association);
                }
            }

            //Validating and building the association edges

            //iterating through the nodes
            for(Long personFromId : associationsMap.keySet())
            {
                //iterating through the associations registered for the node
                for(Association associationFrom : associationsMap.get(personFromId))
                {
                    String personNameFrom = associationFrom.getPersonNameFrom();
                    String personNameTo = associationFrom.getPersonNameTo();

                    //iterating through the persons whose name matches the other end of the association
                    if(nameToId.get(personNameTo) != null)
                    {
                        for(Long personToId : nameToId.get(personNameTo))
                        {
                            //iterating through the associations of the person at the other end of the association
                            if(associationsMap.get(personToId) != null)
                            {
                                List<Association> associationsToRemove = new ArrayList<>();
                                for(Association associationTo : associationsMap.get(personToId))
                                {
                                    if(associationTo.getPersonNameTo().equals(personNameFrom) && nameToId.get(personNameFrom).contains(personFromId))
                                    {
                                        if(nameToId.get(personNameFrom).size() == 1)
                                        {
                                            Relationship relationship = graphDb.getNodeById(personIdToNodeMap.get(personFromId))
                                                    .createRelationshipTo( graphDb.getNodeById(personIdToNodeMap.get(personToId)), EdgeTypes.ASSOCIATES_WITH );
                                            if(associationFrom.getType() != null)
                                            {
                                                relationship.setProperty("Association Type",associationFrom.getType());
                                            }
                                            associationsToRemove.add(associationTo);
                                        }
                                        else
                                        {
                                            boolean alreadyConnected = false;
                                            for(Relationship rel : graphDb.getNodeById(personIdToNodeMap.get(personFromId)).getRelationships())
                                            {
                                                if( ( rel.getOtherNode(graphDb.getNodeById(personIdToNodeMap.get(personFromId))).
                                                        equals(graphDb.getNodeById(personIdToNodeMap.get(personToId))) ) )
                                                {
                                                    alreadyConnected = true;
                                                    break;
                                                }
                                            }

                                            if(!alreadyConnected)
                                            {
                                                Relationship relationship = graphDb.getNodeById(personIdToNodeMap.get(personFromId))
                                                        .createRelationshipTo( graphDb.getNodeById(personIdToNodeMap.get(personToId)), EdgeTypes.PROBABLY_ASSOCIATES_WITH );
                                                if(associationFrom.getType() != null)
                                                {
                                                    relationship.setProperty("Association Type",associationFrom.getType());
                                                }
                                            }
//                                            associationsToRemove.add(associationTo);
                                        }
                                    }
                                }
                                associationsMap.get(personToId).removeAll(associationsToRemove);
                            }
                        }
                    }
                }
            }

            tx.success();
        }

        Map<Long,List<String>> AToNamesMap = new HashMap<>();


        //detecting names referred in the A's description
        try(Transaction txAs = graphDb.beginTx() )
        {
            for(Long id : AsIDs)
            {
                Node ANode = graphDb.getNodeById(id);
                String A = (String) ANode.getProperty("A");
                for(String name : nameToId.keySet())
                {
                    if(A.contains(name)) {
                        if(AToNamesMap.get(id) == null)
                        {
                            AToNamesMap.put(id,new ArrayList<>());
                        }
                        AToNamesMap.get(id).add(name);
                    }
                }
            }

            List<Long> groupedAs = new ArrayList<>();

            for(Long id : AsIDs)
            {
                if(AToNamesMap.get(id)!= null && AToNamesMap.get(id).size() > 1)
                {
                    for(Long otherAID : AToNamesMap.keySet())
                    {
                        if(id != otherAID && !groupedAs.contains(otherAID) && !groupedAs.contains(id))
                        {
                            if(compareNamesLists(AToNamesMap.get(id), AToNamesMap.get(otherAID)))
                            {
                                Relationship rel = graphDb.getNodeById(otherAID).getSingleRelationship(EdgeTypes.HAS_DONE,Direction.INCOMING);
                                Node otherPersonNode = rel.getStartNode();
                                if(nameToId.get(otherPersonNode.getProperty("Name")) != null && nameToId.get(otherPersonNode.getProperty("Name")).size() > 1)
                                {
                                    otherPersonNode.createRelationshipTo(graphDb.getNodeById(id), EdgeTypes.HAS_PROBABLY_DONE);
                                }
                                else
                                {
                                    otherPersonNode.createRelationshipTo(graphDb.getNodeById(id), EdgeTypes.HAD_DONE);
                                }
                                rel.delete();
                                graphDb.getNodeById(otherAID).delete();
                                groupedAs.add(otherAID);
                            }
                        }
                    }
                }
                groupedAs.add(id);
            }

            txAs.success();
        }
    }


    private static void registerShutdownHook( final GraphDatabaseService graphDb )
    {
        // Registers a shutdown hook for the Neo4j instance so that it
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
        // running application).
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                graphDb.shutdown();
            }
        } );
    }
}

当我打开浏览器并将其连接到我为嵌入式neo4j公开的螺栓时,浏览器能够显示我的图形数据库中的所有节点(目前小于100),然后冻结,导致整个系统冻结(MacBook Pro 2016,16GB)。这种情况发生在3/5左右。 我知道我的交易方式并不理想,但正如我所说,所有这些处理都在neo4j浏览器启动之前发生。 你能告诉我如何解决这个问题吗? 你能在我的代码中看到任何东西(连接保持打开状态等)这是neo4j浏览器的已知问题吗?

0 个答案:

没有答案