1 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 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 43 protected OzoneCompatible target; 44 45 protected ObjectID objID; 46 47 protected String name; 48 49 protected transient int invokeCount; 50 51 protected long modTime; 52 53 62 protected int garbageCollectionLevel; 63 64 65 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 96 public Class targetClass() { 97 try { 98 return target.getClass(); 99 } catch (NullPointerException e) { 100 if (target != null) { 101 throw e; 102 } else { 103 throw new NullPointerException (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 return invokeCount > 0; 152 } 153 154 155 public Object invokeTarget(Env env, String methodName, String sig, Object [] args) throws Exception { 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 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 _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 name() { 209 return name; 210 } 211 212 213 public void setName(String _name) { 214 name = _name; 215 } 216 217 218 public final void writeExternal(ObjectOutput out) throws IOException { 219 out.writeByte(subSerialVersionUID); 221 out.writeObject(target); 222 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 { 236 byte streamVersion = in.readByte(); 238 target = (OzoneCompatible) in.readObject(); 239 if (target != null) { 240 target.setContainer(this); 241 } 242 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 269 public int ensureGarbageCollectionLevel(int newGarbageCollectionLevel) { 270 int difference = this.garbageCollectionLevel - newGarbageCollectionLevel; 271 272 if (difference < 0) { this.garbageCollectionLevel = newGarbageCollectionLevel; 274 touch(); 275 } 276 277 return difference; 278 } 279 280 281 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 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 } 307 308 public void unpin() { 309 } 311 } 312 313 | Popular Tags |