KickJava   Java API By Example, From Geeks To Geeks.

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


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: GammaContainer.java,v 1.2 2004/03/21 21:05:51 leomekenkamp Exp $
7

8 package org.ozoneDB.core.storage.gammaStore;
9
10 import java.io.Externalizable JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInput JavaDoc;
13 import java.io.ObjectOutput JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedHashSet JavaDoc;
16 import java.util.Map JavaDoc;
17 import javax.naming.Reference JavaDoc;
18 import org.ozoneDB.OzoneCompatible;
19 import org.ozoneDB.DxLib.DxArrayBag;
20 import org.ozoneDB.DxLib.DxCollection;
21 import org.ozoneDB.DxLib.DxIterator;
22 import org.ozoneDB.core.AbstractObjectContainer;
23 import org.ozoneDB.core.Env;
24 import org.ozoneDB.core.Lock;
25 import org.ozoneDB.core.MROWLock;
26 import org.ozoneDB.core.ObjectContainer;
27 import org.ozoneDB.core.ObjectID;
28 import org.ozoneDB.core.Permissions;
29 import org.ozoneDB.core.Transaction;
30 import org.ozoneDB.core.TransactionID;
31 import org.ozoneDB.core.storage.Cache;
32 import org.ozoneDB.util.LogWriter;
33
34
35
36
37 /**
38  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
39  * @version $Id: GammaContainer.java,v 1.2 2004/03/21 21:05:51 leomekenkamp Exp $
40  */

41 public final class GammaContainer extends AbstractObjectContainer implements Externalizable JavaDoc {
42
43     private static final long serialVersionUID = 1L;
44
45     private OzoneCompatible target;
46
47     private ObjectID objectID;
48
49     private String JavaDoc name;
50
51     private Lock lock;
52     
53     private transient GammaStore gammaStore;
54     
55     private Permissions permissions;
56     
57     
58     /**
59      * The garbage collection level of this ObjectContainer. This number is
60      * compared to the currentGarbageCollectionLevel of the database. Is this
61      * number smaller, then this object may be reachable. Is it equal, then this
62      * object is reachable, but it's descendants are not yet considered. Is it
63      * greater, then this object is reachable and this object is processed, it's
64      * descendants have been considered.
65      * At the end of the mark-process, every object which still is not reachable
66      * must be unreachable, so at this and, a smaller garbageCollectionLevel
67      * than currentGarbageCollectionLevel means that this object may be deleted.
68      */

69     private int garbageCollectionLevel;
70
71 // /**
72
// * Constructor for object serialization via Externalizable.
73
// */
74
// public GammaContainer() {
75
// }
76

77     public GammaContainer(ObjectID objectID, GammaStore gammaStore) {
78         state = STATE_CREATED;
79         this.objectID = objectID;
80         setGammaStore(gammaStore);
81         lock = new MROWLock();
82         permissions = new Permissions();
83     }
84
85     private void setGammaStore(GammaStore gammaStore) {
86         this.gammaStore = gammaStore;
87     }
88     
89     private GammaStore getGammaStore() {
90         return gammaStore;
91     }
92     
93     public long modTime() {
94         return 0;
95     }
96
97
98     protected boolean isDeleted() {
99         return (state() & ObjectContainer.STATE_DELETED) > 0;
100     }
101
102
103     protected boolean isCreated() {
104         return (state() & ObjectContainer.STATE_CREATED) > 0;
105     }
106
107
108     /**
109      * Returns the Class for the target object. This method is used by
110      * AbstractObjectContainer.
111      */

112     public Class JavaDoc targetClass() {
113         return getTarget().getClass();
114     }
115
116
117     public synchronized void setTarget(OzoneCompatible target) {
118         if (getTarget() != null) {
119             getTarget().setContainer(null);
120         }
121         this.target = target;
122         target.setContainer(this);
123     }
124
125
126     public OzoneCompatible target() {
127         return getTarget();
128     }
129
130     public OzoneCompatible getTarget() {
131         return target;
132     }
133
134
135     public void touch() {
136         getGammaStore().getContainerCache().get(getObjectId());
137     }
138
139
140     public Lock lock() {
141         return lock;
142     }
143
144
145     public void updateLockLevel(Transaction ta) throws Exception JavaDoc {
146         // CRITICAL: what to do here?
147
}
148
149
150     public synchronized void notifyAllTAs(Transaction ta) {
151         lock.notifyAll();
152     }
153
154
155     public Permissions permissions() {
156         return permissions;
157     }
158
159
160     public int lockLevel(Transaction ta) {
161         return lock.level(ta);
162     }
163
164
165     public boolean isInvoked() {
166         // CRITICAL: what to do here?
167
return true;
168     }
169
170     public void deleteTarget() {
171         if (Env.currentEnv().logWriter.hasTarget(LogWriter.DEBUG3)) {
172             Env.currentEnv().logWriter.newEntry(this, "deleteTarget(): ", LogWriter.DEBUG3);
173         }
174         raiseState(STATE_DELETED);
175     }
176
177
178     public synchronized void nameTarget(String JavaDoc name) {
179         this.name = name;
180     }
181
182
183     public DxCollection allLockers() {
184         DxCollection lockerIDs = lock.lockerIDs();
185
186         DxArrayBag result = new DxArrayBag(lockerIDs.count());
187         DxIterator it = lockerIDs.iterator();
188         while (it.next() != null) {
189             result.add(Env.currentEnv().transactionManager.taForID((TransactionID) it.object()));
190         }
191
192         return result;
193     }
194
195
196     public boolean equals(Object JavaDoc object) {
197         if (object != null && object instanceof GammaContainer) {
198             GammaContainer gammaContainer = (GammaContainer) object;
199             return objectID.equals(gammaContainer.objectID);
200         }
201         return false;
202     }
203
204
205     public ObjectID id() {
206         return objectID;
207     }
208     
209     public ObjectID getObjectId() {
210         return objectID;
211     }
212
213
214     public String JavaDoc name() {
215         return name;
216     }
217
218
219     public void setName(String JavaDoc name) {
220         this.name = name;
221     }
222
223
224     public final void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
225 // out.writeLong(objectID.value());
226
// if (name == null) {
227
// out.writeByte(0);
228
// } else {
229
// out.writeByte(1);
230
// out.writeUTF(name);
231
// }
232
out.writeObject(target);
233         out.writeByte((byte) state);
234         out.writeInt(garbageCollectionLevel);
235     }
236
237
238     public final void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
239 // objectID = new ObjectID(in.readLong());
240
// name = (in.readByte() == 0) ? null : in.readUTF();
241
target = (OzoneCompatible) in.readObject();
242         if (target != null) {
243             target.setContainer(this);
244         }
245         state = (int) in.readByte();
246         garbageCollectionLevel = in.readInt();
247     }
248
249     /**
250      Ensures that the garbageCollectionLevel is at least the given currentGarbageCollectionLevel.
251      The return value is meaningful if the supplied newGarbageCollectionLevel is the currentGarbageCollectionLevel
252
253      @return
254      <=0 if this object still has to be processed.
255      This is the case if it belongs to the surelyReachable set but not to the processedReachable set
256      > otherwise
257
258      <0 if this object has been updated
259      =0 if this object has not been updated, it is surelyReachable
260      >0 if this object has not been updated, it is processedReachable
261      */

262     public int ensureGarbageCollectionLevel(int newGarbageCollectionLevel) {
263         int difference = this.garbageCollectionLevel - newGarbageCollectionLevel;
264
265         if (difference < 0) { // This object's garbageCollectionLevel must be updated
266
this.garbageCollectionLevel = newGarbageCollectionLevel;
267             // touch(); TODO: find out if there is need to touch
268
}
269
270         return difference;
271     }
272
273
274     /**
275      Returns the garbageCollectionLevel this ObjectContainer has reached due to (not) calling {@link #ensureGarbageCollectionLevel}.
276      */

277     public int getGarbageCollectionLevel() {
278         return garbageCollectionLevel;
279     }
280
281     /**
282      * GammaStore does not not care about pin.
283      */

284     public void pin() {
285     }
286
287     /**
288      * GammaStore does not not care about pin.
289      */

290     public void unpin() {
291     }
292
293     /**
294      * GammaStore does not not care about pin.
295      */

296     public boolean isPinned() {
297         return true;
298     }
299
300     public String JavaDoc toString() {
301         return "GammaContainer[" + objectID + ", " + name + ", " + target + "]";
302     }
303     
304 }
305
Popular Tags