Resolving HHH-5588: Improve support for entityNames in envers.

Adding entityNameCache for resolving entityNames in envers.
This commit is contained in:
Hernan 2010-11-15 00:13:05 -02:00 committed by adamw
parent 328c098ab9
commit eaeb4f8343
1 changed files with 72 additions and 2 deletions

View File

@ -23,33 +23,103 @@
*/ */
package org.hibernate.envers.reader; package org.hibernate.envers.reader;
import static org.hibernate.envers.tools.Tools.newHashMap;
import static org.hibernate.envers.tools.Triple.make;
import java.util.Map; import java.util.Map;
import static org.hibernate.envers.tools.Tools.newHashMap;
import org.hibernate.envers.tools.Triple; import org.hibernate.envers.tools.Triple;
import static org.hibernate.envers.tools.Triple.make; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* First level cache for versioned entities, versions reader-scoped. Each entity is uniquely identified by a * First level cache for versioned entities, versions reader-scoped. Each entity is uniquely identified by a
* revision number and entity id. * revision number and entity id.
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
* @author Hernán Chanfreau
*/ */
public class FirstLevelCache { public class FirstLevelCache {
private static final Logger log = LoggerFactory.getLogger(FirstLevelCache.class);
/**
* cache for resolve an object for a given id, revision and entityName.
*/
private final Map<Triple<String, Number, Object>, Object> cache; private final Map<Triple<String, Number, Object>, Object> cache;
/**
* used to resolve the entityName for a given id, revision and entity.
*/
private final Map<Triple<Object, Number, Object>, String> entityNameCache;
public FirstLevelCache() { public FirstLevelCache() {
cache = newHashMap(); cache = newHashMap();
entityNameCache = newHashMap();
} }
public Object get(String entityName, Number revision, Object id) { public Object get(String entityName, Number revision, Object id) {
if (log.isDebugEnabled()) {
log.debug("Resolving object from First Level Cache: "
+ "EntytiName:" + entityName + " - primaryKey:" + id
+ " - revision:" + revision);
}
return cache.get(make(entityName, revision, id)); return cache.get(make(entityName, revision, id));
} }
public void put(String entityName, Number revision, Object id, Object entity) { public void put(String entityName, Number revision, Object id, Object entity) {
if (log.isDebugEnabled()) {
log.debug("Caching entity on First Level Cache: "
+ " - primaryKey:" + id + " - revision:" + revision
+ " - entityName:" + entityName);
}
cache.put(make(entityName, revision, id), entity); cache.put(make(entityName, revision, id), entity);
} }
public boolean contains(String entityName, Number revision, Object id) { public boolean contains(String entityName, Number revision, Object id) {
return cache.containsKey(make(entityName, revision, id)); return cache.containsKey(make(entityName, revision, id));
} }
/**
* Adds the entityName into the cache. The key is a triple make with primaryKey, revision and entity
* @param entityName, value of the cache
* @param id, primaryKey
* @param revision, revision number
* @param entity, object retrieved by envers
*/
public void putOnEntityNameCache(Object id, Number revision, Object entity, String entityName) {
if (log.isDebugEnabled()) {
log.debug("Caching entityName on First Level Cache: "
+ " - primaryKey:" + id + " - revision:" + revision
+ " - entity:" + entity.getClass().getName() + " -> entityName:" + entityName);
}
entityNameCache.put(make(id, revision, entity), entityName);
}
/**
* Gets the entityName from the cache. The key is a triple make with primaryKey, revision and entity
* @param entityName, value of the cache
* @param id, primaryKey
* @param revision, revision number
* @param entity, object retrieved by envers
*/
public String getFromEntityNameCache(Object id, Number revision, Object entity) {
if (log.isDebugEnabled()) {
log.debug("Trying to resolve entityName from First Level Cache:"
+ " - primaryKey:" + id + " - revision:" + revision
+ " - entity:" + entity);
}
return entityNameCache.get(make(id, revision, entity));
}
/**
* @param id
* , primaryKey
* @param revision
* , revision number
* @param entity
* , object retrieved by envers
* @return true if entityNameCache contains the triple
*/
public boolean containsEntityName(Object id, Number revision, Object entity) {
return entityNameCache.containsKey(make(id, revision, entity));
}
} }