📄 tapestorage.java
字号:
/* * Title: GridSim Toolkit * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation * of Parallel and Distributed Systems such as Clusters and Grids * Licence: GPL - http://www.gnu.org/copyleft/gpl.html */package gridsim.datagrid.storage;import gridsim.datagrid.File;import gridsim.ParameterException;import java.util.ArrayList;import java.util.List;import java.util.LinkedList;import java.util.Iterator;/** * An implementation of a tape storage system. It simulates the behaviour of a * typical tape storage with the following assumptions: * <ol> * <li> a constant operation for rewind, access and transfer time * <li> for every operation, the tape needs to be rewinded to the front * <li> a tape is supposed to be for backup purposes, once it is full, * individual files can't be deleted unless the tape is cleared * </ol> * <br> * The default values for this storage are those of * a HP Ultrium tape with the following parameters: * <ul> * <li> access time = 142 seconds from the beginning of a tape * <li> rewind time = 284 seconds from the ending of a tape * <li> max transfer rate = 8 MB/sec (raw data and uncompressed) * </ul> * * @author Uros Cibej and Anthony Sulistio * @since GridSim Toolkit 4.0 * @see gridsim.datagrid.storage.Storage */public class TapeStorage implements Storage { /** a list storing the names of all the files on the tape drive */ private ArrayList nameList_; /** a list storing all the files stored on the tape */ private LinkedList fileList_; /** the name of the tape drive*/ private String name_; /** the current size of files on the tape in MB */ private double currentSize_; /** the total capacity of the tape in MB*/ private double capacity_; /** the total rewind time in seconds */ private int rewindTime_; /** the total access time in seconds */ private int accessTime_; /** the maximum transfer rate in MB/sec */ private double maxTransferRate_; /** * Constructs a new tape storage with a given name and capacity. * * @param name the name of the new tape drive * @param capacity the capacity in MB * @throws ParameterException when the name and the capacity are not valid */ public TapeStorage(String name, double capacity) throws ParameterException { if (name == null || name.length() == 0) { throw new ParameterException( "TapeStorage(): Error - invalid tape drive name."); } if (capacity <= 0) { throw new ParameterException( "TapeStorage(): Error - capacity <= 0."); } name_ = name; capacity_ = capacity; init(); } /** * Constructs a new tape storage with a given capacity. * In this case the name of the storage is a default name. * @param capacity the capacity in MB * @throws ParameterException when the capacity is not valid */ public TapeStorage(double capacity) throws ParameterException { if (capacity <= 0) { throw new ParameterException( "TapeStorage(): Error - capacity <= 0."); } name_ = "TapeStorage"; capacity_ = capacity; init(); } /** * The initialization of the tape is done in this method. The most common * parameters, such as access time, rewind time and maximum transfer rate, * are set. The default values are set to simulate the HP Ultrium Tape. * Furthermore, the necessary lists are created. */ private void init() { nameList_ = new ArrayList(); fileList_ = new LinkedList(); currentSize_ = 0; // NOTE: Default value is taken from HP Ultrium Tape accessTime_ = 142; // sec from beginning of tape rewindTime_ = 284; // sec from end of tape // MB/sec for raw data not compressed (15MB/s) maxTransferRate_ = 8; } /** * Gets the available space on this storage in MB. * * @return the available space in MB */ public double getAvailableSpace() { return capacity_ - currentSize_; } /** * Checks if the storage is full or not. * * @return <tt>true</tt> if the storage is full, <tt>false</tt> * otherwise */ public boolean isFull() { if (currentSize_ == capacity_) { return true; } return false; } /** * Gets the number of files stored on this storage. * * @return the number of stored files */ public int getNumStoredFile() { return fileList_.size(); } /** * Makes a reservation of the space on the storage to store a file. * * @param fileSize the size to be reserved in MB * * @return <tt>true</tt> if reservation succeeded, <tt>false</tt> * otherwise */ public boolean reserveSpace(int fileSize) { if (fileSize <= 0) { return false; } if (currentSize_ + fileSize >= capacity_) { return false; } currentSize_ += fileSize; return true; } /** * Adds a file for which the space has already been reserved. * The time taken (in seconds) for adding the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * @param file the file to be added * * @return the time (in seconds) required to add the file */ public double addReservedFile(File file) { if (file == null) { return 0; } currentSize_ -= file.getSize(); double result = addFile(file); // if add file fails, then set the current size back to its old value if (result == 0.0) { currentSize_ += file.getSize(); } return result; } /** * Checks whether there is enough space on the storage for a certain file. * * @param fileSize * a FileAttribute object to compare to * @return <tt>true</tt> if enough space available, <tt>false</tt> * otherwise */ public boolean hasPotentialAvailableSpace(int fileSize) { if (fileSize <= 0) { return false; } // check if enough space left if (getAvailableSpace() > fileSize) { return true; } Iterator it = fileList_.iterator(); File file = null; int deletedFileSize = 0; // if not enough space, then if want to clear some files boolean result = false; while (it.hasNext()) { file = (File) it.next(); if (file.isReadOnly() == false) { deletedFileSize += file.getSize(); } if (deletedFileSize > fileSize) { result = true; break; } } return result; } /** * Gets the total capacity of the storage in MB. * * @return the capacity of the storage in MB */ public double getCapacity() { return capacity_; } /** * Gets the current size of the stored files in MB. * * @return the current size of the stored files in MB */ public double getCurrentSize() { return currentSize_; } /** * Gets the name of the storage. * * @return the name of this storage */ public String getName() { return name_; } /** * Gets the total access time of this tape drive in seconds. * @return the total access time in seconds */ public int getTotalAccessTime() { return accessTime_; } /** * Sets the total access time for this tape in seconds. * * @param time the total access time in seconds * @return <tt>true</tt> if the setting succeeds, <tt>false</tt> * otherwise */ public boolean setTotalAccessTime(int time) { if (time <= 0) { return false; } accessTime_ = time; return true; } /** * Gets the maximum transfer rate of the storage in MB/sec. * * @return the maximum transfer rate in MB/sec */ public double getMaxTransferRate() { return maxTransferRate_; } /** * Sets the maximum transfer rate of this storage system in MB/sec. * * @param rate the maximum transfer rate in MB/sec * @return <tt>true</tt> if the setting succeeded, <tt>false</tt> * otherwise */ public boolean setMaxTransferRate(int rate) { if (rate <= 0) { return false; } maxTransferRate_ = rate; return true; } /** * Sets the total rewind time of the tape. The total rewind time is the time * needed to rewind the tape from the end to the beginning. * * @param time the total rewind time in seconds * @return <tt>true</tt> if the setting succeeded, <tt>false</tt> otherwise */ public boolean setTotalRewindTime(int time) { if (time <= 0) { return false; } rewindTime_ = time; return true; } /** * Gets the total rewind time of the tape in seconds. * * @return the total rewind time in seconds */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -