mikemccand commented on a change in pull request #895: LUCENE-8985: 
SynonymGraphFilter cannot handle input stream with tokens filtered.
URL: https://github.com/apache/lucene-solr/pull/895#discussion_r356639466
 
 

 ##########
 File path: 
lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymGraphFilter.java
 ##########
 @@ -178,201 +157,136 @@ public SynonymGraphFilter(TokenStream input, 
SynonymMap synonyms, boolean ignore
     this.fstReader = fst.getBytesReader();
     scratchArc = new FST.Arc<>();
     this.ignoreCase = ignoreCase;
+    this.tokenQueue = new ArrayDeque<>();
+    this.lookahead = new ArrayDeque<>();
   }
 
   @Override
   public boolean incrementToken() throws IOException {
-    //System.out.println("\nS: incrToken lastNodeOut=" + lastNodeOut + " 
nextNodeOut=" + nextNodeOut);
-
-    assert lastNodeOut <= nextNodeOut;
-      
-    if (outputBuffer.isEmpty() == false) {
-      // We still have pending outputs from a prior synonym match:
-      releaseBufferedToken();
-      //System.out.println("  syn: ret buffered=" + this);
-      assert liveToken == false;
-      return true;
-    }
-
-    // Try to parse a new synonym match at the current token:
-
-    if (parse()) {
-      // A new match was found:
-      releaseBufferedToken();
-      //System.out.println("  syn: after parse, ret buffered=" + this);
-      assert liveToken == false;
-      return true;
-    }
-
-    if (lookaheadNextRead == lookaheadNextWrite) {
-
-      // Fast path: parse pulled one token, but it didn't match
-      // the start for any synonym, so we now return it "live" w/o having
-      // cloned all of its atts:
-      if (finished) {
-        //System.out.println("  syn: ret END");
+      if (!tokenQueue.isEmpty()){
+        BufferedToken token = tokenQueue.poll();
+        releaseBufferedToken(token);
+        return true;
+      }
+      if (inputFinished && lookahead.isEmpty()) {
         return false;
       }
+      if (parse()) {
+        releaseCurrentToken();
+        return true;
+      }
+      return releaseBufferedToken();
+  }
 
-      assert liveToken;
-      liveToken = false;
-
-      // NOTE: no need to change posInc since it's relative, i.e. whatever
-      // node our output is upto will just increase by the incoming posInc.
-      // We also don't need to change posLen, but only because we cannot
-      // consume a graph, so the incoming token can never span a future
-      // synonym match.
-
-    } else {
-      // We still have buffered lookahead tokens from a previous
-      // parse attempt that required lookahead; just replay them now:
-      //System.out.println("  restore buffer");
-      assert lookaheadNextRead < lookaheadNextWrite: "read=" + 
lookaheadNextRead + " write=" + lookaheadNextWrite;
-      BufferedInputToken token = lookahead.get(lookaheadNextRead);
-      lookaheadNextRead++;
-
-      restoreState(token.state);
-
-      lookahead.freeBefore(lookaheadNextRead);
-
-      //System.out.println("  after restore offset=" + offsetAtt.startOffset() 
+ "-" + offsetAtt.endOffset());
-      assert liveToken == false;
+  private boolean releaseBufferedToken() {
+    if (tokenQueue.isEmpty()) {
+      return false;
     }
-
-    lastNodeOut += posIncrAtt.getPositionIncrement();
-    nextNodeOut = lastNodeOut + posLenAtt.getPositionLength();
-
-    //System.out.println("  syn: ret lookahead=" + this);
-
+    BufferedToken token = tokenQueue.poll();
+    releaseBufferedToken(token);
     return true;
   }
 
-  private void releaseBufferedToken() throws IOException {
-    //System.out.println("  releaseBufferedToken");
-
-    BufferedOutputToken token = outputBuffer.pollFirst();
+  private void releaseCurrentToken() {
+    prevInputStartNode += posIncrAtt.getPositionIncrement();
+    int startNode = prevInputStartNode + deltaStartNode;
+    posIncrAtt.setPositionIncrement(startNode - prevOutputStartNode);
+    prevOutputStartNode = startNode;
+  }
 
+  private void releaseBufferedToken(BufferedToken token) {
     if (token.state != null) {
-      // This is an original input token (keepOrig=true case):
-      //System.out.println("    hasState");
       restoreState(token.state);
-      //System.out.println("    startOffset=" + offsetAtt.startOffset() + " 
endOffset=" + offsetAtt.endOffset());
     } else {
       clearAttributes();
-      //System.out.println("    no state");
-      termAtt.append(token.term);
-
+      termAtt.copyBuffer(token.term, 0, token.term.length);
       // We better have a match already:
-      assert matchStartOffset != -1;
-
-      offsetAtt.setOffset(matchStartOffset, matchEndOffset);
-      //System.out.println("    startOffset=" + matchStartOffset + " 
endOffset=" + matchEndOffset);
+      offsetAtt.setOffset(token.startOffset, token.endOffset);
       typeAtt.setType(TYPE_SYNONYM);
     }
 
-    //System.out.println("    lastNodeOut=" + lastNodeOut);
-    //System.out.println("    term=" + termAtt);
-
-    posIncrAtt.setPositionIncrement(token.startNode - lastNodeOut);
-    lastNodeOut = token.startNode;
+    posIncrAtt.setPositionIncrement(token.startNode - prevOutputStartNode);
+    prevOutputStartNode = token.startNode;
     posLenAtt.setPositionLength(token.endNode - token.startNode);
+    // System.out.println("releaseBufferedToken term: " + termAtt.toString() + 
",posIncrAtt:" + posIncrAtt.getPositionIncrement() + ", token.startNode:" + 
token.startNode + ", token.endNode:" + token.endNode + ", tokenInQueue:" + 
tokenQueue.size());
   }
 
-  /** Scans the next input token(s) to see if a synonym matches.  Returns true
-   *  if a match was found. */
-  private boolean parse() throws IOException {
-    // System.out.println(Thread.currentThread().getName() + ": S: parse: " + 
System.identityHashCode(this));
+  // return new pendingOutput, return null if doesn't match.
+  private BytesRef termMatch(char[] term, BytesRef pendingOutput, int 
bufferLen) throws  IOException{
+    int bufUpto = 0;
+    while (bufUpto < bufferLen) {
+      final int codePoint = Character.codePointAt(term, bufUpto, bufferLen);
+      if (fst.findTargetArc(ignoreCase ? Character.toLowerCase(codePoint) : 
codePoint, scratchArc, scratchArc, fstReader) == null) {
+        return null;
+      }
+      // Accum the output
+      pendingOutput = fst.outputs.add(pendingOutput, scratchArc.output());
+      bufUpto += Character.charCount(codePoint);
+    }
+    return pendingOutput;
+  }
 
-    // Holds the longest match we've seen so far:
+  /** Scans the next input token(s) to see if a synonym matches. returns true 
if the token is not captured in Queue.*/
+  private boolean parse() throws  IOException {
     BytesRef matchOutput = null;
-    int matchInputLength = 0;
-
     BytesRef pendingOutput = fst.outputs.getNoOutput();
     fst.getFirstArc(scratchArc);
-
     assert scratchArc.output() == fst.outputs.getNoOutput();
-
-    // How many tokens in the current match
-    int matchLength = 0;
-    boolean doFinalCapture = false;
-
-    int lookaheadUpto = lookaheadNextRead;
-    matchStartOffset = -1;
-
-    byToken:
-    while (true) {
-      //System.out.println("  cycle lookaheadUpto=" + lookaheadUpto + " 
maxPos=" + lookahead.getMaxPos());
-      
-      // Pull next token's chars:
-      final char[] buffer;
-      final int bufferLen;
-      final int inputEndOffset;
-
-      if (lookaheadUpto <= lookahead.getMaxPos()) {
-        // Still in our lookahead buffer
-        BufferedInputToken token = lookahead.get(lookaheadUpto);
-        lookaheadUpto++;
-        buffer = token.term.chars();
-        bufferLen = token.term.length();
-        inputEndOffset = token.endOffset;
-        //System.out.println("    use buffer now max=" + 
lookahead.getMaxPos());
-        if (matchStartOffset == -1) {
-          matchStartOffset = token.startOffset;
+    int lookaheadIndex = 0;
+    int matchedLength = 0;
+    Iterator<BufferedToken> it = lookahead.iterator();
+    int lastEndNode = -1;
+    int lastEndOffset = 0;
+    int lastEndNode2 = 0;
+    boolean restored = false;
+    while(true){
+      BufferedToken token = null;
+      if (it.hasNext()){
+        token = it.next();
+        if (lastEndNode != -1 && lastEndNode != token.startNode){
+          // it's not consecutive
+          break;
         }
-      } else {
-
-        // We used up our lookahead buffer of input tokens
-        // -- pull next real input token:
-
-        assert finished || liveToken == false;
-
-        if (finished) {
-          //System.out.println("    break: finished");
+        pendingOutput = termMatch(token.term, pendingOutput, 
token.term.length);
+      }else{
 
 Review comment:
   Spaces around `}` and `{`.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to