aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/main
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2022-03-14 11:27:18 -0500
committersonartech <sonartech@sonarsource.com>2022-03-18 20:02:57 +0000
commit1269984e8e09338c057d068d715ade7df5a0c354 (patch)
treeb483c992fd8c57b79188c936fb9bfb6ff7955ed8 /server/sonar-db-dao/src/main
parente987de516356fe37ee8eb35793d5fd709e2a099b (diff)
downloadsonarqube-1269984e8e09338c057d068d715ade7df5a0c354.tar.gz
sonarqube-1269984e8e09338c057d068d715ade7df5a0c354.zip
SONAR-16115 Store plugin's scanner cache in SonarQube
Diffstat (limited to 'server/sonar-db-dao/src/main')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java9
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/DbInputStream.java46
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskInputDao.java31
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java7
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java1
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheDao.java82
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheMapper.java28
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml4
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerCacheMapper.xml15
11 files changed, 199 insertions, 28 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
index b07fd319638..4649066af46 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
@@ -77,6 +77,7 @@ import org.sonar.db.qualityprofile.QualityProfileDao;
import org.sonar.db.qualityprofile.QualityProfileExportDao;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleRepositoryDao;
+import org.sonar.db.scannercache.ScannerCacheDao;
import org.sonar.db.schemamigration.SchemaMigrationDao;
import org.sonar.db.source.FileSourceDao;
import org.sonar.db.user.GroupDao;
@@ -156,6 +157,7 @@ public class DaoModule extends Module {
RuleDao.class,
RuleRepositoryDao.class,
SamlMessageIdDao.class,
+ ScannerCacheDao.class,
SnapshotDao.class,
SchemaMigrationDao.class,
SessionTokensDao.class,
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
index 260f9430cdd..2268ea41b53 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
@@ -57,8 +57,8 @@ import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao;
import org.sonar.db.permission.template.PermissionTemplateDao;
import org.sonar.db.plugin.PluginDao;
import org.sonar.db.portfolio.PortfolioDao;
-import org.sonar.db.project.ProjectDao;
import org.sonar.db.project.ProjectBadgeTokenDao;
+import org.sonar.db.project.ProjectDao;
import org.sonar.db.property.InternalComponentPropertiesDao;
import org.sonar.db.property.InternalPropertiesDao;
import org.sonar.db.property.PropertiesDao;
@@ -77,6 +77,7 @@ import org.sonar.db.qualityprofile.QualityProfileDao;
import org.sonar.db.qualityprofile.QualityProfileExportDao;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleRepositoryDao;
+import org.sonar.db.scannercache.ScannerCacheDao;
import org.sonar.db.schemamigration.SchemaMigrationDao;
import org.sonar.db.source.FileSourceDao;
import org.sonar.db.user.GroupDao;
@@ -168,6 +169,7 @@ public class DbClient {
private final UserDismissedMessagesDao userDismissedMessagesDao;
private final ApplicationProjectsDao applicationProjectsDao;
private final ProjectBadgeTokenDao projectBadgeTokenDao;
+ private final ScannerCacheDao scannerCacheDao;
public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) {
this.database = database;
@@ -248,6 +250,7 @@ public class DbClient {
samlMessageIdDao = getDao(map, SamlMessageIdDao.class);
userDismissedMessagesDao = getDao(map, UserDismissedMessagesDao.class);
applicationProjectsDao = getDao(map, ApplicationProjectsDao.class);
+ scannerCacheDao = getDao(map, ScannerCacheDao.class);
}
public DbSession openSession(boolean batch) {
@@ -547,4 +550,8 @@ public class DbClient {
public ProjectBadgeTokenDao projectBadgeTokenDao() {
return projectBadgeTokenDao;
}
+
+ public ScannerCacheDao scannerCacheDao() {
+ return scannerCacheDao;
+ }
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbInputStream.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbInputStream.java
new file mode 100644
index 00000000000..e956ad4e8b7
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbInputStream.java
@@ -0,0 +1,46 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db;
+
+import java.io.InputStream;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.internal.apachecommons.io.input.ProxyInputStream;
+
+public class DbInputStream extends ProxyInputStream {
+ private final PreparedStatement stmt;
+ private final ResultSet rs;
+ private final InputStream stream;
+
+ public DbInputStream(PreparedStatement stmt, ResultSet rs, InputStream stream) {
+ super(stream);
+ this.stmt = stmt;
+ this.rs = rs;
+ this.stream = stream;
+ }
+
+ @Override
+ public void close() {
+ IOUtils.closeQuietly(stream);
+ DatabaseUtils.closeQuietly(rs);
+ DatabaseUtils.closeQuietly(stmt);
+ }
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
index 6917d3c656f..6792608657e 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
@@ -136,6 +136,7 @@ import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleMapper;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.rule.RuleRepositoryMapper;
+import org.sonar.db.scannercache.ScannerCacheMapper;
import org.sonar.db.schemamigration.SchemaMigrationDto;
import org.sonar.db.schemamigration.SchemaMigrationMapper;
import org.sonar.db.source.FileSourceMapper;
@@ -299,6 +300,7 @@ public class MyBatis {
RuleMapper.class,
RuleRepositoryMapper.class,
SamlMessageIdMapper.class,
+ ScannerCacheMapper.class,
SchemaMigrationMapper.class,
SessionTokenMapper.class,
SnapshotMapper.class,
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskInputDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskInputDao.java
index a83dfb6801e..74e0b66b954 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskInputDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskInputDao.java
@@ -27,9 +27,9 @@ import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
-import org.apache.commons.io.IOUtils;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
+import org.sonar.db.DbInputStream;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
@@ -57,16 +57,16 @@ public class CeTaskInputDao implements Dao {
}
}
- public Optional<DataStream> selectData(DbSession dbSession, String taskUuid) {
+ public Optional<DbInputStream> selectData(DbSession dbSession, String taskUuid) {
PreparedStatement stmt = null;
ResultSet rs = null;
- DataStream result = null;
+ DbInputStream result = null;
try {
stmt = dbSession.getConnection().prepareStatement("SELECT input_data FROM ce_task_input WHERE task_uuid=? AND input_data IS NOT NULL");
stmt.setString(1, taskUuid);
rs = stmt.executeQuery();
if (rs.next()) {
- result = new DataStream(stmt, rs, rs.getBinaryStream(1));
+ result = new DbInputStream(stmt, rs, rs.getBinaryStream(1));
return Optional.of(result);
}
return Optional.empty();
@@ -88,27 +88,4 @@ public class CeTaskInputDao implements Dao {
CeTaskInputMapper mapper = dbSession.getMapper(CeTaskInputMapper.class);
DatabaseUtils.executeLargeUpdates(uuids, mapper::deleteByUuids);
}
-
- public static class DataStream implements AutoCloseable {
- private final PreparedStatement stmt;
- private final ResultSet rs;
- private final InputStream stream;
-
- private DataStream(PreparedStatement stmt, ResultSet rs, InputStream stream) {
- this.stmt = stmt;
- this.rs = rs;
- this.stream = stream;
- }
-
- public InputStream getInputStream() {
- return stream;
- }
-
- @Override
- public void close() {
- IOUtils.closeQuietly(stream);
- DatabaseUtils.closeQuietly(rs);
- DatabaseUtils.closeQuietly(stmt);
- }
- }
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
index 80c818cfb27..c5d24de7b82 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
@@ -485,4 +485,11 @@ class PurgeCommands {
profiler.stop();
}
+ public void deleteScannerCache(String rootUuid) {
+ profiler.start("deleteScannerCache (scanner_cache)");
+ purgeMapper.deleteScannerCacheByBranchUuid(rootUuid);
+ session.commit();
+ profiler.stop();
+ }
+
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
index db1ec59a32a..ddbe7b627df 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
@@ -175,4 +175,5 @@ public interface PurgeMapper {
void deleteUserDismissedMessagesByProjectUuid(@Param("projectUuid") String projectUuid);
+ void deleteScannerCacheByBranchUuid(@Param("branchUuid") String branchUuid);
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheDao.java
new file mode 100644
index 00000000000..717180948d9
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheDao.java
@@ -0,0 +1,82 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.scannercache;
+
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import javax.annotation.CheckForNull;
+import org.sonar.db.Dao;
+import org.sonar.db.DbInputStream;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.db.DbSession;
+
+public class ScannerCacheDao implements Dao {
+ public void removeAll(DbSession session) {
+ mapper(session).removeAll();
+ }
+
+ public void remove(DbSession session, String branchUuid) {
+ mapper(session).remove(branchUuid);
+ }
+
+ public void insert(DbSession dbSession, String branchUuid, InputStream data) {
+ Connection connection = dbSession.getConnection();
+ try (PreparedStatement stmt = connection.prepareStatement(
+ "INSERT INTO scanner_cache (branch_uuid, data) VALUES (?, ?)")) {
+ stmt.setString(1, branchUuid);
+ stmt.setBinaryStream(2, data);
+ stmt.executeUpdate();
+ connection.commit();
+ } catch (SQLException e) {
+ throw new IllegalStateException("Fail to insert cache for branch " + branchUuid, e);
+ }
+ }
+
+ @CheckForNull
+ public DbInputStream selectData(DbSession dbSession, String branchUuid) {
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ DbInputStream result = null;
+ try {
+ stmt = dbSession.getConnection().prepareStatement("SELECT data FROM scanner_cache WHERE branch_uuid=?");
+ stmt.setString(1, branchUuid);
+ rs = stmt.executeQuery();
+ if (rs.next()) {
+ result = new DbInputStream(stmt, rs, rs.getBinaryStream(1));
+ return result;
+ }
+ return null;
+ } catch (SQLException e) {
+ throw new IllegalStateException("Fail to select cache for branch " + branchUuid, e);
+ } finally {
+ if (result == null) {
+ DatabaseUtils.closeQuietly(rs);
+ DatabaseUtils.closeQuietly(stmt);
+ }
+ }
+ }
+
+ private static ScannerCacheMapper mapper(DbSession session) {
+ return session.getMapper(ScannerCacheMapper.class);
+ }
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheMapper.java
new file mode 100644
index 00000000000..200d5eb1a38
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scannercache/ScannerCacheMapper.java
@@ -0,0 +1,28 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.scannercache;
+
+import org.apache.ibatis.annotations.Param;
+
+public interface ScannerCacheMapper {
+ void removeAll();
+
+ void remove(@Param("branchUuid") String branchUuid);
+}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
index 8b21a47bfe7..738bf2e737d 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
@@ -592,5 +592,9 @@
<delete id="deleteUserDismissedMessagesByProjectUuid">
delete from user_dismissed_messages where project_uuid = #{projectUuid,jdbcType=VARCHAR}
</delete>
+
+ <delete id="deleteScannerCacheByBranchUuid">
+ delete from scanner_cache where branch_uuid = #{branchUuid,jdbcType=VARCHAR}
+ </delete>
</mapper>
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerCacheMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerCacheMapper.xml
new file mode 100644
index 00000000000..887f694c8c0
--- /dev/null
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerCacheMapper.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.db.scannercache.ScannerCacheMapper">
+ <delete id="removeAll">
+ delete from scanner_cache
+ </delete>
+
+ <delete id="remove">
+ delete from scanner_cache where branch_uuid = #{branchUuid,jdbcType=VARCHAR}
+ </delete>
+
+</mapper>
+