Copilot commented on code in PR #7505:
URL: https://github.com/apache/ignite-3/pull/7505#discussion_r2753637623


##########
modules/platforms/dotnet/Apache.Ignite/Internal/Table/Serialization/Mappers/KeyValuePairCompositeMapper.cs:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+
+namespace Apache.Ignite.Internal.Table.Serialization.Mappers;
+
+using System.Collections.Generic;
+using Apache.Ignite.Table.Mapper;
+
+/// <summary>
+/// Key-value mapper.
+/// </summary>
+/// <typeparam name="TKey">Key type.</typeparam>
+/// <typeparam name="TValue">Value type.</typeparam>
+internal sealed record KeyValuePairCompositeMapper<TKey, 
TValue>(OneColumnMapper<TKey> KeyMapper, OneColumnMapper<TValue> ValMapper)
+    : IMapper<KeyValuePair<TKey, TValue>>
+{
+    /// <inheritdoc />
+    public void Write(KeyValuePair<TKey, TValue> obj, ref RowWriter rowWriter, 
IMapperSchema schema)
+    {
+        bool keyWritten = false;
+        bool valueWritten = false;
+
+        foreach (var column in schema.Columns)
+        {
+            if (!keyWritten && column is Column { IsKey: true })
+            {
+                KeyMapper.Writer(obj.Key, ref rowWriter, column);
+                keyWritten = true;
+            }
+            else if (!valueWritten)
+            {
+                ValMapper.Writer(obj.Value, ref rowWriter, column);
+                valueWritten = true;
+            }

Review Comment:
   KeyValuePairCompositeMapper currently writes the value into the first 
non-key column *or* (if the second column is also a key) into a key column. 
This breaks mapping for tables with composite primary keys (and also for 
key-only operations when there is more than one key column). To match existing 
KvPair primitive mapping behavior, write the key for every key column and write 
the value for every non-key column (or explicitly validate and throw when the 
schema isn't a single-key/single-value table).



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Table/Serialization/Mappers/OneColumnMapper.cs:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+
+namespace Apache.Ignite.Internal.Table.Serialization.Mappers;
+
+using System.Diagnostics.CodeAnalysis;
+using Apache.Ignite.Table.Mapper;
+

Review Comment:
   This new internal mapper file uses `using Apache.Ignite.Table.Mapper;` while 
other files under `Apache.Ignite.Internal.*` typically use the shorter `using 
Ignite.Table.Mapper;` (e.g., 
Internal/Table/Serialization/MapperSerializerHandler.cs:23). Consider aligning 
with the existing convention for consistency.



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Table/Serialization/Mappers/KeyValuePairCompositeMapper.cs:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+
+namespace Apache.Ignite.Internal.Table.Serialization.Mappers;
+
+using System.Collections.Generic;
+using Apache.Ignite.Table.Mapper;
+
+/// <summary>
+/// Key-value mapper.
+/// </summary>
+/// <typeparam name="TKey">Key type.</typeparam>
+/// <typeparam name="TValue">Value type.</typeparam>
+internal sealed record KeyValuePairCompositeMapper<TKey, 
TValue>(OneColumnMapper<TKey> KeyMapper, OneColumnMapper<TValue> ValMapper)
+    : IMapper<KeyValuePair<TKey, TValue>>
+{
+    /// <inheritdoc />
+    public void Write(KeyValuePair<TKey, TValue> obj, ref RowWriter rowWriter, 
IMapperSchema schema)
+    {
+        bool keyWritten = false;
+        bool valueWritten = false;
+
+        foreach (var column in schema.Columns)
+        {
+            if (!keyWritten && column is Column { IsKey: true })
+            {
+                KeyMapper.Writer(obj.Key, ref rowWriter, column);
+                keyWritten = true;
+            }
+            else if (!valueWritten)
+            {
+                ValMapper.Writer(obj.Value, ref rowWriter, column);
+                valueWritten = true;
+            }
+            else
+            {
+                rowWriter.Skip();
+            }
+        }
+    }
+
+    /// <inheritdoc />
+    public KeyValuePair<TKey, TValue> Read(ref RowReader rowReader, 
IMapperSchema schema)
+    {
+        TKey key = default!;
+        TValue value = default!;
+
+        bool keyRead = false;
+        bool valueRead = false;
+
+        foreach (var column in schema.Columns)
+        {
+            if (!keyRead && column is Column { IsKey: true })
+            {
+                key = KeyMapper.Reader(ref rowReader, column);
+                keyRead = true;
+            }
+            else if (!valueRead)
+            {
+                value = ValMapper.Reader(ref rowReader, column);
+                valueRead = true;
+            }
+            else
+            {
+                rowReader.Skip();
+            }

Review Comment:
   KeyValuePairCompositeMapper.Read treats the first key column as the key and 
the next column as the value, which can accidentally read a key column into the 
value when a table has multiple key columns (or when schema is key-only with >1 
key column). Consider iterating all columns and assigning key/value based on 
IsKey (or validate the schema shape and throw).



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Table/Table.cs:
##########
@@ -148,7 +150,26 @@ public Table(QualifiedName qualifiedName, int id, 
ClientFailoverSocket socket, S
         /// <inheritdoc/>
         [RequiresUnreferencedCode(ReflectionUtils.TrimWarning)]
         public IRecordView<T> GetRecordView<T>()
-            where T : notnull => GetRecordViewInternal<T>();
+            where T : notnull
+        {
+            var simpleMapper = OneColumnMappers.TryCreate<T>();
+
+            if (!RuntimeFeature.IsDynamicCodeSupported)
+            {
+                if (simpleMapper == null)
+                {
+                    throw new InvalidOperationException(
+                        "Dynamic code generation is not supported in the 
current environment. " +
+                        "Provide an explicit IMapper<T> implementation for 
type " + typeof(T).FullName);
+                }
+
+                return GetRecordView(simpleMapper);
+            }
+
+            return simpleMapper is not null
+                ? GetRecordView(simpleMapper)
+                : GetRecordViewInternal<T>();

Review Comment:
   GetRecordView<T>() used to return a cached RecordView instance per T (via 
GetRecordViewInternal). With the new primitive mapper path, supported primitive 
types now return a new RecordView on each call, which can increase allocations 
and changes reference identity semantics. Consider caching the 
primitive-mapper-based RecordView instances too (e.g., in _recordViews) so 
repeated calls behave like before.



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Table/Serialization/Mappers/KeyValuePairCompositeMapper.cs:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+
+namespace Apache.Ignite.Internal.Table.Serialization.Mappers;
+
+using System.Collections.Generic;
+using Apache.Ignite.Table.Mapper;
+

Review Comment:
   This new internal mapper file uses `using Apache.Ignite.Table.Mapper;` while 
most other internal code uses `using Ignite.Table.Mapper;` (e.g., 
Internal/Table/Serialization/MapperSerializerHandler.cs:23). Consider aligning 
the `using` style across internal mappers for consistency.



-- 
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]

Reply via email to