Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrientDB, Find Index Insertion point

I have a 'bill' vertex with 'date' property and made an automatic SB-tree not unique index on the bill's date for faster searches, now I want every inserted bill to have edge with next bill and create a linked nodes structure,

the solutions I got so far:

1- to use gremlin to calculate the minimal difference between the inserted bill's date and every other bill to get the closest bill, but it required me to scan all the bills and didn't use the index

2- I could get the keys of the index and use Collections.binarySearch() and get the index of insertion point, hence get the adjacent bills,

but I wonder is there any other better solutions to have linked bills, and how can I find Insertion point in the OrientDB index using SQL, any ideas?

like image 399
DrAhmedJava Avatar asked Jan 29 '26 17:01

DrAhmedJava


1 Answers

I tried your example with a simple DB of two classes:

  • Bill (extends V) with the property billDate as datetime;
  • nextBill (extends E).

with this query

create vertex Bill set billDate=sysdate(), in_nextBill=(select @rid from Bill where billDate in (select max(billDate) from Bill))

you can at the same time create the bill and the edge referred to the previous.

EDITED

I created a Javascript Function that removes the edge between two records and inserts a new record (with relative edges) between the previous two. The function accepts three input parameters: * date1: datetime format; * date2: datetime format; * newBillDate: datetime format is the date of new bill you want to insert between the previous two ;

var g=orient.getGraph();
var d1=g.command('sql','select from Bill where billDate in "'+date1+'"');
var d2=g.command('sql','select from Bill where billDate in "'+date2+'"');
var startDate=d1[0];
var endDate=d2[0];
if(endDate.getRecord().field("billDate").getTime()<startDate.getRecord().field("billDate").getTime()){
  var temp=endDate;
  endDate=startDate;
  startDate=temp;
}
var selectEdge=g.command('sql','select from nextBill where in='+endDate.getId()+' and out='+startDate.getId());
g.command('sql','delete edge '+selectEdge[0].getId());
var newIns=g.command('sql','create vertex Bill set billDate="'+newBillDate+'"');
g.commit();
g.command('sql','create edge nextBill from '+startDate.getRecord().getIdentity()+' to '+newIns.getRecord().getIdentity());
g.command('sql','create edge nextBill from '+newIns.getRecord().getIdentity()+' to '+endDate.getRecord().getIdentity());
like image 156
LucaS Avatar answered Feb 01 '26 14:02

LucaS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!