[
https://issues.apache.org/jira/browse/TINKERPOP-479?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16061852#comment-16061852
]
Robert Dale commented on TINKERPOP-479:
---------------------------------------
Continuing the previous conversations. I think {{addV()}} should only do what
it claims: add. Adding boolean switches just muddies it. Get or create should
be its own step, like {{mergeV()}} or {{upsertV()}}, but otherwise behave like
{{addV()}}.
{noformat}
g.V().upsertV("person").property("name","jon")
{noformat}
This acts like {{g.V().has("person", "name", "jon")}} if it doesn't exist, then
create it like {{g.addV("person").property("name","jon")}}.
By using {{property()}}, it gets around the problem of {{has('age', gt(4))}}
taking predicates.
A problem that this has is how to delineate where the primary (or filter) keys
end and the mutations begin. {{barrier()}} or {{cap()}} could be used because
it iterates the traversal up to it. Or taking a suggestion from above, another
new step:
{noformat}
g.V().upsertV("person").property("name","jon").getOrCreate().property('age', 3)
{noformat}
Then to handle the desire to know if the vertex was newly created or not,
{{getOrCreate()}} could take a parameter like {{cap()}} to store that
side-effect.
{noformat}
g.V().upsertV("person").property("name","jon").getOrCreate('a').property('age',
3).choose(cap('a'),property('createdTime', new Date()))
{noformat}
I might not be using {{cap()}} correctly but the idea is that it gets the
side-effect 'a' and if it's true, meaning it did not exist but was now created,
then set the 'createdTime' property.
> Consider Providing "getOrCreate" Functionality
> ----------------------------------------------
>
> Key: TINKERPOP-479
> URL: https://issues.apache.org/jira/browse/TINKERPOP-479
> Project: TinkerPop
> Issue Type: Improvement
> Components: structure
> Affects Versions: 3.0.2-incubating
> Reporter: stephen mallette
>
> One of the most commonly written functions used is good ol' "getOrCreate"
> where you want to get a {{Vertex}} if it exists or create it with supplied
> properties if it does not. We currently have a "helper" function for this on
> {{ElementHelper}}
> https://github.com/tinkerpop/tinkerpop3/blob/6d0f00865f673cb0739f6f310e1868425f732924/gremlin-core/src/main/java/com/tinkerpop/gremlin/structure/util/ElementHelper.java#L62
> but perhaps it is time to treat this issue as a first class citizen as part
> of the Graph API. I think that some vendors might actually be able to
> optimize this function as well.
> Another aspect of "getOrCreate" is "upsert" as well as options to ensure
> uniqueness. All of these things we've at some point or another built
> variations of outside of TinkerPop for applications, data loading, etc.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)