2016-08-21 77 views
0

我通過Java API使用orientDB 2.2.7,並已建立了以下頂點:極限邊緣的數量可以被添加到一個頂點orientDB

Vertex Car 
Vertex Driver 
Edge Co-Owner 

每節車廂只可以得到最多五個所有者,我已經設法通過代碼來做到這一點,我的問題是在orientDB中有什麼機制可以限制類型共同所有者的邊的數量可以添加到頂點車?

+0

你好,我不認爲有一個在OrientDB這樣的功能,但你可以寫一個服務器端的JS功能,你會從你的Java應用程序 –

+0

或撥打也許這可能是有用的http://orientdb.com/docs/master/Hook.html –

回答

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] 
+0

謝謝你的回答,這是偉大的,但我的問題是如何用Java API來做到這一點,因爲限制是商業方面而不是比代碼方面,所以用戶可以改變它而無需編寫代碼,我會檢查Java鉤子,再次謝謝你 –

相關問題