You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/05/16 08:48:34 UTC

[james-project] 19/23: JAMES-2719 Refactor IndexCreationFactory to an immutable structure

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

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

commit 2d5473fd86aff3ce91fb64ada9e7fc4d5a39864f
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Thu May 16 10:31:26 2019 +0700

    JAMES-2719 Refactor IndexCreationFactory to an immutable structure
---
 .../james/backends/es/v6/IndexCreationFactory.java | 224 ++++++++++++---------
 .../backends/es/v6/IndexCreationFactoryTest.java   |   1 +
 2 files changed, 126 insertions(+), 99 deletions(-)

diff --git a/backends-common/elasticsearch-v6/src/main/java/org/apache/james/backends/es/v6/IndexCreationFactory.java b/backends-common/elasticsearch-v6/src/main/java/org/apache/james/backends/es/v6/IndexCreationFactory.java
index 1a520fa..2cba4ad 100644
--- a/backends-common/elasticsearch-v6/src/main/java/org/apache/james/backends/es/v6/IndexCreationFactory.java
+++ b/backends-common/elasticsearch-v6/src/main/java/org/apache/james/backends/es/v6/IndexCreationFactory.java
@@ -23,7 +23,6 @@ import static org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest
 import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
 
 import java.io.IOException;
-import java.util.ArrayList;
 
 import javax.inject.Inject;
 
