korlov42 commented on code in PR #2728:
URL: https://github.com/apache/ignite-3/pull/2728#discussion_r1370139755


##########
modules/schema/src/main/java/org/apache/ignite/internal/schema/BinaryTuplePrefix.java:
##########
@@ -73,6 +97,102 @@ public static BinaryTuplePrefix fromBinaryTuple(BinaryTuple 
tuple) {
         return new BinaryTuplePrefix(tuple.elementCount(), prefixBuffer);
     }
 
+    private static BinaryTuplePrefix expandTuple(int size, BinaryTuple tuple) {
+        assert size > tuple.elementCount();
+
+        var stats = new Sink() {
+            int dataBeginOffset = 0;
+            int dataEndOffset = 0;
+
+            @Override
+            public void nextElement(int index, int begin, int end) {
+                if (index == 0) {
+                    dataBeginOffset = begin;
+                }
+
+                dataEndOffset = end;
+            }
+        };
+
+        tuple.parse(stats);
+
+        ByteBuffer tupleBuffer = tuple.byteBuffer();
+
+        byte flags = tupleBuffer.get(0);
+        int entrySize = BinaryTupleCommon.flagsToEntrySize(flags);
+
+        ByteBuffer prefixBuffer = ByteBuffer.allocate(
+                        tupleBuffer.remaining()
+                                + (entrySize * (size - tuple.elementCount()))
+                                + Integer.BYTES)
+                .order(ORDER)
+                .put(tupleBuffer.slice().limit(stats.dataBeginOffset)); // 
header
+
+        int payloadEndPosition = stats.dataEndOffset - stats.dataBeginOffset;
+        for (int idx = tuple.elementCount(); idx < size; idx++) {
+            switch (entrySize) {
+                case Byte.BYTES:
+                    prefixBuffer.put((byte) payloadEndPosition);
+                    break;
+                case Short.BYTES:
+                    prefixBuffer.putShort((short) payloadEndPosition);
+                    break;
+                case Integer.BYTES:
+                    prefixBuffer.putInt(payloadEndPosition);
+                    break;
+                default:
+                    assert false;
+            }
+        }
+
+        prefixBuffer
+                
.put(tupleBuffer.slice().position(stats.dataBeginOffset).limit(stats.dataEndOffset))
 // payload
+                .putInt(tuple.elementCount())
+                .flip();
+
+        prefixBuffer.put(0, (byte) (flags | PREFIX_FLAG));
+
+        return new BinaryTuplePrefix(size, prefixBuffer);
+    }
+
+    private static BinaryTuplePrefix truncateTuple(int size, BinaryTuple 
tuple) {
+        assert size < tuple.elementCount();
+
+        var stats = new Sink() {
+            int dataBeginOffset = 0;
+            int dataEndOffset = 0;
+
+            @Override
+            public void nextElement(int index, int begin, int end) {
+                if (index == 0) {
+                    dataBeginOffset = begin;
+                }
+
+                if (index < size) {
+                    dataEndOffset = end;
+                }
+            }
+        };
+
+        tuple.parse(stats);
+
+        BinaryTuplePrefixBuilder builder = new BinaryTuplePrefixBuilder(size, 
size, stats.dataEndOffset - stats.dataBeginOffset);
+
+        tuple.parse((index, begin, end) -> {

Review Comment:
   sorry, I thought it all about array copying...
   
   if it's about iterating value by value, then it's because we can just copy 
prefix of header as we do in `expandTuple` method. Due to truncation, the size 
of element in offset map may be adjusted as well, I don't want do reimplement 
that logic once again.
   
   A new API (like `BinTuple.project(int... fields):ByteBuffer`) will 
definitely help to make this case a lot cleaner, but the truth is, 
`truncateTuple` is used only in tests for now. I'm not sure if anyone will use 
it future, thus don't pay too much attention to this method. I believe we can 
revise it once it will start cause any problem



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