mayankshriv commented on a change in pull request #4547: Indexed table URL: https://github.com/apache/incubator-pinot/pull/4547#discussion_r316003385
########## File path: pinot-core/src/main/java/org/apache/pinot/core/data/table/IndexedTable.java ########## @@ -0,0 +1,163 @@ +/** + * 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.table; + +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.data.order.OrderByUtils; +import org.apache.pinot.core.query.aggregation.function.AggregationFunction; +import org.apache.pinot.core.query.aggregation.function.AggregationFunctionUtils; + + +/** + * {@link Table} implementation for aggregating TableRecords based on combination of keys + */ +public class IndexedTable implements Table { + + // When table reaches max capacity, we will allow 20% more records to get inserted (bufferedCapacity) + // If records beyond bufferedCapacity are received, the table will undergo sort and evict upto evictCapacity (10% more than capacity) + // This is to ensure that for a small number beyond capacity, a fair chance is given to all records which have the potential to climb up the order + /** 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<Record> _records; + private ConcurrentMap<Record, 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; + + _bufferedCapacity = (int) (maxCapacity * BUFFER_FACTOR); + _evictCapacity = (int) (maxCapacity * EVICTION_FACTOR); + + _records = new ArrayList<>(_bufferedCapacity); + _lookupTable = new ConcurrentHashMap<>(_bufferedCapacity); + _readWriteLock = new ReentrantReadWriteLock(); + + _aggregationFunctions = new ArrayList<>(aggregationInfos.size()); + for (AggregationInfo aggregationInfo : aggregationInfos) { + _aggregationFunctions.add( + AggregationFunctionUtils.getAggregationFunctionContext(aggregationInfo).getAggregationFunction()); + } + } + + @Override + public boolean upsert(@Nonnull Record newRecord) { + + Object[] keys = newRecord.getKeys(); + Preconditions.checkNotNull(keys, "Cannot upsert record with null keys"); + + _lookupTable.compute(newRecord, (k, index) -> { + if (index == null && size() >= _bufferedCapacity) { + _readWriteLock.writeLock().lock(); Review comment: I think a lock is needed to check the size as well to ensure we never go beyond capacity. However, this will have a performance penalty, as each upsert will acquire a write lock. So may be better to add a comment that we can go over the capacity (at most num threads), but not worth the performance penalty to fix. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
