KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > classicStore > ObjectSpace


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
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9 package org.ozoneDB.core.storage.classicStore;
10
11 import org.ozoneDB.DxLib.*;
12 import org.ozoneDB.OzoneCompatible;
13 import org.ozoneDB.Setup;
14 import org.ozoneDB.core.*;
15 import org.ozoneDB.core.storage.classicStore.ClassicObjectContainer;
16 import org.ozoneDB.core.storage.classicStore.ClassicStoreException;
17 import org.ozoneDB.core.storage.classicStore.ClassicTransaction;
18 import org.ozoneDB.core.storage.classicStore.ClusterSpace;
19 import org.ozoneDB.util.LogWriter;
20
21
22 /** */
23 public final class ObjectSpace extends Object JavaDoc {
24     /** */
25     final static long serialVersionUID = 2;
26     final static byte subSerialVersionUID = 1;
27
28     /** The environment of this object. */
29     protected transient Env env;
30     protected transient ClusterSpace clusterSpace;
31
32     /** */
33     protected DxMap objectTable;
34     protected DxMap nameTable;
35
36
37     /**
38      */

39     public ObjectSpace( Env _env ) {
40         env = _env;
41         clusterSpace = new ClusterSpace( _env );
42     }
43
44
45     /**
46      */

47     public void startup() throws Exception JavaDoc {
48         env.logWriter.newEntry( this, "startup...", LogWriter.INFO );
49         int tableBuffSize = env.config.intProperty( Setup.CS_TABLE_BUFF_SIZE, -1 );
50         if (tableBuffSize == -1) {
51             env.logWriter.newEntry( this, "Property " + Setup.CS_TABLE_BUFF_SIZE + " is not set.", LogWriter.WARN );
52             tableBuffSize = 20;
53         }
54         //objectTable = new DxDiskHashMap (env.databaseDir + Env.OS_DIR + File.separator + "idtable", tableBuffSize / 256, 12);
55
objectTable = new DxHashMap( 1000 );
56         nameTable = new DxHashMap( 10 );
57         clusterSpace.startup();
58     }
59
60
61     /**
62      */

63     public void shutdown() throws Exception JavaDoc {
64         env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO );
65         clusterSpace.shutdown();
66         //objectTable.printCacheStatistics();
67
if (objectTable instanceof DxDiskHashMap) {
68             ((DxDiskHashMap)objectTable).cleanFiles();
69         }
70     }
71
72
73     /**
74      */

75     public synchronized void addObject( ObjectContainer container ) {
76         //use a copy of the objID because objectTable is a diskHash
77
objectTable.addForKey( container, container.id().clone() );
78         if (container.name() != null) {
79             nameTable.addForKey( container, new String JavaDoc( container.name() ) );
80         }
81     }
82
83
84     /**
85      * Creates a new object container for the given target with the given oid
86      * and an optional name.
87      */

88     public synchronized ObjectContainer newContainer( Transaction ta, OzoneCompatible target, ObjectID objID,
89             Permissions permissions ) throws ClassicStoreException {
90         ClassicObjectContainer container = new ClassicObjectContainer( target, objID, permissions );
91         if (!objectTable.addForKey( container, objID.clone() )) {
92             throw new ClassicStoreException( "ObjectID " + objID + " already exists !" );
93         }
94
95         return container;
96     }
97
98
99     /**
100      * @param id The object id to search for.
101      * @return The object container with the given id or null.
102      */

103     public ObjectContainer objectForID( ObjectID id ) {
104         return (ObjectContainer)objectTable.elementForKey( id );
105     }
106
107
108     /**
109      * Applies a name to an object.
110      * @param container The container to name.
111      * @param name The new name of the container.
112      */

113     public void nameObject( ObjectContainer container, String JavaDoc name ) throws ClassicStoreException {
114         if (container.name() != null) {
115             nameTable.removeForKey( container.name() );
116         }
117         if (name != null) {
118             if (!nameTable.addForKey( container, new String JavaDoc( name ) )) {
119                 throw new ClassicStoreException( "Name '" + name + "' already exists !" );
120             }
121             container.nameTarget( name );
122         }
123     }
124
125
126     /**
127      * @param name
128      * @return The object container with the given name.
129      */

130     public ObjectContainer objectForName( String JavaDoc name ) {
131         return (ObjectContainer)nameTable.elementForKey( name );
132     }
133
134
135     /**
136      * entfernt ObjectContainer fuer entsprechende ObjectID;
137      */

138     public synchronized void deleteObject( ObjectContainer toRemove ) {
139         synchronized (toRemove) {
140             objectTable.removeForKey( toRemove.id() );
141             if (toRemove.name() != null) {
142                 nameTable.removeForKey( toRemove.name() );
143             }
144         }
145     }
146
147
148     /** */
149     public synchronized void prepareCommitObjects( ClassicTransaction ta ) {
150         env.logWriter.newEntry( this, "prepareCommitObjects: transaction " + ta.taID(), LogWriter.DEBUG3 );
151
152         //at first we have to insert new objects + containers into the object
153
//space, so that we have access to them while further commiting
154
DxListBag created = new DxListBag();
155         DxListBag modified = new DxListBag();
156         DxIterator it = ta.idTable.iterator();
157         ClassicObjectContainer container;
158
159         while ((container = (ClassicObjectContainer)it.next()) != null) {
160             if (container.isCreated()) {
161                 addObject( container );
162                 created.add( container );
163             } else {
164                 if (container.lockLevel( ta ) > Lock.LEVEL_READ) {
165                     modified.add( container.id() );
166                 }
167             }
168         }
169
170         //make all changes persistent
171
clusterSpace.prepareCommit( ta.taID(), created, modified );
172
173         // remove all deleted objects from the object space _after_ commiting
174
// them; otherwise we wouldn't have access to them while commiting
175
// commit all written objects _after_ we know everthing worked fine;
176
// if not, the transaction has to abort
177
it = ta.idTable.iterator();
178         while ((container = (ClassicObjectContainer)it.next()) != null) {
179             if (container.isDeleted()) {
180                 deleteObject( container );
181             } else {
182                 container.commitTarget( ta );
183             }
184         }
185     }
186
187
188     /** */
189     public synchronized void commitObjects( Transaction ta ) {
190         clusterSpace.commitTransaction( ta.taID() );
191     }
192
193
194     /** */
195     public synchronized void abortObjects( ClassicTransaction ta ) throws Exception JavaDoc {
196         DxIterator it = ta.idTable.iterator();
197         ClassicObjectContainer container;
198         while ((container = (ClassicObjectContainer)it.next()) != null) {
199             container.abortTarget( ta );
200         }
201         clusterSpace.abortTransaction( ta.taID() );
202     }
203 }
204
Popular Tags