neo4j服务器插件开发的最佳实践是什么?

时间:2012-11-24 18:17:16

标签: neo4j

我能够使用这些文档在我的服务器上安装示例插件:http://docs.neo4j.org/chunked/milestone/server-plugins.html

现在我想开发自己的插件,但我不确定如何调试和单元测试它们。我在哪里可以阅读有关服务器插件开发的这个和最佳实践的更多信息?

2 个答案:

答案 0 :(得分:1)

您可以像简单的java项目一样开发插件,测试插件很简单,只需在单元测试中实例化它们,然后将Node,GraphDatabaseService和参数传递给插件方法并检查结果。

答案 1 :(得分:1)

我的插件:

package ru.a360.neo4j.plugins;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.server.plugins.*;
import org.neo4j.server.rest.repr.Representation;
import org.neo4j.server.rest.repr.ValueRepresentation;
import org.neo4j.tooling.GlobalGraphOperations;

import java.util.logging.Logger;

@Description("An extension to the Neo4j Server for find routes between two nodes")
public class WarmUp extends ServerPlugin
{
    private Logger logger = Logger.getLogger(WarmUp.class.getName());

    public WarmUp()
    {
    }

    @Name("warm_up")
    @Description("Warm up all nodes and relationships")
    @PluginTarget(GraphDatabaseService.class)
    public Representation warmUp(@Source GraphDatabaseService graphDb)
    {
        long t0 = System.currentTimeMillis();
        int relCount = 0;
        int nodesCount = 0;
        GlobalGraphOperations ggo = GlobalGraphOperations.at(graphDb);
        for (Node n: ggo.getAllNodes())
        {
            for (String prop: n.getPropertyKeys())
            {
                n.getProperty(prop);
            }
            nodesCount++;
        }
        for (Relationship rel: ggo.getAllRelationships())
        {
            for (String prop: rel.getPropertyKeys())
            {
                rel.getProperty(prop);
            }
            relCount++;
        }
        logger.info("warmup;" + (System.currentTimeMillis() - t0) / 1000.0 );
        return ValueRepresentation.string("WARMED UP " + nodesCount + " NODES AND " + relCount + " RELATIONSHIPS");
    }
}

main / java / META-INF / services / org.neo4j.server.plugins.ServerPlugin中有新字符串:

ru.a360.neo4j.plugins.WarmUp

我想写这样的测试(test / java / ru / a360 / neo4j / plugins / TestNeo.java):

package ru.a360.neo4j.plugins;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestNeo {

    @Before
    public void prepareTestData()
    {
    }

    @After
    public void destroyTestDatabase()
    {
    }

    @Test
    public void myTest1()
    {
    }
}