Custom Marshaller Usage

Configure db4o to use the custom marshaller for the Item class:

CustomMarshallerExample.java: configureMarshaller
1private static void configureMarshaller(){ 2 marshaller = new ItemMarshaller(); 3 Db4o.configure().objectClass(Item.class).marshallWith(marshaller); 4 }

Now you can work with Item class objects just as usual:

CustomMarshallerExample.java: storeObjects
01private static void storeObjects(){ 02 new File(DBFILE).delete(); 03 ObjectContainer container = Db4o.openFile(DBFILE); 04 try { 05 Item item; 06 long t1 = System.currentTimeMillis(); 07 for (int i = 0; i < 10000; i++){ 08 item = new Item(0xFFAF, 0xFFFFFFF, 120); 09 container.set(item); 10 } 11 long t2 = System.currentTimeMillis(); 12 long timeElapsed = t2 - t1; 13 System.out.println("Time to store the objects ="+ timeElapsed + " ms"); 14 } finally { 15 container.close(); 16 } 17 }

CustomMarshallerExample.java: retrieveObjects
01private static void retrieveObjects(){ 02 ObjectContainer container = Db4o.openFile(DBFILE); 03 try { 04 long t1 = System.currentTimeMillis(); 05 ObjectSet result = container.get(new Item()); 06 long t2 = System.currentTimeMillis(); 07 long timeElapsed = t2 - t1; 08 System.out.println("Time elapsed for the query ="+ timeElapsed + " ms"); 09 listResult(result); 10 } finally { 11 container.close(); 12 } 13 }

Custom marshallers can help you to improve performance for selected classes if quering for fields is not required. The impact can be especially noticeable on bulk operations. It is recommended  to test the performance impact of a custom marshaller with your real application model.