Mercurial > hg > openmind
changeset 41:2079c35739c8
more comments and cleanup.
author | casties |
---|---|
date | Mon, 17 Oct 2016 12:28:39 +0200 |
parents | c181cb6f1761 |
children | a807a99a7e20 |
files | src/main/java/org/mpi/openmind/cache/AbstractCacheService.java src/main/java/org/mpi/openmind/cache/CacheService.java src/main/java/org/mpi/openmind/repository/services/PersistenceService.java |
diffstat | 3 files changed, 219 insertions(+), 275 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/cache/AbstractCacheService.java Thu Oct 13 21:07:44 2016 +0200 +++ b/src/main/java/org/mpi/openmind/cache/AbstractCacheService.java Mon Oct 17 12:28:39 2016 +0200 @@ -1,6 +1,5 @@ package org.mpi.openmind.cache; -import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -52,16 +51,28 @@ this.relLoadedByName = new HashMap<String, Boolean>(); } + /** + * Remove the entity from the cache. + * + * @param entId + * @param oc + * @return + */ protected Entity removeEntity(Long entId, String oc){ return this.entMap.remove(new DuplexKey<String, Long>(oc, entId)); } + /** + * Put the entity in the cache. + * + * @param ent + */ protected void updateLWEntity(Entity ent){ this.entMap.put(ent.getKey(), ent); } /** - * Returns (a clone of) an entity form the cache by id. + * Returns (a clone of) an entity from the cache by id. * * @param id * @return @@ -130,7 +141,7 @@ // load entities from database List<Entity> list = getPs().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, null, Node.TYPE_ABOX, oc, null, true, -1); - // put entities in cache map + // put entities in cache for (Entity ent : list) { entMap.put(ent.getKey(), ent); } @@ -146,6 +157,12 @@ return list; } + /** + * Saves the Entities (without content) in the database and puts them in the cache. + * + * @param nodeList + * @throws Exception + */ public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList) throws Exception{ this.getPs().saveEntityListAsNodeWithoutContent(nodeList); for(Entity ent : nodeList){ @@ -179,16 +196,34 @@ return false; } + /** + * Put attribute in cache. + * + * @param att + */ protected void updateAtt(Attribute att){ this.attMap.put(att.getKey(), att); } + /** + * Put list of attributes in cache. + * + * @param list + */ protected void updateAttList(List<Attribute> list){ for(Attribute att : list){ this.attMap.put(att.getKey(), att); } } + /** + * Return a list of (cloned) Attributes of the Entity. + * + * Loads Attributes from the database into the cache. + * + * @param entId + * @return + */ public List<Attribute> getAttsBySrcId(Long entId){ List<Attribute> list = new ArrayList<Attribute>(); for(Attribute att : getAttsBySrcIdReadOnly(entId)){ @@ -266,33 +301,52 @@ return false; } + /** + * Put Relation in cache. + * + * @param rel + */ protected void updateRel(Relation rel){ this.relMap.put(rel.getKey(), rel); } + /** + * Put list of Relations in cache. + * + * @param list + */ protected void updateRelList(List<Relation> list){ for(Relation rel : list){ this.relMap.put(rel.getKey(), rel); } } - public List<Relation> getRelsBySrcId(long srcId) throws Exception{ - if(relLoadedBySrcId.get(srcId) == null || relLoadedBySrcId.get(srcId) == false){ - synchronized (relMap) { - if(relLoadedBySrcId.get(srcId) == null){ - //logger.info("getRelsBySrcId fromDB srcId=" + srcId); - relLoadedBySrcId.put(srcId, false); - List<Relation> list = - getPs().getSourceRelationsFromDB(this.getEntityByIdReadOnly(srcId), null, null, -1, false); - for(Relation rel : list){ - this.relMap.put(rel.getKey(), rel); - } - relLoadedBySrcId.put(srcId, true); - } - } - } - return relMap.getValuesByBKey(srcId); - } + /** + * Return Relations with matching source id from cache. + * + * Loads Relations from db and stores in cache if not in cache. + * + * @param srcId + * @return + * @throws Exception + */ + public List<Relation> getRelsBySrcId(long srcId) throws Exception { + if (relLoadedBySrcId.get(srcId) == null || relLoadedBySrcId.get(srcId) == false) { + // Relation not in cache + synchronized (relMap) { + if (relLoadedBySrcId.get(srcId) == null) { + relLoadedBySrcId.put(srcId, false); + List<Relation> list = getPs().getSourceRelationsFromDB(this.getEntityByIdReadOnly(srcId), null, + null, -1, false); + for (Relation rel : list) { + this.relMap.put(rel.getKey(), rel); + } + relLoadedBySrcId.put(srcId, true); + } + } + } + return relMap.getValuesByBKey(srcId); + } public List<Relation> getRelsByTarId(Long tarId){ if(relLoadedByTarId.get(tarId) == null || relLoadedByTarId.get(tarId) == false){
--- a/src/main/java/org/mpi/openmind/cache/CacheService.java Thu Oct 13 21:07:44 2016 +0200 +++ b/src/main/java/org/mpi/openmind/cache/CacheService.java Mon Oct 17 12:28:39 2016 +0200 @@ -321,6 +321,8 @@ } /** + * Puts the entity and its Attributes and Relations in the cache. + * * This method should be called when a entity has been changed. * It means that every trail of the old entity will be removed from the cache. * @param ent @@ -343,6 +345,12 @@ } } + /** + * Remove the Entity and its Attributes and Relations from the cache. + * + * @param entId + * @param oc + */ public void deleteEntity(Long entId, String oc){ try { this.removeEntity(entId, oc); @@ -361,7 +369,6 @@ } } catch (Exception e) { - e.printStackTrace(); logger.error(e.getMessage(), e); } }
--- a/src/main/java/org/mpi/openmind/repository/services/PersistenceService.java Thu Oct 13 21:07:44 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/services/PersistenceService.java Mon Oct 17 12:28:39 2016 +0200 @@ -235,164 +235,121 @@ return relations; } - public List<Relation> getSourceRelationsFromDB(Entity entity, - String relationName, String tarObjClass, int maxResult, - boolean includeEntities) throws Exception { - - List<Relation> relations = new ArrayList<Relation>(); - long start = System.currentTimeMillis(); - - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - - String hql = "from Relation rel " + "where " - + "rel.sourceId = :id AND " - + "rel.sourceModif = :modif AND " // TODO: previous versions are same without considering the modif - + "rel.systemStatus = :systemStatus "; - - if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ - hql += " AND rel.sourceModif = :modif "; - } - if (StringUtils.isNotEmpty(relationName)) { - hql += " AND rel.ownValue = :relationName "; - } - if (StringUtils.isNotEmpty(tarObjClass)) { - hql += " AND rel.targetObjectClass = :tarObjClass "; - } - - Query query = session.createQuery(hql); - query.setLong("id", entity.getId()); - query.setLong("modif", entity.getModificationTime()); - query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); - - - //if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ - // query.setLong("modif", entity.getModificationTime()); - //} - if (StringUtils.isNotEmpty(relationName)) { - query.setString("relationName", relationName); - } - if (StringUtils.isNotEmpty(tarObjClass)) { - query.setString("tarObjClass", tarObjClass); - } - if (maxResult > 0) { - query.setMaxResults(maxResult); - } - - List<Relation> list = query.list(); - if (includeEntities) { - for (Relation rel : list) { - List<Entity> tarList = getLightweightEntities(session, - Node.SYS_STATUS_CURRENT_VERSION, rel.getTargetId(), - null, null, null, false, 1); - if (tarList.size() > 0) { - rel.setSource(entity); - rel.setTarget(tarList.get(0)); - relations.add(rel); - } - } - - Collections.sort(relations, new RelationSortByTargetOW()); - } else { - relations = list; - } - - // loading attributes - for (Relation rel : relations) { - // FIXME: getNodes finds all attributes if id=null! - if (rel.getId() == null) { - logger.error("Relation with id=null! Abort loading attributes."); - continue; - } - List<Node> attrs = this.getNodes(session, null, - rel.getSystemStatus(), rel.getId(), - rel.getModificationTime(), null, null); - for (Node attNode : attrs) { - if (attNode instanceof Attribute) { - rel.addAttribute((Attribute) attNode); - } - } - } - - session.getTransaction().commit(); - - logger.debug("getSourceRelations - execution time[ms]: " - + (System.currentTimeMillis() - start)); - return relations; - } - /** - * <p> - * Returns the relations directly for the given entity from the DB, which - * conforms the given input. - * </p> - * <p> - * Only the variable tarObjClass can be null. - * </p> - * <p> - * If its expected all existing element from the DB, the variable maxResult - * should be -1, otherwise this value limits the returned result-set. - * </p> + * Return the source Relations of the given entity. * * @param entity * @param relationName * @param tarObjClass * @param maxResult + * @param includeEntities * @return + * @throws Exception */ - public List<Relation> getSourceRelationsError(Entity entity, - String relationName, String tarObjClass, int maxResult) - throws Exception { - - List<Relation> relations = new ArrayList<Relation>(); - - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); + public List<Relation> getSourceRelationsFromDB(Entity entity, String relationName, String tarObjClass, + int maxResult, boolean includeEntities) throws Exception { - String hql = "from Relation rel, Entity tar " + "where " - + "rel.sourceId = :id AND " + "rel.sourceModif = :mod AND " - + "rel.ownValue = :relationName AND " - + "rel.systemStatus = :systemStatus AND " - + "tar.systemStatus = :systemStatus AND " - + "tar.id = rel.targetId "; - - if (StringUtils.isNotEmpty(tarObjClass)) { - hql += " AND rel.targetObjectClass = :tarObjClass "; - } - - hql += " order by tar.ownValue "; + List<Relation> relations = new ArrayList<Relation>(); + long start = System.currentTimeMillis(); - Query query = session.createQuery(hql); - query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); - query.setLong("id", entity.getId()); - query.setLong("mod", entity.getModificationTime()); - query.setString("relationName", relationName); - if (StringUtils.isNotEmpty(tarObjClass)) { - query.setString("tarObjClass", tarObjClass); - } - if (maxResult > 0) { - query.setMaxResults(maxResult); - } - - List<Object> list = query.list(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + try { + session.getTransaction().begin(); + + /* + * build query string + */ + String hql = "from Relation rel where" + + " rel.sourceId = :id" + + " AND rel.sourceModif = :modif" + + " AND rel.systemStatus = :systemStatus "; + // TODO: previous versions are same without considering the modif + + if (StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)) { + hql += " AND rel.sourceModif = :modif"; + } + if (StringUtils.isNotEmpty(relationName)) { + hql += " AND rel.ownValue = :relationName"; + } + if (StringUtils.isNotEmpty(tarObjClass)) { + hql += " AND rel.targetObjectClass = :tarObjClass"; + } + + /* + * create query object + */ + Query query = session.createQuery(hql); + + /* + * add query parameters + */ + query.setLong("id", entity.getId()); + query.setLong("modif", entity.getModificationTime()); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + if (StringUtils.isNotEmpty(relationName)) { + query.setString("relationName", relationName); + } + if (StringUtils.isNotEmpty(tarObjClass)) { + query.setString("tarObjClass", tarObjClass); + } + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + /* + * run query + */ + List<Relation> list = query.list(); + + if (includeEntities) { + /* + * load target Entities for the Relations + */ + for (Relation rel : list) { + List<Entity> tarList = getLightweightEntities(session, Node.SYS_STATUS_CURRENT_VERSION, + rel.getTargetId(), null, null, null, false, 1); + if (tarList.size() > 0) { + rel.setSource(entity); + rel.setTarget(tarList.get(0)); + relations.add(rel); + } + } + + Collections.sort(relations, new RelationSortByTargetOW()); + } else { + relations = list; + } + + /* + * loading relation Attributes + */ + for (Relation rel : relations) { + // DANGER: getNodes returns all attributes in the db if id=null! + if (rel.getId() == null) { + logger.error("Relation with id=null! Abort loading attributes."); + continue; + } + List<Node> attrs = this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), + rel.getModificationTime(), null, null); + for (Node attNode : attrs) { + if (attNode instanceof Attribute) { + rel.addAttribute((Attribute) attNode); + } + } + } + } catch (Exception e) { + logger.error(e); + } finally { + session.getTransaction().commit(); + } - for (Object o : list) { - Object[] array = (Object[]) o; - if (array.length > 0) { - Relation rel = (Relation) array[0]; - Entity tar = (Entity) array[1]; - tar.setLightweight(true); - rel.setSource(entity); - rel.setTarget(tar); - relations.add(rel); - } - } - session.getTransaction().commit(); + logger.debug("getSourceRelations - execution time[ms]: " + (System.currentTimeMillis() - start)); + return relations; + } - return relations; - } - public Long getTargetRelationsCount(Entity entity, String relationName, + public Long getTargetRelationsCount(Entity entity, String relationName, String srcObjClass) { Long count = null; try { @@ -431,46 +388,16 @@ return count; } - public List<Relation> getTargetRelationsJDBCFromDB(Entity entity, - String relationName, String srcObjClass, int maxResult, - boolean includeEntities) { - List<Relation> relations = null; - long start = System.currentTimeMillis(); - try { - - Connection conn = JDBCUtils.getConn(); - - String sql = "select * from node " + "where " + "target_id = '" - + entity.getId() + "' AND " + "target_modif = '" - + entity.getModificationTime() + "' AND " - + "system_status = '" + Node.SYS_STATUS_CURRENT_VERSION - + "' "; - - if (StringUtils.isNotEmpty(relationName)) { - sql += " AND own_value = '" + relationName + "' "; - } - if (StringUtils.isNotEmpty(srcObjClass)) { - sql += " AND source_obj_class = '" + srcObjClass + "' "; - } - - // logger.info(sql); - Statement st = conn.createStatement(); - ResultSet rs = st.executeQuery(sql); - relations = JDBCUtils.rs2Rels(rs); - - conn.close(); - } catch (Exception e) { - logger.error(e.getMessage(), e); - e.printStackTrace(); - } - - logger.debug("GetTargetRelations JDBC - execution time[ms]: " - + (System.currentTimeMillis() - start)); - return relations; - } - - public List<Relation> getTargetRelationsFromDB(Entity entity, + /** + * @param entity + * @param relationName + * @param srcObjClass + * @param maxResult + * @param includeEntities + * @return + */ + public List<Relation> getTargetRelationsFromDB(Entity entity, String relationName, String srcObjClass, int maxResult, boolean includeEntities) { List<Relation> relations = new ArrayList<Relation>(); @@ -933,50 +860,9 @@ return list; } - /* - * public List<Entity> searchEntityStartingBy(String objectClass, - * List<String> ownValueList , int maxResult){ List<Entity> entities = new - * ArrayList<Entity>(); try { - * logger.debug("### Starting Search Entity Starting by " + ownValueList + - * " ###"); Long start = System.currentTimeMillis(); - * - * Session session = HibernateUtil.getSessionFactory() .getCurrentSession(); - * session.getTransaction().begin(); - * - * String hql = "from Entity " + "where " + - * "objectClass = :objectClass AND " + "ownValue REGEXP '^[0-9]' AND " + - * "systemStatus = :systemStatus "; - * - * - * if(ownValueList.size() > 0){ hql += " AND ( "; } for(int i=0; i < - * ownValueList.size(); i++){ if( i > 0) hql += " OR "; hql += - * "ownValue like :ownValue" + i + " "; } if(ownValueList.size() > 0){ hql - * += " ) "; } hql += "GROUP by id " + "ORDER by ownValue "; - * - * Query query = session.createQuery(hql); if(maxResult > 0){ - * query.setMaxResults(maxResult); } query.setString("systemStatus", - * Node.SYS_STATUS_CURRENT_VERSION); query.setString("objectClass", - * objectClass); - * - * for(int i=0; i < ownValueList.size(); i++){ query.setString("ownValue" + - * i, ownValueList.get(i)); } - * - * entities = query.list(); - * - * Long end = System.currentTimeMillis(); Long diff = end - start; - * logger.info("Search Entity Starting By - Time execution: " + diff - * / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + "[min] " + diff / - * 1000 + "[sec]"); - * - * session.getTransaction().commit(); } catch (Exception e) { - * logger.error(e.getMessage()); e.printStackTrace(); } return entities; } - */ - /** - * <p> * This method returns a list of entities with same attributes and same own * value than the given entity - * </p> * * @param entity * @param substring @@ -1207,44 +1093,41 @@ } /** - * Returns a list of all attribute for an entity directly from the DB no - * implemented yet! + * Returns a list of all attribute for an entity directly from the DB. * * @param entity * @param name * @return */ - public List<Attribute> getAllAttributes(Long entId, int maxResult) { - List<Attribute> atts = null; - long start = System.currentTimeMillis(); - try { - Session session = HibernateUtil.getSessionFactory() - .getCurrentSession(); - session.getTransaction().begin(); + public List<Attribute> getAllAttributes(Long entId, int maxResult) { + List<Attribute> atts = null; + long start = System.currentTimeMillis(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + try { + session.getTransaction().begin(); - String hql = "from Attribute " + "where " - + "systemStatus = :systemStatus AND " + "sourceId = :id " - + "order by objectClass"; + String hql = "from Attribute where" + + " systemStatus = :systemStatus" + + " AND sourceId = :id " + + " order by objectClass"; - Query query = session.createQuery(hql); - if (maxResult > 0) { - query.setMaxResults(maxResult); - } - query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); - query.setLong("id", entId); + Query query = session.createQuery(hql); + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entId); - atts = query.list(); + atts = query.list(); - session.getTransaction().commit(); - - } catch (Exception e) { - logger.error(e.getMessage(), e); - e.printStackTrace(); - } - logger.debug("getAllAttributes - execution time[ms]: " - + (System.currentTimeMillis() - start)); - return atts; - } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } finally { + session.getTransaction().commit(); + } + logger.debug("getAllAttributes - execution time[ms]: " + (System.currentTimeMillis() - start)); + return atts; + } /** * Here the entities can more than one time in the result set.