kishoreg commented on a change in pull request #4547: Indexed table
URL: https://github.com/apache/incubator-pinot/pull/4547#discussion_r315949299
 
 

 ##########
 File path: 
pinot-core/src/main/java/org/apache/pinot/core/data/IndexedTableImpl.java
 ##########
 @@ -0,0 +1,169 @@
+/**
+ * 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.core.data;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import javax.annotation.Nonnull;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.pinot.common.request.AggregationInfo;
+import org.apache.pinot.common.request.SelectionSort;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.query.aggregation.function.AggregationFunction;
+import 
org.apache.pinot.core.query.aggregation.function.AggregationFunctionUtils;
+
+
+/**
+ * {@link IndexedTable} implementation for aggregating TableRecords based on 
combination of keys
+ */
+public class IndexedTableImpl implements IndexedTable {
+
+  /** Factor used to add buffer to maxCapacity of the Collection used **/
+  private static final double BUFFER_FACTOR = 1.2;
+  /** Factor used to decide eviction threshold **/
+  private static final double EVICTION_FACTOR = 1.1;
+
+  private List<TableRecord> _records = new ArrayList<>();
+  private ConcurrentMap<TableRecord, Integer> _lookupTable;
+  private ReentrantReadWriteLock _readWriteLock;
+
+  private DataSchema _dataSchema;
+  private List<AggregationInfo> _aggregationInfos;
+  private List<AggregationFunction> _aggregationFunctions;
+  private List<SelectionSort> _orderBy;
+  private int _evictCapacity;
+  private int _bufferedCapacity;
+
+  @Override
+  public void init(@Nonnull DataSchema dataSchema, List<AggregationInfo> 
aggregationInfos, List<SelectionSort> orderBy,
+      int maxCapacity) {
+    _dataSchema = dataSchema;
+    _aggregationInfos = aggregationInfos;
+    _orderBy = orderBy;
+
+    // maintain a buffer size 20% more than max capacity
+    // this is to ensure bottom records get a fair chance
+    _bufferedCapacity = (int) (maxCapacity * BUFFER_FACTOR);
+    // after eviction, bring down size to 10% more than max capacity
+    _evictCapacity = (int) (maxCapacity * EVICTION_FACTOR);
+
+    _lookupTable = new ConcurrentHashMap<>(_bufferedCapacity);
+    _readWriteLock = new ReentrantReadWriteLock();
+
+    if (CollectionUtils.isNotEmpty(aggregationInfos)) {
+      _aggregationFunctions = new ArrayList<>(aggregationInfos.size());
+      for (AggregationInfo aggregationInfo : aggregationInfos) {
+        _aggregationFunctions.add(
+            
AggregationFunctionUtils.getAggregationFunctionContext(aggregationInfo).getAggregationFunction());
+      }
+    }
+  }
+
+  @Override
+  public boolean upsert(@Nonnull TableRecord newRecord) {
+
+    Object[] keys = newRecord._keys;
+    Preconditions.checkNotNull(keys, "Cannot upsert record with null keys");
+
+    if (size() >= _bufferedCapacity && !_lookupTable.containsKey(newRecord)) {
+      _readWriteLock.writeLock().lock();
+      try {
+        if (size() >= _bufferedCapacity) {
+          sort();
+          _records = _records.subList(0, _evictCapacity);
+          rebuildLookupTable();
+        }
+      } finally {
+        _readWriteLock.writeLock().unlock();
+      }
+    }
+
+    _readWriteLock.readLock().lock();
+    try {
+      _lookupTable.compute(newRecord, (k, index) -> {
 
 Review comment:
   this can be merged with containsKey in previous section

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to