Jackie-Jiang commented on a change in pull request #8384: URL: https://github.com/apache/pinot/pull/8384#discussion_r834688008
########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/text/NativeTextIndexReader.java ########## @@ -0,0 +1,141 @@ +/** + * 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.segment.index.readers.text; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Collections; +import java.util.PrimitiveIterator; +import org.apache.avro.util.ByteBufferInputStream; +import org.apache.pinot.segment.local.segment.index.readers.BitmapInvertedIndexReader; +import org.apache.pinot.segment.local.utils.nativefst.FST; +import org.apache.pinot.segment.local.utils.nativefst.ImmutableFST; +import org.apache.pinot.segment.local.utils.nativefst.NativeTextIndexCreator; +import org.apache.pinot.segment.local.utils.nativefst.utils.RegexpMatcher; +import org.apache.pinot.segment.spi.index.reader.TextIndexReader; +import org.apache.pinot.segment.spi.memory.PinotDataBuffer; +import org.apache.pinot.segment.spi.store.SegmentDirectoryPaths; +import org.roaringbitmap.RoaringBitmapWriter; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; +import org.slf4j.LoggerFactory; + + +public class NativeTextIndexReader implements TextIndexReader { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NativeTextIndexReader.class); Review comment: (minor) Import `org.slf4j.Logger` ########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/SegmentColumnarIndexCreator.java ########## @@ -211,7 +208,15 @@ public void init(SegmentGeneratorConfig segmentCreationSpec, SegmentIndexCreatio } if (textIndexColumns.contains(columnName)) { - _textIndexCreatorMap.put(columnName, _indexCreatorProvider.newTextIndexCreator(context.forTextIndex(true))); + Map<String, String> columnProperties = _columnProperties.get(columnName); + FSTType fstType = FSTType.LUCENE; + Review comment: (minor, readability) Can we remove the empty lines in this if block? I find them a little bit hard to read (the logic flow for the empty line separated blocks are continuous) ########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/loader/IndexLoadingConfig.java ########## @@ -197,6 +198,11 @@ private void extractTextIndexColumnsFromTableConfig(TableConfig tableConfig) { String column = fieldConfig.getName(); if (fieldConfig.getIndexType() == FieldConfig.IndexType.TEXT) { _textIndexColumns.add(column); + Map<String, String> propertiesMap = fieldConfig.getProperties(); + Review comment: (minor, readability) Remove this empty line ########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/text/NativeTextIndexReader.java ########## @@ -0,0 +1,141 @@ +/** + * 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.segment.index.readers.text; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Collections; +import java.util.PrimitiveIterator; +import org.apache.avro.util.ByteBufferInputStream; +import org.apache.pinot.segment.local.segment.index.readers.BitmapInvertedIndexReader; +import org.apache.pinot.segment.local.utils.nativefst.FST; +import org.apache.pinot.segment.local.utils.nativefst.ImmutableFST; +import org.apache.pinot.segment.local.utils.nativefst.NativeTextIndexCreator; +import org.apache.pinot.segment.local.utils.nativefst.utils.RegexpMatcher; +import org.apache.pinot.segment.spi.index.reader.TextIndexReader; +import org.apache.pinot.segment.spi.memory.PinotDataBuffer; +import org.apache.pinot.segment.spi.store.SegmentDirectoryPaths; +import org.roaringbitmap.RoaringBitmapWriter; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; +import org.slf4j.LoggerFactory; + + +public class NativeTextIndexReader implements TextIndexReader { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NativeTextIndexReader.class); + + private final String _column; + private int _numDocs; + private final File _indexFile; + private final PinotDataBuffer _buffer; + + private FST _fst; + private BitmapInvertedIndexReader _invertedIndex; + + public NativeTextIndexReader(String column, File indexDir, int numDocs) { + _column = column; + _numDocs = numDocs; + try { + _indexFile = getTextIndexFile(indexDir); + _buffer = PinotDataBuffer.loadBigEndianFile(_indexFile); + + populateIndexes(); + } catch (Exception e) { + LOGGER.error("Failed to instantiate Lucene text index reader for column {}, exception {}", column, + e.getMessage()); + throw new RuntimeException(e); + } + } + + private File getTextIndexFile(File segmentIndexDir) { + // will return null if file does not exist + File file = SegmentDirectoryPaths.findTextIndexIndexFileNative(segmentIndexDir, _column); + if (file == null) { + throw new IllegalStateException("Failed to find text index file for column: " + _column); + } + return file; + } + + private void populateIndexes() { + long invertedIndexLength = _buffer.getLong(4); + long fstDataLength = _buffer.getLong(12); + int numBitMaps = _buffer.getInt(20); + + long fstDataStartOffset = NativeTextIndexCreator.HEADER_LENGTH; + long fstDataEndOffset = fstDataStartOffset + fstDataLength; + ByteBuffer byteBuffer = _buffer.toDirectByteBuffer(fstDataStartOffset, (int) fstDataLength); + byte[] arr = new byte[byteBuffer.remaining()]; + // byteBuffer.get(arr); + try { + _fst = FST.read(new ByteBufferInputStream(Collections.singletonList(byteBuffer)), ImmutableFST.class, true); + } catch (IOException e) { + throw new RuntimeException(e.getMessage()); + } + + long invertedIndexEndOffset = fstDataEndOffset + invertedIndexLength; + _invertedIndex = + new BitmapInvertedIndexReader(_buffer.view(fstDataEndOffset, invertedIndexEndOffset, ByteOrder.BIG_ENDIAN), + numBitMaps); + } + + @Override + public ImmutableRoaringBitmap getDictIds(String searchQuery) { + try { + RoaringBitmapWriter<MutableRoaringBitmap> writer = RoaringBitmapWriter.bufferWriter().get(); + RegexpMatcher.regexMatch(searchQuery, _fst, writer::add); + return writer.get(); + } catch (Exception e) { + throw new RuntimeException("Caught exception while running query: " + searchQuery, e); + } + } + + @Override + public MutableRoaringBitmap getDocIds(String searchQuery) { Review comment: As one of the basic requirement, we should support logical operators in the search query ########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/text/NativeTextIndexReader.java ########## @@ -0,0 +1,141 @@ +/** + * 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.segment.index.readers.text; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Collections; +import java.util.PrimitiveIterator; +import org.apache.avro.util.ByteBufferInputStream; +import org.apache.pinot.segment.local.segment.index.readers.BitmapInvertedIndexReader; +import org.apache.pinot.segment.local.utils.nativefst.FST; +import org.apache.pinot.segment.local.utils.nativefst.ImmutableFST; +import org.apache.pinot.segment.local.utils.nativefst.NativeTextIndexCreator; +import org.apache.pinot.segment.local.utils.nativefst.utils.RegexpMatcher; +import org.apache.pinot.segment.spi.index.reader.TextIndexReader; +import org.apache.pinot.segment.spi.memory.PinotDataBuffer; +import org.apache.pinot.segment.spi.store.SegmentDirectoryPaths; +import org.roaringbitmap.RoaringBitmapWriter; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; +import org.slf4j.LoggerFactory; + + +public class NativeTextIndexReader implements TextIndexReader { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NativeTextIndexReader.class); + + private final String _column; + private int _numDocs; + private final File _indexFile; + private final PinotDataBuffer _buffer; + + private FST _fst; + private BitmapInvertedIndexReader _invertedIndex; + + public NativeTextIndexReader(String column, File indexDir, int numDocs) { Review comment: The constructor should take a `PinotDataBuffer` instead of `column` and `indexDir`. The `PinotDataBuffer` can be part of the combined index file ########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/text/NativeTextIndexReader.java ########## @@ -0,0 +1,141 @@ +/** + * 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.segment.index.readers.text; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Collections; +import java.util.PrimitiveIterator; +import org.apache.avro.util.ByteBufferInputStream; +import org.apache.pinot.segment.local.segment.index.readers.BitmapInvertedIndexReader; +import org.apache.pinot.segment.local.utils.nativefst.FST; +import org.apache.pinot.segment.local.utils.nativefst.ImmutableFST; +import org.apache.pinot.segment.local.utils.nativefst.NativeTextIndexCreator; +import org.apache.pinot.segment.local.utils.nativefst.utils.RegexpMatcher; +import org.apache.pinot.segment.spi.index.reader.TextIndexReader; +import org.apache.pinot.segment.spi.memory.PinotDataBuffer; +import org.apache.pinot.segment.spi.store.SegmentDirectoryPaths; +import org.roaringbitmap.RoaringBitmapWriter; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; +import org.slf4j.LoggerFactory; + + +public class NativeTextIndexReader implements TextIndexReader { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NativeTextIndexReader.class); + + private final String _column; + private int _numDocs; Review comment: All the variables can be made `final` ########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/text/NativeTextIndexReader.java ########## @@ -0,0 +1,141 @@ +/** + * 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.segment.index.readers.text; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Collections; +import java.util.PrimitiveIterator; +import org.apache.avro.util.ByteBufferInputStream; +import org.apache.pinot.segment.local.segment.index.readers.BitmapInvertedIndexReader; +import org.apache.pinot.segment.local.utils.nativefst.FST; +import org.apache.pinot.segment.local.utils.nativefst.ImmutableFST; +import org.apache.pinot.segment.local.utils.nativefst.NativeTextIndexCreator; +import org.apache.pinot.segment.local.utils.nativefst.utils.RegexpMatcher; +import org.apache.pinot.segment.spi.index.reader.TextIndexReader; +import org.apache.pinot.segment.spi.memory.PinotDataBuffer; +import org.apache.pinot.segment.spi.store.SegmentDirectoryPaths; +import org.roaringbitmap.RoaringBitmapWriter; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; +import org.slf4j.LoggerFactory; + + +public class NativeTextIndexReader implements TextIndexReader { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NativeTextIndexReader.class); + + private final String _column; + private int _numDocs; + private final File _indexFile; + private final PinotDataBuffer _buffer; + + private FST _fst; + private BitmapInvertedIndexReader _invertedIndex; + + public NativeTextIndexReader(String column, File indexDir, int numDocs) { + _column = column; + _numDocs = numDocs; + try { + _indexFile = getTextIndexFile(indexDir); + _buffer = PinotDataBuffer.loadBigEndianFile(_indexFile); + + populateIndexes(); + } catch (Exception e) { + LOGGER.error("Failed to instantiate Lucene text index reader for column {}, exception {}", column, + e.getMessage()); + throw new RuntimeException(e); + } + } + + private File getTextIndexFile(File segmentIndexDir) { + // will return null if file does not exist + File file = SegmentDirectoryPaths.findTextIndexIndexFileNative(segmentIndexDir, _column); + if (file == null) { + throw new IllegalStateException("Failed to find text index file for column: " + _column); + } + return file; + } + + private void populateIndexes() { + long invertedIndexLength = _buffer.getLong(4); + long fstDataLength = _buffer.getLong(12); + int numBitMaps = _buffer.getInt(20); + + long fstDataStartOffset = NativeTextIndexCreator.HEADER_LENGTH; + long fstDataEndOffset = fstDataStartOffset + fstDataLength; + ByteBuffer byteBuffer = _buffer.toDirectByteBuffer(fstDataStartOffset, (int) fstDataLength); + byte[] arr = new byte[byteBuffer.remaining()]; + // byteBuffer.get(arr); + try { + _fst = FST.read(new ByteBufferInputStream(Collections.singletonList(byteBuffer)), ImmutableFST.class, true); + } catch (IOException e) { + throw new RuntimeException(e.getMessage()); + } + + long invertedIndexEndOffset = fstDataEndOffset + invertedIndexLength; + _invertedIndex = + new BitmapInvertedIndexReader(_buffer.view(fstDataEndOffset, invertedIndexEndOffset, ByteOrder.BIG_ENDIAN), + numBitMaps); + } + + @Override + public ImmutableRoaringBitmap getDictIds(String searchQuery) { + try { Review comment: Should throw `UnsupportedException` here as we should never ask for dictionary ids from text index (the returned dictionary id is not the dictionary id for the column, but for the tokens) ########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/nativefst/NativeTextIndexCreator.java ########## @@ -0,0 +1,173 @@ +/** + * 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.utils.nativefst; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import org.apache.commons.io.FileUtils; +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.inv.BitmapInvertedIndexWriter; +import org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator; +import org.apache.pinot.segment.local.utils.nativefst.builder.FSTBuilder; +import org.apache.pinot.segment.spi.V1Constants; +import org.apache.pinot.segment.spi.index.creator.TextIndexCreator; +import org.roaringbitmap.Container; +import org.roaringbitmap.RoaringBitmap; +import org.roaringbitmap.RoaringBitmapWriter; + +import static java.nio.charset.StandardCharsets.UTF_8; + + +public class NativeTextIndexCreator implements TextIndexCreator { + static final String TEMP_DIR_SUFFIX = ".nativetext.idx.tmp"; + static final String FST_FILE_NAME = "native.fst"; + static final String INVERTED_INDEX_FILE_NAME = "inverted.index.buf"; + public static final int HEADER_LENGTH = 24; + + private String _columnName; + private FSTBuilder _fstBuilder; + private final File _indexFile; + private final File _tempDir; + private final File _fstIndexFile; + private final File _invertedIndexFile; + final Map<String, RoaringBitmapWriter<RoaringBitmap>> _postingListMap = new TreeMap<>(); + final RoaringBitmapWriter.Wizard<Container, RoaringBitmap> _bitmapWriterWizard = RoaringBitmapWriter.writer(); + private int _nextDocId = 0; + private int _fstDataSize; + private int _numBitMaps; + + public NativeTextIndexCreator(String column, File indexDir) + throws IOException { + _columnName = column; + _fstBuilder = new FSTBuilder(); + _indexFile = new File(indexDir, column + V1Constants.Indexes.NATIVE_TEXT_INDEX_FILE_EXTENSION); + _tempDir = new File(indexDir, column + TEMP_DIR_SUFFIX); + if (_tempDir.exists()) { + FileUtils.cleanDirectory(_tempDir); + } else { + FileUtils.forceMkdir(_tempDir); + } + _fstIndexFile = new File(_tempDir, FST_FILE_NAME); + _invertedIndexFile = new File(_tempDir, INVERTED_INDEX_FILE_NAME); + } + + @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()); + } + + for (String token : tokens) { + addToPostingList(token); + } + + _nextDocId++; + } + + @Override + public void add(String[] documents, int length) { + for (int i = 0; i < length; i++) { + add(documents[i]); + } Review comment: +1 on not support MV at present. We may add another format when adding the MV support in the future -- 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