KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > wizardStore > WizardObjectContainer


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.wizardStore;
10
11 import java.io.*;
12
13 import org.ozoneDB.DxLib.DxCollection;
14 import org.ozoneDB.OzoneCompatible;
15 import org.ozoneDB.core.*;
16 import org.ozoneDB.core.storage.wizardStore.WizardCluster;
17 import org.ozoneDB.core.storage.StorageObjectContainer;
18 import org.ozoneDB.core.storage.Cluster;
19 import org.ozoneDB.util.LogWriter;
20
21
22 /**
23  * The "Wizard" implementation of the ObjectContainer interface. Much of the
24  * lock functionality is implemented in the WizardCluster class.
25  *
26  * Note: only the join/commit/abort methods are synchronized. All other methods
27  * are guaranteed to run exclusively through the explicite locks
28  *
29  *
30  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
31  * @author <A HREF="http://www.medium.net/">Medium.net</A>
32  * @version $Revision$Date$
33  */

34 public class WizardObjectContainer extends StorageObjectContainer {
35
36     protected final static long serialVersionUID = 1L;
37     protected final static byte subSerialVersionUID = 2;
38
39     /**
40      * The currently commited target object of the container.
41      */

42     protected OzoneCompatible target;
43
44     protected ObjectID objID;
45
46     protected String JavaDoc name;
47
48     protected transient int invokeCount;
49
50     protected long modTime;
51
52     /**
53      The garbage collection level of this ObjectContainer. This number is compared to the
54      currentGarbageCollectionLevel of the database.
55      Is this number smaller, then this object may be reachable.
56      Is it equal, then this object is reachable, but it's descendants are not yet considered.
57      Is it greater, then this object is reachable and this object is processed, it's descendants have been considered.
58      At the end of the mark-process, every object which still is not reachable must be unreachable, so at this and,
59      a smaller garbageCollectionLevel than currentGarbageCollectionLevel means that this object may be deleted.
60      */

61     protected int garbageCollectionLevel;
62
63
64     /**
65      * Constructor for object serialization via Externalizable.
66      */

67     public WizardObjectContainer() {
68     }
69
70
71     public WizardObjectContainer(ObjectID _objID) {
72         state = STATE_CREATED;
73         objID = _objID;
74     }
75
76     public void setCluster(Cluster cluster) {
77         this.cluster = cluster;
78         if (cluster == null) {
79             // cluster is invalidated by the ClusterStore
80
if (Env.currentEnv().logWriter.hasTarget(LogWriter.DEBUG3)) {
81                 Env.currentEnv().logWriter.newEntry(this, "setCluster(null).", LogWriter.DEBUG3);
82             }
83         }
84     }
85
86     public Cluster getCluster() {
87         return cluster;
88     }
89
90
91     public long modTime() {
92         return getCluster().modTime();
93     }
94
95
96     public boolean isDeleted() {
97         return (state() & ObjectContainer.STATE_DELETED) > 0;
98     }
99
100
101     protected boolean isCreated() {
102         return (state() & ObjectContainer.STATE_CREATED) > 0;
103     }
104
105
106     /**
107      * Returns the Class for the target object. This method is used by
108      * AbstractObjectContainer.
109      */

110     public Class JavaDoc targetClass() {
111         try {
112             return target.getClass();
113         } catch (NullPointerException JavaDoc e) {
114             if (target != null) {
115                 throw e;
116             } else {
117                 throw new NullPointerException JavaDoc(this + ": getCluster()=" + getCluster() + ": target=" + target + ".");
118             }
119         }
120     }
121
122
123     public synchronized void setTarget(OzoneCompatible _target) {
124         if (target != null) {
125             target.setContainer(null);
126         }
127         target = _target;
128         target.setContainer(this);
129     }
130
131
132     public OzoneCompatible target() {
133         return target;
134     }
135
136
137     public void touch() {
138         getCluster().touch();
139     }
140
141
142     public Lock lock() {
143         return getCluster().lock();
144     }
145
146
147     public synchronized void notifyAllTAs(Transaction ta) {
148         getCluster().lock().notifyAll();
149     }
150
151
152     public Permissions permissions() {
153         return getCluster().permissions();
154     }
155
156
157     public int lockLevel(Transaction ta) {
158         return getCluster().lock().level(ta);
159     }
160
161
162     public boolean isInvoked() {
163         // for performance reasons we assume that the specified transaction
164
// is the locking transaction
165
return invokeCount > 0;
166     }
167
168
169     public Object JavaDoc invokeTarget(Env env, String JavaDoc methodName, String JavaDoc sig, Object JavaDoc[] args) throws Exception JavaDoc {
170         if (Env.currentEnv().logWriter.hasTarget(LogWriter.DEBUG3)) {
171             Env.currentEnv().logWriter.newEntry(this, "invokeTarget(): " + target() + " " + methodName + ", " + sig + ", " + args, LogWriter.DEBUG3);
172         }
173         invokeCount++;
174
175         Object JavaDoc result;
176
177         try {
178             result = super.invokeTarget(env, methodName, sig, args);
179         } finally {
180             invokeCount--;
181         }
182
183         return result;
184     }
185
186     public void invokeOnActivate() {
187         invokeCount++;
188
189         try {
190             super.invokeOnActivate();
191         } finally {
192             invokeCount--;
193         }
194     }
195
196     public void deleteTarget() {
197         if (Env.currentEnv().logWriter.hasTarget(LogWriter.DEBUG3)) {
198             Env.currentEnv().logWriter.newEntry(this, "deleteTarget(): ", LogWriter.DEBUG3);
199         }
200         raiseState(STATE_DELETED);
201     }
202
203
204     public synchronized void nameTarget(String JavaDoc _name) {
205         if (Env.currentEnv().logWriter.hasTarget(LogWriter.DEBUG3)) {
206             Env.currentEnv().logWriter.newEntry(this, "nameTarget(): ", LogWriter.DEBUG3);
207         }
208         name = _name;
209     }
210
211
212     public DxCollection allLockers() {
213         return getCluster().allLockers();
214     }
215
216
217     public ObjectID id() {
218         return objID;
219     }
220
221
222     public String JavaDoc name() {
223         return name;
224     }
225
226
227     public void setName(String JavaDoc _name) {
228         name = _name;
229     }
230
231
232     public void writeExternal(ObjectOutput out) throws IOException {
233         // System.out.println ("container.writeExternal()...");
234
out.writeByte(subSerialVersionUID);
235         out.writeObject(target);
236         // out.writeObject (objID);
237
out.writeLong(objID.value());
238         if (name == null) {
239             out.writeByte(0);
240         } else {
241             out.writeByte(1);
242             out.writeUTF(name);
243         }
244         out.writeByte((byte) state);
245         out.writeInt(garbageCollectionLevel);
246     }
247
248
249     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
250         // System.out.println ("container.readExternal()...");
251
byte streamVersion = in.readByte();
252         target = (OzoneCompatible) in.readObject();
253         if (target != null) {
254             target.setContainer(this);
255         }
256         // objID = (ObjectID)in.readObject();
257
objID = new ObjectID(in.readLong());
258         name = null;
259         if (in.readByte() != 0) {
260             name = in.readUTF();
261         }
262         state = (int) in.readByte();
263         if (streamVersion >= 2) {
264             garbageCollectionLevel = in.readInt();
265         } else {
266             garbageCollectionLevel = 0;
267         }
268     }
269
270     /**
271      Ensures that the garbageCollectionLevel is at least the given currentGarbageCollectionLevel.
272      The return value is meaningful if the supplied newGarbageCollectionLevel is the currentGarbageCollectionLevel
273
274      @return
275      <=0 if this object still has to be processed.
276      This is the case if it belongs to the surelyReachable set but not to the processedReachable set
277      > otherwise
278
279      <0 if this object has been updated
280      =0 if this object has not been updated, it is surelyReachable
281      >0 if this object has not been updated, it is processedReachable
282      */

