You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by to...@apache.org on 2014/12/03 23:29:11 UTC

[04/22] incubator-usergrid git commit: add index naming util

add index naming util


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/b16c728f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/b16c728f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/b16c728f

Branch: refs/heads/two-dot-o
Commit: b16c728f0526c0ac7730e4c2c35cd47dd64fcd6b
Parents: 3ff891e
Author: Shawn Feldman <sf...@apache.org>
Authored: Fri Nov 21 15:47:52 2014 -0700
Committer: Shawn Feldman <sf...@apache.org>
Committed: Fri Nov 21 15:47:52 2014 -0700

----------------------------------------------------------------------
 .../persistence/index/IndexIdentifier.java      | 58 ++++++++++++++++++++
 .../index/impl/EsEntityIndexBatchImpl.java      |  6 +-
 .../index/impl/EsEntityIndexImpl.java           | 19 +++----
 .../persistence/index/impl/IndexingUtils.java   | 25 +++------
 4 files changed, 77 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b16c728f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java
new file mode 100644
index 0000000..13af8eb
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java
@@ -0,0 +1,58 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  *  contributor license agreements.  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.  For additional information regarding
+ *  * copyright in this work, please see the NOTICE file in the top level
+ *  * directory of this distribution.
+ *
+ */
+
+package org.apache.usergrid.persistence.index;
+
+import org.apache.usergrid.persistence.core.scope.ApplicationScope;
+import org.apache.usergrid.persistence.index.impl.IndexingUtils;
+
+/**
+ * Class is used to generate an index name and alias name
+ */
+public class IndexIdentifier{
+    private final IndexFig config;
+    private final ApplicationScope applicationScope;
+
+    public IndexIdentifier(IndexFig config, ApplicationScope applicationScope) {
+        this.config = config;
+        this.applicationScope = applicationScope;
+    }
+
+    public String getAlias() {
+        return getIndexBase() + config.getAliasPostfix();
+    }
+
+    public String getIndex(int i) {
+        if (i > 0) {
+            return getIndexBase() + "_v" + (i + 1);
+        } else {
+            return getIndexBase();
+        }
+    }
+
+    String getIndexBase() {
+        StringBuilder sb = new StringBuilder();
+        sb.append(config.getIndexPrefix()).append(IndexingUtils.SEPARATOR);
+        IndexingUtils.idString(sb, applicationScope.getApplication());
+        return sb.toString();
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b16c728f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexBatchImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexBatchImpl.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexBatchImpl.java
index da1e930..9589e66 100644
--- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexBatchImpl.java
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexBatchImpl.java
@@ -26,6 +26,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
+import org.apache.usergrid.persistence.index.IndexIdentifier;
 import org.elasticsearch.action.bulk.BulkItemResponse;
 import org.elasticsearch.action.bulk.BulkRequestBuilder;
 import org.elasticsearch.action.bulk.BulkResponse;
@@ -67,7 +68,6 @@ import static org.apache.usergrid.persistence.index.impl.IndexingUtils.GEO_PREFI
 import static org.apache.usergrid.persistence.index.impl.IndexingUtils.NUMBER_PREFIX;
 import static org.apache.usergrid.persistence.index.impl.IndexingUtils.STRING_PREFIX;
 import static org.apache.usergrid.persistence.index.impl.IndexingUtils.createIndexDocId;
-import static org.apache.usergrid.persistence.index.impl.IndexingUtils.createIndexName;
 import static org.apache.usergrid.persistence.index.impl.IndexingUtils.createContextName;
 
 
@@ -82,6 +82,7 @@ public class EsEntityIndexBatchImpl implements EntityIndexBatch {
     private final boolean refresh;
 
     private final String aliasName;
+    private final IndexIdentifier indexIdentifier;
 
     private BulkRequestBuilder bulkRequest;
 
@@ -98,7 +99,8 @@ public class EsEntityIndexBatchImpl implements EntityIndexBatch {
         this.applicationScope = applicationScope;
         this.client = client;
         this.failureMonitor = failureMonitor;
-        this.aliasName = IndexingUtils.createAliasName( config, applicationScope );
+        this.indexIdentifier = IndexingUtils.createIndexIdentifier(config, applicationScope);
+        this.aliasName = indexIdentifier.getAlias();
         this.refresh = config.isForcedRefresh();
         this.autoFlushSize = autoFlushSize;
         initBatch();

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b16c728f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java
index 5aa9123..baa3551 100644
--- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java
@@ -25,6 +25,7 @@ import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.usergrid.persistence.index.*;
 import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
 import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
 import org.elasticsearch.action.admin.cluster.tasks.PendingClusterTasksRequest;
@@ -65,11 +66,6 @@ import org.slf4j.LoggerFactory;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
 import org.apache.usergrid.persistence.core.util.Health;
 import org.apache.usergrid.persistence.core.util.ValidationUtils;
-import org.apache.usergrid.persistence.index.EntityIndex;
-import org.apache.usergrid.persistence.index.EntityIndexBatch;
-import org.apache.usergrid.persistence.index.IndexFig;
-import org.apache.usergrid.persistence.index.IndexScope;
-import org.apache.usergrid.persistence.index.SearchTypes;
 import org.apache.usergrid.persistence.index.exceptions.IndexException;
 import org.apache.usergrid.persistence.index.query.CandidateResult;
 import org.apache.usergrid.persistence.index.query.CandidateResults;
@@ -98,6 +94,7 @@ public class EsEntityIndexImpl implements EntityIndex {
     private static final AtomicBoolean mappingsCreated = new AtomicBoolean( false );
 
     private final String aliasName;
+    private final IndexIdentifier indexIdentifier;
 
     /**
      * We purposefully make this per instance. Some indexes may work, while others may fail
@@ -136,7 +133,8 @@ public class EsEntityIndexImpl implements EntityIndex {
         this.esProvider = provider;
         this.config = config;
         this.cursorTimeout = config.getQueryCursorTimeout();
-        this.aliasName = IndexingUtils.createAliasName(config,appScope);
+        this.indexIdentifier = IndexingUtils.createIndexIdentifier(config,appScope);
+        this.aliasName = indexIdentifier.getAlias();
         this.failureMonitor = new FailureMonitorImpl( config, provider );
     }
 
@@ -177,8 +175,7 @@ public class EsEntityIndexImpl implements EntityIndex {
     }
 
     private void createIndexAndAlias(AdminClient admin, Settings settings) {
-        String indexName = IndexingUtils.createIndexBaseName(config.getIndexPrefix(), applicationScope);
-        String indexVersionName =  IndexingUtils.createIndexName(indexName, 0);
+        String indexVersionName =  indexIdentifier.getIndex(0);
         final CreateIndexResponse cir = admin.indices().prepareCreate( indexVersionName ).setSettings( settings ).execute().actionGet();
         //check if alias exists and get the alias
         admin.indices().prepareAliases().addAlias(indexVersionName,aliasName).execute().actionGet();
@@ -231,9 +228,9 @@ public class EsEntityIndexImpl implements EntityIndex {
                 XContentFactory.jsonBuilder(), "_default_" );
 
         PutIndexTemplateResponse pitr = esProvider.getClient().admin().indices()
-                .preparePutTemplate( "usergrid_template" )
+                .preparePutTemplate("usergrid_template")
                 // set mapping as the default for all types
-                .setTemplate( config.getIndexPrefix() + "*" ).addMapping( "_default_", xcb ) 
+                .setTemplate(config.getIndexPrefix() + "*").addMapping( "_default_", xcb )
                 .execute().actionGet();
 
         if(!pitr.isAcknowledged()){
@@ -346,7 +343,7 @@ public class EsEntityIndexImpl implements EntityIndex {
             logger.debug( "Executing query with cursor: {} ", scrollId );
 
             SearchScrollRequestBuilder ssrb = esProvider.getClient()
-                    .prepareSearchScroll( scrollId ).setScroll( cursorTimeout + "m" );
+                    .prepareSearchScroll(scrollId).setScroll( cursorTimeout + "m" );
 
             try {
                 searchResponse = ssrb.execute().actionGet();

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b16c728f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/IndexingUtils.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/IndexingUtils.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/IndexingUtils.java
index 315a043..b3ce7f8 100644
--- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/IndexingUtils.java
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/IndexingUtils.java
@@ -23,6 +23,7 @@ import java.util.UUID;
 
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
 import org.apache.usergrid.persistence.index.IndexFig;
+import org.apache.usergrid.persistence.index.IndexIdentifier;
 import org.apache.usergrid.persistence.index.IndexScope;
 import org.apache.usergrid.persistence.model.entity.Entity;
 import org.apache.usergrid.persistence.model.entity.Id;
@@ -90,30 +91,18 @@ public class IndexingUtils {
 
 
     /**
-     * Create the index name based on our prefix+appUUID+AppType
-     * @param prefix
+     * Create the facilities to retrieve an index name and alias name
+     * @param fig
      * @param applicationScope
      * @return
      */
-    public static String createIndexBaseName(String prefix, ApplicationScope applicationScope) {
-        StringBuilder sb = new StringBuilder();
-        sb.append( prefix ).append( SEPARATOR );
-        idString( sb, applicationScope.getApplication() );
-        return sb.toString();
+    public static IndexIdentifier createIndexIdentifier(IndexFig fig, ApplicationScope applicationScope) {
+        return new IndexIdentifier(fig,applicationScope);
     }
 
-    public static String createIndexName(String indexName, int i) {
-        if(i>0) {
-            return indexName + "_v" + (i + 1);
-        }else{
-            return indexName;
-        }
-    }
 
-    public static String createAliasName( IndexFig config,ApplicationScope applicationScope) {
-        String indexName =IndexingUtils.createIndexBaseName(config.getIndexPrefix(), applicationScope);
-        return indexName + config.getAliasPostfix();
-    }
+
+
 
     /**
      * Create the index doc from the given entity