KickJava   Java API By Example, From Geeks To Geeks.

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


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.DxCollection;
12 import org.ozoneDB.DxLib.DxIterator;
13 import org.ozoneDB.Setup;
14 import org.ozoneDB.OzoneInternalException;
15 import org.ozoneDB.core.*;
16 import org.ozoneDB.core.storage.classicStore.ClassicObjectContainer;
17 import org.ozoneDB.core.storage.classicStore.ClassicStore;
18 import org.ozoneDB.core.storage.classicStore.Cluster;
19 import org.ozoneDB.core.storage.classicStore.ClusterID;
20 import org.ozoneDB.util.LogWriter;
21
22 import java.io.IOException JavaDoc;
23
24
25 /**
26  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
27  */

28 public class ClusterSpace {
29     /** The environment of this object. */
30     protected Env env;
31     protected ClassicStore classicStore;
32
33     protected PersistenceSpace persistenceSpace = null;
34     protected DeathObjectBuffer dobjBuffer = new DeathObjectBuffer();
35     protected int activatedObjects = 0;
36     protected int loadedClusters = 0;
37     protected int maxBufferSize = 10 * Cluster.MAX_SIZE;
38
39
40     /**
41      * Constructor
42      */

43     public ClusterSpace( Env _env ) {
44         env = _env;
45         classicStore = (ClassicStore)env.getStoreManager();
46         persistenceSpace = new PersistenceSpace( env );
47
48         Cluster.MAX_SIZE = env.config.intProperty( Setup.CS_CLUSTER_SIZE, -1 );
49         if (Cluster.MAX_SIZE == -1) {
50             env.logWriter.newEntry( this, "Property " + Setup.CS_TABLE_BUFF_SIZE + " is not set.", LogWriter.WARN );
51             env.logWriter.newEntry( this, " " + Setup.CS_TABLE_BUFF_SIZE + " = 64*1024", LogWriter.WARN );
52             Cluster.MAX_SIZE = 64 * 1024;
53         }
54
55         maxBufferSize = env.config.intProperty( Setup.CS_CLUSTER_SPACE_SIZE, -1 );
56         if (maxBufferSize == -1) {
57             env.logWriter.newEntry( this, "Property " + Setup.CS_CLUSTER_SPACE_SIZE + " is not set.", LogWriter.WARN );
58             env.logWriter.newEntry( this, " " + Setup.CS_CLUSTER_SPACE_SIZE + " = 5MB", LogWriter.WARN );
59             maxBufferSize = 5 * 1024 * 1024;
60         }
61     }
62
63
64     /**
65      */

66     protected synchronized void startup() throws Exception JavaDoc {
67         env.logWriter.newEntry( this, "startup...", LogWriter.INFO );
68         persistenceSpace.startup();
69         persistenceSpace.fillObjectSpace();
70     }
71
72
73     /**
74      */

75     protected synchronized void shutdown() throws Exception JavaDoc {
76         env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO );
77         persistenceSpace.shutdown();
78     }
79
80
81     /**
82      */

83     protected void setSizes( int csSize, int clSize ) {
84         Cluster.MAX_SIZE = csSize;
85         maxBufferSize = csSize;
86         if (maxBufferSize < Cluster.MAX_SIZE) {
87             maxBufferSize = Cluster.MAX_SIZE;
88         }
89         if (csSize < dobjBuffer.size()) {
90             freeSpace( dobjBuffer.size() - csSize );
91         }
92     }
93
94
95     /**
96      */

97     protected synchronized void activateObject( ObjectContainer container ) throws Exception JavaDoc {
98         // ObjectContainer os = env.objectSpace.objectForID (oid);
99
ClassicObjectContainer os = (ClassicObjectContainer)container;
100         if (os.target() == null) {
101             DeathObject dobj = dobjBuffer.objectForId( os.id() );
102             if (dobj == null) {
103                 loadCluster( os.clusterID() );
104                 dobj = dobjBuffer.objectForId( os.id() );
105             }
106             if (dobj != null) {
107                 activatedObjects++;
108                 //env.logWriter.newEntry ("activatedObjects / loadedClusters: " + ((double)activatedObjects/loadedClusters), LogWriter.DEBUG3);
109
//env.logWriter.newEntry ("ClusterSpace.activateObject: " + oid + " of " + dobjBuffer.count() + "(" + dobjBuffer.size() + ")", LogWriter.DEBUG3);
110
os.setObject( dobj.enlive() );
111             }
112         }
113     //env.logWriter.newEntry ("current buffer size: " + dobjBuffer.size(), LogWriter.DEBUG3);
114
}
115
116
117     /**
118      */

