This is an automated email from the ASF dual-hosted git repository.

matthieu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 92da31b306bdc92e483e6b95043bfc02e7c331cd
Author: Gautier DI FOLCO <gdifo...@linagora.com>
AuthorDate: Fri Jul 5 11:51:08 2019 +0200

    JAMES-2815 Store directly small blobs in ObjectStore
---
 .../blob/objectstorage/ObjectStorageBlobsDAO.java  | 28 +++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git 
a/server/blob/blob-objectstorage/src/main/java/org/apache/james/blob/objectstorage/ObjectStorageBlobsDAO.java
 
b/server/blob/blob-objectstorage/src/main/java/org/apache/james/blob/objectstorage/ObjectStorageBlobsDAO.java
index 394529d..94f63fe 100644
--- 
a/server/blob/blob-objectstorage/src/main/java/org/apache/james/blob/objectstorage/ObjectStorageBlobsDAO.java
+++ 
b/server/blob/blob-objectstorage/src/main/java/org/apache/james/blob/objectstorage/ObjectStorageBlobsDAO.java
@@ -19,6 +19,7 @@
 
 package org.apache.james.blob.objectstorage;
 
+import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
@@ -49,7 +50,7 @@ import reactor.core.scheduler.Schedulers;
 public class ObjectStorageBlobsDAO implements BlobStore {
     private static final Location DEFAULT_LOCATION = null;
     private static final Logger LOGGER = 
LoggerFactory.getLogger(ObjectStorageBlobsDAO.class);
-
+    private static final int BUFFERED_SIZE = 256 * 1024;
 
     private final BlobId.Factory blobIdFactory;
 
@@ -111,6 +112,31 @@ public class ObjectStorageBlobsDAO implements BlobStore {
     public Mono<BlobId> save(BucketName bucketName, InputStream data) {
         Preconditions.checkNotNull(data);
 
+        return Mono.defer(() -> savingStrategySelection(bucketName, data));
+    }
+
+    private Mono<BlobId> savingStrategySelection(BucketName bucketName, 
InputStream data) {
+        InputStream bufferedData = new BufferedInputStream(data, BUFFERED_SIZE 
+ 1);
+        try {
+            if (isItABigStream(bufferedData)) {
+                return saveBigStream(bucketName, bufferedData);
+            } else {
+                return save(bucketName, IOUtils.toByteArray(bufferedData));
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private boolean isItABigStream(InputStream bufferedData) throws 
IOException {
+        bufferedData.mark(0);
+        bufferedData.skip(BUFFERED_SIZE);
+        boolean isItABigStream = bufferedData.read() != -1;
+        bufferedData.reset();
+        return isItABigStream;
+    }
+
+    private Mono<BlobId> saveBigStream(BucketName bucketName, InputStream 
data) {
         BlobId tmpId = blobIdFactory.randomId();
         HashingInputStream hashingInputStream = new 
HashingInputStream(Hashing.sha256(), data);
         Payload payload = payloadCodec.write(hashingInputStream);


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

Reply via email to