Skip to content

Commit

Permalink
Fixing IndexSettings
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Sep 30, 2024
1 parent ad61d2a commit 7041878
Show file tree
Hide file tree
Showing 34 changed files with 814 additions and 277 deletions.
2 changes: 1 addition & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ After:
- The `smartcn` builder method has been renamed to `smartcnTokenizer`.

### indices.Translog
- The `durability` property now accepts a `TranslogDurability` enum instead of a `String`.
- The `durability` property now accepts a `TranslogDurability` enum instead of a `String`. This also affects the `IndexSettings.translogDurability` alias property.

### indices.Alias
- The `indexRouting', `routing` and `searchRouting` properties now accept a `List<String>` instead of a `String`.
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,6 @@ public void testFoo() {
assertEquals("Raise the bar", foo.bar().name());
}

@Test
public void testIndexSettingsTranslogOptionsParsing() {

final IndexSettings indexSettings = IndexSettings.of(
_1 -> _1.translog(
Translog.of(tr -> tr.syncInterval(Time.of(t -> t.time("10s"))).durability(TranslogDurability.Async).flushThresholdSize("256mb"))
)
);

final String str = toJson(indexSettings);
assertEquals("{\"translog\":{\"durability\":\"async\",\"flush_threshold_size\":\"256mb\"," + "\"sync_interval\":\"10s\"}}", str);

IndexSettings deserialized = fromJson(str, IndexSettings._DESERIALIZER);
assertEquals(indexSettings.translog().syncInterval().time(), deserialized.translog().syncInterval().time());
assertEquals(indexSettings.translog().durability(), deserialized.translog().durability());
assertEquals(indexSettings.translog().flushThresholdSize(), deserialized.translog().flushThresholdSize());

final String deprecatedForm = "{\"translog\":{\"sync_interval\":\"10s\"},\"translog.durability\":\"async\",\"translog"
+ ".flush_threshold_size\":\"256mb\"}";
IndexSettings deprecatedDeserialized = fromJson(deprecatedForm, IndexSettings._DESERIALIZER);
assertEquals(indexSettings.translog().syncInterval().time(), deprecatedDeserialized.translog().syncInterval().time());
assertEquals(indexSettings.translog().durability(), deprecatedDeserialized.translog().durability());
assertEquals(indexSettings.translog().flushThresholdSize(), deprecatedDeserialized.translog().flushThresholdSize());
}

@Test
public void testIndexSettingsMappingParsing() {

Expand All @@ -112,8 +87,8 @@ public void testIndexSettingsMappingParsing() {

final String str = toJson(mapping);
assertEquals(
"{\"total_fields\":{\"limit\":1},\"depth\":{\"limit\":2},\"nested_fields\":{\"limit\":3},"
+ "\"nested_objects\":{\"limit\":4},\"field_name_length\":{\"limit\":5}}",
"{\"depth\":{\"limit\":2},\"field_name_length\":{\"limit\":5},\"nested_fields\":{\"limit\":3},"
+ "\"nested_objects\":{\"limit\":4},\"total_fields\":{\"limit\":1}}",
str
);

Expand Down Expand Up @@ -299,7 +274,7 @@ public void testIndexSettingsIndexing() {
final String str = toJson(indexing);
assertEquals(
"{\"slowlog\":{\"level\":\"info\",\"source\":0,\"threshold\":{\"index\":{"
+ "\"warn\":\"5000ms\",\"info\":\"1000ms\",\"debug\":\"500ms\",\"trace\":\"200ms\"}}}}",
+ "\"debug\":\"500ms\",\"info\":\"1000ms\",\"trace\":\"200ms\",\"warn\":\"5000ms\"}}}}",
str
);

Expand Down Expand Up @@ -339,9 +314,9 @@ public void testIndexSettingsSearch() {
final String str = toJson(search);
assertEquals(
"{\"idle\":{\"after\":\"5s\"},\"slowlog\":{\"level\":\"info\","
+ "\"threshold\":{\"query\":{\"warn\":\"5000ms\",\"info\":\"1000ms\",\"debug\":\"500ms\","
+ "\"trace\":\"200ms\"},\"fetch\":{\"warn\":\"50ms\",\"info\":\"10ms\",\"debug\":\"5ms\","
+ "\"trace\":\"2ms\"}}}}",
+ "\"threshold\":{\"fetch\":{\"debug\":\"5ms\",\"info\":\"10ms\","
+ "\"trace\":\"2ms\",\"warn\":\"50ms\"},\"query\":{\"debug\":\"500ms\",\"info\":\"1000ms\","
+ "\"trace\":\"200ms\",\"warn\":\"5000ms\"}}}}",
str
);

Expand Down
4 changes: 4 additions & 0 deletions java-codegen/opensearch-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47256,6 +47256,10 @@ components:
type: integer
translog:
$ref: '#/components/schemas/indices._common:Translog'
translog.durability:
$ref: '#/components/schemas/indices._common:TranslogDurability'
translog.flush_threshold_size:
$ref: '#/components/schemas/_common:HumanReadableByteCount'
query_string:
$ref: '#/components/schemas/indices._common:IndexSettingsQueryString'
query_string.lenient:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.codegen.model;

import java.util.Collection;
Expand All @@ -7,7 +15,14 @@ public class DictionaryResponseShape extends Shape {
private final Type keyType;
private final Type valueType;

public DictionaryResponseShape(Namespace parent, String className, String typedefName, String description, Type keyType, Type valueType) {
public DictionaryResponseShape(
Namespace parent,
String className,
String typedefName,
String description,
Type keyType,
Type valueType
) {
super(parent, className, typedefName, description);
this.keyType = keyType;
this.valueType = valueType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

package org.opensearch.client.codegen.model;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.codegen.utils.Markdown;
Expand All @@ -26,6 +30,7 @@ public class Field {
@Nullable
private final Deprecation deprecation;
private final boolean isAdditionalProperties;
private final Set<String> aliases = new HashSet<>();

public Field(
@Nonnull String wireName,
Expand Down Expand Up @@ -85,4 +90,13 @@ public String getDescription() {
public Deprecation getDeprecation() {
return deprecation;
}

@Nonnull
public Collection<String> getAliases() {
return Collections.unmodifiableSet(aliases);
}

public void addAlias(String alias) {
aliases.add(alias);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.opensearch.client.codegen.renderer.TemplateLoader;
import org.opensearch.client.codegen.renderer.TemplateRenderer;
import org.opensearch.client.codegen.renderer.TemplateValueFormatter;
import org.opensearch.client.codegen.utils.ObjectBuilderBase;
import org.opensearch.client.codegen.utils.Strings;
import org.opensearch.client.codegen.utils.builder.ObjectBuilderBase;

public final class ShapeRenderingContext implements AutoCloseable {
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.logging.log4j.Logger;
import org.opensearch.client.codegen.model.overrides.Overrides;
import org.opensearch.client.codegen.model.overrides.PropertyOverride;
import org.opensearch.client.codegen.model.overrides.SchemaOverride;
import org.opensearch.client.codegen.openapi.HttpStatusCode;
import org.opensearch.client.codegen.openapi.In;
import org.opensearch.client.codegen.openapi.JsonPointer;
Expand Down Expand Up @@ -237,10 +238,7 @@ private Field visit(OpenApiParameter parameter) {
private Shape visit(OpenApiSchema schema) {
var namespace = schema.getNamespace().orElseThrow();
var name = schema.getName().orElseThrow();
var className = name;
if ("_types.query_dsl".equals(namespace) && "QueryContainer".equals(name)) {
className = "Query";
}
var className = overrides.getSchema(schema.getPointer()).flatMap(SchemaOverride::getClassName).orElse(name);
return visit(root.child(namespace), className, namespace + "." + name, schema);
}

Expand Down Expand Up @@ -309,7 +307,14 @@ private Shape visit(Namespace parent, String className, String typedefName, Open
});
} else if (schema.determineSingleType().orElse(null) == OpenApiSchemaType.Object) {
if (schema.getProperties().isEmpty() && schema.getAdditionalProperties().isPresent()) {
shape = new DictionaryResponseShape(parent, className, typedefName, description, Types.Java.Lang.String, mapType(schema.getAdditionalProperties().orElseThrow()));
shape = new DictionaryResponseShape(
parent,
className,
typedefName,
description,
Types.Java.Lang.String,
mapType(schema.getAdditionalProperties().orElseThrow())
);
visitedSchemas.putIfAbsent(schema, shape);
} else {
var objShape = new ObjectShape(parent, className, typedefName, description);
Expand Down Expand Up @@ -342,8 +347,15 @@ private void visitInto(OpenApiSchema schema, ObjectShape shape) {
final var overrides = this.overrides.getSchema(schema.getPointer());

properties.forEach((k, v) -> {
var type = overrides.flatMap(so -> so.getProperty(k)).flatMap(PropertyOverride::getMappedType).orElseGet(() -> mapType(v));
shape.addBodyField(new Field(k, type, required.contains(k), v.getDescription().orElse(null), null));
var propOverrides = overrides.map(so -> so.getProperty(k));

var type = propOverrides.flatMap(PropertyOverride::getMappedType).orElseGet(() -> mapType(v));

var field = new Field(k, type, required.contains(k), v.getDescription().orElse(null), null);

propOverrides.flatMap(PropertyOverride::getAliases).ifPresent(aliases -> aliases.forEach(field::addAlias));

shape.addBodyField(field);
});

if (!additionalProperties.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.opensearch.client.codegen.renderer.lambdas.TypeIsDefinedLambda;
import org.opensearch.client.codegen.renderer.lambdas.TypeQueryParamifyLambda;
import org.opensearch.client.codegen.renderer.lambdas.TypeSerializerLambda;
import org.opensearch.client.codegen.utils.ObjectBuilderBase;
import org.opensearch.client.codegen.utils.Strings;
import org.opensearch.client.codegen.utils.builder.ObjectBuilderBase;

public class Type {
private static final Set<String> PRIMITIVES = Set.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.codegen.utils.ObjectBuilderBase;
import org.opensearch.client.codegen.utils.Strings;
import org.opensearch.client.codegen.utils.builder.ObjectBuilderBase;

public final class TypeParameterDefinition {
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.function.Function;
import javax.annotation.Nonnull;
import org.opensearch.client.codegen.utils.Either;
import org.opensearch.client.codegen.utils.ObjectBuilderBase;
import org.opensearch.client.codegen.utils.builder.ObjectBuilderBase;

public final class TypeParameterDiamond {
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.codegen.openapi.JsonPointer;
import org.opensearch.client.codegen.utils.ObjectBuilder;
import org.opensearch.client.codegen.utils.ObjectBuilderBase;
import org.opensearch.client.codegen.utils.builder.ObjectBuilder;
import org.opensearch.client.codegen.utils.builder.ObjectBuilderBase;

public class Overrides {
public static final Overrides OVERRIDES = builder().build();
private static final JsonPointer SCHEMAS = JsonPointer.of("components", "schemas");

public static final Overrides OVERRIDES = builder().withSchemas(
s -> s.with(SCHEMAS.append("_common.query_dsl:QueryContainer"), so -> so.withClassName("Query"))
.with(SCHEMAS.append("indices._common:IndexSettings"), so -> so.withAliasProvider((k) -> {
switch (k) {
case "index":
case "indexing":
case "mapping":
case "search":
case "settings":
case "top_metrics_max_size":
return null;
default:
return Set.of("index." + k);
}
}))
).build();

@Nonnull
private final Map<JsonPointer, SchemaOverride> schemas;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,39 @@

package org.opensearch.client.codegen.model.overrides;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.codegen.model.Type;
import org.opensearch.client.codegen.utils.MapBuilderBase;
import org.opensearch.client.codegen.utils.ObjectBuilderBase;
import org.opensearch.client.codegen.utils.builder.ObjectBuilder;
import org.opensearch.client.codegen.utils.builder.ObjectBuilderBase;
import org.opensearch.client.codegen.utils.builder.ObjectMapBuilderBase;
import org.opensearch.client.codegen.utils.builder.SetBuilder;

public final class PropertyOverride {
@Nullable
private final Type mappedType;
@Nullable
private final Set<String> aliases;

private PropertyOverride(Builder builder) {
this.mappedType = builder.mappedType;
this.aliases = builder.aliases;
}

@Nonnull
public Optional<Type> getMappedType() {
return Optional.ofNullable(mappedType);
}

@Nonnull
public Optional<Set<String>> getAliases() {
return Optional.ofNullable(aliases);
}

@Nonnull
public static Builder builder() {
return new Builder();
Expand All @@ -41,6 +54,8 @@ public static MapBuilder mapBuilder() {
public static final class Builder extends ObjectBuilderBase<PropertyOverride, Builder> {
@Nullable
private Type mappedType;
@Nullable
private Set<String> aliases;

private Builder() {}

Expand All @@ -55,9 +70,21 @@ public Builder withMappedType(@Nullable Type mappedType) {
this.mappedType = mappedType;
return this;
}

@Nonnull
public Builder withAliases(@Nullable Set<String> aliases) {
this.aliases = aliases;
return this;
}

@Nonnull
public Builder withAliases(@Nonnull Function<SetBuilder<String>, ObjectBuilder<Set<String>>> fn) {
this.aliases = Objects.requireNonNull(fn, "fn must not be null").apply(new SetBuilder<>()).build();
return this;
}
}

public static final class MapBuilder extends MapBuilderBase<String, PropertyOverride, Builder, MapBuilder> {
public static final class MapBuilder extends ObjectMapBuilderBase<String, PropertyOverride, Builder, MapBuilder> {
private MapBuilder() {}

@Nonnull
Expand Down
Loading

0 comments on commit 7041878

Please sign in to comment.