119     protected synchronized void prepareCommit( TransactionID tid, DxCollection created, DxCollection modified )
120             throws OzoneInternalException {
121         env.logWriter.newEntry( this, "ClusterSpace.commitObjects: " + created.count() + " : " + modified.count(),
122                 LogWriter.DEBUG3 );
123         //long start = System.currentTimeMillis();
124

125         persistenceSpace.startTransaction( tid );
126
127         DeathObject dobj;
128
129         // we commit the modified objects first
130
DxIterator it = modified.iterator();
131         ObjectID id;
132         while ((id = (ObjectID)it.next()) != null) {
133             ClassicObjectContainer os = (ClassicObjectContainer)classicStore.objectSpace.objectForID( id );
134             dobj = dobjBuffer.objectForId( id );
135             if (dobj == null) {
136                 loadCluster( os.clusterID );
137                 dobj = dobjBuffer.objectForId( id );
138             }
139
140             // write the leak
141
persistenceSpace.writeLeak( dobj.clusterID(), dobj );
142
143             if (!os.isDeleted()) {
144                 dobjBuffer.remove( id );
145                 try {
146                     persistenceSpace.writeObject( dobj, true, true );
147                 } catch (IOException JavaDoc e) {
148                     throw new OzoneInternalException("prepareCommit() failed for modified objects (failed to write object)",e);
149                 }
150                 freeSpace( dobj.size() );
151                 dobjBuffer.add( dobj );
152             } else {
153                 dobjBuffer.remove( id );
154                 os.setObject( null );
155             }
156         }
157
158         // commit the new objects
159
it = created.iterator();
160         ObjectContainer os;
161         while ((os = (ObjectContainer)it.next()) != null) {
162             dobj = new DeathObject( os.id() );
163             try {
164                 persistenceSpace.writeObject( dobj, true, false );
165             } catch (IOException JavaDoc e) {
166                 throw new OzoneInternalException("prepareCommit() failed for new objects (failed to write object)",e);
167             }
168             freeSpace( dobj.size() );
169             dobjBuffer.add( dobj );
170         }
171
172         persistenceSpace.prepareCommitTransaction( tid );
173     //env.logWriter.newEntry ("commit time: " + (System.currentTimeMillis() - start), LogWriter.DEBUG3);
174
}
175
176
177     /**
178      */

179     protected synchronized void commitTransaction( TransactionID tid ) {
180         persistenceSpace.commitTransaction( tid );
181     }
182
183
184     /**
185      */

186     protected synchronized void abortTransaction( TransactionID tid ) throws Exception JavaDoc {
187         persistenceSpace.abortTransaction( tid );
188     }
189
190
191     /**
192      */

193     protected synchronized void touchObject( ObjectID oid ) {
194         //env.logWriter.newEntry ("ClusterSpace.touchObject: " + oid, LogWriter.DEBUG3);
195
dobjBuffer.moveToTop( oid );
196     }
197
198
199     /**
200      * laedt den cluster cid und fuegt die objekte in die tabelle ein
201      */

202     private boolean loadCluster( ClusterID cid ) throws OzoneInternalException {
203         //env.logWriter.newEntry ("ClusterSpace.loadCluster: " + cid, LogWriter.DEBUG3);
204
loadedClusters++;
205         // cluster anfordern
206
Cluster cl = persistenceSpace.readCluster( cid, Cluster.DATA );
207
208         // free enough space to insert all objects
209
freeSpace( cl.size() );
210
211         // insert the objects
212
DxIterator it = cl.objects().iterator();
213         DeathObject dobj;
214         while ((dobj = (DeathObject)it.next()) != null) {
215             dobjBuffer.add( dobj );
216         }
217
218         return true;
219     }
220
221
222     private boolean freeSpace( long size ) {
223         //env.logWriter.newEntry ("ClusterSpace.freeSpace: " + size, LogWriter.DEBUG3);
224
while (dobjBuffer.size() + size > maxBufferSize) {
225             DeathObject dobj = dobjBuffer.pushFromBottom();
226             if (dobj != null) {
227                 dobj.container().setObject( null );
228             }
229         }
230         return true;
231     }
232 }
233
Popular Tags