Immediate Queries

This is the default query mode: the whole query result is evaluated upon query execution and object IDs list is produced as a result.

QueryModesExample.java: testImmediateQueries
01public static void testImmediateQueries() { 02 System.out.println("Testing query performance on 10000 pilot objects in Immediate mode"); 03 fillUpDB(10000); 04 ObjectContainer db = Db4o.openFile(YAPFILENAME); 05 try { 06 db.ext().configure().queries().evaluationMode(QueryEvaluationMode.IMMEDIATE); 07 QueryStats stats = new QueryStats(); 08 stats.connect(db); 09 Query query = db.query(); 10 query.constrain(Pilot.class); 11 query.descend("points").constrain(99).greater(); 12 query.execute(); 13 long executionTime = stats.executionTime(); 14 System.out.println("Query execution time: " + executionTime); 15 } finally { 16 db.close(); 17 } 18 }

Obviously object evaluation takes some time and in a case of big resultsets you will have to wait for a long time before the first result will be returned. This is especially unpleasant in a client-server setup, when query processing can block the server for seconds or even minutes.

This mode makes the whole objects result set available at once - ID list is built based on the committed state in the database. As soon as a result is delivered it won't be changed neither by changes in current transaction neither by committed changes from another transactions.

Note, that resultset contains only references to objects, you were querying for, which means that if an object field has changed by the time of the actual object retrieval from the object set - you will get the new field value:

QueryModesExample.java: testImmediateChanged
01public static void testImmediateChanged() { 02 System.out.println("Testing immediate mode with field changes"); 03 fillUpDB(10); 04 ObjectContainer db = Db4o.openFile(YAPFILENAME); 05 try { 06 db.ext().configure().queries().evaluationMode(QueryEvaluationMode.IMMEDIATE); 07 Query query1 = db.query(); 08 query1.constrain(Pilot.class); 09 query1.descend("points").constrain(5).smaller(); 10 ObjectSet result1 = query1.execute(); 11 12 // change field 13 Query query2 = db.query(); 14 query2.constrain(Pilot.class); 15 query2.descend("points").constrain(2); 16 ObjectSet result2 = query2.execute(); 17 Pilot pilot2 = (Pilot)result2.get(0); 18 pilot2.addPoints(22); 19 db.set(pilot2); 20 listResult(result1); 21 } finally { 22 db.close(); 23 } 24 }

Immediate Mode Pros And Cons

Pros:

  • If the query is intended to iterate through the entire resulting ObjectSet, this mode will be slightly faster than the others.
  • The query will process without intermediate side effects from changed objects (by the caller or by other transactions).

Cons:

  • Query processing can block the server for a long time.
  • In comparison to the other modes it will take longest until the first results are returned.
  • The ObjectSet will require a considerate amount of memory to hold the IDs of all found objects.