kkrugler commented on code in PR #8636: URL: https://github.com/apache/pinot/pull/8636#discussion_r866241674
########## 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: I think you can reuse an `Analyzer`, though it's a bit tricky. Here's code we use to extract terms from text, by wrapping a Lucene Analyzer (note pending optimization to have a reusable `Reader`): ``` java public class TermsParser implements Closeable { private Analyzer _analyzer; // These need to be thread local, since Lucene has them as thread local as well. private ThreadLocal<TokenStream> _tokenStream = new ThreadLocal<TokenStream>(); private ThreadLocal<CharTermAttribute> _token = new ThreadLocal<CharTermAttribute>(); /** * Set up an Analyzer to use while parsing text. * * * @param termsAnalyzerFactory */ public TermsParser(Analyzer analyzer) { _analyzer = analyzer; } public Iterable<String> parse(CharSequence in) { // TODO make reader reusable, so we can reset it return parse(new CharSequenceReader(in)); } public Iterable<String> parse(Reader in) { try { if (_tokenStream.get() != null) { _tokenStream.get().close(); } _tokenStream.set(_analyzer.tokenStream("content", in)); _tokenStream.get().reset(); if (_token.get() == null) { _token.set((CharTermAttribute) _tokenStream.get().addAttribute(CharTermAttribute.class)); } } catch (IOException e) { throw new RuntimeException("Impossible exception", e); } _token.get().setEmpty(); return new Iterable<String>() { @Override public Iterator<String> iterator() { return new TokenIterator(_tokenStream.get(), _token.get()); } }; } private static class TokenIterator implements Iterator<String> { private TokenStream _tokenStream; private CharTermAttribute _token; public TokenIterator(TokenStream tokenStream, CharTermAttribute token) { _tokenStream = tokenStream; _token = token; } @Override public boolean hasNext() { if (_token.length() == 0) { try { _tokenStream.incrementToken(); } catch (IOException e) { throw new RuntimeException("Impossible exception", e); } } return _token.length() > 0; } @Override public String next() { if (!hasNext()) { throw new NoSuchElementException(); } String result = new String(_token.buffer(), 0, _token.length()); _token.setEmpty(); return result; } } @Override public void close() { _analyzer.close(); } } ``` -- 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