Project

General

Profile

Revision 8991d6e9

ID8991d6e913241f27849c6b6e2e4c528f042ba985
Parent b4c19aff
Child b486d565

Added by Andreas Müller 12 months ago

ref #7709 rename addNewEntity to putNewEntity and check for existing entity

View differences:

cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/CdmTransientEntityCacher.java
35 35

  
36 36
/**
37 37
 * This cache handles transient (id>0) and volatile (id=0) CdmBase objects.
38
 * Volatile objects need to be added via {@link #addNewEntity(CdmBase)}
38
 * Volatile objects need to be added via {@link #putNewEntity(CdmBase)}
39 39
 * and their id is updated as soon as a transient object with the same
40 40
 * uuid is added to the cacher.
41 41
 *
......
201 201
        return cacheLoader.getFromCdmlibModelCache(className);
202 202
    }
203 203

  
204
    public void addNewEntity(CdmBase newEntity) {
204
    private void putNewEntity(CdmBase newEntity) {
205 205
        if(newEntity != null && newEntity.getId() == 0 && newEntity.getUuid() != null) {
206
            newEntitiesMap.put(newEntity.getUuid(), newEntity);
206
            CdmBase cachedEntity = newEntitiesMap.get(newEntity.getUuid());
207
            if (cachedEntity == null){
208
                newEntitiesMap.put(newEntity.getUuid(), newEntity);
209
            }
207 210
        }
208 211
    }
209 212

  
210 213
    /**
211 214
     * Puts the passed <code>cdmEntity</code> into the according caches
212
     * (cache, newEntitiesMap, permanentCache) as long it does not yet exist there.
215
     * (cache, newEntitiesMap, permanentCache(TODO still needs to be checked, not implemented yet))
216
     * as long it does not yet exist there.
213 217
     * <p>
214 218
     * The adjacent <b>ENTITY GRAPH WILL NOT BE LOADED RECURSIVELY</b>
215 219
     */
216 220
    @Override
217
    public void put(CdmBase cdmEntity) {
218

  
219
        CdmBase cachedCdmEntity = permanentCache.load(cdmEntity);
220
        if(cachedCdmEntity != null) {
221
            logger.info("Cdm Entity with id : " + cdmEntity.getId() + " already exists in permanent cache. Ignoring put.");
222
            return;
223
        }
224
        CdmEntityCacheKey<?> key = new CdmEntityCacheKey<>(cdmEntity);
225

  
226
        cachedCdmEntity = getFromCache(key);
227
        if(cachedCdmEntity == null) {
228
            CdmBase cdmEntityToCache = cdmEntity;
229
            CdmBase newInMapEntity = newEntitiesMap.get(cdmEntity.getUuid());
230
            if(newInMapEntity != null) {
231
                newInMapEntity.setId(cdmEntity.getId());
232
                cdmEntityToCache = newInMapEntity;
233
            }
234
            putToCache(key, cdmEntityToCache);
235
            cdmEntityToCache.initListener();
236
            newEntitiesMap.remove(cdmEntity.getUuid());
237
            if (logger.isDebugEnabled()){logger.debug(" - object of type " + cdmEntityToCache.getClass().getName() + " with id " + cdmEntityToCache.getId() + " put in cache");}
221
    public void putToCache(CdmBase cdmEntity) {
222
        if (cdmEntity == null){
238 223
            return;
224
        }else if (!cdmEntity.isPersited()){
225
            putNewEntity(cdmEntity);
239 226
        }else{
240
            logger.debug(" - object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + " already exists");
227
            CdmBase cachedCdmEntity = permanentCache.load(cdmEntity);
228
            if(cachedCdmEntity != null) {
229
                logger.info("Cdm Entity with id : " + cdmEntity.getId() + " already exists in permanent cache. Ignoring put.");
230
                return;
231
            }
232
            CdmEntityCacheKey<?> key = new CdmEntityCacheKey<>(cdmEntity);
233

  
234
            cachedCdmEntity = getFromCache(key);
235
            if(cachedCdmEntity == null) {
236
                CdmBase cdmEntityToCache = cdmEntity;
237
                CdmBase newInMapEntity = newEntitiesMap.get(cdmEntity.getUuid());
238
                if(newInMapEntity != null) {
239
                    newInMapEntity.setId(cdmEntity.getId());
240
                    cdmEntityToCache = newInMapEntity;
241
                }
242
                putToTransientCache(key, cdmEntityToCache);
243
                cdmEntityToCache.initListener();
244
                newEntitiesMap.remove(cdmEntity.getUuid());
245
                if (logger.isDebugEnabled()){logger.debug(" - object of type " + cdmEntityToCache.getClass().getName() + " with id " + cdmEntityToCache.getId() + " put in cache");}
246
                return;
247
            }else{
248
                logger.debug(" - object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + " already exists");
249
            }
241 250
        }
242 251
    }
243 252

  
244
    protected void putToCache(CdmEntityCacheKey<?> key, CdmBase cdmEntityToCache) {
253
    /**
254
     * Puts the entity to the cache for transient entities. If the entity is not transient
255
     * but volatile (id = 0) an {@link IllegalArgumentException} is thrown
256
     */
257
    protected void putToTransientCache(CdmEntityCacheKey<?> key, CdmBase cdmEntityToCache) throws IllegalArgumentException {
258
        if (key.getPersistenceId() == 0){
259
            throw new IllegalArgumentException("Volatile objects are not allowed in the transient object cache. Use newEntitiesMap instead.");
260
        }
245 261
        getCache().put(new Element(key, cdmEntityToCache));
246 262
    }
247 263

  
......
268 284

  
269 285
    @Override
270 286
    public <T extends CdmBase> T getFromCache(T cdmBase) {
271

  
272
        CdmEntityCacheKey<T> cacheId = generateKey((T)ProxyUtils.deproxy(cdmBase));
273
        // first try this cache
274
        T  cachedCdmEntity = getFromCache(cacheId);
275

  
276
        if(cachedCdmEntity == null) {
277
            // ... then try the permanent cache
278
            //TODO also use generics and clazz parameter for getFromCache(uuid)
279
            cachedCdmEntity = (T)permanentCache.getFromCache(cdmBase.getUuid());
287
        if (!cdmBase.isPersited()){
288
            return (T)newEntitiesMap.get(cdmBase.getUuid());
289
        }else{
290
            CdmEntityCacheKey<T> cacheId = generateKey(ProxyUtils.deproxy(cdmBase));
291
            // first try this cache
292
            T  cachedCdmEntity = getFromCache(cacheId);
293

  
294
            if(cachedCdmEntity == null) {
295
                // ... then try the permanent cache
296
                //TODO also use generics and clazz parameter for getFromCache(uuid)
297
                cachedCdmEntity = (T)permanentCache.getFromCache(cdmBase.getUuid());
298
            }
299
            return cachedCdmEntity;
280 300
        }
281 301

  
282
        return cachedCdmEntity;
283 302
    }
284 303

  
285 304
    public List<CdmBase> getAllEntities() {

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)