0

I need to implement auto-increment functionality for neo4j. I did it with a user defined procedure (thaks to this answer) as follows:

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

But what i exactly need is a function with the same functionality.

How to do this with a function? Is it possible to call the procedure from user defined function? Could anybody point me to some useful example?

Thanks,

Vadim
  • 21
  • 3

1 Answers1

0

User-defined functions are read only, meaning that they cannot change the DB. So, since your procedure updates a UniqueId node, it cannot be turned into a function.

cybersam
  • 63,203
  • 6
  • 53
  • 76