📄 magicstore.java
字号:
} else { return container; } } } } } // search global table ONLY if ta doesn't contain the container if (cid == null) { cid = (ClusterID) idTable.elementForKey(id); } if (cid == null) { return null; } else { // System.out.println ("-"); Cluster cluster = clusterStore.loadCluster(cid, magicTa); if (cluster == null) { throw new ObjectNotFoundException("No object registered for ID: " + id); } if (transaction != null) { transaction.lrucid = cid; } try { container = cluster.containerForID(id); if (container.isDeleted()) { return null; } else { return container; } } catch (NullPointerException e) { env.logWriter.newEntry(this, "NullPointerException during " + cluster + ".containerForID(" + id + "). We were able to load a cluster for that ID but the cluster did not contain the container. This is an inconsistent view.", e, LogWriter.ERROR); throw e; } } } public DxSet objectNames(Transaction ta) { env.logWriter.newEntry(this, "Returning objectnames", LogWriter.DEBUG); return nameTable.keySet(); } public synchronized ObjectContainer containerForName(Transaction ta, String name) throws Exception { if (name == null) { return null; } MagicTransaction wizardTa = (MagicTransaction) ta; if (ta == null || wizardTa.nameTable == null) { ObjectID oid = (ObjectID) nameTable.elementForKey(name); return oid != null ? containerForID(ta, oid) : null; } if (ta != null) { ObjectID oid = (ObjectID) wizardTa.nameTable.elementForKey(name); return oid != null ? containerForID(ta, oid) : null; } return null; } public synchronized void nameContainer(Transaction ta, ObjectContainer container, String name) throws PermissionDeniedException { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "nameContainer(), nameTable=" + nameTable + ".", LogWriter.DEBUG3); } MagicTransaction transaction = (MagicTransaction) ta; if (transaction.nameTable == null) { Object o = null; try { java.lang.reflect.Method method = nameTable.getClass().getDeclaredMethod("clone", null); if (env.logWriter.hasTarget(LogWriter.DEBUG)) { env.logWriter.newEntry(this, "nameContainer(), nameTable=" + nameTable + ", nameTable.getClass().getDeclaredMethod(\"clone\",null)=" + method + ".", LogWriter.DEBUG); } o = method.invoke(nameTable, null); } catch (Exception e) { env.logWriter.newEntry(this, "nameContainer(): caught: ", e, LogWriter.INFO); } if (o == null) { o = nameTable.clone(); } transaction.nameTable = (DxMap) o; } String oldName = container.name(); if (oldName != null) { transaction.nameTable.removeForKey(oldName); transaction.nameTableChanges_push(new NameTableChange(container.id(), oldName, NameTableChange.STATE_REMOVED)); } container.nameTarget(name); if (name != null) { if (oldName == null) { garbageCollector.notifyNewObjectName(container); } transaction.nameTable.addForKey(container.id(), name); transaction.nameTableChanges_push(new NameTableChange(container.id(), name, NameTableChange.STATE_ADDED)); } } public synchronized DxBag clusterOfID(ObjectID id) throws Exception { throw new RuntimeException("this method is implementation specific and should be removed from StoreManager"); } public synchronized void prepareCommitTransaction(Transaction ta) throws IOException, ClassNotFoundException { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "prepareCommitTransaction()", LogWriter.DEBUG3); } MagicTransaction transaction = (MagicTransaction) ta; // initialize transaction data fields transaction.commitClusterIDs = new DxHashSet(64); DxIterator it = transaction.idTable.iterator();// env.logWriter.newEntry( this, "idTable count: " + taData.idTable.count(), LogWriter.DEBUG2 ); ClusterID cid; while ((cid = (ClusterID) it.next()) != null) { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "checking cluster: " + cid + " ...", LogWriter.DEBUG3); } if (!transaction.commitClusterIDs.contains(cid)) {// env.logWriter.newEntry( this, "prepare commit cluster: " + cid, LogWriter.DEBUG2 ); transaction.commitClusterIDs.add(cid); Cluster cluster = clusterStore.loadCluster(cid, transaction); // we don't need to do all the stuff if there is no WRITE // lock if (cluster.lock().level(ta) >= Lock.LEVEL_WRITE) { // delete containers from cluster and // prepare newID and deletedID tables DxIterator it2 = cluster.containers().iterator(); StorageObjectContainer container; while ((container = (StorageObjectContainer) it2.next()) != null) { if (container.isDeleted()) { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "container deleted: " + container.id(), LogWriter.DEBUG3); } clusterStore.invalidateContainer(container); transaction.idTableChanges_push(new IDTableChange(container.id(), cid, IDTableChange.STATE_REMOVED)); if (container.name() != null) { transaction.nameTableChanges_push(new NameTableChange(container.id(), container.name(), NameTableChange.STATE_REMOVED)); } } container.clearState(); } } clusterStore.prepareCommitCluster(ta, cid); } } } protected boolean isCleanShutdown() { return !(new File(COMMIT_FLAG_NAME).exists()); } protected void beginCommit(Transaction ta) throws IOException {// env.logWriter.newEntry( this, "beginCommit...", LogWriter.DEBUG2 ); // do nothing if this was just a read transaction if (ta.maxLockLevel() < Lock.LEVEL_WRITE) { return; } int commitCount = 0; // read the current commitCount from file File f = new File(env.getDatabaseDir() + COMMIT_FLAG_NAME); if (f.exists()) { DataInputStream in = null; try { in = new DataInputStream(new FileInputStream(f)); commitCount = in.readInt(); } finally { if (in != null) { in.close(); } } } commitCount++; DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream(f)); out.writeInt(commitCount); } finally { if (out != null) { out.close(); } } } protected void endCommit(Transaction ta) throws IOException {// env.logWriter.newEntry( this, "endCommit...", LogWriter.DEBUG2 ); // do nothing if this was just a read transaction if (ta.maxLockLevel() < Lock.LEVEL_WRITE) { return; } int commitCount = 0; // read the current commitCount from file File f = new File(env.getDatabaseDir() + COMMIT_FLAG_NAME); if (f.exists()) { DataInputStream in = null; try { in = new DataInputStream(new FileInputStream(f)); commitCount = in.readInt(); } finally { if (in != null) { in.close(); } } } else { throw new RuntimeException("No commit flag file present."); } commitCount--; if (commitCount > 0) { DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream(f)); out.writeInt(commitCount); } finally { if (out != null) { out.close(); } } } else { if (!(f.delete())) { throw new RuntimeException("Unable to delete commit flag file. "); } } } public synchronized void commitTransaction(Transaction ta) throws IOException, ClassNotFoundException { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "commitTransaction()", LogWriter.DEBUG3); } beginCommit(ta); MagicTransaction transaction = (MagicTransaction) ta; // actually write changed clusters to disk DxSet commitClusterIDs = transaction.commitClusterIDs; DxIterator it = commitClusterIDs.iterator(); ClusterID cid; while ((cid = (ClusterID) it.next()) != null) { if (env.logWriter.hasTarget(LogWriter.DEBUG2)) { env.logWriter.newEntry(this, "commit cluster: " + cid, LogWriter.DEBUG2); } clusterStore.commitCluster(ta, cid); } // update global idTable boolean isIDTableChanged = false; IDTableChange idChange = null; while ((idChange = transaction.idTableChanges_pop()) != null) { isIDTableChanged = true; switch (idChange.state) { case IDTableChange.STATE_ADDED: {// env.logWriter.newEntry( this, "commit added oid: " + idChange.oid, LogWriter.DEBUG2 ); if (idTable.addForKey(idChange.cid, idChange.oid) == false) { throw new IllegalStateException("Unable to add OID to global ID table."); } break; } case IDTableChange.STATE_REMOVED: { if (false) { env.logWriter.newEntry(this, "commit removed oid: " + idChange.oid, LogWriter.DEBUG2); } if (idTable.removeForKey(idChange.oid) == null) { throw new IllegalStateException("Unable to remove OID from global ID table."); } break; } default: { throw new RuntimeException("ID change table entry has inproper state."); } } } // write changes to disk if (isIDTableChanged) { commitIDTable(); } // update global nameTable boolean isNameTableChanged = false; NameTableChange nameChange = transaction.nameTableChanges_pop(); while (nameChange != null) { isNameTableChanged = true; switch (nameChange.state) { case NameTableChange.STATE_ADDED: { nameTable.addForKey(nameChange.oid, nameChange.name); break; } case NameTableChange.STATE_REMOVED: { nameTable.removeForKey(nameChange.name); break; } default: { throw new RuntimeException("Name change table entry has inproper state."); } } nameChange = transaction.nameTableChanges_pop(); } // write changes to disk if (isNameTableChanged) { commitNameTable(); } endCommit(ta); if (env.logWriter.hasTarget(LogWriter.DEBUG2)) { env.logWriter.newEntry(this, " idTable.count(): " + idTable.count(), LogWriter.DEBUG2); env.logWriter.newEntry(this, " nameTable.count(): " + nameTable.count(), LogWriter.DEBUG2); } } /** * @param ta ID of the comitting transaction. */ public synchronized void abortTransaction(Transaction ta) throws IOException, ClassNotFoundException { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "abortTransaction()", LogWriter.DEBUG3); } clusterStore.abortTransaction((MagicTransaction) ta); } /** Tells this StoreManager to report every named object to the garbage collector. */ public void reportNamedObjectsToGarbageCollector() { synchronized (this) { DxIterator i = nameTable.elementSet().iterator(); while (i.next() != null) { garbageCollector.notifyNamedObject((ObjectID) i.object()); } } } public Transaction createTransaction(Env env, User user) { return new MagicTransaction(env, user); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -