This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new dbcb2f24ed65 CAMEL-27050: camel-jbang - camel catalog component reads
only the first object of each component JSON (#30203)
dbcb2f24ed65 is described below
commit dbcb2f24ed656530ca2c819b8ee21afc1ca697e7
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 16 17:02:49 2026 +0100
CAMEL-27050: camel-jbang - camel catalog component reads only the first
object of each component JSON (#30203)
camel catalog component parsed the whole model JSON of all 422 components
(14 MB). It now reads each JSON only up to the end of its first "component"
object, which holds every field the list shows, and falls back to the whole
model when that is not possible. The command goes from about 440 ms to 220 ms;
the output is the same.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../core/commands/catalog/CatalogComponent.java | 2 +-
.../core/commands/catalog/CatalogModelHeader.java | 164 +++++++++++++++++++++
.../commands/catalog/CatalogModelHeaderTest.java | 104 +++++++++++++
3 files changed, 269 insertions(+), 1 deletion(-)
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogComponent.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogComponent.java
index d231f61a940e..60c96f7a333f 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogComponent.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogComponent.java
@@ -38,7 +38,7 @@ public class CatalogComponent extends CatalogBaseCommand {
List<CatalogBaseCommand.Row> collectRows() {
List<Row> rows = new ArrayList<>();
for (String name : findComponentNames(catalog)) {
- ComponentModel model = catalog.componentModel(name);
+ ComponentModel model = CatalogModelHeader.componentModel(catalog,
name);
if (model != null) {
Row row = new Row();
row.name = model.getScheme();
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogModelHeader.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogModelHeader.java
new file mode 100644
index 000000000000..869c4f431f9c
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogModelHeader.java
@@ -0,0 +1,164 @@
+/*
+ * 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.camel.dsl.jbang.core.commands.catalog;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.tooling.model.ComponentModel;
+import org.apache.camel.tooling.model.JsonMapper;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+
+/**
+ * The component models of the catalog for camel catalog component, read from
the first object of their JSON only
+ * ({@code "component"}), which has the name, title, support level,
description, label and Maven coordinates that the
+ * list shows. The options and headers that follow are most of the JSON (the
422 component models are 14 MB), and
+ * reading and parsing all of it made camel catalog component take 400 ms. The
models of data formats, languages and
+ * others are small: reading the first object first costs more than it saves.
+ * <p>
+ * When the JSON is not in the catalog (such as a component added to it at
runtime) or does not start with that object,
+ * the whole model is read from the catalog instead.
+ */
+final class CatalogModelHeader {
+
+ private static final String EXTENSION = ".json";
+
+ private CatalogModelHeader() {
+ }
+
+ // no lambdas or method references here: the first of each in a JVM costs
milliseconds, about what a list saves
+
+ static ComponentModel componentModel(CamelCatalog catalog, String name) {
+ JsonObject json = read(catalog,
catalog.getRuntimeProvider().getComponentJSonSchemaDirectory(), name,
"component");
+ try {
+ if (json != null) {
+ return JsonMapper.generateComponentModel(json);
+ }
+ } catch (RuntimeException e) {
+ // the catalog reads the whole model
+ }
+ return catalog.componentModel(name);
+ }
+
+ /**
+ * The first object of the JSON of the model, as a JSON object of its own,
or {@code null} when the JSON is not in
+ * the catalog or does not start with that object.
+ */
+ private static JsonObject read(CamelCatalog catalog, String directory,
String name, String key) {
+ if (directory == null) {
+ return null;
+ }
+ try (InputStream is =
catalog.getVersionManager().getResourceAsStream(directory + "/" + name +
EXTENSION)) {
+ if (is == null) {
+ return null;
+ }
+ String json = firstObject(new InputStreamReader(is,
StandardCharsets.UTF_8), key);
+ return json != null ? (JsonObject) Jsoner.deserialize(json) : null;
+ } catch (Exception e) {
+ // not the expected JSON: the catalog reads the whole model
+ return null;
+ }
+ }
+
+ /**
+ * Reads a JSON object up to the end of the value of its first key, when
that key is the given one and its value is
+ * an object, and returns that part as a JSON object of its own: {@code
{"key": {...}}}.
+ *
+ * @param reader the JSON
+ * @param key the first key
+ * @return the JSON of the first key, or {@code null} when the JSON
does not start with that key and an
+ * object
+ */
+ static String firstObject(Reader reader, String key) throws IOException {
+ Text text = new Text(reader);
+ if (text.nextNonBlank() != '{' || text.nextNonBlank() != '"') {
+ return null;
+ }
+ for (int i = 0; i < key.length(); i++) {
+ if (text.next() != key.charAt(i)) {
+ return null;
+ }
+ }
+ if (text.next() != '"' || text.nextNonBlank() != ':' ||
text.nextNonBlank() != '{') {
+ return null;
+ }
+ int begin = text.pos - 1;
+ int depth = 1;
+ boolean inString = false;
+ boolean escaped = false;
+ while (depth > 0) {
+ int c = text.next();
+ if (c == -1) {
+ // the JSON ended before the object did
+ return null;
+ } else if (inString) {
+ if (escaped) {
+ escaped = false;
+ } else if (c == '\\') {
+ escaped = true;
+ } else if (c == '"') {
+ inString = false;
+ }
+ } else if (c == '"') {
+ inString = true;
+ } else if (c == '{') {
+ depth++;
+ } else if (c == '}') {
+ depth--;
+ }
+ }
+ return "{\"" + key + "\":" + text.chars.substring(begin, text.pos) +
"}";
+ }
+
+ /**
+ * The characters read so far, read in chunks (one character at a time
costs more than reading the whole JSON).
+ */
+ private static final class Text {
+ private final Reader reader;
+ private final char[] chunk = new char[4096];
+ private final StringBuilder chars = new StringBuilder(4096);
+ private int pos;
+
+ Text(Reader reader) {
+ this.reader = reader;
+ }
+
+ int next() throws IOException {
+ while (pos >= chars.length()) {
+ int n = reader.read(chunk);
+ if (n == -1) {
+ return -1;
+ }
+ chars.append(chunk, 0, n);
+ }
+ return chars.charAt(pos++);
+ }
+
+ int nextNonBlank() throws IOException {
+ int c = next();
+ while (c != -1 && Character.isWhitespace(c)) {
+ c = next();
+ }
+ return c;
+ }
+ }
+}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogModelHeaderTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogModelHeaderTest.java
new file mode 100644
index 000000000000..34f8802a2ab7
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogModelHeaderTest.java
@@ -0,0 +1,104 @@
+/*
+ * 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.camel.dsl.jbang.core.commands.catalog;
+
+import java.io.StringReader;
+import java.util.List;
+import java.util.function.BiFunction;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.tooling.model.ArtifactModel;
+import org.apache.camel.tooling.model.ComponentModel;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class CatalogModelHeaderTest {
+
+ @Test
+ void firstObjectOnly() throws Exception {
+ String json = """
+ {
+ "component": {
+ "name": "foo",
+ "nested": { "a": 1 }
+ },
+ "componentProperties": { "x": { "description": "never read"
} }
+ }""";
+
+ assertThat(CatalogModelHeader.firstObject(new StringReader(json),
"component"))
+ .isEqualTo("{\"component\":{\n \"name\": \"foo\",\n
\"nested\": { \"a\": 1 }\n }}");
+ }
+
+ @Test
+ void bracesAndQuotesInStrings() throws Exception {
+ String json = "{\"component\":{\"description\":\"a } and a \\\" and a
{\",\"x\":\"\\\\\"},\"properties\":{}}";
+
+ assertThat(CatalogModelHeader.firstObject(new StringReader(json),
"component"))
+ .isEqualTo("{\"component\":{\"description\":\"a } and a \\\"
and a {\",\"x\":\"\\\\\"}}");
+ }
+
+ @Test
+ void notTheExpectedJson() throws Exception {
+ // another first key, a key that only starts the same, not an object,
or the JSON ends first
+ assertThat(CatalogModelHeader.firstObject(new
StringReader("{\"properties\":{},\"component\":{}}"), "component"))
+ .isNull();
+ assertThat(CatalogModelHeader.firstObject(new
StringReader("{\"componentX\":{}}"), "component")).isNull();
+ assertThat(CatalogModelHeader.firstObject(new
StringReader("{\"component\":\"foo\"}"), "component")).isNull();
+ assertThat(CatalogModelHeader.firstObject(new StringReader("[]"),
"component")).isNull();
+ assertThat(CatalogModelHeader.firstObject(new
StringReader("{\"component\":{\"name\":\"foo\""), "component"))
+ .isNull();
+ }
+
+ @Test
+ void sameAsTheWholeModel() {
+ CamelCatalog catalog = new DefaultCamelCatalog();
+
+ assertSame(catalog, catalog.findComponentNames(),
CatalogModelHeader::componentModel, CamelCatalog::componentModel);
+ }
+
+ // the fields that the lists of camel catalog show
+ private static <T extends ArtifactModel<?>> void assertSame(
+ CamelCatalog catalog, List<String> names, BiFunction<CamelCatalog,
String, T> header,
+ BiFunction<CamelCatalog, String, T> whole) {
+ assertThat(names).isNotEmpty();
+ for (String name : names) {
+ T expected = whole.apply(catalog, name);
+ T actual = header.apply(catalog, name);
+ if (expected == null) {
+ assertThat(actual).as(name).isNull();
+ continue;
+ }
+ assertThat(actual).as(name).isNotNull();
+
assertThat(actual.getName()).as(name).isEqualTo(expected.getName());
+ if (actual instanceof ComponentModel component) {
+
assertThat(component.getScheme()).as(name).isEqualTo(((ComponentModel)
expected).getScheme());
+ }
+
assertThat(actual.getTitle()).as(name).isEqualTo(expected.getTitle());
+
assertThat(actual.getSupportLevel()).as(name).isEqualTo(expected.getSupportLevel());
+
assertThat(actual.getFirstVersionShort()).as(name).isEqualTo(expected.getFirstVersionShort());
+
assertThat(actual.getDescription()).as(name).isEqualTo(expected.getDescription());
+
assertThat(actual.getLabel()).as(name).isEqualTo(expected.getLabel());
+
assertThat(actual.isDeprecated()).as(name).isEqualTo(expected.isDeprecated());
+
assertThat(actual.isNativeSupported()).as(name).isEqualTo(expected.isNativeSupported());
+
assertThat(actual.getGroupId()).as(name).isEqualTo(expected.getGroupId());
+
assertThat(actual.getArtifactId()).as(name).isEqualTo(expected.getArtifactId());
+
assertThat(actual.getVersion()).as(name).isEqualTo(expected.getVersion());
+ }
+ }
+}