在 Gremlin/Tinkerpop 中,我想在 Vertex 上执行版本化 upsert。如果有一个现有的顶点,我只想在版本与版本号属性匹配时对其进行变异。
下面是尝试使用gremlinjs
. 它无法创建顶点,以后的查询无法找到它。
(以前的版本有一个编译错误,但这是一个 javscriptism 未记录的语法问题)
[更新] 请参阅有关问题的答案的评论。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;
};
参考:
版本
- janusgraph/janusgraph:最新
- gremlinjs 3.4.4