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 2015/06/29 10:35:15 UTC

svn commit: r1688131 - in /james/mailbox/trunk/elasticsearch/src: main/java/org/apache/james/mailbox/elasticsearch/IndexCreationFactory.java test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java

Author: btellier
Date: Mon Jun 29 08:35:14 2015
New Revision: 1688131

URL: http://svn.apache.org/r1688131
Log:
MAILBOX-155 Adding Index creation and destruction in tests

Added:
    james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/IndexCreationFactory.java
Modified:
    james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java

Added: james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/IndexCreationFactory.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/IndexCreationFactory.java?rev=1688131&view=auto
==============================================================================
--- james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/IndexCreationFactory.java (added)
+++ james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/IndexCreationFactory.java Mon Jun 29 08:35:14 2015
@@ -0,0 +1,90 @@
+/****************************************************************
+ * 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.mailbox.elasticsearch;
+
+import org.elasticsearch.client.Client;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.indices.IndexAlreadyExistsException;
+import org.elasticsearch.node.Node;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
+
+public class IndexCreationFactory {
+
+    private static Logger LOGGER = LoggerFactory.getLogger(IndexCreationFactory.class);
+
+    public static Node createIndex(Node node, int nbShards, int nbReplica) {
+        try {
+            return createIndex(node, normalSettings(nbShards, nbReplica));
+        } catch (IOException e) {
+            LOGGER.error("Error while creating index : ", e);
+            return node;
+        }
+    }
+
+    public static Node createIndex(Node node) {
+        try {
+            return createIndex(node, settingForInMemory());
+        } catch (IOException e) {
+            LOGGER.error("Error while creating index : ", e);
+            return node;
+        }
+    }
+
+    private static Node createIndex(Node node, XContentBuilder settings) {
+        try {
+            try (Client client = node.client()) {
+                client.admin()
+                    .indices()
+                    .prepareCreate(ElasticSearchIndexer.MAILBOX_INDEX)
+                    .setSettings(settings)
+                    .execute()
+                    .actionGet();
+            }
+        } catch (IndexAlreadyExistsException exception) {
+            LOGGER.info("Index [" + ElasticSearchIndexer.MAILBOX_INDEX + "] already exist");
+        }
+        return node;
+    }
+
+    public static XContentBuilder settingForInMemory() throws IOException {
+        return generateSetting(1, 0, Optional.of(jsonBuilder().startObject().field("type", "memory").endObject()));
+    }
+
+    public static XContentBuilder normalSettings(int nbShards, int nbReplica) throws IOException{
+        return generateSetting(nbShards, nbReplica, Optional.empty());
+    }
+
+    private static XContentBuilder generateSetting(int nbShards, int nbReplica, Optional<XContentBuilder> store) throws IOException {
+        XContentBuilder contentBuilder = jsonBuilder().startObject()
+            .field("number_of_shards", nbShards)
+            .field("number_of_replicas", nbReplica);
+        if (store.isPresent()) {
+            contentBuilder.field("store", store.get());
+        }
+        return contentBuilder.endObject();
+    }
+
+}

Modified: james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java?rev=1688131&r1=1688130&r2=1688131&view=diff
==============================================================================
--- james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java (original)
+++ james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java Mon Jun 29 08:35:14 2015
@@ -26,10 +26,10 @@ import static org.elasticsearch.node.Nod
 
 import java.io.IOException;
 
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
 import org.elasticsearch.action.admin.indices.flush.FlushRequestBuilder;
 import org.elasticsearch.client.Client;
 import org.elasticsearch.common.settings.ImmutableSettings;
-import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.node.Node;
 import org.junit.rules.TemporaryFolder;
 import org.slf4j.Logger;
@@ -55,9 +55,11 @@ public class EmbeddedElasticSearch {
     public static void shutDown(Node node) {
         EmbeddedElasticSearch.awaitForElasticSearch(node);
         try (Client client = node.client()) {
-            client.prepareDeleteByQuery(ElasticSearchIndexer.MAILBOX_INDEX)
-                .setQuery(QueryBuilders.matchAllQuery())
-                .get();
+            node.client()
+                .admin()
+                .indices()
+                .delete(new DeleteIndexRequest(ElasticSearchIndexer.MAILBOX_INDEX))
+                .actionGet();
         } catch (Exception e) {
             LOGGER.warn("Error while closing ES connection", e);
         }



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