Jackie-Jiang commented on code in PR #8861: URL: https://github.com/apache/pinot/pull/8861#discussion_r892672071
########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableFSTIndex.java: ########## @@ -0,0 +1,80 @@ +/** + * 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.concurrent.locks.ReentrantReadWriteLock; +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.RoaringBitmapWriter; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +public class NativeMutableFSTIndex implements MutableTextIndex { + private final String _column; + private final MutableFST _fst; + private int _dictId; Review Comment: (minor) Move this after the final variables for readability ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/indexsegment/mutable/MutableSegmentImpl.java: ########## @@ -306,6 +307,21 @@ public long getLatestIngestionTimestamp() { MutableInvertedIndex invertedIndexReader = invertedIndexColumns.contains(column) ? indexProvider.newInvertedIndex(context.forInvertedIndex()) : null; + MutableTextIndex fstIndex = null; + //FST Index Review Comment: (nit) Add a space after the `//` ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableFSTIndex.java: ########## @@ -0,0 +1,80 @@ +/** + * 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.concurrent.locks.ReentrantReadWriteLock; +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.RoaringBitmapWriter; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +public class NativeMutableFSTIndex implements MutableTextIndex { + private final String _column; + private final MutableFST _fst; + private int _dictId; + private final ReentrantReadWriteLock.ReadLock _readLock; + private final ReentrantReadWriteLock.WriteLock _writeLock; + + public NativeMutableFSTIndex(String column) { + _column = column; + _fst = new MutableFSTImpl(); + + ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); + _readLock = readWriteLock.readLock(); + _writeLock = readWriteLock.writeLock(); + } + + @Override + public void add(String document) { + _writeLock.lock(); + try { + _fst.addPath(document, _dictId); + _dictId++; + } finally { + _writeLock.unlock(); + } + } + + @Override + public ImmutableRoaringBitmap getDictIds(String searchQuery) { + RoaringBitmapWriter<MutableRoaringBitmap> writer = RoaringBitmapWriter.bufferWriter().get(); + _readLock.lock(); + try { + RealTimeRegexpMatcher.regexMatch(searchQuery, _fst, writer::add); + return writer.get(); + } finally { + _readLock.unlock(); + } + } + + @Override + public MutableRoaringBitmap getDocIds(String searchQuery) { + return null; Review Comment: Let's throw an unsupported exception with some error message for debugging purpose ########## pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableFSTIndexTest.java: ########## @@ -0,0 +1,83 @@ +/** + * 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.Arrays; +import java.util.List; +import javax.annotation.Nullable; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; + + +public class NativeMutableFSTIndexTest { + private static final String TEXT_COLUMN_NAME = "testColumnName"; + private NativeMutableFSTIndex _nativeMutableFSTIndex; + + @BeforeClass + public void setUp() + throws Exception { + _nativeMutableFSTIndex = new NativeMutableFSTIndex(TEXT_COLUMN_NAME); + List<String> documents = getTextData(); + + for (String doc : documents) { + _nativeMutableFSTIndex.add(doc); + } + } + + @AfterClass + public void tearDown() + throws IOException { + _nativeMutableFSTIndex.close(); + } + + @Test + public void testQueries() { + String nativeQuery = "P.*"; + List<Integer> resultList = Arrays.asList(0, 9); + testSelectionResults(nativeQuery, 2, resultList); + + nativeQuery = "a.*"; + resultList = Arrays.asList(5, 6); + testSelectionResults(nativeQuery, 2, resultList); + + nativeQuery = ".*ed"; + resultList = Arrays.asList(6); + testSelectionResults(nativeQuery, 1, resultList); + } + + private List<String> getTextData() { + return Arrays.asList("Prince", "Andrew", "kept", "looking", "with", "an", "amused", "smile", "from", "Pierre"); + } + + private void testSelectionResults(String nativeQuery, int resultCount, @Nullable List<Integer> results) { + ImmutableRoaringBitmap resultMap = _nativeMutableFSTIndex.getDictIds(nativeQuery); + assertEquals(resultMap.getCardinality(), resultCount); + + if (results != null) { + for (int result : results) { + assertEquals(resultMap.contains(result), true); Review Comment: (minor) `assertTrue` ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/indexsegment/mutable/MutableSegmentImpl.java: ########## @@ -306,6 +307,21 @@ public long getLatestIngestionTimestamp() { MutableInvertedIndex invertedIndexReader = invertedIndexColumns.contains(column) ? indexProvider.newInvertedIndex(context.forInvertedIndex()) : null; + MutableTextIndex fstIndex = null; + //FST Index Review Comment: We can remove the TODO on line 246 -- 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]
