Browse Source

SONAR-3755 use persistent cache

tags/3.6
Simon Brandhof 11 years ago
parent
commit
cb4fd33308

+ 20
- 13
sonar-batch/src/main/java/org/sonar/batch/index/Caches.java View File

@@ -49,21 +49,11 @@ public class Caches implements BatchComponent, Startable {
private Persistit persistit;
private Volume volume;

public <K extends Serializable, V extends Serializable> Cache<K, V> createCache(String cacheName) {
Preconditions.checkState(volume != null && volume.isOpened(), "Caches are not started");
Preconditions.checkState(!cacheNames.contains(cacheName), "Cache is already created: " + cacheName);
try {
Exchange exchange = persistit.getExchange(volume, cacheName, true);
Cache<K, V> cache = new Cache<K, V>(cacheName, exchange);
cacheNames.add(cacheName);
return cache;
} catch (Exception e) {
throw new IllegalStateException("Fail to create cache: " + cacheName, e);
}
public Caches() {
initPersistit();
}

@Override
public void start() {
private void initPersistit() {
try {
tempDir = Files.createTempDir();
persistit = new Persistit();
@@ -85,6 +75,23 @@ public class Caches implements BatchComponent, Startable {
}
}

public <K extends Serializable, V extends Serializable> Cache<K, V> createCache(String cacheName) {
Preconditions.checkState(volume != null && volume.isOpened(), "Caches are not initialized");
Preconditions.checkState(!cacheNames.contains(cacheName), "Cache is already created: " + cacheName);
try {
Exchange exchange = persistit.getExchange(volume, cacheName, true);
Cache<K, V> cache = new Cache<K, V>(cacheName, exchange);
cacheNames.add(cacheName);
return cache;
} catch (Exception e) {
throw new IllegalStateException("Fail to create cache: " + cacheName, e);
}
}

@Override
public void start() {
}

@Override
public void stop() {
if (persistit != null) {

+ 10
- 17
sonar-batch/src/main/java/org/sonar/batch/issue/IssueCache.java View File

@@ -19,13 +19,12 @@
*/
package org.sonar.batch.issue;

import com.google.common.collect.Maps;
import org.sonar.api.BatchComponent;
import org.sonar.api.issue.Issue;
import org.sonar.batch.index.Cache;
import org.sonar.batch.index.Caches;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
* Shared issues among all project modules
@@ -33,28 +32,22 @@ import java.util.Map;
public class IssueCache implements BatchComponent {

// component key -> issue key -> issue
private final Map<String, Map<String, Issue>> componentIssues = Maps.newHashMap();
private final Cache<String, Issue> cache;

public IssueCache(Caches caches) {
cache = caches.createCache("issues");
}

public Collection<Issue> componentIssues(String componentKey) {
Map<String, Issue> issuesByKey = componentIssues.get(componentKey);
return issuesByKey == null ? Collections.<Issue>emptyList() : issuesByKey.values();
return cache.values(componentKey);
}

public Issue componentIssue(String componentKey, String issueKey) {
Map<String, Issue> issuesByKey = componentIssues.get(componentKey);
if (issuesByKey != null) {
return issuesByKey.get(issueKey);
}
return null;
return cache.get(componentKey, issueKey);
}

public IssueCache addOrUpdate(Issue issue) {
Map<String, Issue> issuesByKey = componentIssues.get(issue.componentKey());
if (issuesByKey == null) {
issuesByKey = Maps.newHashMap();
componentIssues.put(issue.componentKey(), issuesByKey);
}
issuesByKey.put(issue.key(), issue);
cache.put(issue.componentKey(), issue.key(), issue);
return this;
}
}

+ 0
- 2
sonar-batch/src/main/java/org/sonar/batch/issue/IssuePersister.java View File

@@ -74,6 +74,4 @@ public class IssuePersister implements ScanPersister {
}
}
}


}

+ 0
- 1
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java View File

@@ -90,7 +90,6 @@ public class ProjectScanContainer extends ComponentContainer {

// issues
IssueWorkflow.class,
ScanIssues.class,
DeprecatedViolations.class,
IssueCache.class,
IssuePersister.class,

+ 1
- 16
sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java View File

@@ -37,12 +37,7 @@ public class CachesTest {
}

@Test
public void should_start_and_stop_persistit() throws Exception {
assertThat(caches.tempDir()).isNull();
assertThat(caches.persistit()).isNull();

caches.start();

public void should_stop_and_clean_temp_dir() throws Exception {
File tempDir = caches.tempDir();
assertThat(tempDir).isDirectory().exists();
assertThat(caches.persistit()).isNotNull();
@@ -74,16 +69,6 @@ public class CachesTest {
}
}

@Test
public void should_not_create_cache_before_starting() {
try {
caches.createCache("too_early");
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Caches are not started");
}
}

static class Element implements Serializable {

}

+ 2
- 2
sonar-batch/src/test/java/org/sonar/batch/issue/IssueCacheTest.java View File

@@ -50,7 +50,7 @@ public class IssueCacheTest {

@Test
public void should_add_new_issue() throws Exception {
IssueCache cache = new IssueCache();
IssueCache cache = new IssueCache(caches);
DefaultIssue issue1 = new DefaultIssue().setKey("111").setComponentKey("org.struts.Action");
DefaultIssue issue2 = new DefaultIssue().setKey("222").setComponentKey("org.struts.Action");
DefaultIssue issue3 = new DefaultIssue().setKey("333").setComponentKey("org.struts.Filter");
@@ -62,7 +62,7 @@ public class IssueCacheTest {

@Test
public void should_update_existing_issue() throws Exception {
IssueCache cache = new IssueCache();
IssueCache cache = new IssueCache(caches);
DefaultIssue issue = new DefaultIssue().setKey("111").setComponentKey("org.struts.Action").setSeverity(Severity.BLOCKER);
cache.addOrUpdate(issue);


+ 2
- 0
sonar-core/src/test/java/org/sonar/core/issue/workflow/SetResolutionTest.java View File

@@ -24,6 +24,7 @@ import org.sonar.api.issue.Issue;
import org.sonar.core.issue.DefaultIssue;

import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;

public class SetResolutionTest {
@Test
@@ -38,6 +39,7 @@ public class SetResolutionTest {
public void resolution_should_not_be_empty() throws Exception {
try {
new SetResolution("");
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Resolution must be set");
}

+ 5
- 0
sonar-core/src/test/java/org/sonar/core/issue/workflow/TransitionTest.java View File

@@ -23,6 +23,7 @@ import org.junit.Test;
import org.sonar.core.issue.DefaultIssue;

import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -64,6 +65,7 @@ public class TransitionTest {
public void key_should_be_set() throws Exception {
try {
Transition.builder("").from("OPEN").to("CLOSED").build();
fail();
} catch (Exception e) {
assertThat(e).hasMessage("Transition key must be set");
}
@@ -73,6 +75,7 @@ public class TransitionTest {
public void key_should_be_lower_case() throws Exception {
try {
Transition.builder("CLOSE").from("OPEN").to("CLOSED").build();
fail();
} catch (Exception e) {
assertThat(e).hasMessage("Transition key must be lower-case");
}
@@ -82,6 +85,7 @@ public class TransitionTest {
public void originating_status_should_be_set() throws Exception {
try {
Transition.builder("close").from("").to("CLOSED").build();
fail();
} catch (Exception e) {
assertThat(e).hasMessage("Originating status must be set");
}
@@ -91,6 +95,7 @@ public class TransitionTest {
public void destination_status_should_be_set() throws Exception {
try {
Transition.builder("close").from("OPEN").to("").build();
fail();
} catch (Exception e) {
assertThat(e).hasMessage("Destination status must be set");
}

+ 2
- 1
sonar-plugin-api/src/main/java/org/sonar/api/issue/Issue.java View File

@@ -21,13 +21,14 @@ package org.sonar.api.issue;

import org.sonar.api.rule.RuleKey;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
* @since 3.6
*/
public interface Issue {
public interface Issue extends Serializable {

int DESCRIPTION_MAX_SIZE = 4000;
String STATUS_OPEN = "OPEN";

Loading…
Cancel
Save