maedhroz commented on code in PR #2409: URL: https://github.com/apache/cassandra/pull/2409#discussion_r1228355204
########## src/java/org/apache/cassandra/index/sai/disk/v1/bbtree/NumericIndexWriter.java: ########## @@ -0,0 +1,179 @@ +/* + * 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.cassandra.index.sai.disk.v1.bbtree; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; + +import org.apache.cassandra.index.sai.IndexContext; +import org.apache.cassandra.index.sai.disk.format.IndexComponent; +import org.apache.cassandra.index.sai.disk.format.IndexDescriptor; +import org.apache.cassandra.index.sai.disk.v1.segment.SegmentMetadata; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.packed.PackedInts; +import org.apache.lucene.util.packed.PackedLongValues; + +import static com.google.common.base.Preconditions.checkArgument; + + +/** + * Specialized writer for 1-dim point values, that builds them into a BKD tree with auxiliary posting lists on eligible + * tree levels. + * + * Given sorted input {@link org.apache.lucene.codecs.MutablePointValues}, 1-dim case allows to optimise flush process, because we don't need to + * buffer all point values to sort them. + */ +public class NumericIndexWriter +{ + public static final int MAX_POINTS_IN_LEAF_NODE = BlockBalancedTreeWriter.DEFAULT_MAX_POINTS_IN_LEAF_NODE; + + private final BlockBalancedTreeWriter writer; + private final IndexDescriptor indexDescriptor; + private final IndexContext indexContext; + private final int bytesPerValue; + + /** + * @param maxSegmentRowId maximum possible segment row ID, used to create `maxDoc` for kd-tree + */ + public NumericIndexWriter(IndexDescriptor indexDescriptor, + IndexContext indexContext, + int bytesPerValue, + long maxSegmentRowId) + { + this(indexDescriptor, indexContext, MAX_POINTS_IN_LEAF_NODE, bytesPerValue, maxSegmentRowId); + } + + @VisibleForTesting + public NumericIndexWriter(IndexDescriptor indexDescriptor, + IndexContext indexContext, + int maxPointsInLeafNode, + int bytesPerValue, + long maxSegmentRowId) + { + checkArgument(maxSegmentRowId >= 0, "[%s] maxRowId must be non-negative value, but got %s", indexContext.getIndexName(), maxSegmentRowId); + + this.indexDescriptor = indexDescriptor; + this.indexContext = indexContext; + this.bytesPerValue = bytesPerValue; + this.writer = new BlockBalancedTreeWriter(maxSegmentRowId + 1, bytesPerValue, maxPointsInLeafNode); + } + + @Override + public String toString() + { + return MoreObjects.toStringHelper(this) + .add("bytesPerDim", bytesPerValue) + .add("bufferedPoints", writer.getPointCount()) + .toString(); + } + + public static class LeafCallback implements BlockBalancedTreeWriter.OneDimensionBKDWriterCallback + { + final List<PackedLongValues> postings = new ArrayList<>(); + + public int numLeaves() + { + return postings.size(); + } + + @Override + public void writeLeafDocs(BlockBalancedTreeWriter.RowIDAndIndex[] sortedByRowID, int offset, int count) + { + PackedLongValues.Builder builder = PackedLongValues.monotonicBuilder(PackedInts.COMPACT); + + for (int i = offset; i < count; ++i) + { + builder.add(sortedByRowID[i].rowID); + } + postings.add(builder.build()); + } + } + + /** + * Writes a k-d tree and posting lists from a {@link org.apache.lucene.codecs.MutablePointValues}. + * + * @param values points to write + * + * @return metadata describing the location and size of this kd-tree in the overall SSTable kd-tree component file + */ + public SegmentMetadata.ComponentMetadataMap writeAll(IntersectingPointValues values) throws IOException Review Comment: For variable names and such, I'm fine with "tree" as a prefix, tbh It's not like it's unclear which tree we mean in this context. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

