1 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 34 public class WizardObjectContainer extends StorageObjectContainer { 35 36 protected final static long serialVersionUID = 1L; 37 protected final static byte subSerialVersionUID = 2; 38 39 42 protected OzoneCompatible target; 43 44 protected ObjectID objID; 45 46 protected String name; 47 48 protected transient int invokeCount; 49 50 protected long modTime; 51 52 61 protected int garbageCollectionLevel; 62 63 64 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 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 110 public Class targetClass() { 111 try { 112 return target.getClass(); 113 } catch (NullPointerException e) { 114 if (target != null) { 115 throw e; 116 } else { 117 throw new NullPointerException (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 return invokeCount > 0; 166 } 167 168 169 public Object invokeTarget(Env env, String methodName, String sig, Object [] args) throws Exception { 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 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 _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 name() { 223 return name; 224 } 225 226 227 public void setName(String _name) { 228 name = _name; 229 } 230 231 232 public void writeExternal(ObjectOutput out) throws IOException { 233 out.writeByte(subSerialVersionUID); 235 out.writeObject(target); 236 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 { 250 byte streamVersion = in.readByte(); 252 target = (OzoneCompatible) in.readObject(); 253 if (target != null) { 254 target.setContainer(this); 255 } 256 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 283 public int ensureGarbageCollectionLevel(int newGarbageCollectionLevel) { 284 int difference = this.garbageCollectionLevel - newGarbageCollectionLevel; 285 286 if (difference < 0) { this.garbageCollectionLevel = newGarbageCollectionLevel; 288 touch(); 289 } 290 291 return difference; 292 } 293 294 295 298 public int getGarbageCollectionLevel() { 299 return garbageCollectionLevel; 300 } 301 302 307 public void pin() { 308 Cluster cluster = getCluster(); 309 if (cluster instanceof WizardCluster) { 310 ((WizardCluster)cluster).pin(); 311 } 312 } 313 314 318 public void unpin() { 319 Cluster cluster = getCluster(); 320 if (cluster instanceof WizardCluster) { 321 ((WizardCluster)cluster).unpin(); 322 } 323 } 324 325 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 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 | Popular Tags |