-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ProjectCache for FORGE-1141 - Shell is noticably slow when …
…working in project folder
- Loading branch information
1 parent
f827441
commit 1f55c8e
Showing
6 changed files
with
278 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
projects/api/src/main/java/org/jboss/forge/addon/projects/spi/ProjectCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.projects.spi; | ||
|
||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.addon.resource.DirectoryResource; | ||
import org.jboss.forge.furnace.services.Exported; | ||
|
||
/** | ||
* Responsible for caching {@link Project} instances so that they do not need to be re-built or re-generated for | ||
* multiple consumers. | ||
* | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
@Exported | ||
public interface ProjectCache | ||
{ | ||
/** | ||
* Retrieve a {@link Project} from the cache, using {@link Project#getProjectRoot()} as the key. | ||
*/ | ||
Project get(DirectoryResource target); | ||
|
||
/** | ||
* Invalidate the cache, forcing all stored projects to be re-built. | ||
*/ | ||
void invalidate(); | ||
|
||
/** | ||
* Store the given {@link Project} into this cache. | ||
*/ | ||
void store(Project project); | ||
} |
46 changes: 46 additions & 0 deletions
46
projects/impl/src/main/java/org/jboss/forge/addon/projects/impl/InMemoryProjectCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.projects.impl; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.addon.projects.spi.ProjectCache; | ||
import org.jboss.forge.addon.resource.DirectoryResource; | ||
|
||
/** | ||
* A simple in-memory {@link ProjectCache}. | ||
* | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
@Singleton | ||
public class InMemoryProjectCache implements ProjectCache | ||
{ | ||
private Map<String, Project> projects = new ConcurrentHashMap<String, Project>(); | ||
|
||
@Override | ||
public Project get(DirectoryResource dir) | ||
{ | ||
return projects.get(dir.getFullyQualifiedName()); | ||
} | ||
|
||
@Override | ||
public void invalidate() | ||
{ | ||
this.projects.clear(); | ||
} | ||
|
||
@Override | ||
public void store(Project project) | ||
{ | ||
this.projects.put(project.getProjectRoot().getFullyQualifiedName(), project); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...ests/src/test/java/org/jboss/forge/addon/projects/impl/cache/ProjectFactoryCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.jboss.forge.addon.projects.impl.cache; | ||
|
||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.addon.projects.ProjectFactory; | ||
import org.jboss.forge.addon.projects.impl.MockProjectListener; | ||
import org.jboss.forge.addon.projects.spi.ProjectCache; | ||
import org.jboss.forge.arquillian.AddonDependency; | ||
import org.jboss.forge.arquillian.Dependencies; | ||
import org.jboss.forge.arquillian.archive.ForgeArchive; | ||
import org.jboss.forge.furnace.repositories.AddonDependencyEntry; | ||
import org.jboss.forge.furnace.services.Imported; | ||
import org.jboss.forge.furnace.util.Predicate; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(Arquillian.class) | ||
public class ProjectFactoryCacheTest | ||
{ | ||
@Deployment | ||
@Dependencies({ | ||
@AddonDependency(name = "org.jboss.forge.addon:resources"), | ||
@AddonDependency(name = "org.jboss.forge.addon:projects"), | ||
@AddonDependency(name = "org.jboss.forge.addon:ui"), | ||
@AddonDependency(name = "org.jboss.forge.addon:maven") | ||
}) | ||
public static ForgeArchive getDeployment() | ||
{ | ||
ForgeArchive archive = ShrinkWrap | ||
.create(ForgeArchive.class) | ||
.addClass(MockProjectListener.class) | ||
.addBeansXML() | ||
.addAsAddonDependencies( | ||
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"), | ||
AddonDependencyEntry.create("org.jboss.forge.addon:projects") | ||
); | ||
|
||
return archive; | ||
} | ||
|
||
@Inject | ||
private Imported<ProjectCache> caches; | ||
|
||
@Inject | ||
private ProjectFactory projectFactory; | ||
|
||
@Test | ||
public void testInjectionNotNull() | ||
{ | ||
Assert.assertNotNull(caches); | ||
} | ||
|
||
@Test | ||
public void testFindProjectFromCache() throws Exception | ||
{ | ||
Project project = projectFactory.createTempProject(); | ||
|
||
Assert.assertNotNull(project); | ||
Project found = projectFactory.findProject(project.getProjectRoot()); | ||
Assert.assertNotNull(found); | ||
Assert.assertSame(project, found); | ||
|
||
Assert.assertNull(projectFactory.findProject(project.getProjectRoot(), new Predicate<Project>() | ||
{ | ||
@Override | ||
public boolean accept(Project type) | ||
{ | ||
return false; | ||
} | ||
})); | ||
|
||
Project found2 = projectFactory.findProject(project.getProjectRoot().getChildDirectory("src/main/java")); | ||
Assert.assertNotNull(found2); | ||
Assert.assertSame(found, found2); | ||
|
||
Project project2 = projectFactory.createTempProject(); | ||
Assert.assertNotSame(found2, project2); | ||
Assert.assertNotEquals(found2.getProjectRoot().getFullyQualifiedName(), project2.getProjectRoot() | ||
.getFullyQualifiedName()); | ||
|
||
project.getProjectRoot().delete(true); | ||
} | ||
|
||
} |