2017-05-30 49 views
0

我需要實現neo4j的自動增量功能。 我有一個用戶定義的過程(thaks到this answer)做了如下:如何使用用戶功能在neo4j中實現自動增量

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; 
    } 
    ... 
} 

,但我究竟需要的是具有相同功能的函數。

如何用函數做到這一點?是否可以從用戶定義的函數調用過程?有人能指點我一些有用的例子嗎?

感謝,

回答

0

User-defined functions是隻讀的,這意味着它們不能改變DB。所以,由於你的程序更新了一個UniqueId節點,所以它不能變成一個函數。

相關問題