283     public int ensureGarbageCollectionLevel(int newGarbageCollectionLevel) {
284         int difference = this.garbageCollectionLevel - newGarbageCollectionLevel;
285
286         if (difference < 0) { // This object's garbageCollectionLevel must be updated
287
this.garbageCollectionLevel = newGarbageCollectionLevel;
288             touch();
289         }
290
291         return difference;
292     }
293
294
295     /**
296      Returns the garbageCollectionLevel this ObjectContainer has reached due to (not) calling {@link #ensureGarbageCollectionLevel}.
297      */

298     public int getGarbageCollectionLevel() {
299         return garbageCollectionLevel;
300     }
301
302     /**
303      Pins this ObjectContainer.
304      Every caller of this method must pair this call with a call to {@link #unpin}.
305      An ObjectContainer remains in main memory at least as long as it is pinned.
306      */

307     public void pin() {
308         Cluster cluster = getCluster();
309         if (cluster instanceof WizardCluster) {
310             ((WizardCluster)cluster).pin();
311         }
312     }
313
314     /**
315      Unpins this ObjectContainer.
316      This method must be called exactly once for every call to {@link #pin}.
317      */

318     public void unpin() {
319         Cluster cluster = getCluster();
320         if (cluster instanceof WizardCluster) {
321             ((WizardCluster)cluster).unpin();
322         }
323     }
324
325     /**
326      Returns wether this ObjectContainer is pinned.
327      */

328     public boolean isPinned() {
329         Cluster cluster = getCluster();
330         if (cluster instanceof WizardCluster) {
331             return ((WizardCluster)cluster).isPinned();
332         } else {
333             return false;
334         }
335     }
336
337     public boolean hasSameClusterAs(WizardObjectContainer container) {
338         return container.getCluster() == getCluster();
339     }
340
341     public String JavaDoc toString() {
342         if (name!=null) {
343             return "WizardObjectContainer[target="+target+",objID="+objID+",name=\""+name+"\",modTime="+modTime+",cluster="+cluster+"]";
344         } else {
345             return "WizardObjectContainer[target="+target+",objID="+objID+",name="+name+",modTime="+modTime+",cluster="+cluster+"]";
346         }
347     }
348 }
349
350 // :indentSize=4:tabSize=4:noTabs=true:
351
Popular Tags