-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: petar.dzepina <[email protected]>
- Loading branch information
Showing
4 changed files
with
159 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...n/kotlin/org/opensearch/indexmanagement/rollup/util/RollupFieldValueExpressionResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.indexmanagement.rollup.util | ||
|
||
import org.opensearch.cluster.metadata.IndexAbstraction | ||
import org.opensearch.cluster.metadata.IndexMetadata | ||
import org.opensearch.cluster.service.ClusterService | ||
import org.opensearch.common.xcontent.XContentFactory | ||
import org.opensearch.indexmanagement.indexstatemanagement.util.XCONTENT_WITHOUT_TYPE | ||
import org.opensearch.indexmanagement.opensearchapi.toMap | ||
import org.opensearch.indexmanagement.rollup.model.Rollup | ||
import org.opensearch.script.Script | ||
import org.opensearch.script.ScriptService | ||
import org.opensearch.script.ScriptType | ||
import org.opensearch.script.TemplateScript | ||
|
||
object RollupFieldValueExpressionResolver { | ||
|
||
private val validTopContextFields = setOf(Rollup.SOURCE_INDEX_FIELD) | ||
|
||
private lateinit var scriptService: ScriptService | ||
private lateinit var clusterService: ClusterService | ||
fun resolve(rollup: Rollup, fieldValue: String): String { | ||
val script = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, fieldValue, mapOf()) | ||
|
||
val contextMap = rollup.toXContent(XContentFactory.jsonBuilder(), XCONTENT_WITHOUT_TYPE) | ||
.toMap() | ||
.filterKeys { key -> key in validTopContextFields } | ||
|
||
var compiledValue = scriptService.compile(script, TemplateScript.CONTEXT) | ||
.newInstance(script.params + mapOf("ctx" to contextMap)) | ||
.execute() | ||
|
||
if (isAlias(compiledValue)) { | ||
compiledValue = getWriteIndexNameForAlias(compiledValue) | ||
} | ||
|
||
return if (compiledValue.isNullOrBlank()) fieldValue else compiledValue | ||
} | ||
|
||
fun registerScriptService(scriptService: ScriptService) { | ||
this.scriptService = scriptService | ||
} | ||
fun hasAlias(index: String): Boolean { | ||
val aliases = clusterService.state().metadata().indices.get(index)?.aliases | ||
if (aliases != null) { | ||
return aliases.size() > 0 | ||
} | ||
return false | ||
} | ||
fun isAlias(index: String): Boolean { | ||
return clusterService.state().metadata().indicesLookup?.get(index) is IndexAbstraction.Alias | ||
} | ||
fun getWriteIndexNameForAlias(alias: String): String? { | ||
return clusterService.state().metadata().indicesLookup?.get(alias)?.writeIndex?.index?.name | ||
} | ||
|
||
fun getBackingIndicesForAlias(alias: String): MutableList<IndexMetadata>? { | ||
return clusterService.state().metadata().indicesLookup?.get(alias)?.indices | ||
} | ||
|
||
fun registerServices(scriptService: ScriptService, clusterService: ClusterService) { | ||
this.scriptService = scriptService | ||
this.clusterService = clusterService | ||
} | ||
} |