@@ -39,125 +38,152 @@ import org.slf4j.LoggerFactory;
 
 import com.github.fge.lambdas.Throwing;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
 
 public class IndexCreationFactory {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(IndexCreationFactory.class);
-    private static final String INDEX_ALREADY_EXISTS_EXCEPTION_MESSAGE = "type=resource_already_exists_exception";
-    private static final String CASE_INSENSITIVE = "case_insensitive";
-    private static final String KEEP_MAIL_AND_URL = "keep_mail_and_url";
-    private static final String SNOWBALL_KEEP_MAIL_AND_URL = "snowball_keep_mail_and_token";
-    private static final String ENGLISH_SNOWBALL = "english_snowball";
+    static class AliasSpecificationStep {
+        private final int nbShards;
+        private final int nbReplica;
+        private final IndexName indexName;
+        private final ImmutableList.Builder<AliasName> aliases;
+
+        AliasSpecificationStep(int nbShards, int nbReplica, IndexName indexName) {
+            this.nbShards = nbShards;
+            this.nbReplica = nbReplica;
+            this.indexName = indexName;
+            this.aliases = ImmutableList.builder();
+        }
 
-    private IndexName indexName;
-    private ArrayList<AliasName> aliases;
-    private int nbShards;
-    private int nbReplica;
+        public AliasSpecificationStep addAlias(AliasName aliasName) {
+            Preconditions.checkNotNull(aliasName);
+            this.aliases.add(aliasName);
+            return this;
+        }
 
-    @Inject
-    public IndexCreationFactory(ElasticSearchConfiguration configuration) {
-        indexName = null;
-        aliases = new ArrayList<>();
-        nbShards = configuration.getNbShards();
-        nbReplica = configuration.getNbReplica();
+        public RestHighLevelClient createIndexAndAliases(RestHighLevelClient client) {
+            return new IndexCreationPerformer(nbShards, nbReplica, indexName, aliases.build()).createIndexAndAliases(client);
+        }
     }
 
-    public IndexCreationFactory useIndex(IndexName indexName) {
-        Preconditions.checkNotNull(indexName);
-        this.indexName = indexName;
-        return this;
-    }
+    static class IndexCreationPerformer {
+        private final int nbShards;
+        private final int nbReplica;
+        private final IndexName indexName;
+        private final ImmutableList<AliasName> aliases;
+
+        public IndexCreationPerformer(int nbShards, int nbReplica, IndexName indexName, ImmutableList<AliasName> aliases) {
+            this.nbShards = nbShards;
+            this.nbReplica = nbReplica;
+            this.indexName = indexName;
+            this.aliases = aliases;
+        }
 
-    public IndexCreationFactory addAlias(AliasName aliasName) {
-        Preconditions.checkNotNull(aliasName);
-        this.aliases.add(aliasName);
-        return this;
-    }
+        public RestHighLevelClient createIndexAndAliases(RestHighLevelClient client) {
+            Preconditions.checkNotNull(indexName);
+            try {
+                createIndexIfNeeded(client, indexName, generateSetting(nbShards, nbReplica));
+                aliases.forEach(Throwing.consumer(alias -> createAliasIfNeeded(client, indexName, alias)));
+            } catch (IOException e) {
+                LOGGER.error("Error while creating index : ", e);
+            }
+            return client;
+        }
 
-    public RestHighLevelClient createIndexAndAliases(RestHighLevelClient client) {
-        Preconditions.checkNotNull(indexName);
-        try {
-            createIndexIfNeeded(client, indexName, generateSetting(nbShards, nbReplica));
-            aliases.forEach(Throwing.consumer(alias -> createAliasIfNeeded(client, indexName, alias)));
-        } catch (IOException e) {
-            LOGGER.error("Error while creating index : ", e);
+        private void createAliasIfNeeded(RestHighLevelClient client, IndexName indexName, AliasName aliasName) throws IOException {
+            if (!aliasExist(client, aliasName)) {
+                client.indices()
+                    .updateAliases(
+                        new IndicesAliasesRequest().addAliasAction(
+                            new AliasActions(AliasActions.Type.ADD)
+                                .index(indexName.getValue())
+                                .alias(aliasName.getValue())),
+                        RequestOptions.DEFAULT);
+            }
         }
-        return client;
-    }
 
-    private void createAliasIfNeeded(RestHighLevelClient client, IndexName indexName, AliasName aliasName) throws IOException {
-        if (!aliasExist(client, aliasName)) {
-            client.indices()
-                .updateAliases(
-                    new IndicesAliasesRequest().addAliasAction(
-                        new AliasActions(AliasActions.Type.ADD)
-                            .index(indexName.getValue())
-                            .alias(aliasName.getValue())),
+        private boolean aliasExist(RestHighLevelClient client, AliasName aliasName) throws IOException {
+            return client.indices()
+                .existsAlias(new GetAliasesRequest().aliases(aliasName.getValue()),
                     RequestOptions.DEFAULT);
         }
-    }
 
-    private boolean aliasExist(RestHighLevelClient client, AliasName aliasName) throws IOException {
-        return client.indices()
-            .existsAlias(new GetAliasesRequest().aliases(aliasName.getValue()),
-                RequestOptions.DEFAULT);
-    }
-
-    private void createIndexIfNeeded(RestHighLevelClient client, IndexName indexName, XContentBuilder settings) throws IOException {
-        try {
-            client.indices()
-                .create(
-                    new CreateIndexRequest(indexName.getValue())
-                        .source(settings),
-                    RequestOptions.DEFAULT);
-        } catch (ElasticsearchStatusException exception) {
-            if (exception.getMessage().contains(INDEX_ALREADY_EXISTS_EXCEPTION_MESSAGE)) {
-                LOGGER.info("Index [{}] already exist", indexName);
-            } else {
-                throw exception;
+        private void createIndexIfNeeded(RestHighLevelClient client, IndexName indexName, XContentBuilder settings) throws IOException {
+            try {
+                client.indices()
+                    .create(
+                        new CreateIndexRequest(indexName.getValue())
+                            .source(settings),
+                        RequestOptions.DEFAULT);
+            } catch (ElasticsearchStatusException exception) {
+                if (exception.getMessage().contains(INDEX_ALREADY_EXISTS_EXCEPTION_MESSAGE)) {
+                    LOGGER.info("Index [{}] already exist", indexName);
+                } else {
+                    throw exception;
+                }
             }
         }
-    }
 
-    private XContentBuilder generateSetting(int nbShards, int nbReplica) throws IOException {
-        return jsonBuilder()
-            .startObject()
-                .startObject("settings")
-                    .field("number_of_shards", nbShards)
-                    .field("number_of_replicas", nbReplica)
-                    .startObject("analysis")
-                        .startObject("analyzer")
-                            .startObject(CASE_INSENSITIVE)
-                                .field("tokenizer", "keyword")
-                                .startArray("filter")
-                                    .value("lowercase")
-                                .endArray()
+        private XContentBuilder generateSetting(int nbShards, int nbReplica) throws IOException {
+            return jsonBuilder()
+                .startObject()
+                    .startObject("settings")
+                        .field("number_of_shards", nbShards)
+                        .field("number_of_replicas", nbReplica)
+                        .startObject("analysis")
+                            .startObject("analyzer")
+                                .startObject(CASE_INSENSITIVE)
+                                    .field("tokenizer", "keyword")
+                                    .startArray("filter")
+                                        .value("lowercase")
+                                    .endArray()
+                                .endObject()
+                                .startObject(KEEP_MAIL_AND_URL)
+                                    .field("tokenizer", "uax_url_email")
+                                    .startArray("filter")
+                                        .value("lowercase")
+                                        .value("stop")
+                                    .endArray()
+                                .endObject()
+                                .startObject(SNOWBALL_KEEP_MAIL_AND_URL)
+                                    .field("tokenizer", "uax_url_email")
+                                    .startArray("filter")
+                                        .value("lowercase")
+                                        .value("stop")
+                                        .value(ENGLISH_SNOWBALL)
+                                    .endArray()
+                                .endObject()
                             .endObject()
-                            .startObject(KEEP_MAIL_AND_URL)
-                                .field("tokenizer", "uax_url_email")
-                                .startArray("filter")
-                                    .value("lowercase")
-                                    .value("stop")
-                                .endArray()
-                            .endObject()
-                            .startObject(SNOWBALL_KEEP_MAIL_AND_URL)
-                                .field("tokenizer", "uax_url_email")
-                                .startArray("filter")
-                                    .value("lowercase")
-                                    .value("stop")
-                                    .value(ENGLISH_SNOWBALL)
-                                .endArray()
-                            .endObject()
-                        .endObject()
-                        .startObject("filter")
-                            .startObject(ENGLISH_SNOWBALL)
-                                .field("type", "snowball")
-                                .field("language", "English")
+                            .startObject("filter")
+                                .startObject(ENGLISH_SNOWBALL)
+                                    .field("type", "snowball")
+                                    .field("language", "English")
+                                .endObject()
                             .endObject()
                         .endObject()
                     .endObject()
-                .endObject()
-            .endObject();
+                .endObject();
+        }
     }
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(IndexCreationFactory.class);
+    private static final String INDEX_ALREADY_EXISTS_EXCEPTION_MESSAGE = "type=resource_already_exists_exception";
+    private static final String CASE_INSENSITIVE = "case_insensitive";
+    private static final String KEEP_MAIL_AND_URL = "keep_mail_and_url";
+    private static final String SNOWBALL_KEEP_MAIL_AND_URL = "snowball_keep_mail_and_token";
+    private static final String ENGLISH_SNOWBALL = "english_snowball";
+
+    private final int nbShards;
+    private final int nbReplica;
+
+    @Inject
+    public IndexCreationFactory(ElasticSearchConfiguration configuration) {
+        this.nbShards = configuration.getNbShards();
+        this.nbReplica = configuration.getNbReplica();
+    }
+
+    public AliasSpecificationStep useIndex(IndexName indexName) {
+        Preconditions.checkNotNull(indexName);
+        return new AliasSpecificationStep(nbShards, nbReplica, indexName);
+    }
 }
diff --git a/backends-common/elasticsearch-v6/src/test/java/org/apache/james/backends/es/v6/IndexCreationFactoryTest.java b/backends-common/elasticsearch-v6/src/test/java/org/apache/james/backends/es/v6/IndexCreationFactoryTest.java
index 4f1efd3..49be066 100644
--- a/backends-common/elasticsearch-v6/src/test/java/org/apache/james/backends/es/v6/IndexCreationFactoryTest.java
+++ b/backends-common/elasticsearch-v6/src/test/java/org/apache/james/backends/es/v6/IndexCreationFactoryTest.java
@@ -62,6 +62,7 @@ public class IndexCreationFactoryTest {
     public void addAliasShouldThrowWhenNull() {
         assertThatThrownBy(() ->
             new IndexCreationFactory(ElasticSearchConfiguration.DEFAULT_CONFIGURATION)
+                .useIndex(INDEX_NAME)
                 .addAlias(null))
             .isInstanceOf(NullPointerException.class);
     }


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