您如何使用gremlin javascript中的克隆功能?

时间:2019-09-19 23:29:25

标签: javascript gremlin

尝试保存和重用一个query- clone();

我已经尝试过query.clone()clone(query)statics.clone(query)graphPredicate.clone(query)

参考:http://tinkerpop.apache.org/docs/current/recipes/#traversal-component-reuse


const { Graph } = gremlin.structure;
const { P: graphPredicate, statics } = gremlin.process;
const { _ } = gremlin.process.statics;

  const g = graph.traversal().withRemote(gremlinConnection);


const query = statics.hasLabel(‘Movie’).hasId(gt(‘C’))

const count = g.V().flatMap(query.clone()).count().next()```

`Exception: TypeError: Cannot read property 'clone' of undefined`

1 个答案:

答案 0 :(得分:0)

javascript中没有clone()。但是您可以轻松创建一个:

function clone(traversal) {
  return new gremlin.process.GraphTraversal(
    traversal.graph,
    traversal.traversalStrategies,
    new gremlin.process.Bytecode(traversal.bytecode)
  )
}

您还可以将其添加到遍历本身:http://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript-dsl

class MyTraversal extends gremlin.process.GraphTraversal {
  constructor(graph, traversalStrategies, bytecode) {
    super(graph, traversalStrategies, bytecode)
  }

  clone() {
    return new MyTraversal(
      this.graph,
      this.traversalStrategies,
      new gremlin.process.Bytecode(this.bytecode)
    )
  }
}

class MyTraversalSource extends gremlin.process.GraphTraversalSource {
  constructor(graph, strategies, bytecode) {
    super(graph, strategies, bytecode, MyTraversalSource, MyTraversal)
  }
}

const g = traversal(MyTraversalSource).withRemote(dc)