KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > server > InTransactionBaseImageImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.server;
25
26 import org.objectweb.jalisto.se.api.JalistoProperties;
27 import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess;
28 import org.objectweb.jalisto.se.api.internal.*;
29 import org.objectweb.jalisto.se.exception.JalistoException;
30 import org.objectweb.jalisto.se.impl.trace.Trace;
31 import org.objectweb.jalisto.se.impl.InFileAddress;
32 import org.objectweb.jalisto.se.JalistoFactory;
33
34 import java.util.*;
35
36 public class InTransactionBaseImageImpl implements InTransactionBaseImage {
37
38     protected InTransactionBaseImageImpl(PluggablePhysicalFileAccess physicalAccess, JalistoProperties properties) {
39         this.trace = JalistoFactory.getInternalFactory().getTracer(properties);
40         this.keepInMemory = properties.isKeepingInMemory();
41         this.physicalAccess = physicalAccess;
42         this.deletedObjects = new HashMap();
43         this.newObjects = new HashMap();
44         this.modifiedObjects = new HashMap();
45         this.readObjects = JalistoFactory.getInternalFactory().getCache(properties, properties.getPageCacheSize(),
46                                                           "inTransactionBaseImageCache");
47     }
48
49
50     public void commit() {
51         physicalAccess.startCommit();
52         Iterator values = deletedObjects.values().iterator();
53         while (values.hasNext()) {
54             physicalAccess.deleteFileObject((InFileAddress) values.next());
55         }
56         values = newObjects.values().iterator();
57         while (values.hasNext()) {
58             JalistoObject fo = (JalistoObject) values.next();
59             physicalAccess.insertFileObject(fo.getIfa(), fo);
60         }
61         values = modifiedObjects.values().iterator();
62         while (values.hasNext()) {
63             JalistoObject fo = (JalistoObject) values.next();
64             physicalAccess.updateFileObject(fo.getIfa(), fo);
65         }
66         finishCommit();
67         physicalAccess.commit();
68     }
69
70     public void rollback() {
71         clear();
72         readObjects.clear();
73         physicalAccess.rollback();
74     }
75
76     protected void finishCommit() {
77         if (keepInMemory) {
78             readObjects.putAll(modifiedObjects);
79             readObjects.putAll(newObjects);
80         }
81         clear();
82     }
83
84     protected void clear() {
85         if (!keepInMemory) {
86             readObjects.clear();
87         }
88         modifiedObjects.clear();
89         newObjects.clear();
90         deletedObjects.clear();
91     }
92
93
94     public void insertFileObject(InFileAddress ifa, JalistoObject fo) {
95         String JavaDoc address = ifa.getAddress();
96         if (modifiedObjects.containsKey(address) ||
97             newObjects.containsKey(address) ||
98             readObjects.containsKey(address)) {
99             throw new JalistoException("you could not insert a object already in base : " + ifa + ", " + fo);
100         }
101         if (deletedObjects.containsKey(address)) {
102             deletedObjects.remove(address);
103             modifiedObjects.put(address, fo);
104         } else {
105             newObjects.put(address, fo);
106         }
107     }
108
109     public JalistoObject readFileObjectAt(InFileAddress ifa) {
110         String JavaDoc address = ifa.getAddress();
111         if (deletedObjects.containsKey(address)) {
112             throw new JalistoException("object has been removed from base : " + ifa);
113         }
114         JalistoObject result = (JalistoObject) readObjects.get(address);
115         if (result != null)
116             return result;
117         result = (JalistoObject) modifiedObjects.get(address);
118         if (result != null)
119             return result;
120         result = (JalistoObject) newObjects.get(address);
121         if (result != null)
122             return result;
123         result = physicalAccess.readFileObjectAt(ifa);
124         readObjects.put(address, result);
125         return result;
126     }
127
128     public void updateFileObject(InFileAddress ifa, JalistoObject fo) {
129         String JavaDoc address = ifa.getAddress();
130         if (deletedObjects.containsKey(address)) {
131             throw new JalistoException("object has been removed from base : " + ifa + ", " + fo);
132         }
133         if (newObjects.containsKey(address)) {
134             newObjects.put(address, fo);
135         } else {
136             modifiedObjects.put(address, fo);
137         }
138         readObjects.remove(address);
139     }
140
141     public void deleteFileObject(InFileAddress ifa) {
142         String JavaDoc address = ifa.getAddress();
143         if (deletedObjects.containsKey(address)) {
144             throw new JalistoException("object has already been removed from base : " + ifa);
145         }
146         if (newObjects.remove(address) == null) {
147             modifiedObjects.remove(address);
148         }
149         readObjects.remove(address);
150         deletedObjects.put(address, ifa);
151     }
152
153
154     public boolean isWorking() {
155         return false;
156     }
157
158     public boolean startWorking() {
159         return true;
160     }
161
162     public void stopWorking() {
163     }
164
165
166     public int getDeletedObjectsSize() {
167         return deletedObjects.size();
168     }
169
170     public int getModifiedObjectsSize() {
171         return modifiedObjects.size();
172     }
173
174     public int getNewObjectsSize() {
175         return newObjects.size();
176     }
177
178     public Map getReadObjects() {
179         return readObjects;
180     }
181
182     public boolean isKeepInMemory() {
183         return keepInMemory;
184     }
185
186     public void setKeepInMemory(boolean keepInMemory) {
187         this.keepInMemory = keepInMemory;
188     }
189
190     public Collection getKeysStartingWith(String JavaDoc filter, boolean withOrder) {
191         ArrayList keys = new ArrayList(physicalAccess.getKeysStartingWith(filter));
192         Iterator newKeys = newObjects.keySet().iterator();
193         while (newKeys.hasNext()) {
194             String JavaDoc key = (String JavaDoc) newKeys.next();
195             if (key.startsWith(filter)) {
196                 keys.add(key);
197             }
198         }
199         Iterator deletedKeys = deletedObjects.keySet().iterator();
200         while (deletedKeys.hasNext()) {
201             String JavaDoc key = (String JavaDoc) deletedKeys.next();
202             if (key.startsWith(filter)) {
203                 keys.remove(key);
204             }
205         }
206         if (withOrder) {
207             Collections.sort(keys);
208         }
209         return keys;
210     }
211
212
213     protected Map deletedObjects;
214     protected Map modifiedObjects;
215     protected Map newObjects;
216     protected Map readObjects;
217     protected PluggablePhysicalFileAccess physicalAccess;
218     protected Trace trace;
219     protected boolean keepInMemory;
220
221
222     /**
223      * ******************************* STATIC CONSTRUCTOR ******************************************
224      */

225
226     public static InTransactionBaseImage getInTransactionBaseImageInstance(
227             PluggablePhysicalFileAccess physicalAccess, JalistoProperties properties) {
228         return new InTransactionBaseImageImpl(physicalAccess, properties);
229     }
230 }
231
Popular Tags