valepakh commented on code in PR #3267: URL: https://github.com/apache/ignite-3/pull/3267#discussion_r1511125697
########## modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/IgniteCatalogSqlImpl.java: ########## @@ -0,0 +1,57 @@ +/* + * 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.ignite.internal.catalog.sql; + +import org.apache.ignite.catalog.IgniteCatalog; +import org.apache.ignite.catalog.Options; +import org.apache.ignite.catalog.Query; +import org.apache.ignite.sql.IgniteSql; + +/** + * Implementation of the catalog. + */ +public class IgniteCatalogSqlImpl implements IgniteCatalog { + private final IgniteSql sql; + + private final Options options; + + public IgniteCatalogSqlImpl(IgniteSql sql, Options options) { + this.options = options; + this.sql = sql; + } + + @Override + public Query create(Class<?> keyClass, Class<?> valueClass) { + return new CreateFromAnnotationsImpl(sql, options).processKeyValueClasses(keyClass, valueClass); Review Comment: > Why we allocate CreateFromAnnotationsImpl each invocation? How else can we create an object? This needs to be created each time because it contains object that are filled from the `process...` methods. ########## modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/Colocate.java: ########## @@ -0,0 +1,44 @@ +/* + * 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.ignite.internal.catalog.sql; + +import static org.apache.ignite.internal.catalog.sql.QueryPartCollection.partsList; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +class Colocate extends QueryPart { + + private final List<Name> names; + + Colocate(String... columnNames) { + this(Arrays.asList(columnNames)); Review Comment: > I absolutely don't understand what happened here. Class Name accept vararg of String but here we map String vararg to List and after that we create List where each name contains only one columnName. Is it really correct code? There are two constructors which accept either an array or a list of string column names, then it maps the strings to `Name`, which represents an identified. ########## modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/CreateFromAnnotationsImpl.java: ########## @@ -0,0 +1,199 @@ +/* + * 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.ignite.internal.catalog.sql; + +import static org.apache.ignite.catalog.ColumnSorted.column; +import static org.apache.ignite.internal.catalog.sql.QueryUtils.mapArrayToList; +import static org.apache.ignite.table.mapper.Mapper.nativelySupported; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import org.apache.ignite.catalog.ColumnSorted; +import org.apache.ignite.catalog.ColumnType; +import org.apache.ignite.catalog.DefaultZone; +import org.apache.ignite.catalog.IndexType; +import org.apache.ignite.catalog.Options; +import org.apache.ignite.catalog.annotations.Column; +import org.apache.ignite.catalog.annotations.ColumnRef; +import org.apache.ignite.catalog.annotations.Id; +import org.apache.ignite.catalog.annotations.Index; +import org.apache.ignite.catalog.annotations.Table; +import org.apache.ignite.catalog.annotations.Zone; +import org.apache.ignite.sql.IgniteSql; + +class CreateFromAnnotationsImpl extends AbstractCatalogQuery { + private CreateZoneImpl createZone; + + private CreateTableImpl createTable; + + private IndexType pkType; + + CreateFromAnnotationsImpl(IgniteSql sql, Options options) { + super(sql, options); + } + + CreateFromAnnotationsImpl processKeyValueClasses(Class<?> keyClass, Class<?> valueClass) { + if (keyClass.getAnnotation(Table.class) == null && valueClass.getAnnotation(Table.class) == null) { + throw new IllegalArgumentException( + "Cannot find @Table annotation on " + keyClass.getName() + " or " + valueClass.getName() + + ". At least one of these classes must be annotated in order to create a query object." + ); + } + + processAnnotations(keyClass, true); + processAnnotations(valueClass, false); + return this; + } + + CreateFromAnnotationsImpl processRecordClass(Class<?> recordCls) { + if (recordCls.getAnnotation(Table.class) == null) { + throw new IllegalArgumentException("Cannot find @Table annotation on " + recordCls.getName() + + ". This class must be annotated with in order to create a query object."); + } + + processAnnotations(recordCls, true); + return this; + } + + @Override + protected void accept(QueryContext ctx) { + if (createZone != null) { + ctx.visit(createZone).formatSeparator(); + } + if (createTable != null) { + ctx.visit(createTable).formatSeparator(); + } + } + + private void processAnnotations(Class<?> clazz, boolean isKeyClass) { + if (createTable == null) { + createTable = new CreateTableImpl(sql, options).ifNotExists(); + } + + Table table = clazz.getAnnotation(Table.class); + if (table != null) { + createTable.name(table.value().isEmpty() ? clazz.getSimpleName() : table.value()); + + processZone(table); + processTable(table); + } + + processColumns(createTable, pkType, clazz, isKeyClass); + } + + private void processZone(Table table) { + Class<?> zoneRef = table.zone(); + if (zoneRef == DefaultZone.class) { + return; + } + Zone zone = zoneRef.getAnnotation(Zone.class); + if (zone != null) { + createZone = new CreateZoneImpl(sql, options).ifNotExists(); + + String zoneName = zone.value().isEmpty() ? zoneRef.getSimpleName() : zone.value(); + createTable.zone(zoneName); + createZone.name(zoneName); + createZone.engine(zone.engine()); + if (zone.partitions() > 0) { + createZone.partitions(zone.partitions()); + } + if (zone.replicas() > 0) { + createZone.replicas(zone.replicas()); + } + + // TODO https://issues.apache.org/jira/browse/IGNITE-21428 + } + } + + private void processTable(Table table) { + for (Index ix : table.indexes()) { + List<ColumnSorted> indexColumns = mapArrayToList(ix.columns(), col -> column(col.value(), col.sort())); + String name = toIndexName(ix); + createTable.addIndex(name, ix.type(), indexColumns); + } + + ColumnRef[] colocateBy = table.colocateBy(); + if (colocateBy != null && colocateBy.length > 0) { + createTable.colocateBy(mapArrayToList(colocateBy, ColumnRef::value)); + } + + pkType = table.primaryKeyType(); + } + + private static String toIndexName(Index ix) { + if (!ix.value().isEmpty()) { + return ix.value(); + } + List<String> list = new ArrayList<>(); + list.add("ix"); + for (ColumnRef columnRef : ix.columns()) { + list.add(columnRef.value()); + } + return String.join("_", list); + } + + static void processColumns(CreateTableImpl createTable, IndexType pkType, Class<?> clazz, boolean isKeyClass) { Review Comment: > Can be private It's a future-proof method, it will be used in the builders later. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
