saurabhd336 commented on code in PR #8708:
URL: https://github.com/apache/pinot/pull/8708#discussion_r887552243


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/dedup/PartitionDedupMetadataManager.java:
##########
@@ -0,0 +1,154 @@
+/**
+ * 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.pinot.segment.local.dedup;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.helix.HelixManager;
+import org.apache.pinot.common.metrics.ServerGauge;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.segment.local.segment.readers.PinotSegmentColumnReader;
+import org.apache.pinot.segment.local.utils.HashUtils;
+import org.apache.pinot.segment.local.utils.RecordInfo;
+import org.apache.pinot.segment.local.utils.tablestate.TableState;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.spi.config.table.HashFunction;
+import org.apache.pinot.spi.data.readers.PrimaryKey;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PartitionDedupMetadataManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PartitionDedupMetadataManager.class);
+
+  private final HelixManager _helixManager;
+  private final String _tableNameWithType;
+  private final List<String> _primaryKeyColumns;
+  private final int _partitionId;
+  private final ServerMetrics _serverMetrics;
+  private final HashFunction _hashFunction;
+  private boolean _allSegmentsLoaded;
+
+  // TODO(saurabh) : We can replace this with a ocncurrent Set
+  @VisibleForTesting
+  final ConcurrentHashMap<Object, IndexSegment> _primaryKeySet = new 
ConcurrentHashMap<>();
+
+  public PartitionDedupMetadataManager(HelixManager helixManager, String 
tableNameWithType,
+      List<String> primaryKeyColumns, int partitionId, ServerMetrics 
serverMetrics, HashFunction hashFunction) {
+    _helixManager = helixManager;
+    _tableNameWithType = tableNameWithType;
+    _primaryKeyColumns = primaryKeyColumns;
+    _partitionId = partitionId;
+    _serverMetrics = serverMetrics;
+    _hashFunction = hashFunction;
+  }
+
+  public void addSegment(IndexSegment segment) {
+    // Add all PKs to _primaryKeySet
+    Iterator<RecordInfo> recordInfoIterator = getRecordInfoIterator(segment, 
_primaryKeyColumns);
+    while (recordInfoIterator.hasNext()) {
+      RecordInfo recordInfo = recordInfoIterator.next();
+      _primaryKeySet.put(HashUtils.hashPrimaryKey(recordInfo.getPrimaryKey(), 
_hashFunction), segment);
+    }
+    _serverMetrics.setValueOfPartitionGauge(_tableNameWithType, _partitionId, 
ServerGauge.DEDUP_PRIMARY_KEYS_COUNT,
+        _primaryKeySet.size());
+  }
+
+  public void removeSegment(IndexSegment segment) {
+    // TODO(saurabh): Explain reload scenario here
+    Iterator<RecordInfo> recordInfoIterator = getRecordInfoIterator(segment, 
_primaryKeyColumns);
+    while (recordInfoIterator.hasNext()) {
+      RecordInfo recordInfo = recordInfoIterator.next();
+      
_primaryKeySet.compute(HashUtils.hashPrimaryKey(recordInfo.getPrimaryKey(), 
_hashFunction),
+          (primaryKey, currentSegment) -> {
+            if (currentSegment == segment) {
+              return null;
+            } else {
+              return currentSegment;
+            }
+          });
+    }
+    _serverMetrics.setValueOfPartitionGauge(_tableNameWithType, _partitionId, 
ServerGauge.DEDUP_PRIMARY_KEYS_COUNT,
+        _primaryKeySet.size());
+  }
+
+  @VisibleForTesting
+  public static Iterator<RecordInfo> getRecordInfoIterator(IndexSegment 
segment, List<String> primaryKeyColumns) {

Review Comment:
   Ack. Didn't see any big impact of changing the method signature to accept 
PK, hence made that change too.



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to