richardstartin commented on a change in pull request #8411: URL: https://github.com/apache/pinot/pull/8411#discussion_r838098563
########## File path: pinot-core/src/main/java/org/apache/pinot/core/operator/filter/BitmapCollection.java ########## @@ -0,0 +1,103 @@ +/** + * 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.operator.filter; + +import org.roaringbitmap.buffer.BufferFastAggregation; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +public class BitmapCollection { + private final int _numDocs; + private boolean _inverted; + private final ImmutableRoaringBitmap[] _bitmaps; + + public BitmapCollection(int numDocs, boolean inverted, ImmutableRoaringBitmap... bitmaps) { + _numDocs = numDocs; + _inverted = inverted; + _bitmaps = bitmaps; + } + + public boolean isInverted() { + return _inverted; + } + + public ImmutableRoaringBitmap[] getBitmaps() { + return _bitmaps; + } + + public BitmapCollection invert() { + _inverted = !_inverted; + return this; + } + + public int andCardinality(BitmapCollection other) { + if (!_inverted) { + if (!other._inverted) { + return ImmutableRoaringBitmap.andCardinality(reduceInternal(), other.reduceInternal()); + } + return ImmutableRoaringBitmap.andNotCardinality(reduceInternal(), other.reduceInternal()); + } else { + if (!other._inverted) { + return ImmutableRoaringBitmap.andNotCardinality(other.reduceInternal(), reduceInternal()); + } + return _numDocs - ImmutableRoaringBitmap.orCardinality(reduceInternal(), other.reduceInternal()); + } + } + + public int orCardinality(BitmapCollection other) { + if (!_inverted) { + if (!other._inverted) { + return ImmutableRoaringBitmap.orCardinality(reduceInternal(), other.reduceInternal()); + } + ImmutableRoaringBitmap reduced = other.reduceInternal(); + return _numDocs - reduced.getCardinality() - ImmutableRoaringBitmap.andCardinality(reduceInternal(), reduced); + } else { + if (!other._inverted) { + ImmutableRoaringBitmap reduced = reduceInternal(); + return _numDocs - reduced.getCardinality() + + ImmutableRoaringBitmap.andCardinality(other.reduceInternal(), reduced); + } + return _numDocs - ImmutableRoaringBitmap.andCardinality(reduceInternal(), other.reduceInternal()); + } + } + + private ImmutableRoaringBitmap reduceInternal() { + if (_bitmaps.length == 1) { + return _bitmaps[0]; + } + return BufferFastAggregation.or(_bitmaps); + } + + public ImmutableRoaringBitmap reduce() { + if (!_inverted) { + return reduceInternal(); + } + return invertedOr(); + } + + private MutableRoaringBitmap invertedOr() { Review comment: I don’t think this is necessary. -- 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