001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.cPlanner.classes;
016:
017: import org.griphyn.common.catalog.ReplicaCatalogEntry;
018:
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.HashMap;
022: import java.util.Collection;
023: import java.util.Set;
024: import java.util.HashSet;
025: import java.util.List;
026:
027: /**
028: * A Replica Store that allows us to store the entries from a replica catalog.
029: * The store map is indexed by LFN's and values stored are ReplicaLocation
030: * objects.
031: *
032: * @author Karan Vahi
033: * @author Gaurang Mehta
034: *
035: * @version $Revision: 191 $
036: *
037: * @see org.griphyn.common.catalog.ReplicaCatalogEntry
038: */
039: public class ReplicaStore extends Data implements Cloneable {
040:
041: /**
042: * The replica store.
043: */
044: private Map mStore;
045:
046: /**
047: * Default constructor.
048: */
049: public ReplicaStore() {
050: mStore = new HashMap();
051: }
052:
053: /**
054: * Overloaded constructor.
055: * Intializes the member variables to the values passed.
056: *
057: * @param rces map indexed by LFN's and each value is a collection
058: * of replica catalog entries for the LFN.
059: */
060: public ReplicaStore(Map rces) {
061: mStore = new HashMap(rces.size());
062: store(rces);
063: }
064:
065: /**
066: * Stores replica catalog entries into the store. It overwrites any
067: * existing entries with the same LFN's. The <code>ReplicaCatlogEntry</code>
068: * ends up being stored as a <code>ReplicaLocation</code> object.
069: *
070: * @param rces map indexed by LFN's and each value is a collection
071: * of replica catalog entries for the LFN.
072: */
073: public void store(Map rces) {
074: String lfn;
075: Map.Entry entry;
076: Collection values;
077:
078: //traverse through all the entries and render them into
079: //ReplicaLocation objects before storing in the store
080: for (Iterator it = rces.entrySet().iterator(); it.hasNext();) {
081: entry = (Map.Entry) it.next();
082: lfn = (String) entry.getKey();
083: values = (Collection) entry.getValue();
084: //only put in if the values are not empty
085: if (!values.isEmpty()) {
086: put(lfn, new ReplicaLocation(lfn, values));
087: }
088: }
089: }
090:
091: /**
092: * Adds ReplicaCatalogEntries into the store. Any existing
093: * mapping of the same LFN and PFN will be replaced, including all its
094: * attributes. The <code>ReplicaCatlogEntry</code>
095: * ends up being stored as a <code>ReplicaLocation</code> object.
096: *
097: * @param rces map indexed by LFN's and each value is a collection
098: * of replica catalog entries for the LFN.
099: */
100: public void add(Map rces) {
101: String lfn;
102: Map.Entry entry;
103: Collection values;
104: ReplicaLocation rl;
105:
106: //traverse through all the entries and render them into
107: //ReplicaLocation objects before storing in the store
108: for (Iterator it = rces.entrySet().iterator(); it.hasNext();) {
109: entry = (Map.Entry) it.next();
110: lfn = (String) entry.getKey();
111: values = (Collection) entry.getValue();
112: add(lfn, values);
113: }
114: }
115:
116: /**
117: * Adds replica catalog entries into the store. Any existing
118: * mapping of the same LFN and PFN will be replaced, including all its
119: * attributes.
120: *
121: * @param lfn the lfn.
122: * @param tuples list of <code>ReplicaCatalogEntry<code> containing the PFN and the
123: * attributes.
124: */
125: public void add(String lfn, Collection tuples) {
126: //add only if tuples is not empty
127: if (tuples.isEmpty()) {
128: return;
129: }
130: this .add(new ReplicaLocation(lfn, tuples));
131: }
132:
133: /**
134: * Adds replica catalog entries into the store. Any existing
135: * mapping of the same LFN and PFN will be replaced, including all its
136: * attributes.
137: *
138: * @param rl the <code>ReplicaLocation</code> containing a pfn and all
139: * the attributes.
140: */
141: public void add(ReplicaLocation rl) {
142: String lfn = rl.getLFN();
143:
144: if (this .containsLFN(lfn)) {
145: //add to the existing Replica Location
146: ReplicaLocation existing = this .get(lfn);
147: existing.addPFNs(rl.getPFNList());
148: } else {
149: //store directly in the store.
150: put(lfn, rl);
151: }
152: }
153:
154: /**
155: * Returns a <code>ReplicaLocation</code> corresponding to the LFN.
156: *
157: * @param lfn the lfn for which the ReplicaLocation is required.
158: *
159: * @return <code>ReplicaLocation</code> if entry exists else null.
160: */
161: public ReplicaLocation getReplicaLocation(String lfn) {
162: return get(lfn);
163: }
164:
165: /**
166: * Returns an iterator to the list of <code>ReplicaLocation</code>
167: * objects stored in the store.
168: *
169: * @return Iterator.
170: */
171: public Iterator replicaLocationIterator() {
172: return this .mStore.values().iterator();
173: }
174:
175: /**
176: * Returns the set of LFN's for which the mappings are stored in the store.
177: *
178: * @return Set
179: */
180: public Set getLFNs() {
181: return this .mStore.keySet();
182: }
183:
184: /**
185: * Returns a <code>Set</code> of lfns for which the mappings are stored in
186: * the store, amongst the <code>Set</code> passed as input.
187: *
188: * @param lfns the collections of lfns
189: *
190: * @return Set
191: */
192: public Set getLFNs(Set lfns) {
193: Set s = new HashSet();
194: String lfn;
195: for (Iterator it = lfns.iterator(); it.hasNext();) {
196: lfn = (String) it.next();
197: if (this .containsLFN(lfn)) {
198: s.add(lfn);
199: }
200: }
201: return s;
202: }
203:
204: /**
205: * Returns the number of LFN's for which the mappings are stored in the
206: * store.
207: *
208: * @return int
209: */
210: public int getLFNCount() {
211: return this .mStore.size();
212: }
213:
214: /**
215: * Returns the clone of the object.
216: *
217: * @return the clone
218: */
219: public Object clone() {
220:
221: //clone is not implemented fully.
222: throw new RuntimeException("Clone not implemented for "
223: + this .getClass().getName());
224:
225: // return rc;
226: }
227:
228: /**
229: * Returns the textual description of the data class.
230: *
231: * @return the textual description.
232: */
233: public String toString() {
234: StringBuffer sb = new StringBuffer();
235: for (Iterator it = this .replicaLocationIterator(); it.hasNext();) {
236: sb.append(it.next());
237: sb.append("\n");
238: }
239: return sb.toString();
240: }
241:
242: /**
243: * Returns a boolean indicating whether the store has a mapping for a
244: * particular LFN or not.
245: *
246: * @param lfn the logical file name of the file.
247: *
248: * @return boolean
249: */
250: public boolean containsLFN(String lfn) {
251: return mStore.containsKey(lfn);
252: }
253:
254: /**
255: * Inserts entry in the store overwriting any existing entry.
256: *
257: * @param key the key
258: * @param value <code>ReplicaLocation</code> object.
259: *
260: * @return Object
261: */
262: protected Object put(String key, ReplicaLocation value) {
263: return mStore.put(key, value);
264: }
265:
266: /**
267: * Returns an entry corresponding to the LFN
268: *
269: * @param lfn the LFN
270: *
271: * @return <code>ReplicaLocation</code> object if exists, else null.
272: */
273: protected ReplicaLocation get(String key) {
274: Object result = mStore.get(key);
275: return (result == null) ? null : (ReplicaLocation) result;
276: }
277:
278: }
|