james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [03/17] james-project git commit: JAMES-2426 Zipper should take a Stream to avoid having memory consumption issues
Date Wed, 27 Jun 2018 14:44:25 GMT
JAMES-2426 Zipper should take a Stream to avoid having memory consumption issues


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/73bf8a52
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/73bf8a52
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/73bf8a52

Branch: refs/heads/master
Commit: 73bf8a52b7ddc889f3fe144f4eab3df7b6489247
Parents: 9be830a
Author: Matthieu Baechler <matthieu@apache.org>
Authored: Mon Jun 18 11:58:59 2018 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Wed Jun 27 16:36:12 2018 +0200

----------------------------------------------------------------------
 .../java/org/apache/james/mailbox/backup/Backup.java |  8 ++++++--
 .../java/org/apache/james/mailbox/backup/Zipper.java | 10 ++++++----
 .../org/apache/james/mailbox/backup/ZipperTest.java  | 15 +++++++--------
 3 files changed, 19 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/73bf8a52/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Backup.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Backup.java b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Backup.java
index 80f246f..1a8d42a 100644
--- a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Backup.java
+++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Backup.java
@@ -20,11 +20,15 @@ package org.apache.james.mailbox.backup;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.List;
+import java.util.stream.Stream;
 
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
 public interface Backup {
 
-    void archive(List<MailboxMessage> messages, OutputStream destination) throws IOException;
+    /**
+     * @param messages a stream of MailboxMessages that will be consumed
+     * @param destination an OutputStream in which the zip will be written
+     */
+    void archive(Stream<MailboxMessage> messages, OutputStream destination) throws
IOException;
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/73bf8a52/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java
index e4c2518..9be38a5 100644
--- a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java
+++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java
@@ -21,7 +21,7 @@ package org.apache.james.mailbox.backup;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.List;
+import java.util.stream.Stream;
 
 import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -29,17 +29,19 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 import org.apache.commons.io.IOUtils;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
+import com.github.fge.lambdas.Throwing;
+
 public class Zipper implements Backup {
     public Zipper() {
         ExtraFieldUtils.register(SizeExtraField.class);
     }
 
     @Override
-    public void archive(List<MailboxMessage> messages, OutputStream destination) throws
IOException {
+    public void archive(Stream<MailboxMessage> messages, OutputStream destination)
throws IOException {
         try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination))
{
-            for (MailboxMessage message: messages) {
+            messages.forEach(Throwing.<MailboxMessage>consumer(message -> {
                 storeInArchive(message, archiveOutputStream);
-            }
+            }).sneakyThrow());
             archiveOutputStream.finish();
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/73bf8a52/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
index f475440..a2d9e6f 100644
--- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
@@ -30,6 +30,7 @@ import static org.apache.james.mailbox.backup.ZipAssert.assertThatZip;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.util.stream.Stream;
 
 import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.apache.james.junit.TemporaryFolderExtension;
@@ -38,8 +39,6 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 
-import com.google.common.collect.ImmutableList;
-
 @ExtendWith(TemporaryFolderExtension.class)
 public class ZipperTest {
     private Zipper testee;
@@ -53,7 +52,7 @@ public class ZipperTest {
 
     @Test
     void archiveShouldWriteEmptyValidArchiveWhenNoMessage() throws Exception {
-        testee.archive(ImmutableList.of(), new FileOutputStream(destination));
+        testee.archive(Stream.of(), new FileOutputStream(destination));
 
         try (ZipFile zipFile = new ZipFile(destination)) {
             assertThatZip(zipFile).hasNoEntry();
@@ -62,7 +61,7 @@ public class ZipperTest {
 
     @Test
     void archiveShouldWriteOneMessageWhenOne() throws Exception {
-        testee.archive(ImmutableList.of(MESSAGE_1), new FileOutputStream(destination));
+        testee.archive(Stream.of(MESSAGE_1), new FileOutputStream(destination));
 
         try (ZipFile zipFile = new ZipFile(destination)) {
             assertThatZip(zipFile)
@@ -74,7 +73,7 @@ public class ZipperTest {
 
     @Test
     void archiveShouldWriteTwoMessagesWhenTwo() throws Exception {
-        testee.archive(ImmutableList.of(MESSAGE_1, MESSAGE_2), new FileOutputStream(destination));
+        testee.archive(Stream.of(MESSAGE_1, MESSAGE_2), new FileOutputStream(destination));
 
         try (ZipFile zipFile = new ZipFile(destination)) {
             assertThatZip(zipFile)
@@ -88,8 +87,8 @@ public class ZipperTest {
 
     @Test
     void archiveShouldOverwriteContent() throws Exception {
-        testee.archive(ImmutableList.of(MESSAGE_1), new FileOutputStream(destination));
-        testee.archive(ImmutableList.of(MESSAGE_2), new FileOutputStream(destination));
+        testee.archive(Stream.of(MESSAGE_1), new FileOutputStream(destination));
+        testee.archive(Stream.of(MESSAGE_2), new FileOutputStream(destination));
 
         try (ZipFile zipFile = new ZipFile(destination)) {
             assertThatZip(zipFile)
@@ -101,7 +100,7 @@ public class ZipperTest {
 
     @Test
     void archiveShouldWriteSizeMetadata() throws Exception {
-        testee.archive(ImmutableList.of(MESSAGE_1), new FileOutputStream(destination));
+        testee.archive(Stream.of(MESSAGE_1), new FileOutputStream(destination));
 
         try (ZipFile zipFile = new ZipFile(destination)) {
             assertThatZip(zipFile)


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message