我需要为neo4j实现自动增量功能。 我使用用户定义的过程(感谢this answer)进行了如下操作:
public class Sequence {
public @Context GraphDatabaseService db;
@Procedure(name = "sequence.id", mode = Mode.WRITE)
@Description("sequence.id(name, length) - return next 'length' value(s) form sequence with name 'name'")
public Stream<ListResult> sequence_id(@Name("name") final String name,
@Name(value = "length", defaultValue = "1") Long length) {
try (
final Result results = db.execute(
"WITH {length} as length\n" +
"MERGE (id:UniqueId{name:{name}})\n" +
"SET id.count = coalesce(id.count, 1) + length\n" +
"WITH length, id.count - length AS base\n" +
"UNWIND range(0, length - 1) AS idx\n" +
"RETURN collect(base + idx) AS value", with("name", name, "length", length))) {
return results.<List>columnAs("value").map(ListResult::new).stream();
}
private Map<String, Object> with(String key1, Object value1, String key2, Object value2 ) {
Map<String, Object> map = new HashMap<>();
map.put(key1, value1);
map.put(key2, value2);
return map;
}
...
}
但我真正需要的是具有相同功能的功能。
如何使用功能执行此操作?是否可以从用户定义的函数调用该过程?有人能指出一些有用的例子吗?
谢谢,