[jbosstools-commits] JBoss Tools SVN: r43570 - in workspace/akazakov/db: org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/entity and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Mon Sep 10 16:38:43 EDT 2012


Author: akazakov
Date: 2012-09-10 16:38:43 -0400 (Mon, 10 Sep 2012)
New Revision: 43570

Modified:
   workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java
   workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/entity/BeanEntity.java
   workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java
Log:
https://issues.jboss.org/browse/JBIDE-12446

Modified: workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java
===================================================================
--- workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java	2012-09-10 20:33:09 UTC (rev 43569)
+++ workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java	2012-09-10 20:38:43 UTC (rev 43570)
@@ -34,9 +34,66 @@
  */
 public class CDIDataBase implements ICDICache {
 
-	private Map<String, Short> projects = new HashMap<String, Short>();
-	private short nextIndex = 0;
+	private Context currentContext = new Context();
 
+	private static class Context {
+		private Map<String, Short> projects = new HashMap<String, Short>();
+		private Map<Integer, IBean> allBeans = new HashMap<Integer, IBean>();
+		private short nextIndex = 0;
+
+		long getTypeIndex(IParametedType type) {
+			IType iType = type.getType();
+			String name = iType!=null?iType.getFullyQualifiedName():type.getSimpleName();
+			return getTypeIndex(name);
+		}
+
+		long getTypeIndex(String typeName) {
+			long result = ((long)typeName.hashCode() << 32);
+			if(typeName.length() > 1) {
+				result += (typeName.substring(1) + typeName.substring(0, 1)).hashCode();
+			}
+			return  result;
+		}
+
+		Short getProjectIndex(ICDIProject project) {
+			String projectName = project.getNature().getProject().getName();
+			return projects.get(projectName);
+		}
+
+		Short removeProjectIndex(ICDIProject project) {
+			String projectName = project.getNature().getProject().getName();
+			Short index = projects.get(projectName);
+			if(index!=null) {
+				projects.remove(projectName);
+			}
+			return index;
+		}
+
+		short generateProjectIndex(ICDIProject project) {
+			String projectName = project.getNature().getProject().getName();
+			short index = nextIndex;
+			projects.put(projectName, nextIndex++);
+			return index;
+		}
+
+		Map<Integer, IBean> getAllBeans() {
+			return allBeans;
+		}
+
+		/* (non-Javadoc)
+		 * @see java.lang.Object#clone()
+		 */
+		@Override
+		protected Object clone() {
+			Context context = new Context();
+			context.nextIndex = this.nextIndex;
+			for (Map.Entry<String, Short> project : this.projects.entrySet()) {
+				context.projects.put(project.getKey(), project.getValue());
+			}
+			return context;
+		}
+	}
+
 	public CDIDataBase() {
 		CDIDBManager.getInstance().getEntityManager();
 	}
@@ -47,7 +104,12 @@
 	 */
 	@Override
 	public void rebuild(ICDIProject project, Collection<IBean> beans) {
-		Short projectIndex = removeProjectIndex(project);
+		Context context = null;
+		synchronized (currentContext) {
+			context = (Context)currentContext.clone();
+		}
+
+		Short projectIndex = context.removeProjectIndex(project);
 		EntityManager em = CDIDBManager.getInstance().getEntityManager();
 		EntityTransaction transaction = em.getTransaction();
 		transaction.begin();
@@ -56,11 +118,11 @@
 			em.createQuery("DELETE FROM QualifierEntity b WHERE b.projectIndex = :p").setParameter("p", projectIndex).executeUpdate();
 			em.createQuery("DELETE FROM TypeEntity b WHERE b.projectIndex = :p").setParameter("p", projectIndex).executeUpdate();
 		}
-		projectIndex = generateProjectIndex(project);
+		projectIndex = context.generateProjectIndex(project);
 
 		Map<Integer, QualifierEntity> qualifiers = new HashMap<Integer, QualifierEntity>();
-		Map<Integer, TypeEntity> types = new HashMap<Integer, TypeEntity>();
 		for (IBean bean : beans) {
+			context.getAllBeans().put(bean.getId(), bean);
 			BeanEntity beanEntity = new BeanEntity();
 			beanEntity.setIndex(bean.getId());
 			beanEntity.setProjectIndex(projectIndex);
@@ -79,19 +141,21 @@
 				qualifierEntities.add(qualifierEntity);
 			}
 
+			beanEntity.setQualifiers(qualifierEntities);
+			em.persist(beanEntity);
+
 			Collection<IParametedType> beanTypes = bean.getLegalTypes();
 			for (IParametedType beanType : beanTypes) {
-				long id = getTypeIndex(beanType);
+				long id = context.getTypeIndex(beanType);
 				TypeEntity typeEntity = new TypeEntity();
 				typeEntity.setIndex(id);
 				typeEntity.setBean(beanEntity);
 				em.persist(typeEntity);
 			}
-
-			beanEntity.setQualifiers(qualifierEntities);
-			em.persist(beanEntity);
 		}
 		transaction.commit();
