Search code examples
javadb4o

db4o query optimisation for a scheduling application


I'm just getting started with db4o in a scheduling application and I'm looking for an efficient way to retrieve rooms which are not booked between certain dates.

So, I have a collection of Room objects each of which has a collection of Booking objects (which can be empty). A Booking has a start date and and end date. I want to say 'get all the rooms that have no Bookings between DateA and DateB'.

I'm sure I could do this using a Native Query but since there's a date range involved (my understanding is date ranges aren't optimzed for NQ) and I need to do this query very frequently (many times per second for potentially more 10,000 rooms - the majority of which have no Bookings) I'm looking for more efficient alternatives.

Is there a way to phrase this using SODA? Or a better way to arrange my data model to get round this issue?


Solution

  • Yes you can do this by using SODA Query

    Date fromDate = null ; // assign reservation start dat
    Date toDate = null ; // assign reservation upto
    Query query = db.query();
    query.constrain(Booking.class);
    query.descend ("fromDate").constrain ( fromDate ).greater().equal ().
          and (query.descend ("toDate").constrain (toDate).smaller().equal());
    ObjectSet<Booking> objectSet = query.execute();
    

    Query for all of the rooms which do not have a booking between fromDate and toDate

     Query query = db.query();
    query.constrain(Room.class);
    query.descend ("bookingStartDate").constrain ( fromDate ).greater().equal ().and
    (query.descend ("bookingEndDate").constrain (toDate).smaller().equal()).not();
    ObjectSet<Room> objectSet = query.execute();
    

    See Also : Building SODA Queries