How to apend value to vertex property use only gremlin api

I got a graph with vertexes, and i need to update vertex property by writing some string to the end of old value (append value to the end of old property)

Also i need to do it only by gemlin api?

I need to use only api, cause i send my query another server, where it will be execute

So, for example, if my question would be "set value if it does not present" - i know how to do that

traversal.coalesce(__.has(key), __.property(key, value));

But in my case, i cant use something like this:

Optional<? extends Property<Object>> property = traversal.properties(key).tryNext();
if (property.isPresent()) {
    Object oldValue = property.get().value();
    if (oldValue instanceof String && value instanceof String) {
        traversal.property(key, oldValue .toString() + value.toString());
    }
} else {
    traversal.property(key, value);
}

cause i must to send my query and only after that it will be executed

i try to do something like that:

traversal.coalesce(__.has(key).property(value, __.properties(key).next().value() + value),
__.property(key, value));

But of caurse it is something crazy, and also i cant check, if value is a String before append new value, in suth a case

How can i solve my question?

Upvotes: 1

Views: 761

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

Gremlin does not yet have string manipulation functions, so it is not possible to do this in Gremlin without use of lambdas:

gremlin> g.V().has('person','name','marko').sideEffect{it.get().property('name', it.get().value('name') + 'a')}
==>v[1]
gremlin> g.V().has('person','name','marko')
gremlin> g.V().has('person','name','markoa')
==>v[1]

The above example utilizes TinkerGraph, but depending on the graph database you are using you may see slightly different behavior and might need to write the lambda in a different way to get the same result (e.g. i don't think that approach would work with DS Graph because I don't think that you can call the property(key,value) method on a Vertex object with that implementation).

Obviously, you also have the option to do this mutation in two separate traversals:

gremlin> oldVal = g.V().has('person','name','marko').values('name').next()
==>marko
gremlin> oldVal = g.V().has('person','name','marko').property('name',oldVal + 'a')
==>v[1]
gremlin> g.V().has('person','name','marko')
gremlin> g.V().has('person','name','markoa')
==>v[1]

Hopefully, String manipulation functions as well as other type specific operations like Date functions will be made available in future releases. Some of that discussion is happening on TINKERPOP-2334.

Upvotes: 1

Related Questions