001 /* 002 * DatasetSubset.java 003 * 004 * Created on August 4, 2006, 3:13 PM 005 * 006 * This file is part of the STAR Scheduler. 007 * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory 008 * 009 * STAR Scheduler is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation; either version 2 of the License, or 012 * (at your option) any later version. 013 * 014 * STAR Scheduler is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with STAR Scheduler; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 */ 023 024 package gov.bnl.star.offline.scheduler.dataset; 025 026 import gov.bnl.star.offline.scheduler.dataset.Dataset; 027 import gov.bnl.star.offline.scheduler.dataset.EntryParser; 028 import java.io.BufferedReader; 029 import java.io.FileNotFoundException; 030 import java.io.FileReader; 031 import java.io.IOException; 032 import java.util.ArrayList; 033 import java.util.List; 034 035 /** 036 * This class represents an indexed subset of the dataset. 037 * @author Levente Hajdu 038 */ 039 public class DatasetSubset { 040 041 private static int currentIndex; 042 private static BufferedReader currentDataset; 043 private static List buffereddataset; 044 private static int buffereddataset_Index; 045 046 private int index = 0; 047 public void setIndex(int index){ this.index = index; } 048 public int getIndex(){ return index; } 049 050 private Dataset dataset; 051 public void setDataset(Dataset dataset){this.dataset = dataset;} 052 public Dataset getDataset(){return dataset;} 053 054 /**This constructor should ***not*** be used*/ 055 public DatasetSubset(){} 056 057 /** Creates a new instance of DatasetSubset 058 * @param index The index of this subset 059 * @param dataset The dataset from which holds the subset being indexed. 060 **/ 061 public DatasetSubset(int index, Dataset dataset) { 062 this.index = index; 063 this.dataset = dataset; 064 if(currentDataset == null){ 065 resetDatasetStream(); 066 } 067 } 068 069 070 private void resetDatasetStream(){ 071 072 try{ 073 currentDataset = new BufferedReader( new FileReader(dataset.getDatasetName() )); 074 currentDataset.readLine(); 075 } 076 catch(Exception e){ 077 e.printStackTrace(); 078 throw new RuntimeException("Could not open dataset file \"" + dataset.getDatasetName() + "\""); 079 } 080 currentIndex = 0; 081 } 082 083 084 /**Move the reader stream to the start of this subset 085 * This function will save the stream, if the next index is bigger then the current index same stream can be used to 086 * read in the next index without having to open the file gain. If the stream has already passed the index then a new stearm has to be oppened. 087 */ 088 private void MoveStream(){ 089 090 if(currentIndex == index){ return; //if the stream is already it the right index there is no need to move it anywhere 091 092 }else if((currentIndex > index) || (currentDataset==null) ){ //has the current stream already passed this index 093 resetDatasetStream(); //when resubmiting the currentDataset is null 094 } 095 096 //else the stream is sitting somewhere the index and we have to move it down 097 String entry; 098 while(currentIndex != index){ 099 try { 100 entry = currentDataset.readLine(); 101 } catch (IOException ex) { 102 ex.printStackTrace(); 103 throw new RuntimeException("DatasetSubset[" + index + "] could not ready dataset entry at subset:" + currentIndex); 104 } 105 if(entry.matches(dataset.getSplitRegX())) currentIndex ++; 106 } 107 } 108 109 110 111 /** 112 *@return A list of PhysicalFile objects from the subset. 113 **/ 114 public List getPhysicalFileList(){ 115 116 if(buffereddataset != null){ 117 if(buffereddataset_Index == index) return buffereddataset; //if we just had this value from the last request there is not need to read it from the file agian 118 } 119 120 buffereddataset_Index = index; 121 122 if(currentDataset == null) resetDatasetStream(); //when resubmitting this can null 123 124 MoveStream(); //read ahead to the point in the file we need to read from 125 String entry = ""; 126 List entries = new ArrayList(); 127 128 129 do { 130 if(! entry.equals("")) entries.add(entry); 131 try { 132 entry = currentDataset.readLine(); 133 } catch (IOException ex) { 134 ex.printStackTrace(); 135 throw new RuntimeException("DatasetSubset[" + index + "] could not ready dataset entry at subset:" + currentIndex); 136 } 137 } while (! entry.matches(dataset.getSplitRegX())); 138 139 currentIndex ++; //now that we have passed read the current index, and passed it move the pointer up one 140 141 buffereddataset = dataset.getEntryParser().entryListToPhysicalFileList(entries); 142 143 return buffereddataset; 144 } 145 146 147 String node = null; 148 public void setNode(String node){this.node = node;} 149 public String getNode(){return node;} 150 151 String site = null; 152 public void setSite(String site){this.site = site;} 153 public String getSite(){return site;} 154 155 int eventsInSubset = 0; 156 public int getEventsInSubset(){ return eventsInSubset; } 157 public void setEventsInSubset(int eventsInSubset){ this.eventsInSubset = eventsInSubset; } 158 public void addToEventsInSubset(int moreEvents){ this.eventsInSubset += moreEvents; } 159 160 int filesInSubset = 0; 161 public int getFilesInSubset(){ return filesInSubset; } 162 public void setFilesInSubset(int filesInSubset){ this.filesInSubset = filesInSubset; } 163 public void addOneToFilesInSubset(){ this.filesInSubset ++; } 164 165 }