richardstartin commented on a change in pull request #8016:
URL: https://github.com/apache/pinot/pull/8016#discussion_r784673019



##########
File path: 
pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkMutableFST.java
##########
@@ -0,0 +1,97 @@
+/**
+ * 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.perf;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Objects;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import 
org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher;
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+import org.roaringbitmap.RoaringBitmapWriter;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+
+
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.MICROSECONDS)
+@Warmup(iterations = 3, time = 30)
+@Measurement(iterations = 5, time = 30)
+@Fork(1)
+@State(Scope.Benchmark)
+public class BenchmarkMutableFST {
+  @Param({"q.[aeiou]c.*", ".*a", "b.*", ".*", ".*ated", ".*ba.*"})
+  public String _regex;
+
+  private MutableFST _mutableFST;
+  private org.apache.lucene.util.fst.FST _fst;
+
+  @Setup
+  public void setUp()
+      throws IOException {
+    SortedMap<String, Integer> input = new TreeMap<>();
+    try (BufferedReader bufferedReader = new BufferedReader(new 
InputStreamReader(
+        
Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("data/words.txt")))))
 {
+      String currentWord;
+      int i = 0;
+      while ((currentWord = bufferedReader.readLine()) != null) {
+        _mutableFST.addPath(currentWord, i);
+        input.put(currentWord, i++);
+      }
+    }
+
+    _fst = org.apache.pinot.segment.local.utils.fst.FSTBuilder.buildFST(input);
+  }
+
+  @Benchmark
+  public void testMutableRegex(Blackhole blackhole) {
+    RoaringBitmapWriter<MutableRoaringBitmap> writer = 
RoaringBitmapWriter.bufferWriter().get();
+    RealTimeRegexpMatcher.regexMatch(_regex, _mutableFST, writer::add);
+    blackhole.consume(writer.get());

Review comment:
       When there’s only one value produced, just return it and let the 
framework do the blackhole stuff.

##########
File path: 
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/utils/nativefst/mutablefst/MutableFSTConcurrentTest.java
##########
@@ -0,0 +1,145 @@
+/**
+ * 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.mutablefst;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.apache.commons.lang3.tuple.Pair;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static 
org.apache.pinot.segment.local.utils.nativefst.mutablefst.utils.MutableFSTUtils.regexQueryNrHitsForRealTimeFST;
+import static org.testng.AssertJUnit.assertEquals;
+
+
+public class MutableFSTConcurrentTest {
+  private ExecutorService _threadPool;
+
+  private CountDownLatch _countDownLatch;
+  private Set<String> _resultSet;
+
+  @BeforeClass
+  private void setup() {
+     _threadPool = Executors.newFixedThreadPool(2);
+    _countDownLatch = new CountDownLatch(2);
+    _resultSet = new HashSet<>();
+  }
+
+  @AfterClass
+  private void shutDown() {
+    _threadPool.shutdown();
+  }
+
+  @Test
+  public void testConcurrentWriteAndRead()
+      throws InterruptedException {
+    MutableFST mutableFST = new MutableFSTImpl();
+    List<String> words = new ArrayList<>();
+
+    words.add("ab");
+    words.add("abba");
+    words.add("aba");
+    words.add("bab");
+    words.add("cdd");
+    words.add("efg");
+
+    List<Pair<String, Integer>> wordsWithMetadata = new ArrayList<>();
+    int i = 1;
+
+    for (String currentWord : words) {
+      wordsWithMetadata.add(Pair.of(currentWord, i));
+      i++;
+    }
+
+    _threadPool.submit(() -> {
+      try {
+        performReads(mutableFST, words, 10, 200);
+      } catch (InterruptedException e) {
+        e.printStackTrace();
+      }
+    });
+
+    _threadPool.submit(() -> {
+      try {
+        performWrites(mutableFST, wordsWithMetadata, 10);
+      } catch (InterruptedException e) {
+        e.printStackTrace();
+      }
+    });
+
+    _countDownLatch.await();
+
+    assertEquals(_resultSet.size(), words.size());
+
+    assert (_resultSet.contains("ab"));
+    assert (_resultSet.contains("abba"));
+    assert (_resultSet.contains("aba"));
+    assert (_resultSet.contains("bab"));
+    assert (_resultSet.contains("cdd"));
+    assert (_resultSet.contains("efg"));

Review comment:
       please use `Assert.assertTrue` instead. To make it easier for the next 
person to work with this code, adding a message like "ab" or "\"ab\" not in the 
result set" would make it easier to figure out what's happened if someone 
breaks this code and this test catches it.

##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/nativefst/utils/RealTimeRegexpMatcher.java
##########
@@ -0,0 +1,158 @@
+/**
+ * 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.utils;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import org.apache.pinot.segment.local.utils.nativefst.automaton.Automaton;
+import 
org.apache.pinot.segment.local.utils.nativefst.automaton.CharacterRunAutomaton;
+import org.apache.pinot.segment.local.utils.nativefst.automaton.RegExp;
+import org.apache.pinot.segment.local.utils.nativefst.automaton.State;
+import org.apache.pinot.segment.local.utils.nativefst.automaton.Transition;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableArc;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST;
+import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableState;
+import org.roaringbitmap.IntConsumer;
+
+public class RealTimeRegexpMatcher {
+  private final String _regexQuery;
+  private final MutableFST _fst;
+  private final Automaton _automaton;
+  private final IntConsumer _dest;
+
+  public RealTimeRegexpMatcher(String regexQuery, MutableFST fst, IntConsumer 
dest) {
+    _regexQuery = regexQuery;
+    _fst = fst;
+    _dest = dest;
+
+    _automaton = new RegExp(_regexQuery).toAutomaton();
+  }
+
+  public static void regexMatch(String regexQuery, MutableFST fst, IntConsumer 
dest) {
+    RealTimeRegexpMatcher matcher = new RealTimeRegexpMatcher(regexQuery, fst, 
dest);
+    matcher.regexMatchOnFST();
+  }
+
+  // Matches "input" string with _regexQuery Automaton.
+  public boolean match(String input) {
+    CharacterRunAutomaton characterRunAutomaton = new 
CharacterRunAutomaton(_automaton);
+    return characterRunAutomaton.run(input);
+  }
+
+  /**
+   * This function runs matching on automaton built from regexQuery and the 
FST.
+   * FST stores key (string) to a value (Long). Both are state machines and 
state transition is based on
+   * a input character.
+   *
+   * This algorithm starts with Queue containing (Automaton Start Node, FST 
Start Node).
+   * Each step an entry is popped from the queue:
+   *    1) if the automaton state is accept and the FST Node is final (i.e. 
end node) then the value stored for that FST
+   *       is added to the set of result.
+   *    2) Else next set of transitions on automaton are gathered and for each 
transition target node for that character
+   *       is figured out in FST Node, resulting pair of (automaton state, fst 
node) are added to the queue.
+   *    3) This process is bound to complete since we are making progression 
on the FST (which is a DAG) towards final
+   *       nodes.
+   */
+  public void regexMatchOnFST() {
+    final List<Path> queue = new ArrayList<>();
+
+    if (_automaton.getNumberOfStates() == 0) {
+      return;
+    }
+
+    // Automaton start state and FST start node is added to the queue.
+    queue.add(new Path(_automaton.getInitialState(), _fst.getStartState(), 
null, new ArrayList<>()));
+
+    Set<State> acceptStates = _automaton.getAcceptStates();
+    while (queue.size() != 0) {
+      final Path path = queue.remove(queue.size() - 1);

Review comment:
       When you change this to `ArrayDeque` you can just call 
`queue.removeLast()` instead. This is exactly the sort of thing `Deque` is for.




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