Jackie-Jiang commented on code in PR #8636:
URL: https://github.com/apache/pinot/pull/8636#discussion_r866175850


##########
pinot-core/src/main/java/org/apache/pinot/core/plan/FilterPlanNode.java:
##########
@@ -246,7 +247,8 @@ private BaseFilterOperator 
constructPhysicalOperator(FilterContext filter, int n
           switch (predicate.getType()) {
             case TEXT_CONTAINS:
               TextIndexReader textIndexReader = dataSource.getTextIndex();
-              if (!(textIndexReader instanceof NativeTextIndexReader)) {
+              if (!(textIndexReader instanceof NativeTextIndexReader)

Review Comment:
   Also update the check under `TEXT_MATCH`



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));

Review Comment:
   Is the analyzer stateless? If so, let's create one as `private static final` 
and reuse it instead of creating one per doc



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;

Review Comment:
   (minor) Move these 2 non-final values into a separate block for readability



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());
+    }
+
+    try {
+      _writeLock.lock();
+      for (String token : tokens) {
+        Integer currentDictId = _termToDictIdMapping.get(token);
+        if (currentDictId == null) {
+
+          currentDictId = _nextDictId;
+          _mutableFST.addPath(token, currentDictId);
+          _termToDictIdMapping.put(token, currentDictId);
+          ++_nextDictId;
+        }
+        addToPostingList(currentDictId);
+      }
+      _nextDocId++;
+    } finally {
+      _writeLock.unlock();
+    }
+  }
+
+  @Override
+  public ImmutableRoaringBitmap getDictIds(String searchQuery) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public MutableRoaringBitmap getDocIds(String searchQuery) {
+    MutableRoaringBitmap matchingDocIds = null;
+    try {
+      _readLock.lock();

Review Comment:
   (minor) Move this out of the try block



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());
+    }
+
+    try {
+      _writeLock.lock();
+      for (String token : tokens) {
+        Integer currentDictId = _termToDictIdMapping.get(token);
+        if (currentDictId == null) {
+
+          currentDictId = _nextDictId;
+          _mutableFST.addPath(token, currentDictId);
+          _termToDictIdMapping.put(token, currentDictId);
+          ++_nextDictId;
+        }
+        addToPostingList(currentDictId);
+      }
+      _nextDocId++;
+    } finally {
+      _writeLock.unlock();
+    }
+  }
+
+  @Override
+  public ImmutableRoaringBitmap getDictIds(String searchQuery) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public MutableRoaringBitmap getDocIds(String searchQuery) {
+    MutableRoaringBitmap matchingDocIds = null;
+    try {
+      _readLock.lock();
+      RoaringBitmapWriter<MutableRoaringBitmap> writer = 
RoaringBitmapWriter.bufferWriter().get();
+      RealTimeRegexpMatcher.regexMatch(searchQuery, _mutableFST, writer::add);
+      ImmutableRoaringBitmap matchingDictIds = writer.get();
+
+      for (PeekableIntIterator it = matchingDictIds.getIntIterator(); 
it.hasNext(); ) {
+        int dictId = it.next();
+        if (dictId >= 0) {
+          ImmutableRoaringBitmap docIds = _invertedIndex.getDocIds(dictId);
+          if (matchingDocIds == null) {
+            matchingDocIds = docIds.toMutableRoaringBitmap();
+          } else {
+            matchingDocIds.or(docIds);
+          }
+        }
+      }
+    } finally {
+      _readLock.unlock();
+    }
+
+    return matchingDocIds == null ? new MutableRoaringBitmap() : 
matchingDocIds;
+  }
+
+  @Override
+  public void close()
+      throws IOException {
+  }
+
+  public List<String> analyze(String text, Analyzer analyzer)
+      throws IOException {
+    List<String> result = new ArrayList<>();
+    TokenStream tokenStream = analyzer.tokenStream(_column, text);
+    CharTermAttribute attr = tokenStream.addAttribute(CharTermAttribute.class);
+    tokenStream.reset();
+    while (tokenStream.incrementToken()) {
+      result.add(attr.toString());
+    }
+    return result;
+  }
+
+  /**
+   * Adds the given value to the posting list.
+   */
+  void addToPostingList(int dictId) {

Review Comment:
   (minor) Inline this



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());
+    }
+
+    try {
+      _writeLock.lock();
+      for (String token : tokens) {
+        Integer currentDictId = _termToDictIdMapping.get(token);

Review Comment:
   Use `computeIfAbsent` to avoid the extra map lookup



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());
+    }
+
+    try {
+      _writeLock.lock();
+      for (String token : tokens) {
+        Integer currentDictId = _termToDictIdMapping.get(token);
+        if (currentDictId == null) {
+
+          currentDictId = _nextDictId;
+          _mutableFST.addPath(token, currentDictId);
+          _termToDictIdMapping.put(token, currentDictId);
+          ++_nextDictId;
+        }
+        addToPostingList(currentDictId);
+      }
+      _nextDocId++;
+    } finally {
+      _writeLock.unlock();
+    }
+  }
+
+  @Override
+  public ImmutableRoaringBitmap getDictIds(String searchQuery) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public MutableRoaringBitmap getDocIds(String searchQuery) {
+    MutableRoaringBitmap matchingDocIds = null;
+    try {
+      _readLock.lock();
+      RoaringBitmapWriter<MutableRoaringBitmap> writer = 
RoaringBitmapWriter.bufferWriter().get();
+      RealTimeRegexpMatcher.regexMatch(searchQuery, _mutableFST, writer::add);
+      ImmutableRoaringBitmap matchingDictIds = writer.get();
+
+      for (PeekableIntIterator it = matchingDictIds.getIntIterator(); 
it.hasNext(); ) {
+        int dictId = it.next();
+        if (dictId >= 0) {
+          ImmutableRoaringBitmap docIds = _invertedIndex.getDocIds(dictId);
+          if (matchingDocIds == null) {
+            matchingDocIds = docIds.toMutableRoaringBitmap();
+          } else {
+            matchingDocIds.or(docIds);
+          }
+        }
+      }
+    } finally {
+      _readLock.unlock();
+    }
+
+    return matchingDocIds == null ? new MutableRoaringBitmap() : 
matchingDocIds;

Review Comment:
   No need to create a bitmap for the dict ids. This can be simplified
   ```suggestion
       MutableRoaringBitmap matchingDocIds = new MutableRoaringBitmap();
       _readLock.lock();
       try {
         RealTimeRegexpMatcher.regexMatch(searchQuery, _mutableFST, dictId -> 
matchingDocIds.or(_invertedIndex.getDocIds(dictId)));
         return matchingDocIds;
         }
       } finally {
         _readLock.unlock();
       }
   ```



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());

