001    /*
002     * $RCSfile: QueryResult.java,v $
003     *
004     * Created on April 3, 2003, 9:37 AM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2003 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    package gov.bnl.star.offline.scheduler.catalog;
024    
025    import gov.bnl.star.offline.scheduler.policy.Location;
026    
027    import java.util.*;
028    
029    
030    /**@deprecated please see gov.bnl.star.offline.scheduler.dataset.* for replacements.
031     * @author Gabriele Carcassi & Pavel Jakl
032     * @version $Revision: 1.8 $ $Date: 2006/11/21 00:41:31 $
033     */
034    public class QueryResult {
035        private gov.bnl.star.offline.scheduler.policy.ListMap files = new gov.bnl.star.offline.scheduler.policy.ListMap();
036    
037        public String getReport() {
038            StringBuffer report = new StringBuffer();
039            report.append("nFiles ").append(getLogicalCount()).append(" nCopies ")
040                    .append(getPhysicalCount());
041    
042            return report.toString();
043        }
044    
045        public boolean add(Object logicalName, PhysicalFile file) {
046            return files.add(logicalName, file);
047        }
048    
049        public boolean remove(Object logicalName) {
050            return files.remove(logicalName);
051        }
052    
053        public List getCopies(Object logicalName) {
054            return files.getList(logicalName);
055        }
056    
057        public Set getLogicalNames() {
058            return files.getKeys();
059        }
060    
061        public int getLogicalCount() {
062            return files.getKeys().size();
063        }
064    
065        public int getPhysicalCount() {
066            return files.size();
067        }
068    
069        public boolean removeAllLogicalOfPhysical(java.util.Collection coll) {
070            Iterator iter = coll.iterator();
071    
072            while (iter.hasNext()) {
073                PhysicalFile file = (PhysicalFile) iter.next();
074                remove(file.getLogicalID());
075            }
076    
077            return true;
078        }
079    
080        /**
081         * Get all logical names as java.util.List
082         *
083         * @return List of all Loogical names
084         */
085        public List getListLogicalNames() {
086            return Arrays.asList(getLogicalNames().toArray());
087        }
088    
089        public List getSinglePhysicalNoNFSorHPSS() {
090            List singleCopy = new ArrayList();
091            Set files = getLogicalNames();
092            Iterator iter = files.iterator();
093    
094            while (iter.hasNext()) {
095                Object file = iter.next();
096                List copies = getCopies(file);
097    
098                if (copies.size() == 1) {
099                    PhysicalFile copy=(PhysicalFile) copies.get(0);
100                    if (!"NFS".equals(copy.getStorage()) && !"HPSS".equals(copy.getStorage())) {
101                        singleCopy.add(copy);
102                    }
103                }
104            }
105    
106            return singleCopy;
107        }
108    
109        public List getPhysicalOnLocation(Location location) {
110            if (location.equals(Location.getNFS())) {
111                return getPhysicalOnNFS();
112            } else if (location.equals(Location.getHPSS())) {
113                return getPhysicalOnHPSS();
114            }
115            return getPhysicalOnNode(location.toString());
116        }
117    
118        public List getPhysicalOnNode(String node) {
119            List copiesOnNode = new ArrayList();
120            Set files = getLogicalNames();
121            Iterator iter = files.iterator();
122    
123            while (iter.hasNext()) {
124                Object file = iter.next();
125                List copies = getCopies(file);
126                Iterator iter2 = copies.iterator();
127                boolean notFoundOnNode = true;
128                while (iter2.hasNext() && notFoundOnNode) {
129                    PhysicalFile copy = (PhysicalFile) iter2.next();
130    
131                    if (node.equals(copy.getNode())) {
132                        copiesOnNode.add(copy);
133                        notFoundOnNode = false;
134                    }
135                }
136            }
137    
138            return copiesOnNode;
139        }
140    
141        /**
142         * Get all physical files in HPSS
143         *
144         * @return list of PhysicalFiles
145         */
146        public List getPhysicalOnHPSS() {
147            List copiesToAdd = new ArrayList();
148            Set files = getLogicalNames();
149            Iterator iter = files.iterator();
150    
151            while (iter.hasNext()) {
152                Object file = iter.next();
153                List copies = getCopies(file);
154                Iterator iter2 = copies.iterator();
155                // flag to bound a copy from one logicalID on HPSS to one
156                boolean notOnHPSS = true;
157                while (iter2.hasNext() && notOnHPSS) {
158                    PhysicalFile copy = (PhysicalFile) iter2.next();
159                    if ("HPSS".equals(copy.getStorage())) {
160                        notOnHPSS = false;
161                        copiesToAdd.add(copy);
162                    }
163                }
164            }
165            return copiesToAdd;
166        }
167    
168        public List getPhysicalOnNFS() {
169            List copiesToAdd = new ArrayList();
170            Set files = getLogicalNames();
171            Iterator iter = files.iterator();
172    
173            while (iter.hasNext()) {
174                Object file = iter.next();
175                List copies = getCopies(file);
176                Iterator iter2 = copies.iterator();
177                // flag to bound a copy from one logicalID on NFS to one
178                boolean notOnNFS = true;
179                while (iter2.hasNext() && notOnNFS) {
180                    PhysicalFile copy = (PhysicalFile) iter2.next();
181                    if ("NFS".equals(copy.getStorage())) {
182                        notOnNFS = false;
183                        copiesToAdd.add(copy);
184                    }
185                }
186            }
187            return copiesToAdd;
188        }
189    
190        /**
191         * Get all physical NFS copies, choosen randomly
192         *
193         * @return <code>List</code> of all physicall NFS copies
194         * written by: Pavel Jakl
195         */
196        public List getRandomPhysicalOnNFSOrLocal() {
197            //  instance for random number
198            Random rand = new Random();
199            //  instance for copies to be returned
200            List copiesToAdd = new ArrayList();
201            //  getLogicalIds
202            Set files = getLogicalNames();
203            Iterator iter = files.iterator();
204            //  iterate the logicalIds
205            while (iter.hasNext()) {
206                //  holdAll copiesOnNFS or local to current id
207                ArrayList copiesOnNFSOrLocalToId = new ArrayList();
208                Object file = iter.next();
209                //  get copies for the file
210                List copies = getCopies(file);
211                //  iterate the copies collection
212                Iterator iter2 = copies.iterator();
213                while (iter2.hasNext()) {
214                    PhysicalFile copy = (PhysicalFile) iter2.next();
215                    //  add copies on NFS or local and local
216                    if ("NFS".equals(copy.getStorage()) || "local".equals(copy.getStorage())) {
217                        copiesOnNFSOrLocalToId.add(copy);
218                    }
219                }
220                //  take all copies for current id and choose randomly one
221                PhysicalFile copyToAdd = null;
222                //  to be sure that everything is fine in Catalog
223                if (!copiesOnNFSOrLocalToId.isEmpty()) {
224                    copyToAdd = (PhysicalFile) copiesOnNFSOrLocalToId.get(rand.nextInt(copiesOnNFSOrLocalToId.size()));
225                    //  to be sure that we don't want to add null
226                    if (copyToAdd != null) {
227                        copiesToAdd.add(copyToAdd);
228                    }
229                }
230            }
231            return copiesToAdd;
232        }
233    
234    
235        /**
236         * Get all physical local copies, choosen randomly
237         *
238         * @return <code>List</code> of all physicall local copies
239         * written by: Pavel Jakl
240         */
241        public List getRandomPhysicalLocal() {
242            //  instance for random number
243            Random rand = new Random();
244            //  instance for copies to be returned
245            List copiesToAdd = new ArrayList();
246            //  getLogicalIds
247            Set files = getLogicalNames();
248            Iterator iter = files.iterator();
249            //  iterate the logicalIds
250            while (iter.hasNext()) {
251                //  holdAll copies on local storage to current id
252                ArrayList localCopiesToId = new ArrayList();
253                Object file = iter.next();
254                //  get copies for the file
255                List copies = getCopies(file);
256                //  iterate the copies collection
257                Iterator iter2 = copies.iterator();
258                while (iter2.hasNext()) {
259                    PhysicalFile copy = (PhysicalFile) iter2.next();
260                    //  add only copies on local storage
261                    if ("local".equals(copy.getStorage())) {
262                        localCopiesToId.add(copy);
263                    }
264                }
265                //  take all copies for current id and choose randomly one
266                PhysicalFile copyToAdd = null;
267                //  to be sure that everything is fine in Catalog
268                if (!localCopiesToId.isEmpty()) {
269                    copyToAdd = (PhysicalFile) localCopiesToId.get(rand.nextInt(localCopiesToId.size()));
270                    //  to be sure that we don't want to add null
271                    if (copyToAdd != null) {
272                        copiesToAdd.add(copyToAdd);
273                    }
274                }
275            }
276            return copiesToAdd;
277        }
278    
279        /**
280         * Test method to print a collection of LFN
281         *
282         * @param list list to print
283         */
284        public void printLocalIdsWithCopies(Collection list) {
285            Iterator iter = list.iterator();
286            while (iter.hasNext()) {
287                System.out.println("-----------------START--------------------------\n");
288                Object id = iter.next();
289                System.out.println("Added logical id:" + id + "\n");
290                List copies = getCopies(id);
291                for (int j = 0; j < copies.size(); j++) {
292                    PhysicalFile physicalFile = (PhysicalFile) copies.get(j);
293                    System.out.println("Copy " + (j + 1) + ":" + physicalFile.getStorage() + "\n");
294                }
295                System.out.println("------------------FINISH-------------------------\n");
296    
297            }
298    
299        }
300    
301    }