Project

General

Profile

Revision 86d726ac

ID86d726ac3d96fc2762795b4d5154cc43711e45ef
Parent be3c44be
Child d2a8e3bf

Added by Andreas Müller 7 months ago

add includeIgnore parameter to EntityCacherDebugResult and minor refactoring

View differences:

cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/EntityCacherDebugResult.java
50 50
    public EntityCacherDebugResult() {
51 51
    }
52 52

  
53
    public <T extends CdmBase> EntityCacherDebugResult(CdmTransientEntityCacher cacher, Collection<T> rootEntities) {
53
    public <T extends CdmBase> EntityCacherDebugResult(CdmTransientEntityCacher cacher, Collection<T> rootEntities, boolean includeIgnored) {
54 54
        this.cacher = cacher;
55 55
        init();
56 56

  
57 57
        if(rootEntities != null && !rootEntities.isEmpty()) {
58 58
            for(CdmBase rootEntity : rootEntities) {
59
                debug(rootEntity, true);
59
                debug(rootEntity, true, includeIgnored);
60 60
                String out = toString(duplicateCdmEntityMap, notInCacheList, rootEntity);
61 61
                System.out.println(out);
62 62
                debugOutput.append(out);
......
76 76
        notInCacheList.clear();
77 77
    }
78 78

  
79
    public void addDuplicateEntity(CdmEntityInfo cei, CdmEntityInfo cachedCei) {
79
    private void addDuplicateInGraphEntity(CdmEntityInfo cei, CdmEntityInfo cachedCei) {
80 80
        duplicateCdmEntityMap.put(cei, cachedCei);
81 81
    }
82 82

  
83
    public void addEntityNotInCache(CdmEntityInfo cei) {
83
    private void addEntityNotInCache(CdmEntityInfo cei) {
84 84
        notInCacheList.add(cei);
85 85
    }
86 86

  
......
104 104
            CdmBase rootEntity) {
105 105

  
106 106
        StringBuilder sb = new StringBuilder();
107
        toStringDuplicatesInGraph(duplicateCdmEntityMap, rootEntity, sb);
108

  
109
        sb.append(System.getProperty("line.separator"));
110

  
111
        toStringNotInCache(notInCacheList, sb);
112

  
113
        sb.append(System.getProperty("line.separator"));
114
        return sb.toString();
115
    }
116

  
117
    private void toStringNotInCache(List<CdmEntityInfo> notInCacheList, StringBuilder sb) {
118
        sb.append(System.getProperty("line.separator"));
119

  
120
        if(notInCacheList.isEmpty()) {
121
            sb.append("No entities found in root graph which are not in Cache.");
122
        } else {
123
            sb.append("Entities in root graph which are not in cache (");
124
            sb.append(NotInCacheType.NOT_FOUND.getLabel() + ": " + NotInCacheType.NOT_FOUND.name() + ", ");
125
            sb.append(NotInCacheType.COPY_ENTITY.getLabel() + ": " + NotInCacheType.COPY_ENTITY.name() + "):");
126

  
127
            for(CdmEntityInfo cei : notInCacheList) {
128
                CdmBase cb = (CdmBase) cei.getObject();
129
                CdmEntityInfo parentCei = cei.getParent();
130

  
131
                sb.append(System.getProperty("line.separator"));
132

  
133
                String fieldName = "";
134
                if(cei.getField() != null) {
135
                    fieldName = cei.getField().getName();
136
                }
137
                sb.append(" - ");
138
                if(cei.getNotInCacheDetail() != null){
139
                    sb.append(cei.getNotInCacheDetail().getLabel());
140
                }
141
                sb.append(fieldName + "[" + cb.getUserFriendlyTypeName() + "#" + cb.getId() + "]");
142

  
143
                String parentsPath = "";
144
                while(parentCei != null){
145
                    parentsPath += ".";
146
                    parentsPath += parentCei.getField() != null? parentCei.getField().getName() : "";
147
                    String id = "";
148
                    if(parentCei.getObject() instanceof CdmBase){
149
                        id = "#" + ((CdmBase)parentCei.getObject()).getId();
150
                    }
151
                    parentsPath += "[" + classLabel(parentCei.getObject()) + id + "]";
152
                    parentCei = parentCei.getParent();
153
                }
154

  
155
                sb.append(parentsPath);
156
            }
157
        }
158
    }
159

  
160
    private void toStringDuplicatesInGraph(Map<CdmEntityInfo, CdmEntityInfo> duplicateCdmEntityMap, CdmBase rootEntity,
161
            StringBuilder sb) {
107 162
        sb.append(System.getProperty("line.separator"));
108 163
        sb.append("<<< Root Entity " + rootEntity.getUserFriendlyTypeName() + " with id " + rootEntity.getId() + " >>>");
109 164
        sb.append(System.getProperty("line.separator"));
......
152 207
                sb.append("-----------");
153 208
            }
154 209
        }
155

  
156
        sb.append(System.getProperty("line.separator"));
157
        sb.append(System.getProperty("line.separator"));
158

  
159
        if(notInCacheList.isEmpty()) {
160
            sb.append("No Entities found which are not in Cache.");
161
        } else {
162
            sb.append("Not In Cache Entities (");
163
            sb.append(NotInCacheDetail.NOT_FOUND.getLabel() + ": " + NotInCacheDetail.NOT_FOUND.name() + ", ");
164
            sb.append(NotInCacheDetail.COPY_ENTITY.getLabel() + ": " + NotInCacheDetail.COPY_ENTITY.name() + ")");
165

  
166
            for(CdmEntityInfo cei : notInCacheList) {
167
                CdmBase cb = (CdmBase) cei.getObject();
168
                CdmEntityInfo parentCei = cei.getParent();
169
                Object parentEntity = null;
170
                if(parentCei != null){
171
                    parentEntity = parentCei.getObject();
172
                }
173

  
174
                sb.append(System.getProperty("line.separator"));
175

  
176
                String fieldName = "";
177
                if(cei.getField() != null) {
178
                    fieldName = cei.getField().getName();
179
                }
180
                sb.append(" - ");
181
                if(cei.getNotInCacheDetail() != null){
182
                    sb.append(cei.getNotInCacheDetail().getLabel());
183
                }
184
                sb.append(fieldName + "[" + cb.getUserFriendlyTypeName() + "#" + cb.getId() + "]");
185

  
186
                String parentsPath = "";
187
                while(parentCei != null){
188
                    parentsPath += ".";
189
                    parentsPath += parentCei.getField() != null? parentCei.getField().getName() : "";
190
                    String id = "";
191
                    if(parentCei.getObject() instanceof CdmBase){
192
                        id = "#" + ((CdmBase)parentCei.getObject()).getId();
193
                    }
194
                    parentsPath += "[" + classLabel(parentCei.getObject()) + id + "]";
195
                    parentCei = parentCei.getParent();
196
                }
197

  
198
                sb.append(parentsPath);
199
            }
200
        }
201
        sb.append(System.getProperty("line.separator"));
202
        return sb.toString();
203 210
    }
204 211

  
205 212
    private String classLabel(Object entity){
......
225 232
        return caches;
226 233
    }
227 234

  
228
    private void debug(CdmBase cdmEntity, boolean recursive) {
235
    private void debug(CdmBase cdmEntity, boolean recursive, boolean includeIgnored) {
229 236
        if(cdmEntity == null) {
230 237
            return;
231 238
        }
......
242 249

  
243 250
    private <T extends Object> void debugRecursive(T obj,
244 251
            List<CdmEntityInfo> alreadyVisitedEntities,
245
            CdmEntityInfo cei) {
252
            CdmEntityInfo cei, boolean includeIgnored) {
246 253
        if(obj == null) {
247 254
            return;
248 255
        }
249 256
        if(obj instanceof CdmBase) {
250
            debugRecursive((CdmBase)obj, alreadyVisitedEntities, cei);
257
            debugRecursive((CdmBase)obj, alreadyVisitedEntities, cei, includeIgnored);
251 258
        } else if (obj instanceof Map) {
252
            debug((Map<T,T>)obj, alreadyVisitedEntities, cei);
259
            debug((Map<T,T>)obj, alreadyVisitedEntities, cei, includeIgnored);
253 260
        } else if (obj instanceof Collection) {
254
            debug((Collection<T>)obj, alreadyVisitedEntities, cei);
261
            debug((Collection<T>)obj, alreadyVisitedEntities, cei, includeIgnored);
255 262
        }
256 263
        logger.info("No caching yet for type " + obj.getClass().getName());
257 264
    }
258 265

  
259 266
    private <T extends Object> void debug(Map<T,T> map,
260 267
            List<CdmEntityInfo> alreadyVisitedEntities,
261
            CdmEntityInfo cei) {
268
            CdmEntityInfo cei, boolean includeIgnored) {
262 269
        if(map == null || map.isEmpty()) {
263 270
            return;
264 271
        }
......
274 281
            CdmEntityInfo valueCei = new CdmEntityInfo(ProxyUtils.deproxyIfInitialized(e.getValue()));
275 282
            childCei.addChild(valueCei);
276 283

  
277
            debugRecursive(e.getKey(), alreadyVisitedEntities, keyCei);
278
            debugRecursive(e.getValue(), alreadyVisitedEntities, valueCei);
284
            debugRecursive(e.getKey(), alreadyVisitedEntities, keyCei, includeIgnored);
285
            debugRecursive(e.getValue(), alreadyVisitedEntities, valueCei, includeIgnored);
279 286
        }
280 287
    }
281 288

  
282 289
    private <T extends Object> void debug(Collection<T> collection,
283 290
            List<CdmEntityInfo> alreadyVisitedEntities,
284
            CdmEntityInfo cei) {
291
            CdmEntityInfo cei, boolean includeIgnored) {
285 292
        Iterator<T> collectionItr = collection.iterator();
286 293

  
287 294
        while(collectionItr.hasNext()) {
......
296 303
            if(!alreadyVisited){
297 304
                CdmEntityInfo childCei = new CdmEntityInfo(ProxyUtils.deproxyIfInitialized(obj));
298 305
                cei.addChild(childCei);
299
                debugRecursive(obj, alreadyVisitedEntities, childCei);
306
                debugRecursive(obj, alreadyVisitedEntities, childCei, includeIgnored);
300 307
            }
301 308
        }
302 309
    }
303 310

  
304 311
    private void debugRecursive(CdmBase cdmEntity,
305 312
            List<CdmEntityInfo> alreadyVisitedEntities,
306
            CdmEntityInfo cei) {
313
            CdmEntityInfo cei, boolean includeIgnored) {
307 314

  
308 315
        if(cei.getObject() instanceof CdmBase) {
309 316
           CdmBase cb =  (CdmBase)cei.getObject();
310 317
           cb = ProxyUtils.deproxyIfInitialized(cb);
311 318
           CdmBase cachedCdmEntityInSubGraph = cacher.getFromCache(cb);
312
           if(cachedCdmEntityInSubGraph != cb) {
313
               cei.setNotInCacheDetail(cachedCdmEntityInSubGraph == null ? NotInCacheDetail.NOT_FOUND : NotInCacheDetail.COPY_ENTITY);
319
           if(cachedCdmEntityInSubGraph != cb && (includeIgnored || !cacher.ignoreRecursiveLoad(cb))) {
320
               cei.setNotInCacheDetail(cachedCdmEntityInSubGraph == null ? NotInCacheType.NOT_FOUND : NotInCacheType.COPY_ENTITY);
314 321
               // found a cdm entity which is not in cache - need to record this
315 322
               //logger.info("  - found entity not in cache " + fieldName + "' in object of type " + clazz.getName() + " with id " + cdmEntity.getId());
316 323
               addEntityNotInCache(cei);
......
336 343

  
337 344
    }
338 345

  
346
    private void debugField(CdmBase cdmEntity, List<CdmEntityInfo> alreadyVisitedEntities, CdmEntityInfo cei,
347
            String field, boolean includeIgnored) {
348
        // retrieve the actual object corresponding to the field.
349
        // this object will be either a CdmBase or a Collection / Map
350
        // with CdmBase as the generic type
351
        CdmEntityInfo childCei = getDebugCdmBaseTypeFieldValue(cdmEntity, field, alreadyVisitedEntities, cei, includeIgnored);
352
        if(!childCei.isProxy()) {
353
            Object object = childCei.getObject();
354
            if(object != null && object instanceof CdmBase) {
355
                CdmBase cdmEntityInSubGraph = (CdmBase)object;
356
                if(!containsIdenticalCdmEntity(alreadyVisitedEntities, cdmEntityInSubGraph)) {
357
                    logger.info("recursive debugging object of type " + cdmEntityInSubGraph.getClass().getName() + " with id " + cdmEntityInSubGraph.getId());
358
                    debugRecursive(cdmEntityInSubGraph, alreadyVisitedEntities, childCei, includeIgnored);
359
                } else {
360
                    logger.info("object of type " + cdmEntityInSubGraph.getClass().getName() + " with id " + cdmEntityInSubGraph.getId() + " already visited");
361
                }
362
            }
363
        }
364
    }
339 365

  
340 366
    private CdmEntityInfo getDebugCdmBaseTypeFieldValue(CdmBase cdmEntity,
341 367
            String fieldName,
342 368
            List<CdmEntityInfo> alreadyVisitedEntities,
343
            CdmEntityInfo cei) {
369
            CdmEntityInfo cei, boolean includeIgnored) {
344 370

  
345 371
        CdmEntityInfo childCei = null;
346 372
        Class<?> clazz = cdmEntity.getClass();
......
374 400

  
375 401
                        //logger.info("  - found duplicate entity at " + fieldName + "' in object of type " + clazz.getName() + " with id " + cdmEntity.getId());
376 402
                        CdmEntityInfo dupCei = getDuplicate(alreadyVisitedEntities, cdmEntityInSubGraph);
377
                        if(dupCei != null) {
378
                            addDuplicateEntity(childCei, dupCei);
403
                        if(dupCei != null && (includeIgnored || !cacher.ignoreRecursiveLoad(cdmEntityInSubGraph))) {
404
                            addDuplicateInGraphEntity(childCei, dupCei);
379 405
                        }
380 406

  
381 407
                    } else if(o instanceof Map) {
382
                        debugRecursive((Map)o, alreadyVisitedEntities, childCei);
408
                        debugRecursive((Map)o, alreadyVisitedEntities, childCei, includeIgnored);
383 409
                    } else if(o instanceof Collection) {
384
                        debugRecursive((Collection)o, alreadyVisitedEntities, childCei);
410
                        debugRecursive((Collection)o, alreadyVisitedEntities, childCei, includeIgnored);
385 411
                    }
386 412

  
387 413
                }

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)