0%

Titan 边标签 SIMPLE 和 ONE2ONE 的区别

昨天在读 Titan 文档关于边的多样性时看到两个设置,分别是 SIMPLEONE2ONE,这两个设置的介绍有点绕,我琢磨了很久,最终通过程序弄明白了这两种模式的区别。

SIMPLE: Allows at most one edge of such label between any pair of vertices. In other words, the graph is a simple graph with respect to the label. Ensures that edges are unique for a given label and pairs of vertices.
ONE2ONE: Allows at most one incoming and one outgoing edge of such label on any vertex in the graph. The edge label marriedTo is an example with ONE2ONE multiplicity since a person is married to exactly one other person.

先给结论,一张图来解释:

程序验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
gremlin> graph = TitanFactory.open("conf/titan.properties")
==>standardtitangraph[cassandra:[172.24.8.84]]
gremlin> mgmt = graph.openManagement()
==>com.thinkaurelius.titan.graphdb.database.management.ManagementSystem@d7109be
gremlin> mgmt.makeEdgeLabel('simple').multiplicity(SIMPLE).make()
==>simple
gremlin> mgmt.makeEdgeLabel('one2one').multiplicity(ONE2ONE).make()
==>one2one
gremlin> mgmt.commit()

gremlin> a = graph.addVertex("name", "a")
==>v[4152]
gremlin> b = graph.addVertex("name", "b")
==>v[8248]
gremlin> c = graph.addVertex("name", "c")
==>v[4128]
gremlin> d = graph.addVertex("name", "d")
==>v[4328]

先分别创建 multiplicitySIMPLEONE2ONEEdge Label,然后创建 a b c d 四个点。

首先来验证 SIMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
gremlin> a.addEdge("simple", b)
==>e[1zb-37c-t1-6d4][4152-simple->8248]
gremlin> a.addEdge("simple", b)
An edge with the given label already exists between the pair of vertices and the label [simple] is simple
Display stack trace? [yN] n
gremlin> b.addEdge("simple", a)
==>e[2dj-6d4-t1-37c][8248-simple->4152]
gremlin> a.addEdge("simple", c)
==>e[2rr-37c-t1-36o][4152-simple->4128]
gremlin> a.addEdge("simple", d)
==>e[35z-37c-t1-3c8][4152-simple->4328]
gremlin> a.addEdge("simple", c)
An edge with the given label already exists between the pair of vertices and the label [simple] is simple
Display stack trace? [yN] n
gremlin> c.addEdge("simple", b)
==>e[16s-36o-t1-6d4][4128-simple->8248]

得到的结论是,只要两点之间不存在相同方向的 SIMPLE 边就可以。

然后验证 ONE2ONE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
gremlin> a.addEdge("one2one", b)
==>e[3k7-37c-1lh-6d4][4152-one2one->8248]
gremlin> a.addEdge("one2one", c)
An edge with the given label already exists on the out-vertex and the label [one2one] is out-unique
Display stack trace? [yN] n
gremlin> a.addEdge("one2one", d)
An edge with the given label already exists on the out-vertex and the label [one2one] is out-unique
Display stack trace? [yN] n
gremlin> d.addEdge("one2one", a)
==>e[17h-3c8-1lh-37c][4328-one2one->4152]
gremlin> d.addEdge("one2one", b)
An edge with the given label already exists on the out-vertex and the label [one2one] is out-unique
Display stack trace? [yN] n
gremlin> b.addEdge("one2one", c)
==>e[3yf-6d4-1lh-36o][8248-one2one->4128]

结论是,一个点上的 ONE2ONE 边只能有一次 in 和一次 out