KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > managedobject > ManagedObjectTraverser


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.Map.Entry;
17
18 public class ManagedObjectTraverser {
19
20   private static final String JavaDoc PROCESSED = "PROCESSED";
21   private static final String JavaDoc REACHABLE = "REACHABLE";
22   private static final String JavaDoc REQUIRED = "REQUIRED";
23   private static final String JavaDoc LOOKUP_REQUIRED = "LOOKUP_REQUIRED";
24   private static final String JavaDoc LOOKUP_REACHABLE = "LOOKUP_REACHABLE";
25
26   private int maxReachableObjects;
27   private final Map JavaDoc oids = new HashMap JavaDoc();
28
29   public ManagedObjectTraverser(int maxReachableObjects) {
30     this.maxReachableObjects = maxReachableObjects;
31   }
32
33   public void traverse(Set JavaDoc objects) {
34     markProcessed(objects, true);
35   }
36
37   private void markProcessed(Set JavaDoc objects, boolean traverse) {
38     for (Iterator JavaDoc 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 JavaDoc getObjectsToLookup() {
50     HashSet JavaDoc oidsToLookup = new HashSet JavaDoc(oids.size() < 512 ? oids.size() : 512);
51     for (Iterator JavaDoc i = oids.entrySet().iterator(); i.hasNext();) {
52       Entry e = (Entry) i.next();
53       Object JavaDoc _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 JavaDoc getPendingObjectsToLookup(Set JavaDoc lookedUpObjects) {
66     if(lookedUpObjects.size() > 0) {
67       markProcessed(lookedUpObjects, false);
68     }
69     HashSet JavaDoc oidsToLookup = new HashSet JavaDoc(oids.size() < 512 ? oids.size() : 512);
70     for (Iterator JavaDoc i = oids.entrySet().iterator(); i.hasNext();) {
71       Entry e = (Entry) i.next();
72       Object JavaDoc _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 JavaDoc objectReferences) {
82     for (Iterator JavaDoc i = objectReferences.iterator(); i.hasNext();) {
83       ObjectID oid = (ObjectID) i.next();
84       if (oid.isNull()) continue;
85       Object JavaDoc state = oids.get(oid);
86       if (state == null || state == REACHABLE) {
87         oids.put(oid, REQUIRED);
88       }
89     }
90   }
91
92   public void addReachableObjectIDs(Set JavaDoc objectReferences) {
93     if (maxReachableObjects <= 0) return;
94     for (Iterator JavaDoc i = objectReferences.iterator(); i.hasNext();) {
95       ObjectID oid = (ObjectID) i.next();
96       if (oid.isNull()) continue;
97       Object JavaDoc state = oids.get(oid);
98       if (state == null) {
99         oids.put(oid, REACHABLE);
100       }
101     }
102   }
103
104 }
105
Popular Tags