deemoliu commented on code in PR #16364: URL: https://github.com/apache/pinot/pull/16364#discussion_r2345327764
########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNgramFilteringIndex.java: ########## @@ -0,0 +1,191 @@ +/** + * 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.realtime.impl.invertedindex; + +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import java.io.IOException; +import java.util.ArrayList; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.lucene.analysis.ngram.NGramTokenizer; +import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +/** + * RealtimeNgramFilteringIndex is a mutable inverted index that supports adding document IDs for n-grams. An n-gram + * is a contiguous sequence of n characters from input text. The length of the n-gram is variable between + * [minNgramLength, maxNgramLength] as specified in the index config. Internally, it uses a realtime inverted index + * which maps n-grams to their posting lists (document IDs). The index is thread-safe for single writer and multiple + * readers. + */ +public class RealtimeNgramFilteringIndex implements MutableTextIndex { + private final String _column; + // Under the hood, ngram index is implemented as an inverted index from n-grams to their posting lists. + private final RealtimeInvertedIndex _invertedIndex; + // A mapping from n-grams to their dictionary IDs. + private final Object2IntOpenHashMap<String> _ngramToDictIdMapping; + // Read and write locks for thread safety. + private final ReentrantReadWriteLock.ReadLock _readLock; + private final ReentrantReadWriteLock.WriteLock _writeLock; + + // Next document ID to be assigned. + private int _nextDocId = 0; + private int _nextDictId = 0; + + private final int _minNgramLength; + private final int _maxNgramLength; + + public RealtimeNgramFilteringIndex(String column, int minNgramLength, int maxNgramLength) { + _column = column; + _invertedIndex = new RealtimeInvertedIndex(); + _ngramToDictIdMapping = new Object2IntOpenHashMap<>(); + _minNgramLength = minNgramLength; + _maxNgramLength = maxNgramLength; + + ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); + _readLock = readWriteLock.readLock(); + _writeLock = readWriteLock.writeLock(); + } + + /** + * Index the string value + * + * @param value the input value as a string + */ + @Override + public void add(String value) { + if (value == null) { + return; + } + addHelper(value); + _nextDocId++; + } + + /** + * Index an array of string values. Each string in the array is treated as a separate document. + * + * @param values the documents as an array of strings + */ + @Override + public void add(String[] values) { + if (values == null || values.length == 0) { + return; + } + for (String value : values) { + if (value != null) { + addHelper(value); + } + } + _nextDocId++; + } + + /** + * Returns the matching dictionary ids for the given search query (optional). + * + * @param searchQuery as a literal string + */ + @Override + public ImmutableRoaringBitmap getDictIds(String searchQuery) { + throw new UnsupportedOperationException(); + } + + /** + * Returns the matching document ids for the given search query. + * Returns null if no n-grams are generated from the search query -- either because the search query is null or empty + * or shorter than the minimum n-gram length. + * @param searchQuery as a literal string + */ + @Override + public MutableRoaringBitmap getDocIds(String searchQuery) { + _readLock.lock(); + Iterable<String> ngrams = generateNgrams(searchQuery); + if (!ngrams.iterator().hasNext()) { + return null; // No n-grams generated, return null. + } + ArrayList<MutableRoaringBitmap> bitmapLst = new ArrayList<>(); + try { + for (String ngram : ngrams) { + int dictId = _ngramToDictIdMapping.getInt(ngram); + bitmapLst.add(_invertedIndex.getDocIds(dictId)); + } + if (bitmapLst.isEmpty()) { + return null; // No n-grams found in the index. + } + MutableRoaringBitmap resultBitmap = bitmapLst.get(0); + for (int i = 1; i < bitmapLst.size(); i++) { + resultBitmap.and(bitmapLst.get(i)); + } + return resultBitmap; + } finally { + _readLock.unlock(); + } + } + + /** + * Closes this stream and releases any system resources associated with it. If the stream is already closed then + * invoking this method has no effect. + * + * <p> As noted in {@link AutoCloseable#close()}, cases where the + * close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to + * internally + * <em>mark</em> the {@code Closeable} as closed, prior to throwing + * the {@code IOException}. + * + * @throws IOException if an I/O error occurs + */ + @Override + public void close() + throws IOException { Review Comment: should we add _ngramToDictIdMapping.clear(); -- 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]