Review Comment:
   Keep the underlying stack trace for debugging purpose
   ```suggestion
         throw new RuntimeException(e);
   ```



##########
pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/NativeRealtimeClusterIntegrationTest.java:
##########
@@ -0,0 +1,214 @@
+/**
+ * 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.integration.tests;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.commons.io.FileUtils;
+import org.apache.pinot.spi.config.table.FieldConfig;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.util.TestUtils;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.AssertJUnit.fail;
+
+
+public class NativeRealtimeClusterIntegrationTest extends 
BaseClusterIntegrationTest {

Review Comment:
   Can we refactor `LuceneRealtimeClusterIntegrationTest` to 
`TextIndexRealtimeClusterIntegrationTest` and integrate this into the same test?



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());
+    }
+
+    try {
+      _writeLock.lock();
+      for (String token : tokens) {
+        Integer currentDictId = _termToDictIdMapping.get(token);
+        if (currentDictId == null) {
+

Review Comment:
   (nit) remove empty line



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;

Review Comment:
   Ideally we should use a mutable dictionary to store the value mapping so 
that it can be off-heap. We may add a TODO here and address that separately.
   One low hanging optimization is to use `Object2IntOpenHashMap` which has 
better performance and lower memory footprint



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {

Review Comment:
   Suggest renaming it to `NativeMutableTextIndex`



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());
+    }
+
+    try {
+      _writeLock.lock();
+      for (String token : tokens) {
+        Integer currentDictId = _termToDictIdMapping.get(token);
+        if (currentDictId == null) {
+
+          currentDictId = _nextDictId;

Review Comment:
   (nit) Can be simplified
   ```suggestion
             currentDictId = _nextDictId++;
   ```



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeNativeTextIndex.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import 
org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+public class RealtimeNativeTextIndex implements MutableTextIndex {
+  private final String _column;
+  private final MutableFST _mutableFST;
+  private final RealtimeInvertedIndex _invertedIndex;
+  private final Map<String, Integer> _termToDictIdMapping;
+  private int _nextDocId = 0;
+  private int _nextDictId = 0;
+  private final ReentrantReadWriteLock.ReadLock _readLock;
+  private final ReentrantReadWriteLock.WriteLock _writeLock;
+
+  public RealtimeNativeTextIndex(String column) {
+    _column = column;
+    _mutableFST = new MutableFSTImpl();
+    _termToDictIdMapping = new HashMap<>();
+    _invertedIndex = new RealtimeInvertedIndex();
+
+    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    _readLock = readWriteLock.readLock();
+    _writeLock = readWriteLock.writeLock();
+  }
+
+  @Override
+  public void add(String document) {
+    List<String> tokens;
+    try {
+      tokens = analyze(document, new 
StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET));
+    } catch (IOException e) {
+      throw new RuntimeException(e.getMessage());
+    }
+
+    try {
+      _writeLock.lock();

Review Comment:
   (minor) Move this out of the `try` block



-- 
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

Reply via email to