问题描述
我通过 Java API 使用 orientDB 2.2.7 并创建了以下顶点:
I am using orientDB 2.2.7 via Java API and have created the following vertexes:
Vertex Car
Vertex Driver
Edge Co-Owner
对于每辆车最多只能有五个车主,我设法通过代码以一种简单的方式做到了这一点,我的问题是 orientDB 中是否有任何机制可以限制类型 co-owner can 的边数加入顶点车?
For each car it can only get a maximum of five owners, I have managed to do that by code in a trivial way, my question is there any mechanism in orientDB that can limit the number of edges of type co-owner can be added to vertex car?
推荐答案
我使用 Dynamic Hooks,希望和你的一样.
I made this little test case using Dynamic Hooks, I hope is similar to yours.
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函数:
then you need to create a JS function with the following code:
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 个边来测试它.
then you can test it by trying to add 6 edges.
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]
这篇关于限制可以添加到 orientDB 中的顶点的边数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!