KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > magicStore > MagicObjectContainer


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.magicStore;
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.util.LogWriter;
17
18
19 /**
20  * The "Wizard" implementation of the ObjectContainer interface. Much of the
21  * lock functionality is implemented in the MagicCluster class.
22  *
23  * Note: only the join/commit/abort methods are synchronized. All other methods
24  * are guaranteed to run exclusively through the explicite locks
25  *
26  *
27  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
28  * @author <A HREF="http://www.medium.net/">Medium.net</A>
29  * @author Leo Mekenkamp
30  * @author Per Nyfelt
31  * @version $Revision$Date$
32  * We need to extend WizardObjectContainer to be compatible with wizardStore
33  * note: if we drop backward compatibility it should extend StorageObjectContainer
34  */

35 public final class MagicObjectContainer extends org.ozoneDB.core.wizardStore.WizardObjectContainer implements Externalizable {
36
37     protected final static long serialVersionUID = 1L;
38     protected final static byte subSerialVersionUID = 2;
39
40     /**
41      * The currently commited target object of the container.
42      */

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

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

68     public MagicObjectContainer() {
69     }
70
71
72     public MagicObjectContainer(ObjectID _objID) {
73         state = STATE_CREATED;
74         objID = _objID;
75     }
76
77     public long modTime() {
78         return getCluster().modTime();
79     }
80
81
82     public boolean isDeleted() {
83         return (state() & ObjectContainer.STATE_DELETED) > 0;
84     }
85
86
87     protected boolean isCreated() {
88         return (state() & ObjectContainer.STATE_CREATED) > 0;
89     }
90
91
92     /**
93      * Returns the Class for the target object. This method is used by
94      * AbstractObjectContainer.
95      */

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

269     public int ensureGarbageCollectionLevel(int newGarbageCollectionLevel) {
270         int difference = this.garbageCollectionLevel - newGarbageCollectionLevel;
271
272         if (difference < 0) { // This object's garbageCollectionLevel must be updated
273
this.garbageCollectionLevel = newGarbageCollectionLevel;
274             touch();
275         }
276
277         return difference;
278     }
279
280
281     /**
282      Returns the garbageCollectionLevel this ObjectContainer has reached due to (not) calling {@link #ensureGarbageCollectionLevel}.
283      */

284     public int getGarbageCollectionLevel() {
285         return garbageCollectionLevel;
286     }
287
288     public boolean hasSameClusterAs(MagicObjectContainer container) {
289         return container.getCluster() == getCluster();
290     }
291
292     public String JavaDoc toString() {
293         if (name!=null) {
294             return "MagicObjectContainer[target="+target+",objID="+objID+",name=\""+name+"\",modTime="+modTime+",cluster="+cluster+"]";
295         } else {
296             return "MagicObjectContainer[target="+target+",objID="+objID+",name="+name+",modTime="+modTime+",cluster="+cluster+"]";
297         }
298     }
299
300     public boolean isPinned() {
301         return false;
302     }
303
304     public void pin() {
305         // do nothing;
306
}
307
308     public void unpin() {
309         // do nothing
310
}
311 }
312
313 // :indentSize=4:tabSize=4:noTabs=true:
314
Popular Tags