platinumhamburg commented on code in PR #2485:
URL: https://github.com/apache/fluss/pull/2485#discussion_r2740591633


##########
fluss-common/src/main/java/org/apache/fluss/row/decode/KeyDecoder.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.fluss.row.decode;
+
+import org.apache.fluss.row.InternalRow;
+import org.apache.fluss.types.RowType;
+
+import java.util.List;
+
+/**
+ * Interface for decoding key bytes back to {@link InternalRow}.
+ *
+ * <p>This interface provides functionality to decode compacted key bytes into 
internal row
+ * representation, typically used for primary key decoding in KV tables.
+ */
+public interface KeyDecoder {
+    /** Decode key bytes to a row containing only key fields, without non-key 
fields. */
+    InternalRow decodeKey(byte[] keyBytes);
+
+    /**
+     * Create a key decoder to decode the key array bytes of the input row.
+     *
+     * @param rowType the row type of the input row
+     * @param keyFields the key fields to encode
+     */
+    static KeyDecoder of(RowType rowType, List<String> keyFields) {

Review Comment:
   The format parameter must be provided; CompactedRow is an optional format, 
and others are not supported for now.
   
   



##########
fluss-server/src/main/java/org/apache/fluss/server/replica/ReplicaManager.java:
##########
@@ -617,8 +673,80 @@ public void lookups(
                         tb, new LookupResultForBucket(tb, 
ApiError.fromThrowable(e)));
             }
         }
+
+        if (insertIfNotExists) {
+            checkArgument(
+                    timeoutMs != null && requiredAcks != null,
+                    "timeoutMs and requiredAcks must be set");
+            Map<TableBucket, MissingKeysContext> entriesPerBucketToInsert = 
new HashMap<>();
+            Map<TableBucket, KvRecordBatch> produceEntryData = new HashMap<>();
+            collectMissingKeysForInsert(
+                    entriesPerBucket,
+                    lookupResultForBucketMap,
+                    entriesPerBucketToInsert,
+                    produceEntryData);
+            if (!produceEntryData.isEmpty()) {
+                putRecordsToKv(
+                        timeoutMs,
+                        requiredAcks,
+                        produceEntryData,
+                        null,
+                        (result) ->
+                                responseCallback.accept(
+                                        reLookupAndMerge(
+                                                result,
+                                                entriesPerBucketToInsert,
+                                                lookupResultForBucketMap)));
+            } else {
+                // All keys exist, directly return lookup results
+                responseCallback.accept(lookupResultForBucketMap);
+            }
+        } else {
+            responseCallback.accept(lookupResultForBucketMap);
+        }
         LOG.debug("Lookup from local kv in {}ms", System.currentTimeMillis() - 
startTime);
-        responseCallback.accept(lookupResultForBucketMap);
+    }
+
+    /**
+     * Re-lookup missing keys after insert and merge results back into the 
original lookup result
+     * map.
+     */
+    private Map<TableBucket, LookupResultForBucket> reLookupAndMerge(
+            List<PutKvResultForBucket> putKvResultForBucketList,
+            Map<TableBucket, MissingKeysContext> entriesPerBucketToInsert,
+            Map<TableBucket, LookupResultForBucket> lookupResultForBucketMap) {
+        // Collect failed buckets first to avoid unnecessary re-lookup
+        Set<TableBucket> failedBuckets = new HashSet<>();
+        for (PutKvResultForBucket putKvResultForBucket : 
putKvResultForBucketList) {
+            if (putKvResultForBucket.failed()) {
+                TableBucket tb = putKvResultForBucket.getTableBucket();
+                failedBuckets.add(tb);
+                lookupResultForBucketMap.put(
+                        tb, new LookupResultForBucket(tb, 
putKvResultForBucket.getError()));
+            }
+        }
+
+        // Re-lookup only for successful inserts
+        for (Map.Entry<TableBucket, MissingKeysContext> entry :
+                entriesPerBucketToInsert.entrySet()) {
+            TableBucket tb = entry.getKey();
+            if (failedBuckets.contains(tb)) {
+                continue; // Skip buckets that failed during insert
+            }
+            MissingKeysContext missingKeysContext = entry.getValue();
+            Replica replica = getReplicaOrException(tb);
+            TableMetricGroup tableMetrics = replica.tableMetrics();
+            tableMetrics.totalLookupRequests().inc();

Review Comment:
   No need to increment here.



##########
fluss-common/src/main/java/org/apache/fluss/row/decode/KeyDecoder.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.fluss.row.decode;
+
+import org.apache.fluss.row.InternalRow;
+import org.apache.fluss.types.RowType;
+
+import java.util.List;
+
+/**
+ * Interface for decoding key bytes back to {@link InternalRow}.
+ *
+ * <p>This interface provides functionality to decode compacted key bytes into 
internal row

Review Comment:
   binary key bytes (not just compacted)



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