Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Flint index metadata #70

Merged
merged 18 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ lazy val flintCore = (project in file("flint-core"))
"org.opensearch.client" % "opensearch-rest-high-level-client" % opensearchVersion
exclude ("org.apache.logging.log4j", "log4j-api"),
"com.amazonaws" % "aws-java-sdk" % "1.12.397" % "provided"
exclude ("com.fasterxml.jackson.core", "jackson-databind")),
exclude ("com.fasterxml.jackson.core", "jackson-databind"),
"org.scalactic" %% "scalactic" % "3.2.15" % "test",
"org.scalatest" %% "scalatest" % "3.2.15" % "test",
"org.scalatest" %% "scalatest-flatspec" % "3.2.15" % "test",
"org.scalatestplus" %% "mockito-4-6" % "3.2.15.0" % "test",
"com.stephenn" %% "scalatest-json-jsonassert" % "0.2.5" % "test"),
publish / skip := true)

lazy val pplSparkIntegration = (project in file("ppl-spark-integration"))
Expand Down
25 changes: 11 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ Currently, Flint metadata is only static configuration without version control a

```json
{
"version": "0.1",
"indexConfig": {
"kind": "skipping",
"properties": {
"indexedColumns": [{
"kind": "...",
"columnName": "...",
"columnType": "..."
}]
}
},
"source": "alb_logs",
"state": "active",
"enabled": true
"version": "0.1.0",
"name": "...",
"kind": "skipping",
"source": "...",
"indexedColumns": [{
"kind": "...",
"columnName": "...",
"columnType": "..."
}],
"options": { },
"properties": { }
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.flint.core.metadata

import java.nio.charset.StandardCharsets.UTF_8

import org.opensearch.common.bytes.BytesReference
import org.opensearch.common.xcontent._
import org.opensearch.common.xcontent.json.JsonXContent

/**
* JSON parsing and building helper.
*/
object FlintJsonHelper {

/**
* Build JSON by creating JSON builder and pass it to the given function.
*
* @param block
* building logic with JSON builder
* @return
* JSON string
*/
def buildJson(block: XContentBuilder => Unit): String = {
val builder: XContentBuilder = XContentFactory.jsonBuilder
builder.startObject
block(builder)
builder.endObject()
BytesReference.bytes(builder).utf8ToString
}

/**
* Add an object field of the name to the JSON builder and continue building it with the given
* function.
*
* @param builder
* JSON builder
* @param name
* field name
* @param block
* building logic on the JSON field
*/
def objectField(builder: XContentBuilder, name: String)(block: => Unit): Unit = {
builder.startObject(name)
block
builder.endObject()
}

/**
* Add an optional object field of the name to the JSON builder. Add an empty object field if
* the value is null.
*
* @param builder
* JSON builder
* @param name
* field name
* @param value
* field value
*/
def optionalObjectField(builder: XContentBuilder, name: String, value: AnyRef): Unit = {
if (value == null) {
builder.startObject(name).endObject()
} else {
builder.field(name, value)
}
}

/**
* Create a XContent JSON parser on the given JSON string.
*
* @param json
* JSON string
* @return
* JSON parser
*/
def createJsonParser(json: String): XContentParser = {
JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
json.getBytes(UTF_8))
}

/**
* Parse the given JSON string by creating JSON parser and pass it to the parsing function.
*
* @param json
* JSON string
* @param block
* parsing logic with the parser
*/
def parseJson(json: String)(block: (XContentParser, String) => Unit): Unit = {
val parser = createJsonParser(json)

// Read first root object token and start parsing
parser.nextToken()
parseObjectField(parser)(block)
}

/**
* Parse each inner field in the object field with the given parsing function.
*
* @param parser
* JSON parser
* @param block
* parsing logic on each inner field
*/
def parseObjectField(parser: XContentParser)(block: (XContentParser, String) => Unit): Unit = {
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
val fieldName: String = parser.currentName()
parser.nextToken() // Move to the field value

block(parser, fieldName)
}
}

/**
* Parse each inner field in the array field.
*
* @param parser
* JSON parser
* @param block
* parsing logic on each inner field
*/
def parseArrayField(parser: XContentParser)(block: => Unit): Unit = {
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
block
}
}
}

This file was deleted.

Loading
Loading