KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > gammaStore > GammaStore


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: GammaStore.java,v 1.4 2004/03/21 21:05:51 leomekenkamp Exp $
7

8 package org.ozoneDB.core.storage.gammaStore;
9
10 import java.io.IOException JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.logging.Level JavaDoc;
16 import java.util.logging.Logger JavaDoc;
17 import org.ozoneDB.ObjectNotFoundException;
18 import org.ozoneDB.OzoneCompatible;
19 import org.ozoneDB.PermissionDeniedException;
20 import org.ozoneDB.DxLib.DxBag;
21 import org.ozoneDB.DxLib.DxHashSet;
22 import org.ozoneDB.DxLib.DxIterator;
23 import org.ozoneDB.DxLib.DxSet;
24 import org.ozoneDB.core.Env;
25 import org.ozoneDB.core.GarbageCollector;
26 import org.ozoneDB.core.ObjectContainer;
27 import org.ozoneDB.core.ObjectID;
28 import org.ozoneDB.core.Permissions;
29 import org.ozoneDB.core.ServerComponent;
30 import org.ozoneDB.core.StoreManager;
31 import org.ozoneDB.core.Transaction;
32 import org.ozoneDB.core.User;
33 import org.ozoneDB.core.storage.Cache;
34 import org.ozoneDB.core.storage.PropertyConfigurableFactory;
35 import org.ozoneDB.core.storage.PropertyInfo;
36
37
38 /**
39  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
40  * @version $Id: GammaStore.java,v 1.4 2004/03/21 21:05:51 leomekenkamp Exp $
41  */

