diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2023-01-06 17:42:28 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-01-12 20:02:51 +0000 |
commit | 8b2213ef7a6777709d981a83c4d875951f225cc5 (patch) | |
tree | f32d286387e7919c2721e259a2380754a9041040 /sonar-core | |
parent | 62bfeeb68576a0a81427e12d9168dad3cec18f5b (diff) | |
download | sonarqube-8b2213ef7a6777709d981a83c4d875951f225cc5.tar.gz sonarqube-8b2213ef7a6777709d981a83c4d875951f225cc5.zip |
SONAR-18174 Analyzer cache should be kept in the file system to decrease memory use
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/util/Protobuf.java | 26 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java | 20 |
2 files changed, 40 insertions, 6 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/Protobuf.java b/sonar-core/src/main/java/org/sonar/core/util/Protobuf.java index f6cb9afc064..5278080f211 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/Protobuf.java +++ b/sonar-core/src/main/java/org/sonar/core/util/Protobuf.java @@ -29,6 +29,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.io.IOUtils; @@ -136,9 +137,8 @@ public class Protobuf { } /** - * Reads a stream of messages. This method returns an empty iterator if there are no messages. An - * exception is raised on IO error, if file does not exist or if messages have a - * different type than {@code parser}. + * Reads a stream of messages. This method returns an empty iterator if there are no messages. An + * exception is raised on IO error, if file does not exist or if messages have a different type than {@code parser}. */ public static <MSG extends Message> CloseableIterator<MSG> readStream(File file, Parser<MSG> parser) { try { @@ -151,11 +151,25 @@ public class Protobuf { } /** - * Reads a stream of messages. This method returns an empty iterator if there are no messages. An + * Reads a stream of messages from a gzip file. This method returns an empty iterator if there are no messages. An + * exception is raised on IO error, if file does not exist or if messages have a different type than {@code parser}. + */ + public static <MSG extends Message> CloseableIterator<MSG> readGzipStream(File file, Parser<MSG> parser) { + try { + // the input stream is closed by the CloseableIterator + InputStream input = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))); + return readStream(input, parser); + } catch (Exception e) { + throw ContextException.of("Unable to read messages", e).addContext("file", file); + } + } + + /** + * Reads a stream of messages. This method returns an empty iterator if there are no messages. An * exception is raised on IO error or if messages have a different type than {@code parser}. * <p> - * The stream is not closed by this method. It is closed when {@link CloseableIterator} traverses - * all messages or when {@link CloseableIterator#close()} is called. + * The stream is not closed by this method. It is closed when {@link CloseableIterator} traverses + * all messages or when {@link CloseableIterator#close()} is called. * </p> */ public static <MSG extends Message> CloseableIterator<MSG> readStream(InputStream input, Parser<MSG> parser) { diff --git a/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java b/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java index d1e333a2627..5ed4ca69e18 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java @@ -21,9 +21,12 @@ package org.sonar.core.util; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; @@ -112,6 +115,23 @@ public class ProtobufTest { } @Test + public void read_gzip_stream() throws IOException { + File file = temp.newFile(); + + Fake item1 = Fake.newBuilder().setLabel("one").setLine(1).build(); + Fake item2 = Fake.newBuilder().setLabel("two").setLine(2).build(); + + try (OutputStream os = new GZIPOutputStream(new FileOutputStream(file))) { + item1.writeDelimitedTo(os); + item2.writeDelimitedTo(os); + } + + Iterable<Fake> it = () -> Protobuf.readGzipStream(file, Fake.parser()); + + assertThat(it).containsExactly(item1, item2); + } + + @Test public void fail_to_read_stream() { assertThatThrownBy(() -> { File dir = temp.newFolder(); |