Updating List Objects

As we discussed before updating list members using update depth is quite inefficient. An alternative approach can be retrieving and updating each object from the list separately:

ListOperationsExample.java: updateObject
01private static void updateObject() 02 { 03 long timeElapsed = 0; 04 05 ObjectContainer db = Db4o.openFile(DBFILE); 06 try 07 { 08 // we can set update depth to 0 09 // as we update only the current object 10 db.ext().configure().updateDepth(0); 11 List<ListObject> result = db.<ListObject>query(ListObject.class); 12 if (result.size() == 2) 13 { 14 ListObject lo1 = result.get(0); 15 // Select a DataObject for update 16 DataObject dataobject = lo1.getData().get(0); 17 dataobject.setName("Updated"); 18 dataobject.setData(System.currentTimeMillis()+ " ---- Updated Object "); 19 20 System.out.println("Updated list " + lo1.getName() + " dataobject " + lo1.getData().get(0)); 21 long t1 = System.currentTimeMillis(); 22 // save only the DataObject. List of DataObjects will 23 // automatically include the new value 24 db.set(dataobject); 25 db.commit(); 26 long t2 = System.currentTimeMillis(); 27 timeElapsed = t2 -t1; 28 } 29 } 30 finally 31 { 32 db.close(); 33 } 34 System.out.println("Storing took: " + timeElapsed +" ms.") ; 35 }

In this case only the object of interest is updated, which takes much less time than updating the whole list.