42 public class GammaStore extends ServerComponent implements StoreManager {
43
44     private static final Logger JavaDoc log = Logger.getLogger(GammaStore.class.getName());
45
46     // TODO: move this somewhere else
47
public static final String JavaDoc OZONE_BASE = "org.ozoneDB";
48
49     public static final String JavaDoc GAMMASTORE_BASE = OZONE_BASE + ".core.gammaStore";
50
51     public static final PropertyInfo DIRECTORY = new PropertyInfo(
52         ".directory",
53         "String (path)",
54         null,
55         "main database directory, must be an absolute path",
56         new String JavaDoc[] {
57             "/var/ozone (*nix absolute path)",
58             "c:\\ozoneFiles (windows absolute path)",
59         }
60     );
61
62     public static final PropertyInfo TXSTORAGEFACTORY = new PropertyInfo(
63         ".txStorageFactory",
64         "String (path)",
65         null,
66         "main database directory, must be an absolute path",
67         new String JavaDoc[] {
68             "/var/ozone/tx (*nix absolute path)",
69             "c:\\ozoneFiles\\tx (windows absolute path)",
70         }
71     );
72
73     private static final String JavaDoc PROP_STORAGEFACTORY = ".storageFactory";
74
75     private static final String JavaDoc PROP_NUMCONFIGS = ".numConfigs";
76
77     private static final String JavaDoc PROP_STREAMFACTORY = ".streamFactory";
78
79     private static final String JavaDoc PROP_CONTAINERCACHE = GAMMASTORE_BASE + ".containerCache";
80
81     private StorageFactory[] storageFactories;
82
83     private StreamFactory[] streamFactories;
84
85     private Cache containerCache;
86
87     private IndexManager indexManager;
88
89     private Properties JavaDoc properties;
90
91     private Map JavaDoc containerNames = new HashMap JavaDoc();
92
93     /**
94      * The garbage collector. It should be notified in the event
95      * <ul><li>that a formerly unnamed object receives a name.</li>
96      * <li>that an object is freshly created</li>
97      * </ul>
98      */

99     private GarbageCollector garbageCollector;
100
101     private StorageFactory txStorageFactory;
102     
103     /**
104      * ozoneDB.gammaStore.numConfigs=1
105      * ozoneDB.gammaStore.0.clusterFactory=org.ozoneDB.core.gammaStore.RandomAccessFileCluster.Factory
106      * ozoneDB.gammaStore.0.maxClusterSize=65536
107      * ozoneDB.gammaStore.0.streamFactory=org.ozoneDB.core.gammaStore.GZIPStreamFactory
108      * ozoneDB.gammaStore.containerCache=org.ozoneDB.core.gammaStore.SoftReferenceCache
109      *
110      * While not yet supported there is a possiblility to have multiple
111      * configurations. These could be used to for instance save instances of a
112      * specific class to a different disk because of performance reasons
113      * Because different configurations allow for different streams to be used
114      * during (de)serialization, this could also be used to encrypt only certain
115      * objects.
116      */

117     public GammaStore(Properties JavaDoc properties) {
118         // FIXME: dirty hack, but the whole Env thing is begging to be refactored
119
super(Env.currentEnv());
120         int numConfigs = Integer.parseInt(properties.getProperty(PROP_NUMCONFIGS, "1"));
121         if (numConfigs != 1) {
122             throw new RuntimeException JavaDoc("currently only 1 config supported");
123         }
124
125         // initialize the container cache
126
setContainerCache((Cache) PropertyConfigurableFactory.create(Cache.class, properties, PROP_CONTAINERCACHE));
127
128         // initialize all configs
129
storageFactories = new StorageFactory[numConfigs];
130         streamFactories = new StreamFactory[numConfigs];
131         for (int i = 0; i < numConfigs; i++) {
132             String JavaDoc prop = GAMMASTORE_BASE + "." + i + PROP_STORAGEFACTORY;
133             storageFactories[i] = (StorageFactory) PropertyConfigurableFactory.create(StorageFactory.class, properties, prop);
134 // prop = GAMMASTORE_BASE + "." + i + PROP_STREAMFACTORY;
135
// streamFactories[i] = (StreamFactory) PropertyConfigurableFactory.create(StreamFactory.class, properties, prop);
136
}
137         txStorageFactory = (StorageFactory) PropertyConfigurableFactory.create(StorageFactory.class, properties, GAMMASTORE_BASE + TXSTORAGEFACTORY.getKey());
138         
139         this.properties = properties;
140     }
141     
142     public GammaStore(Env env) {
143         this(env.config);
144     }
145
146     // private so it can be inlined
147
public Cache getContainerCache() {
148         return containerCache;
149     }
150
151     // private so it can be inlined
152
public void setContainerCache(Cache containerCache) {
153         this.containerCache = containerCache;
154     }
155
156     private void abortObjectId(ObjectID objectId) {
157         // delete container from cache
158
// if object newly created: delete (new) image from disk
159
// if existing object: delete (new) image from disk
160
}
161
162
163     public void abortTransaction(Transaction ta) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
164         GammaTransaction transaction = (GammaTransaction) ta;
165         for (Iterator JavaDoc i = transaction.getContainerCache().copyToMap().values().iterator(); i.hasNext(); ) {
166             GammaContainer gammaContainer = (GammaContainer) i.next();
167             abortObjectId(gammaContainer.getObjectId());
168         }
169         transaction.getLogStorage().close();
170 // ObjectInputStream logStream = new ObjectInputStream(transaction.getLogStorageFactory().createInputStream(transaction.logFilename()));
171
// ObjectID objectId;
172
// while((objectId = (ObjectID) logStream.readObject()) != null) {
173
// abortObjectId(objectId);
174
// }
175
}
176
177     /** Force the Store to make a guess which objects are used together with the
178      * container with the specified id.
179      * @param id The ObjectID if the container.
180      *
181      */

182     public DxBag clusterOfID(ObjectID id) throws Exception JavaDoc {
183         return null;
184     }
185
186     public void commitTransaction(Transaction ta) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
187     }
188
189     public ObjectContainer containerForID(Transaction ta, ObjectID id) throws ObjectNotFoundException, IOException JavaDoc, ClassNotFoundException JavaDoc {
190         GammaContainer result = (GammaContainer) containerCache.get(id);
191         if (result == null) {
192             // ask indexmanager for location and read from disk
193
}
194         return result;
195     }
196
197     /** @param name The object name to search for.
198      * @param ta
199      * @return The object container for the name or null.
200      *
201      */

202     public ObjectContainer containerForName(Transaction ta, String JavaDoc name) throws Exception JavaDoc {
203         return null;
204     }
205
206     /** Aid constructor, because a store is instantiated with 'newInstance',
207      * where we've got no arguments.
208      *
209      */

210     public void init(Env env) {
211         setGarbageCollector(env.getGarbageCollector());
212     }
213
214     /** @param ta
215      * @param container
216      * @param name
217      *
218      */

