1 4 package com.tc.objectserver.managedobject; 5 6 import com.tc.object.ObjectID; 7 import com.tc.objectserver.core.api.ManagedObject; 8 import com.tc.objectserver.impl.ManagedObjectReference; 9 import com.tc.util.Assert; 10 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 import java.util.Map.Entry; 17 18 public class ManagedObjectTraverser { 19 20 private static final String PROCESSED = "PROCESSED"; 21 private static final String REACHABLE = "REACHABLE"; 22 private static final String REQUIRED = "REQUIRED"; 23 private static final String LOOKUP_REQUIRED = "LOOKUP_REQUIRED"; 24 private static final String LOOKUP_REACHABLE = "LOOKUP_REACHABLE"; 25 26 private int maxReachableObjects; 27 private final Map oids = new HashMap (); 28 29 public ManagedObjectTraverser(int maxReachableObjects) { 30 this.maxReachableObjects = maxReachableObjects; 31 } 32 33 public void traverse(Set objects) { 34 markProcessed(objects, true); 35 } 36 37 private void markProcessed(Set objects, boolean traverse) { 38 for (Iterator i = objects.iterator(); i.hasNext();) { 39 ManagedObjectReference ref = (ManagedObjectReference) i.next(); 40 ManagedObject mo = ref.getObject(); 41 oids.put(mo.getID(), PROCESSED); 42 maxReachableObjects--; 43 if (traverse) { 44 mo.addObjectReferencesTo(this); 45 } 46 } 47 } 48 49 public Set getObjectsToLookup() { 50 HashSet oidsToLookup = new HashSet (oids.size() < 512 ? oids.size() : 512); 51 for (Iterator i = oids.entrySet().iterator(); i.hasNext();) { 52 Entry e = (Entry) i.next(); 53 Object _state = e.getValue(); 54 if (_state == REQUIRED) { 55 oidsToLookup.add(e.getKey()); 56 e.setValue(LOOKUP_REQUIRED); 57 } else if (maxReachableObjects - oidsToLookup.size() > 0 && _state == REACHABLE) { 58 oidsToLookup.add(e.getKey()); 59 e.setValue(LOOKUP_REACHABLE); 60 } 61 } 62 return oidsToLookup; 63 } 64 65 public Set getPendingObjectsToLookup(Set lookedUpObjects) { 66 if(lookedUpObjects.size() > 0) { 67 markProcessed(lookedUpObjects, false); 68 } 69 HashSet oidsToLookup = new HashSet (oids.size() < 512 ? oids.size() : 512); 70 for (Iterator i = oids.entrySet().iterator(); i.hasNext();) { 71 Entry e = (Entry) i.next(); 72 Object _state = e.getValue(); 73 Assert.assertTrue(_state != REQUIRED); 74 if (_state == LOOKUP_REQUIRED) { 75 oidsToLookup.add(e.getKey()); 76 } 77 } 78 return oidsToLookup; 79 } 80 81 public void addRequiredObjectIDs(Set objectReferences) { 82 for (Iterator i = objectReferences.iterator(); i.hasNext();) { 83 ObjectID oid = (ObjectID) i.next(); 84 if (oid.isNull()) continue; 85 Object state = oids.get(oid); 86 if (state == null || state == REACHABLE) { 87 oids.put(oid, REQUIRED); 88 } 89 } 90 } 91 92 public void addReachableObjectIDs(Set objectReferences) { 93 if (maxReachableObjects <= 0) return; 94 for (Iterator i = objectReferences.iterator(); i.hasNext();) { 95 ObjectID oid = (ObjectID) i.next(); 96 if (oid.isNull()) continue; 97 Object state = oids.get(oid); 98 if (state == null) { 99 oids.put(oid, REACHABLE); 100 } 101 } 102 } 103 104 } 105 | Popular Tags |