isapego commented on code in PR #1051:
URL: https://github.com/apache/ignite-3/pull/1051#discussion_r962252858


##########
modules/platforms/dotnet/Apache.Ignite/Internal/Proto/BinaryTuple/BinaryTupleBuilder.cs:
##########
@@ -0,0 +1,407 @@
+/*
+ * 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.Proto.BinaryTuple
+{
+    using System;
+    using System.Diagnostics;
+    using System.Text;
+    using Buffers;
+
+    /// <summary>
+    /// Binary tuple builder.
+    /// </summary>
+    internal sealed class BinaryTupleBuilder : IDisposable // TODO: Support 
all types (IGNITE-15431).
+    {
+        /** Number of elements in the tuple. */
+        private readonly int _numElements;
+
+        /** Size of an offset table entry. */
+        private readonly int _entrySize;
+
+        /** Position of the varlen offset table. */
+        private readonly int _entryBase;
+
+        /** Starting position of variable-length values. */
+        private readonly int _valueBase;
+
+        /** Buffer for tuple content. */
+        private readonly PooledArrayBufferWriter _buffer = new();
+
+        /** Flag indicating if any NULL values were really put here. */
+        private bool _hasNullValues;
+
+        /** Current element. */
+        private int _elementIndex;
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="BinaryTupleBuilder"/> 
class.
+        /// </summary>
+        /// <param name="numElements">Capacity.</param>
+        /// <param name="allowNulls">Whether nulls are allowed.</param>
+        /// <param name="totalValueSize">Total value size, -1 when 
unknown.</param>
+        public BinaryTupleBuilder(int numElements, bool allowNulls = true, int 
totalValueSize = -1)
+        {
+            Debug.Assert(numElements > 0, "numElements > 0");
+
+            _numElements = numElements;
+
+            int baseOffset = BinaryTupleCommon.HeaderSize;
+            if (allowNulls)
+            {
+                baseOffset += BinaryTupleCommon.NullMapSize(numElements);
+            }
+
+            _entryBase = baseOffset;
+
+            _entrySize = totalValueSize < 0
+                ? 4
+                : 
BinaryTupleCommon.FlagsToEntrySize(BinaryTupleCommon.ValueSizeToFlags(totalValueSize));
+
+            _valueBase = baseOffset + _entrySize * numElements;
+
+            _buffer.GetSpan(_valueBase);
+            _buffer.Advance(_valueBase);
+        }
+
+        /// <summary>
+        /// Gets a value indicating whether null map is present.
+        /// </summary>
+        public bool HasNullMap => _entryBase > BinaryTupleCommon.HeaderSize;
+
+        /// <summary>
+        /// Gets the current element index.
+        /// </summary>
+        public int ElementIndex => _elementIndex;
+
+        /// <summary>
+        /// Appends a null value.
+        /// </summary>
+        public void AppendNull()
+        {
+            if (!HasNullMap)
+            {
+                throw new InvalidOperationException("Appending a NULL value in 
binary tuple builder with disabled NULLs");
+            }
+
+            _hasNullValues = true;
+
+            int nullIndex = BinaryTupleCommon.NullOffset(_elementIndex);
+            byte nullMask = BinaryTupleCommon.NullMask(_elementIndex);
+
+            var span = _buffer.GetSpan(nullIndex, 1);
+
+            span[0] |= nullMask;

Review Comment:
   Why not just 
   ```suggestion
               _buffer[nullIndex] |= nullMask;
   ```



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