1 | |
package org.apache.ojb.broker.accesslayer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collection; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.HashSet; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import org.apache.ojb.broker.Identity; |
26 | |
import org.apache.ojb.broker.PersistenceBroker; |
27 | |
import org.apache.ojb.broker.cache.ObjectCache; |
28 | |
import org.apache.ojb.broker.core.PersistenceBrokerImpl; |
29 | |
import org.apache.ojb.broker.core.proxy.IndirectionHandler; |
30 | |
import org.apache.ojb.broker.core.proxy.ProxyHelper; |
31 | |
import org.apache.ojb.broker.query.Query; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class PlainPrefetcher extends BasePrefetcher |
40 | |
{ |
41 | |
|
42 | |
public PlainPrefetcher(PersistenceBrokerImpl aBroker, Class anItemClass) |
43 | |
{ |
44 | |
super(aBroker, anItemClass); |
45 | |
} |
46 | |
|
47 | |
public void prepareRelationshipSettings() |
48 | |
{ |
49 | |
|
50 | |
} |
51 | |
|
52 | |
public void restoreRelationshipSettings() |
53 | |
{ |
54 | |
|
55 | |
} |
56 | |
|
57 | |
protected void associateBatched(Collection proxies, Collection realSubjects) |
58 | |
{ |
59 | |
PersistenceBroker pb = getBroker(); |
60 | |
IndirectionHandler handler; |
61 | |
Identity id; |
62 | |
Object proxy; |
63 | |
Object realSubject; |
64 | |
HashMap realSubjectsMap = new HashMap(realSubjects.size()); |
65 | |
|
66 | |
for (Iterator it = realSubjects.iterator(); it.hasNext(); ) |
67 | |
{ |
68 | |
realSubject = it.next(); |
69 | |
realSubjectsMap.put(pb.serviceIdentity().buildIdentity(realSubject), realSubject); |
70 | |
} |
71 | |
|
72 | |
for (Iterator it = proxies.iterator(); it.hasNext(); ) |
73 | |
{ |
74 | |
proxy = it.next(); |
75 | |
handler = ProxyHelper.getIndirectionHandler(proxy); |
76 | |
|
77 | |
if (handler == null) |
78 | |
{ |
79 | |
continue; |
80 | |
} |
81 | |
|
82 | |
id = handler.getIdentity(); |
83 | |
realSubject = realSubjectsMap.get(id); |
84 | |
if (realSubject != null) |
85 | |
{ |
86 | |
handler.setRealSubject(realSubject); |
87 | |
} |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected Query[] buildPrefetchQueries(Collection proxies, Collection realSubjects) |
97 | |
{ |
98 | |
Collection queries = new ArrayList(); |
99 | |
Collection idsSubset; |
100 | |
Object proxy; |
101 | |
IndirectionHandler handler; |
102 | |
Identity id; |
103 | |
Class realClass; |
104 | |
HashMap classToIds = new HashMap(); |
105 | |
Class topLevelClass = getItemClassDescriptor().getClassOfObject(); |
106 | |
PersistenceBroker pb = getBroker(); |
107 | |
ObjectCache cache = pb.serviceObjectCache(); |
108 | |
|
109 | |
for (Iterator it = proxies.iterator(); it.hasNext(); ) |
110 | |
{ |
111 | |
proxy = it.next(); |
112 | |
handler = ProxyHelper.getIndirectionHandler(proxy); |
113 | |
|
114 | |
if (handler == null) |
115 | |
{ |
116 | |
continue; |
117 | |
} |
118 | |
|
119 | |
id = handler.getIdentity(); |
120 | |
if (cache.lookup(id) != null) |
121 | |
{ |
122 | |
realSubjects.add(pb.getObjectByIdentity(id)); |
123 | |
continue; |
124 | |
} |
125 | |
realClass = id.getObjectsRealClass(); |
126 | |
if (realClass == null) |
127 | |
{ |
128 | |
realClass = Object.class; |
129 | |
} |
130 | |
idsSubset = (HashSet) classToIds.get(realClass); |
131 | |
if (idsSubset == null) |
132 | |
{ |
133 | |
idsSubset = new HashSet(); |
134 | |
classToIds.put(realClass, idsSubset); |
135 | |
} |
136 | |
idsSubset.add(id); |
137 | |
if (idsSubset.size() == pkLimit) |
138 | |
{ |
139 | |
Query query; |
140 | |
if (realClass == Object.class) |
141 | |
{ |
142 | |
query = buildPrefetchQuery(topLevelClass, idsSubset, true); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | |
query = buildPrefetchQuery(realClass, idsSubset, false); |
147 | |
} |
148 | |
queries.add(query); |
149 | |
idsSubset.clear(); |
150 | |
} |
151 | |
} |
152 | |
|
153 | |
for (Iterator it = classToIds.entrySet().iterator(); it.hasNext(); ) |
154 | |
{ |
155 | |
Map.Entry entry = (Map.Entry) it.next(); |
156 | |
realClass = (Class) entry.getKey(); |
157 | |
idsSubset = (HashSet) entry.getValue(); |
158 | |
if (idsSubset.size() > 0) |
159 | |
{ |
160 | |
Query query; |
161 | |
if (realClass == Object.class) |
162 | |
{ |
163 | |
query = buildPrefetchQuery(topLevelClass, idsSubset, true); |
164 | |
} |
165 | |
else |
166 | |
{ |
167 | |
query = buildPrefetchQuery(realClass, idsSubset, false); |
168 | |
} |
169 | |
queries.add(query); |
170 | |
} |
171 | |
} |
172 | |
|
173 | |
return (Query[]) queries.toArray(new Query[queries.size()]); |
174 | |
} |
175 | |
|
176 | |
protected Query buildPrefetchQuery(Class clazz, Collection ids, boolean withExtents) |
177 | |
{ |
178 | |
Query query = buildPrefetchQuery(clazz, ids, |
179 | |
getDescriptorRepository().getDescriptorFor(clazz).getPkFields()); |
180 | |
query.setWithExtents(withExtents); |
181 | |
return query; |
182 | |
} |
183 | |
} |