219     public void nameContainer(Transaction ta, ObjectContainer container, String JavaDoc name) throws PermissionDeniedException {
220
221     }
222
223     /** Creates a new object container and initializes it with the specified
224      * target object. The new container is immediatly accessible from the calling
225      * transaction via containerByID but it is not joined to this transaction.
226      * It needs to be joined and commited afterwards.
227      *
228      * Iff this method returns normally, the returned container is pinned and thus has to be unpinned.
229      * Iff this method returns normally, the returned container is locked with the given lock level.
230      *
231      * @param ta
232      * @param target
233      * @param objectId
234      * @param permissions
235      * @param lockLevel
236      * @return An container-proxy for the created container.
237      *
238      */

239     public ObjectContainer newContainerAndLock(Transaction ta, OzoneCompatible target, ObjectID objectId, Permissions permissions, int lockLevel) throws Exception JavaDoc {
240         GammaContainer result = new GammaContainer(objectId, this);
241         getContainerCache().put(objectId, result);
242
243         if (target != null) {
244             result.setTarget(target);
245         }
246
247         if (log.isLoggable(Level.FINEST)) log.finest("created new container " + result);
248         getGarbageCollector().notifyNewObjectContainer(result);
249         GammaTransaction gammaTransaction = (GammaTransaction) ta;
250
251         gammaTransaction.getContainerCache().put(result.getObjectId(), result);
252
253         return result;
254     }
255
256     public Object JavaDoc newTransactionData() {
257         // not needed since transactions are created by a StoreManager
258
return null;
259     }
260
261     public DxIterator objectIDIterator() {
262         // TODO: find some way to have the IndexManager return an iterator
263
// over all its object entries
264
return null;
265     }
266
267     /** @param ta the running transaction
268      * @return a String array of the all object names defined
269      *
270      */

271     public DxSet objectNames(Transaction ta) {
272         DxSet result = new DxHashSet();
273         for(Iterator JavaDoc i = getContainerNames().entrySet().iterator(); i.hasNext(); ) {
274             result.add(i.next());
275         }
276         return result;
277     }
278
279     /** Prepare the specified transaction for commit. All operations that may
280      * fail during the commit process should be done here. However, this method
281      * must not change any global data structures such as the idTable that
282      * are used by other transactions too.<p>
283      *
284      * The {@link org.ozoneDB.core.TransactionManager} let this method run exclusivly. However,
285      * {@link #prepareCommitTransaction} and {@link #commitTransaction} are not
286      * an atomar operation.
287      *
288      *
289      * @param ta Transaction that will be commited.
290      *
291      */

292     public void prepareCommitTransaction(Transaction ta) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
293     }
294
295     /** Tells this StoreManager to report every named object to the garbage collector.
296      *
297      */

298     public void reportNamedObjectsToGarbageCollector() {
299     }
300
301     public void shutdown() throws Exception JavaDoc {
302     }
303
304     public void startup() throws Exception JavaDoc {
305         indexManager = new IndexManager(properties, "org.ozoneDB.core.gammaStore.IndexManager", true);
306     }
307
308     /** Update lock level of the given container according to the leve of the
309      * containers lock object.
310      * TODO: is this right?
311      */

312     public void updateLockLevel(Transaction ta, ObjectContainer container) throws IOException JavaDoc {
313         if (log.isLoggable(Level.FINEST)) log.finest("updateLockLevel()");
314         GammaContainer gammaContainer = (GammaContainer) container;
315         getContainerCache().put(gammaContainer.getObjectId(), gammaContainer);
316     }
317
318     public Transaction createTransaction(Env env, User user) {
319         return new GammaTransaction(this, getTxStorageFactory(), env, user);
320     }
321
322     private StorageFactory getTxStorageFactory() {
323         return txStorageFactory;
324     }
325     
326     // private so it can be inlined
327
private Map JavaDoc getContainerNames() {
328         return containerNames;
329     }
330
331     // private so it can be inlined
332
private GarbageCollector getGarbageCollector() {
333         return garbageCollector;
334     }
335
336     // private so it can be inlined
337
private void setGarbageCollector(GarbageCollector garbageCollector) {
338         this.garbageCollector = garbageCollector;
339     }
340
341     public void save() throws Exception JavaDoc {
342     }
343     
344 }
345
Popular Tags