📄 cluster.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.//// $Id: Cluster.java,v 1.26 2000/11/03 13:57:28 daniela Exp $package org.ozoneDB.core.wizardStore;import java.io.*;import org.ozoneDB.DxLib.*;import org.ozoneDB.core.*;import org.ozoneDB.util.*;/** * @author <a href="http://www.softwarebuero.de/">SMB</a> * @version $Revision: 1.26 $Date: 2000/11/03 13:57:28 $ */public final class Cluster implements Externalizable { protected final static long serialVersionUID = 2L; protected final static byte subSerialVersionUID = 1; /** * The environment will be set by the clusterStore. */ protected transient Env env; protected transient ClusterStore clusterStore; protected ClusterID clusterID; /** * Maps ObjectIDs into WizardObjectContainers. */ protected DxMap containers; protected Permissions permissions; protected transient Lock lock; protected transient long lastTouched; protected transient int bytesPerContainer; protected long modTime; /** * Constructor to be used for Externalizable object serialisation. */ public Cluster() { this( null, null, null, 0 ); } public Cluster( ClusterID _clusterID, Permissions _permissions, Lock _lock, int _bpc ) { // the env and clusterStore will be set by clusterStore clusterID = _clusterID; permissions = _permissions; lock = _lock; bytesPerContainer = _bpc; containers = new DxHashMap( 1024 ); } public void finalize() { if (env.logWriter.hasTarget( LogWriter.DEBUG )) { env.logWriter.newEntry( this, "---finalize(): cluster " + clusterID, LogWriter.DEBUG ); } } public long modTime() { return modTime; } /** * Priority of this cluster to stay in the cluster cache. Low return value * means low priority. * @return Cache priority of the cluster. */ protected DxLong cachePriority() { return new DxLong( lastTouched ); } public void setCurrentSize( int byteSize ) { int containerCount = containers.count(); bytesPerContainer = containerCount > 0 ? byteSize / containerCount : clusterStore.currentBytesPerContainer(); } public int size() { return containers.count() * bytesPerContainer; } public ClusterID clusterID() { return clusterID; } /** * @return True if at least one container is currently invoked. */ public boolean isInvoked() { //FIXME: should be a bit in the status byte that is directly changed // by clusters when they are changed WizardObjectContainer container; DxIterator it = containers.iterator(); while ((container = (WizardObjectContainer)it.next()) != null) { if (container.isInvoked()) { return true; } } return false; } public void touch() { lastTouched = clusterStore.touchCount++; } public void registerContainer( WizardObjectContainer container ) { container.cluster = this; containers.addForKey( container, container.id() ); } public void removeContainer( WizardObjectContainer container ) { containers.removeForKey( container.id() ); } public WizardObjectContainer containerForID( ObjectID id ) { return (WizardObjectContainer)containers.elementForKey( id ); } /** * Note: This method must not be synchronized. * * * @param ta * @param lockLevel */ public void updateLockLevel( Transaction ta ) throws Exception { if (env.logWriter.hasTarget( LogWriter.DEBUG )) { env.logWriter.newEntry( this, "updateLockLevel(): " + clusterID, LogWriter.DEBUG ); env.logWriter.newEntry( this, " lock: " + lock.level( ta ), LogWriter.DEBUG ); } if (lock.level( ta ) > Lock.LEVEL_UPGRADE) { saveShadow(); } } public void prepareCommit( Transaction ta ) throws Exception { if (env.logWriter.hasTarget( LogWriter.DEBUG3 )) { env.logWriter.newEntry( this, "prepareCommit()" + clusterID, LogWriter.DEBUG3 ); } if (lock.level( ta ) > Lock.LEVEL_UPGRADE) { synchronized (this) { modTime = System.currentTimeMillis(); } } } public void commit( Transaction ta ) throws Exception { if (env.logWriter.hasTarget( LogWriter.DEBUG3 )) { env.logWriter.newEntry( this, "commit()" + clusterID, LogWriter.DEBUG3 ); } if (lock.level( ta ) > Lock.LEVEL_UPGRADE) { deleteShadow(); } lock.release( ta ); } public void abort( Transaction ta ) throws Exception { if (env.logWriter.hasTarget( LogWriter.DEBUG3 )) { env.logWriter.newEntry( this, "abort()" + clusterID, LogWriter.DEBUG3 ); } if (lock.level( ta ) > Lock.LEVEL_UPGRADE) { restoreShadow(); deleteShadow(); } lock.release( ta ); } // protected void clearContainerStates() { // WizardObjectContainer container; // DxIterator it = containers.iterator(); // while ((container=(WizardObjectContainer)it.next()) != null) { // container.clearState(); // } // } protected DxCollection allLockers() { DxCollection lockerIDs = lock.lockerIDs(); DxArrayBag result = new DxArrayBag( lockerIDs.count() ); DxIterator it = lockerIDs.iterator(); while (it.next() != null) { result.add( env.transactionManager.taForID( (TransactionID)it.object() ) ); } return result; } protected void saveShadow() throws Exception { if (env != null) { env.logWriter.newEntry( this, "saveShadow(): " + clusterID, LogWriter.DEBUG3 ); } String basename = clusterStore.basename( clusterID ); File orig = new File( basename + ClusterStore.POSTFIX_CLUSTER ); File shadow = new File( basename + ClusterStore.POSTFIX_SHADOW ); orig.renameTo( shadow ); } /** * Restore the saved shadow on disk. The content of the receiver stays the * same. The cluster needs to be re-loaded to reflect the changes. */ protected void restoreShadow() throws Exception { if (env != null) { env.logWriter.newEntry( this, "restoreShadow(): " + clusterID, LogWriter.DEBUG3 ); } // restore cluster on disk by renaming back shadow file String basename = clusterStore.basename( clusterID ); File orig = new File( basename + ClusterStore.POSTFIX_CLUSTER ); File shadow = new File( basename + ClusterStore.POSTFIX_SHADOW ); if (!shadow.renameTo( orig )) { throw new IOException( "Unable to rename shadow file." ); } } protected void deleteShadow() throws Exception { if (env.logWriter.hasTarget( LogWriter.DEBUG3 )) { env.logWriter.newEntry( this, "deleteShadow(): " + clusterID, LogWriter.DEBUG3 ); } String basename = clusterStore.basename( clusterID ); File shadow = new File( basename + ClusterStore.POSTFIX_SHADOW ); if (shadow.exists() && !shadow.delete()) { throw new IOException( "Unable to delete shadow file." ); } } /** * Delete this cluster from the disk. */ protected void delete() throws Exception { String basename = clusterStore.basename( clusterID ); new File( basename + ClusterStore.POSTFIX_CLUSTER ).delete(); new File( basename + ClusterStore.POSTFIX_SHADOW ).delete(); new File( basename + ClusterStore.POSTFIX_TEMP ).delete(); new File( basename + ClusterStore.POSTFIX_LOCK ).delete(); } public void writeExternal( ObjectOutput out ) throws IOException { // System.out.println ("cluster.writeExternal()..."); out.writeByte( subSerialVersionUID ); out.writeObject( clusterID ); out.writeObject( permissions ); out.writeLong( modTime ); // out.writeObject (lock); out.writeInt( containers.count() ); DxIterator it = containers.iterator(); Object container; while ((container = it.next()) != null) { out.writeObject( container ); } } public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { // System.out.println ("cluster.readExternal()..."); byte streamUID = in.readByte(); clusterID = (ClusterID)in.readObject(); permissions = (Permissions)in.readObject(); modTime = in.readLong(); // lock = (Lock)in.readObject(); int count = in.readInt(); for (int i = 0; i < count; i++) { WizardObjectContainer container = (WizardObjectContainer)in.readObject(); container.cluster = this; containers.addForKey( container, container.id() ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -