reduce database hits

This commit is contained in:
F43nd1r 2018-01-24 03:39:10 +01:00
parent 5ed6592ccf
commit e7ffc7bdc1
8 changed files with 38 additions and 30 deletions

View file

@ -13,7 +13,7 @@ import org.springframework.web.multipart.commons.CommonsMultipartResolver;
public class Rfc1341MultipartResolver extends CommonsMultipartResolver {
@NonNull
@Override
protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {
protected FileUpload newFileUpload(@NonNull FileItemFactory fileItemFactory) {
return new Rfc1341ServletFileUpload(fileItemFactory);
}
}

View file

@ -4,19 +4,15 @@ import com.faendir.acra.sql.model.App;
import com.faendir.acra.sql.model.Bug;
import com.faendir.acra.sql.model.Report;
import com.faendir.acra.sql.util.CountResult;
import com.faendir.acra.util.Utils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import java.util.Optional;
/**
* @author Lukas
@ -27,7 +23,11 @@ public interface ReportRepository extends JpaRepository<Report, String> {
Slice<Report> findAllByBugApp(@NonNull App app, @NonNull Pageable pageable);
Stream<Report> findAllByBugApp(@NonNull App app);
@Query("select report from Report report join fetch report.bug bug join fetch bug.app app where app = ?1")
List<Report> findAllByAppEager(@NonNull App app);
@Query("select report from Report report join fetch report.bug bug join fetch bug.app app where report.id = ?1")
Optional<Report> findByIdEager(@NonNull String id);
int countAllByBugApp(@NonNull App app);
@ -48,20 +48,4 @@ public interface ReportRepository extends JpaRepository<Report, String> {
@SuppressWarnings("SpringDataRepositoryMethodReturnTypeInspection")
@Query("select new com.faendir.acra.sql.util.CountResult(bug.id, count(report)) from Report report group by report.bug")
List<CountResult<Integer>> countAllByBug();
@Transactional
default void reassignBugs(App app) {
Map<String, Bug> bugs = new HashMap<>();
try(Stream<Report> stream = findAllByBugApp(app)) {
stream.forEach(report -> {
String stacktrace = Utils.generifyStacktrace(report.getStacktrace(), app.getConfiguration());
Bug bug = bugs.get(stacktrace);
if (bug == null) {
bug = new Bug(app, stacktrace, report.getVersionCode(), report.getDate());
}
report.setBug(bug);
bugs.put(stacktrace, save(report).getBug());
});
}
}
}

View file

@ -8,6 +8,7 @@ import org.springframework.lang.NonNull;
import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@ -23,7 +24,7 @@ public class App {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@OneToOne(cascade = CascadeType.ALL, optional = false, orphanRemoval = true)
@OneToOne(cascade = CascadeType.ALL, optional = false, orphanRemoval = true, fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
private User reporter;
private Configuration configuration;

View file

@ -8,6 +8,7 @@ import org.springframework.lang.NonNull;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@ -23,7 +24,7 @@ public class Bug {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, optional = false)
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, optional = false, fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
private App app;
private boolean solved;

View file

@ -11,6 +11,7 @@ import org.springframework.lang.NonNull;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
@ -23,7 +24,7 @@ import java.util.Date;
@Entity
public class Report {
@Id private String id;
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, optional = false)
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, optional = false, fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
private Bug bug;
@Type(type = "text") private String content;

View file

@ -5,8 +5,8 @@ package com.faendir.acra.sql.util;
* @since 19.12.2017
*/
public class CountResult<T> {
T group;
long count;
private T group;
private long count;
public CountResult(T group, long count) {
this.group = group;

View file

@ -131,7 +131,7 @@ public class ReportView extends ParametrizedNamedView<Report> {
@Override
public Report validateAndParseFragment(@NonNull String fragment) {
return reportRepository.findById(fragment).orElse(null);
return reportRepository.findByIdEager(fragment).orElse(null);
}
private static class ExceptionAwareStreamSource implements StreamResource.StreamSource {

View file

@ -4,7 +4,9 @@ import com.faendir.acra.sql.data.AppRepository;
import com.faendir.acra.sql.data.BugRepository;
import com.faendir.acra.sql.data.ReportRepository;
import com.faendir.acra.sql.model.App;
import com.faendir.acra.sql.model.Bug;
import com.faendir.acra.sql.model.Permission;
import com.faendir.acra.sql.model.Report;
import com.faendir.acra.sql.model.User;
import com.faendir.acra.sql.user.UserManager;
import com.faendir.acra.ui.NavigationManager;
@ -13,6 +15,7 @@ import com.faendir.acra.ui.view.base.ConfigurationLabel;
import com.faendir.acra.ui.view.base.MyTabSheet;
import com.faendir.acra.ui.view.base.Popup;
import com.faendir.acra.ui.view.base.ValidatedField;
import com.faendir.acra.util.Utils;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
@ -30,6 +33,9 @@ import org.vaadin.risto.stepper.IntStepper;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Lukas
@ -95,7 +101,22 @@ public class PropertiesTab implements MyTabSheet.Tab {
.addYesNoButtons(p -> {
app.setConfiguration(new App.Configuration(matchByMessage.getValue(), ignoreInstanceIds.getValue(), ignoreAndroidLineNumbers.getValue()));
appRepository.save(app);
reportRepository.reassignBugs(app);
Map<String, Bug> bugs = new HashMap<>();
List<Report> reports = reportRepository.findAllByAppEager(app);
reports.forEach(report -> {
String stacktrace = Utils.generifyStacktrace(report.getStacktrace(), app.getConfiguration());
Bug bug = bugs.get(stacktrace);
if (bug == null) {
if (stacktrace.equals(report.getBug().getStacktrace())) {
bug = report.getBug();
} else {
bug = new Bug(app, stacktrace, report.getVersionCode(), report.getDate());
}
}
report.setBug(bug);
bugs.put(stacktrace, bug);
});
reportRepository.saveAll(reports);
bugRepository.deleteOrphans();
p.close();
})