【发布时间】:2019-10-22 23:51:55
【问题描述】:
在 Gremlin/Tinkerpop 中,我想在 Vertex 上执行版本化 upsert。如果存在现有 Vertex,我只想在版本与版本号属性匹配时对其进行变异。
下面是尝试使用gremlinjs 执行此操作的代码。创建顶点失败,后续查询找不到。
(之前的版本有一个编译错误,但这是一个 javscriptism 未记录的语法问题)
[更新] 请参阅 cmets 以了解问题所在。 工作版本https://gist.github.com/pszabop/3b07fa7caadf1dbd86953a713ed96ce0
//
// o.id o.__version, and o.__lastUpdate have special meaning or are reserved
//
graphdb.upsertVertexVersioned = async function(type, o) {
const g = traversal().withRemote(this.connection);
let oldVersion;
if (!o.id) {
o.id = uuidv4();
}
if (!o.__version) {
o.__version = 0;
oldVersion = 0;
} else {
oldVersion = o.__version;
o.__version++;
}
o.__lastUpdate = Date.now();
// @see http://tinkerpop.apache.org/docs/current/recipes/#element-existence
// The pattern we are using is keys get copied into properties that can be used
// by the graph database for its work, and then the
// entire object is JSON serialized into a generic `obj` property.
// XXX TBD use graphson?
const v1 = await g.V().has(type, 'id', o.id)
.fold()
.coalesce(__.unfold(),
__.addV(type).property('id', o.id)
.property('version', o.__version)
).choose(__.values('version').is(oldVersion),
__.in_()
.property('lastUpdate', o.__lastUpdate) // updated properties go here
.property('version', o.__version) // updated properties go here
.property('obj', JSON.stringify(o)), // updated properties go here
__.out()
).next();
return o;
};
参考资料:
- Understanding different methods for adding edges with Gremlin-Python
- http://tinkerpop.apache.org/docs/current/recipes/
版本
- janusgraph/janusgraph:最新
- gremlinjs 3.4.4
【问题讨论】:
标签: gremlin tinkerpop janusgraph gremlinjs