+
+		currentContext = context;
 	}
 
 	/*
@@ -100,51 +164,23 @@
 	 */
 	@Override
 	public Collection<IBean> getBeansByLegalType(ICDIProject project, String legalType) {
-		Short projectIndex = getProjectIndex(project);
-		EntityManager em = CDIDBManager.getInstance().getEntityManager();
-		EntityTransaction transaction = em.getTransaction();
-		transaction.begin();
+		List<IBean> beans = new ArrayList<IBean>();
+		synchronized (currentContext) {
+			Short projectIndex = currentContext.getProjectIndex(project);
+			EntityManager em = CDIDBManager.getInstance().getEntityManager();
+			EntityTransaction transaction = em.getTransaction();
+			transaction.begin();
 
-		long typeIndex = getTypeIndex(legalType);
-		//TODO
-		List result = em.createQuery("SELECT FROM TypeEntity t, BeanEntity b WHERE t.index = :b AND t.bean.projectIndex = :p AND t.bean.id = b.id ").setParameter("p", projectIndex).setParameter("b", typeIndex).getResultList();
-
-		transaction.commit();
-		return null;
-	}
-
-	private long getTypeIndex(IParametedType type) {
-		IType iType = type.getType();
-		String name = iType!=null?iType.getFullyQualifiedName():type.getSimpleName();
-		return getTypeIndex(name);
-	}
-
-	private long getTypeIndex(String typeName) {
-		long result = ((long)typeName.hashCode() << 32);
-		if(typeName.length() > 1) {
-			result += (typeName.substring(1) + typeName.substring(0, 1)).hashCode();
+			long typeIndex = currentContext.getTypeIndex(legalType);
+			List results = em.createQuery("SELECT FROM t.bean TypeEntity t WHERE t.index = :b AND t.bean.projectIndex = :p").setParameter("p", projectIndex).setParameter("b", typeIndex).getResultList();
+			for (Object result : results) {
+				BeanEntity beanEntity = (BeanEntity)result;
+				int index = beanEntity.getIndex();
+				IBean bean = currentContext.allBeans.get(index);
+				beans.add(bean);
+			}
+			transaction.commit();
 		}
-		return  result;
+		return beans;
 	}
-
-	private Short getProjectIndex(ICDIProject project) {
-		String projectName = project.getNature().getProject().getName();
-		return projects.get(projectName);
-	}
-
-	private Short removeProjectIndex(ICDIProject project) {
-		String projectName = project.getNature().getProject().getName();
-		Short index = projects.get(projectName);
-		if(index!=null) {
-			projects.remove(projectName);
-		}
-		return index;
-	}
-
-	private short generateProjectIndex(ICDIProject project) {
-		String projectName = project.getNature().getProject().getName();
-		short index = nextIndex;
-		projects.put(projectName, nextIndex++);
-		return index;
-	}
 }
\ No newline at end of file

Modified: workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/entity/BeanEntity.java
===================================================================
--- workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/entity/BeanEntity.java	2012-09-10 20:33:09 UTC (rev 43569)
+++ workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/entity/BeanEntity.java	2012-09-10 20:38:43 UTC (rev 43570)
@@ -30,7 +30,7 @@
 	private long id;
 	private int index;
 	private short projectIndex;
-	private List<TypeEntity> types;
+//	private List<TypeEntity> types;
 	private List<QualifierEntity> qualifiers;
 
 	/**
@@ -59,25 +59,25 @@
 		this.index = index;
 	}
 
-	/**
-	 * @return the types
-	 */
-	@OneToMany(cascade={CascadeType.REMOVE}, mappedBy="bean")
-	public List<TypeEntity> getTypes() {
-		return types;
-	}
+//	/**
+//	 * @return the types
+//	 */
+//	@OneToMany(cascade={CascadeType.REMOVE}, mappedBy="bean")
+//	public List<TypeEntity> getTypes() {
+//		return types;
+//	}
+//
+//	/**
+//	 * @param types the types to set
+//	 */
+//	public void setTypes(List<TypeEntity> types) {
+//		this.types = types;
+//	}
 
 	/**
-	 * @param types the types to set
-	 */
-	public void setTypes(List<TypeEntity> types) {
-		this.types = types;
-	}
-
-	/**
 	 * @return the qualifiers
 	 */
-	@ManyToOne
+	@OneToMany
 	@JoinColumn(nullable=true)
 	public List<QualifierEntity> getQualifiers() {
 		return qualifiers;

Modified: workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java
===================================================================
--- workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java	2012-09-10 20:33:09 UTC (rev 43569)
+++ workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java	2012-09-10 20:38:43 UTC (rev 43570)
@@ -51,8 +51,8 @@
 		url = protocol + getDBPath();
 		Connection conn = DriverManager.getConnection(url + ";create=true", props);
 		conn.close();
+		init();
 		started = true;
-		init();
 	}
 
 	public EntityManager getEntityManager() {



More information about the jbosstools-commits mailing list