1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
46
47
48
49
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
60
61
62
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 }
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 }
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 }
105
106 public void store(DataObject dataObject) {
107 db.set(dataObject);
108 objectCache.add(dataObject);
109 }
110
111 public void update(DataObject dataObject) {
112 db.set(dataObject);
113 objectCache.add(dataObject);
114 }
115
116 public void remove(DataObject dataObject) {
117 db.delete(dataObject);
118 objectCache.remove(dataObject);
119 objectsToRemove.add(dataObject);
120 }
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
136
137
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 }