AMashenkov commented on a change in pull request #212: URL: https://github.com/apache/ignite-3/pull/212#discussion_r668918320
########## File path: modules/table/src/main/java/org/apache/ignite/internal/table/LiveSchemaTupleBuilderImpl.java ########## @@ -0,0 +1,145 @@ +/* + * 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.table; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import org.apache.ignite.internal.schema.Column; +import org.apache.ignite.internal.schema.SchemaRegistry; +import org.apache.ignite.internal.schema.marshaller.MarshallerUtil; +import org.apache.ignite.internal.table.distributed.TableManager; +import org.apache.ignite.schema.ColumnType; +import org.apache.ignite.schema.SchemaBuilders; +import org.apache.ignite.table.Tuple; +import org.apache.ignite.table.TupleBuilder; + +import static org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert; + +/** + * Live schema buildable tuple. + * + * Allows to create columns implicitly by adding previously nonexistent columns during insert. + */ +public class LiveSchemaTupleBuilderImpl extends TupleBuilderImpl { + /** Live schema column values. */ + private Map<String, Object> liveSchemaColMap; + + /** Schema registry. */ + private final SchemaRegistry schemaRegistry; + + /** Current table name. */ + private final String tblName; + + /** Table manager. */ + private final TableManager mgr; + + /** + * Constructor. + */ + public LiveSchemaTupleBuilderImpl(SchemaRegistry schemaRegistry, String tblName, TableManager mgr) { + super(schemaRegistry == null ? null : schemaRegistry.schema()); + + Objects.requireNonNull(schemaRegistry); + Objects.requireNonNull(tblName); + Objects.requireNonNull(mgr); + + this.schemaRegistry = schemaRegistry; + this.tblName = tblName; + this.mgr = mgr; + + liveSchemaColMap = null; + } + + /** {@inheritDoc} */ + @Override public TupleBuilder set(String colName, Object val) { + Column col = schema().column(colName); + + if (col == null) { + if (val == null) + return this; + + if (liveSchemaColMap == null) + liveSchemaColMap = new HashMap<>(); + + liveSchemaColMap.put(colName, val); + + return this; + } + super.set(colName, val); + + return this; + } + + /** {@inheritDoc} */ + @Override public Tuple build() { + if (liveSchemaColMap == null) + return this; + + while (!liveSchemaColMap.isEmpty()) { + createColumns(liveSchemaColMap); + + this.schema(schemaRegistry.schema()); + + Map<String, Object> colMap = map; + map = new HashMap<>(); + + colMap.forEach(super::set); + liveSchemaColMap.forEach(super::set); + + liveSchemaColMap.clear(); Review comment: Loop body will never executed more than once as liveSchemaColMap is cleaned here. -- 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]
