View Javadoc

1   /*
2    * @(#)$Id: Db4oStorageEngine.java 730 2009-08-05 21:17:30Z kurmannu $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *	
6    * Purpose      : 	Storage engine implementation for db4o.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Dec 26, 2006     ukurmann    Initial Release
15   * Mar 22, 2007     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  package org.ximtec.igesture.storage;
27  
28  import java.io.File;
29  import java.util.ArrayList;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Set;
33  
34  import org.ximtec.igesture.configuration.Configuration;
35  import org.ximtec.igesture.core.DataObject;
36  import org.ximtec.igesture.core.GestureSet;
37  import org.ximtec.igesture.core.TestSet;
38  
39  import com.db4o.Db4o;
40  import com.db4o.ObjectContainer;
41  import com.db4o.ObjectSet;
42  import com.db4o.query.Predicate;
43  
44  /**
45   * Storage engine implementation for db4o.
46   * 
47   * @version 1.0, Dec 2006
48   * @author Ueli Kurmann, igesture@uelikurmann.ch
49   * @author Beat Signer, signer@inf.ethz.ch
50   */
51  public class Db4oStorageEngine extends DefaultStorageEngine {
52  
53    ObjectContainer db;
54  
55    private Set<Object> objectCache;
56    private Set<Object> objectsToRemove;
57  
58    /**
59     * Constructs a new db4o engine.
60     * 
61     * @param filename
62     *          the database file.
63     */
64    public Db4oStorageEngine(String filename) {
65      Db4o.configure().activationDepth(Integer.MAX_VALUE);
66      Db4o.configure().updateDepth(Integer.MAX_VALUE);
67      Db4o.configure().allowVersionUpdates(true);
68      Db4o.configure().callConstructors(true);
69  
70      db = Db4o.openFile(filename);
71      objectCache = new HashSet<Object>();
72      objectsToRemove = new HashSet<Object>();
73    }
74  
75    public void dispose() {
76      db.rollback();
77      objectCache.clear();
78      db.close();
79  
80    } // dispose
81  
82    @SuppressWarnings("serial")
83    public <T extends DataObject> T load(final Class<T> clazz, final String id) {
84      T result = null;
85      final ObjectSet<T> os = db.query(new Predicate<T>() {
86  
87        @Override
88        public boolean match(T dataObject) {
89          return dataObject.getClass().equals(clazz) && dataObject.getId().equals(id);
90        }
91      });
92  
93      if (!os.isEmpty()) {
94        result = os.get(0);
95      }
96      objectCache.add(result);
97      return result;
98    } // load
99  
100   public <T extends DataObject> List<T> load(Class<T> clazz) {
101     List<T> result = new ArrayList<T>(db.query(clazz));
102     objectCache.addAll(result);
103     return result;
104   } // load
105 
106   public void store(DataObject dataObject) {
107     db.set(dataObject);
108     objectCache.add(dataObject);
109   } // store
110 
111   public void update(DataObject dataObject) {
112     db.set(dataObject);
113     objectCache.add(dataObject);
114   } // update
115 
116   public void remove(DataObject dataObject) {
117     db.delete(dataObject);
118     objectCache.remove(dataObject);
119     objectsToRemove.add(dataObject);
120   } // remove
121 
122   @Override
123   public synchronized void commit() {
124     for (Object obj : objectCache) {
125       db.set(obj);
126     }
127     for (Object obj : objectsToRemove) {
128       db.delete(obj);
129     }
130 
131     db.commit();
132   }
133 
134   /*
135    * (non-Javadoc)
136    * 
137    * @see org.ximtec.igesture.storage.StorageEngine#copyTo(java.io.File)
138    */
139   @Override
140   public synchronized void copyTo(File file) {
141 
142     ObjectContainer workingCopy = Db4o.openFile(file.getAbsolutePath());
143     for (GestureSet gestureSet : db.query(GestureSet.class)) {
144       workingCopy.set(gestureSet);
145     }
146     for (Configuration configuration : db.query(Configuration.class)) {
147       workingCopy.set(configuration);
148     }
149     for (TestSet testSet : db.query(TestSet.class)) {
150       workingCopy.set(testSet);
151     }
152     workingCopy.commit();
153     workingCopy.close();
154 
155     db.rollback();
156     db.close();
157 
158     db = Db4o.openFile(file.getName());
159 
160   }
161 
162 }