我通过Java API使用orientDB 2.2.7并创建了以下顶点:
Vertex Car
Vertex Driver
Edge Co-Owner
对于每辆车,它最多只能获得五个所有者,我已经设法通过代码以一种微不足道的方式做到这一点,我的问题是orientDB中的任何机制都可以限制共同所有者类型的边缘数量被添加到顶点车?
答案 0 :(得分:2)
我使用Dynamic Hooks制作了这个小测试用例,我希望与你的相似。
create class Car extends V
create class Driver extends V
create class CoOwner extends E,OTriggered
ALTER CLASS CoOwner CUSTOM onBeforeCreate=createOwner
create property Car.ID string
create property Car.Model string
create property Car.Color string
create property Driver.Name string
create property Driver.Surname string
create property Driver.CF string
create index Car.ID UNIQUE_HASH_INDEX
create index Driver.CF UNIQUE_HASH_INDEX
create vertex Car set ID="AA666ZZ", Model="Fiat Panda", Color="Black"
create vertex Car set ID="BB222ZZ", Model="Ferrari 458 Italia", Color="Red"
create vertex Car set ID="CC954ZZ", Model="AlfaRomeo Giulietta", Color="Blue"
create vertex Driver set Name="Pino", Surname="Pasticcino", CF="PNOPSC1234567890"
create vertex Driver set Name="Giannaldo", Surname="Pippi", CF="GNLPIP1234567890"
create vertex Driver set Name="Pierromeo", Surname="Pasticcino", CF="PRMPSC1234567890"
create vertex Driver set Name="Adalbrigo", Surname="Brighi", CF="ALRBGH1234567890"
create vertex Driver set Name="Renfrancesco", Surname="Raldi", CF="RFCRLD1234567890"
create vertex Driver set Name="Caldanzio", Surname="Freddi", CF="CDZFRD1234567890"
create vertex Driver set Name="Roberdrigo", Surname="Brighi", CF="RBRBGH1234567890"
create vertex Driver set Name="Gorzorg", Surname="Il Distruggitore", CF="GRZIDG1234567890"
然后你需要使用以下代码创建一个JS函数:
var MAX_EDGES = 5;
var CUR_EDGES = doc.field('out').field('out_CoOwner').size();
print('\n--------------------------------------------------------------------');
print('\n\nMax Edges: ' + MAX_EDGES);
print('\nCoOwner edges: ' + CUR_EDGES);
if ( CUR_EDGES > MAX_EDGES ){
throw new java.lang.RuntimeException("Maximum number of edges reached.");
}
else {
print('OK!');
}
然后你可以尝试添加6条边来测试它。
create edge CoOwner from (select from Car where ID = "AA666ZZ") to (select from Driver where CF = "PNOPSC1234567890")
create edge CoOwner from (select from Car where ID = "AA666ZZ") to (select from Driver where CF = "RFCRLD1234567890")
create edge CoOwner from (select from Car where ID = "AA666ZZ") to (select from Driver where CF = "GNLPIP1234567890")
create edge CoOwner from (select from Car where ID = "AA666ZZ") to (select from Driver where CF = "CDZFRD1234567890")
create edge CoOwner from (select from Car where ID = "AA666ZZ") to (select from Driver where CF = "PRMPSC1234567890")
create edge CoOwner from (select from Car where ID = "AA666ZZ") to (select from Driver where CF = "RBRBGH1234567890")
控制台日志:
--------------------------------------------------------------------
Max Edges: 5
CoOwner edges: 1
OK!
--------------------------------------------------------------------
Max Edges: 5
CoOwner edges: 2
OK!
--------------------------------------------------------------------
Max Edges: 5
CoOwner edges: 3
OK!
--------------------------------------------------------------------
Max Edges: 5
CoOwner edges: 4
OK!
--------------------------------------------------------------------
Max Edges: 5
CoOwner edges: 5
OK!
--------------------------------------------------------------------
Max Edges: 5
CoOwner edges: 6
2016-08-22 17:46:45:292 SEVER Internal server error:
com.orientechnologies.orient.core.command.script.OCommandScriptException: Error on parsing script at position #2: Error on execution of the script
Script: createOwner
-------^
DB name="stack39062533"
--> javax.script.ScriptException: java.lang.RuntimeException: Maximum number of edges reached. in <eval> at line number 10 at column number 2
--> <eval>:10:2 java.lang.RuntimeException: Maximum number of edges reached.
--> java.lang.RuntimeException: Maximum number of edges reached. [ONetworkProtocolHttpDb]