Neo4j:遍历查询超时

在过去的几周中,我一直在花一些业余时间来创建一个应用程序,该应用程序从Open Roads数据生成运行路线-当然已转换并导入到Neo4j中!

我创建了一个用户定义的过程,该过程结合了几个最短路径查询,但是如果它们花费的时间太长,我想退出所有这些最短路径搜索。 我的代码没有超时看起来像这样:

StandardExpander orderedExpander = new OrderedByTypeExpander()
    .add( RelationshipType.withName( "CONNECTS" ), Direction.BOTH );
 
PathFinder<Path> shortestPathFinder = GraphAlgoFactory.shortestPath( expander, 250 );
 
...

我们可以在几个地方检查经过的时间,但是对我来说, 扩展器中expand方法似乎很明显。 我编写了自己的Expander类,如下所示:

public class TimeConstrainedExpander implements PathExpander
{
    private final StandardExpander expander;
    private final long startTime;
    private final Clock clock;
    private int pathsExpanded = 0;
    private long timeLimitInMillis;
 
    public TimeConstrainedExpander( StandardExpander expander, Clock clock, long timeLimitInMillis )
    {
        this.expander = expander;
        this.clock = clock;
        this.startTime = clock.instant().toEpochMilli();
        this.timeLimitInMillis = timeLimitInMillis;
    }
 
    @Override
    public Iterable<Relationship> expand( Path path, BranchState state )
    {
        long timeSoFar = clock.instant().toEpochMilli() - startTime;
        if ( timeSoFar > timeLimitInMillis )
        {
            return Collections.emptyList();
        }
 
        return expander.expand( path, state );
    }
 
    @Override
    public PathExpander reverse()
    {
        return expander.reverse();
    }
}

现在需要更新较早版本的代码片段,以使用我们的新类,这不太麻烦:

StandardExpander orderedExpander = new OrderedByTypeExpander()
    .add( RelationshipType.withName( "CONNECTS" ), Direction.BOTH );
 
TimeConstrainedExpander expander = new TimeConstrainedExpander(orderedExpander, 
    Clock.systemUTC(), 200);
 
PathFinder<Path> shortestPathFinder = GraphAlgoFactory.shortestPath( expander, 250 );
...

我不确定这是否是实现我想要的最佳方法,但是在其他几种方法失败之后,至少这种方法确实有效!

翻译自: https://www.javacodegeeks.com/2017/11/neo4j-traversal-query-timeout.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值