Repository: james-project Updated Branches: refs/heads/master 02d3f17ac -> 1d073e4c0
JAMES-2541 Introduce a naive memory blob store implementation Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/a207098d Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/a207098d Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/a207098d Branch: refs/heads/master Commit: a207098dbc3233a2fb6999be68a8e2fdd0749e5c Parents: 02d3f17 Author: Benoit Tellier <[email protected]> Authored: Thu Sep 6 13:14:03 2018 +0700 Committer: Benoit Tellier <[email protected]> Committed: Mon Sep 10 17:17:40 2018 +0700 ---------------------------------------------------------------------- server/blob/blob-memory/pom.xml | 66 +++++++++++++++++ .../james/blob/memory/MemoryBlobStore.java | 77 ++++++++++++++++++++ .../james/blob/memory/MemoryBlobStoreTest.java | 47 ++++++++++++ server/blob/pom.xml | 1 + 4 files changed, 191 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/a207098d/server/blob/blob-memory/pom.xml ---------------------------------------------------------------------- diff --git a/server/blob/blob-memory/pom.xml b/server/blob/blob-memory/pom.xml new file mode 100644 index 0000000..ecc47f3 --- /dev/null +++ b/server/blob/blob-memory/pom.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>james-server-blob</artifactId> + <groupId>org.apache.james</groupId> + <version>3.2.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>blob-memory</artifactId> + + <name>Apache James :: Server :: Blob :: Memory</name> + + <dependencies> + <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>blob-api</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>blob-api</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/a207098d/server/blob/blob-memory/src/main/java/org/apache/james/blob/memory/MemoryBlobStore.java ---------------------------------------------------------------------- diff --git a/server/blob/blob-memory/src/main/java/org/apache/james/blob/memory/MemoryBlobStore.java b/server/blob/blob-memory/src/main/java/org/apache/james/blob/memory/MemoryBlobStore.java new file mode 100644 index 0000000..ec3be33 --- /dev/null +++ b/server/blob/blob-memory/src/main/java/org/apache/james/blob/memory/MemoryBlobStore.java @@ -0,0 +1,77 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.blob.memory; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.commons.io.IOUtils; +import org.apache.james.blob.api.BlobId; +import org.apache.james.blob.api.BlobStore; + +import com.google.common.base.Preconditions; + +public class MemoryBlobStore implements BlobStore { + private final ConcurrentHashMap<BlobId, byte[]> blobs; + private final BlobId.Factory factory; + + public MemoryBlobStore(BlobId.Factory factory) { + this.factory = factory; + blobs = new ConcurrentHashMap<>(); + } + + @Override + public CompletableFuture<BlobId> save(byte[] data) { + Preconditions.checkNotNull(data); + BlobId blobId = factory.forPayload(data); + + blobs.put(blobId, data); + + return CompletableFuture.completedFuture(blobId); + } + + @Override + public CompletableFuture<BlobId> save(InputStream data) { + Preconditions.checkNotNull(data); + try { + byte[] bytes = IOUtils.toByteArray(data); + return save(bytes); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public CompletableFuture<byte[]> readBytes(BlobId blobId) { + return CompletableFuture.completedFuture(retrieveStoredValue(blobId)); + } + + @Override + public InputStream read(BlobId blobId) { + return new ByteArrayInputStream(retrieveStoredValue(blobId)); + } + + private byte[] retrieveStoredValue(BlobId blobId) { + return blobs.getOrDefault(blobId, new byte[]{}); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a207098d/server/blob/blob-memory/src/test/java/org/apache/james/blob/memory/MemoryBlobStoreTest.java ---------------------------------------------------------------------- diff --git a/server/blob/blob-memory/src/test/java/org/apache/james/blob/memory/MemoryBlobStoreTest.java b/server/blob/blob-memory/src/test/java/org/apache/james/blob/memory/MemoryBlobStoreTest.java new file mode 100644 index 0000000..098506a --- /dev/null +++ b/server/blob/blob-memory/src/test/java/org/apache/james/blob/memory/MemoryBlobStoreTest.java @@ -0,0 +1,47 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.blob.memory; + +import org.apache.james.blob.api.BlobId; +import org.apache.james.blob.api.BlobStore; +import org.apache.james.blob.api.BlobStoreContract; +import org.apache.james.blob.api.HashBlobId; +import org.junit.jupiter.api.BeforeEach; + +public class MemoryBlobStoreTest implements BlobStoreContract { + + private static final HashBlobId.Factory BLOB_ID_FACTORY = new HashBlobId.Factory(); + private MemoryBlobStore memoryBlobStore; + + @BeforeEach + void setUp() { + memoryBlobStore = new MemoryBlobStore(BLOB_ID_FACTORY); + } + + @Override + public BlobStore testee() { + return memoryBlobStore; + } + + @Override + public BlobId.Factory blobIdFactory() { + return BLOB_ID_FACTORY; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a207098d/server/blob/pom.xml ---------------------------------------------------------------------- diff --git a/server/blob/pom.xml b/server/blob/pom.xml index 05942ef..ad1754d 100644 --- a/server/blob/pom.xml +++ b/server/blob/pom.xml @@ -35,6 +35,7 @@ <modules> <module>blob-api</module> <module>blob-cassandra</module> + <module>blob-memory</module> <module>blob-objectstorage</module> </modules> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
