📄 magiccluster.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-@year@ by SMB GmbH. All rights reserved.//// $Id: MagicCluster.java,v 1.2 2004/01/10 21:40:24 per_nyfelt Exp $package org.ozoneDB.core.storage.magicStore;import java.io.Externalizable;import java.io.File;import java.io.IOException;import java.io.ObjectInput;import java.io.ObjectOutput;import java.io.ObjectStreamException;import org.ozoneDB.DxLib.DxArrayBag;import org.ozoneDB.DxLib.DxCollection;import org.ozoneDB.DxLib.DxHashMap;import org.ozoneDB.DxLib.DxIterator;import org.ozoneDB.DxLib.DxLong;import org.ozoneDB.core.Lock;import org.ozoneDB.core.MROWLock;import org.ozoneDB.core.Permissions;import org.ozoneDB.core.Transaction;import org.ozoneDB.core.TransactionID;import org.ozoneDB.core.storage.*;import org.ozoneDB.util.LogWriter;/** * @author <a href="http://www.softwarebuero.de/">SMB</a> * @author <a href="http://www.medium.net/">Medium.net</a> * @author Leo Mekenkamp * @author Per Nyfelt * @version $Revision: 1.2 $Date: 2004/01/10 21:40:24 $ * We need to extend Cluster in wizardStore to be compatible with wizardStore * note: if be abandon compatibility it should extend AbstractCluster */public final class MagicCluster extends org.ozoneDB.core.wizardStore.Cluster implements Externalizable /*, ActiveCacheable*/ { protected final static long serialVersionUID = 2L; protected final static byte subSerialVersionUID = 1; protected transient MROWLock lock; protected transient long lastTouched; protected transient int bytesPerContainer; protected long modTime;// private transient ActiveCacheable.Delegator cacheDelegator; /** * Constructor to be used for Externalizable object serialisation. */ public MagicCluster() { this(null, null, null, 0); } public MagicCluster(ClusterID _clusterID, Permissions _permissions, MROWLock lock, int _bpc) { // the env and clusterStore will be set by clusterStore clusterID = _clusterID; permissions = _permissions; this.lock = lock; bytesPerContainer = _bpc; containers = new DxHashMap(1024); if (lock != null) { lock.setDebugInfo("clusterID=" + clusterID); }// cacheDelegator = new ActiveCacheable.Delegator(); } public void addedTo(Cache addedTo, Object key) {// cacheDelegator.addedTo(addedTo, key); } public void removedFrom(Cache removedFrom, Object key) {// cacheDelegator.removedFrom(removedFrom, key); } public void delete() throws Exception { File clusterFile = new File(clusterStore.basename(clusterID) + ClusterStore.POSTFIX_CLUSTER); if (!clusterFile.delete()) { throw new IOException("Could not delete cluster file " + clusterFile); } } protected synchronized void unregister() {// cacheDelegator.unregister(); } protected void finalize() { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "---finalize(): cluster " + clusterID, LogWriter.DEBUG3); } // any lock levels >= READ has to be persistent if (lock.level(null) >= Lock.LEVEL_READ) { if (env.logWriter.hasTarget(LogWriter.DEBUG)) { env.logWriter.newEntry(this, " write lock to disk: " + clusterID, LogWriter.DEBUG); } try { ((ClusterStore)clusterStore).storeDataImmediately(lock(), clusterStore.basename(clusterID) + ClusterStore.POSTFIX_LOCK); } catch (IOException e) { env.logWriter.newEntry(this, "could not write cluster lock " + clusterID, LogWriter.ERROR); } } else { File lockFile = new File(clusterStore.basename(clusterID) + ClusterStore.POSTFIX_LOCK); if (lockFile.exists()) { lockFile.delete(); } } // clusters with WRITE lock are supposed to be dirty if (lock.level(null) > Lock.LEVEL_UPGRADE) { if (env.logWriter.hasTarget(LogWriter.DEBUG)) { env.logWriter.newEntry(this, " write cluster: " + clusterID, LogWriter.DEBUG); } TransactionID taID = lock.getWriteLockingTransactionID(); if (taID == null) { env.logWriter.newEntry(this, "BUG: cluster is write locked but cannot find locker transaction id" + clusterID, LogWriter.ERROR); } try { ((ClusterStore)clusterStore).storeDataImmediately(this, clusterStore.basename(clusterID) + ClusterStore.POSTFIX_SEPARATOR + taID.value() + ClusterStore.POSTFIX_CLUSTER); } catch (IOException e) { env.logWriter.newEntry(this, "could not write cluster lock " + clusterID, LogWriter.ERROR); } } // mark this cluster to be not valid setLock(null); unregister(); } 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. */ public DxLong cachePriority() { return new DxLong(lastTouched); } public synchronized 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 StorageObjectContainer container; DxIterator it = containers.iterator(); while ((container = (StorageObjectContainer) it.next()) != null) { if (container.isInvoked()) { return true; } } return false; } public synchronized void touch() { lastTouched = clusterStore.touchCount++; } /** * Note: This method must not be synchronized. * * @param ta */ public void updateLockLevel(Transaction ta) throws IOException { if (env.logWriter.hasTarget(LogWriter.DEBUG2)) { env.logWriter.newEntry(this, "updateLockLevel(): " + clusterID, LogWriter.DEBUG2); env.logWriter.newEntry(this, " lock: " + lock.level(ta), LogWriter.DEBUG2); } } 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 IOException { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "commit()" + clusterID, LogWriter.DEBUG3); } if (true || lock != null) { lock.release(ta); } else {// env.logWriter.newEntry( this, this+".commit(): lock="+lock+".", LogWriter.DEBUG3 ); throw new NullPointerException(this + ".commit(" + ta + "): lock=" + lock + "."); } } public void abort(Transaction ta) throws IOException { if (env.logWriter.hasTarget(LogWriter.DEBUG3)) { env.logWriter.newEntry(this, "abort()" + clusterID, LogWriter.DEBUG3); } if (true || lock != null) { lock.release(ta); } else { throw new NullPointerException(this + ".abort(" + ta + "): lock=" + lock + "."); } } public 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; } public String toString() { return "MagicCluster[id=" + clusterID + "]"; } public Lock lock() { return lock; } public void setLock(Lock to) { //TODO: When can the lock be null and what is the significance of that? if (to == null) { if (env.logWriter.hasTarget(LogWriter.DEBUG)) { env.logWriter.newEntry(this, this + ".setLock(" + to + ") (oldLock=" + this.lock + ").", LogWriter.DEBUG); } } this.lock = (MROWLock) to; } public void writeExternal(ObjectOutput out) throws IOException {// // cannot use logwriter because of nullpointer exception// if ((containers.count() == 0 && pinCount != 0) || (containers.count() > 0 && pinCount > 1)) {// System.out.println("pincount for " + this + " not 1 while writing (" + pinCount + " for " + containers.count() + " containers)");// } // 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(); StorageObjectContainer container; while ((container = (StorageObjectContainer) it.next()) != null) { // out.writeObject( container ); container.writeExternal(out); } } public void readExternal(ObjectInput in) throws IOException, ObjectStreamException, ClassNotFoundException { // System.out.println ("cluster.readExternal()..."); byte subSerialUID = 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++) { try { // MagicObjectContainer container = (MagicObjectContainer)in.readObject(); StorageObjectContainer container = new MagicObjectContainer(); container.readExternal(in); container.setCluster(this);// Env.currentEnv().getLogWriter().newEntry(this,this+".readExternal(): container.id()="+container.id()+".",LogWriter.DEBUG2); containers.addForKey(container, container.id()); } catch (ObjectStreamException e) { if (env != null) { env.logWriter.newEntry(this, "ObjectStreamException for cluster " + clusterID + " at container #" + i + ": ", e, LogWriter.ERROR); } else { System.out.println("ObjectStreamException for cluster " + clusterID + " at container #" + i + ": " + e); } throw e; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -