// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi;

import com.yahoo.document.BucketId;
import com.yahoo.document.DocumentOperation;
import com.yahoo.documentapi.messagebus.protocol.DocumentListEntry;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.RemoveDocumentMessage;
import com.yahoo.messagebus.Message;
import com.yahoo.vdslib.DocumentList;
import com.yahoo.vdslib.Entry;

import java.util.LinkedList;
import java.util.List;


/**
 * A visitor data handler that queues up documents in visitor responses and
 * implements the <code>getNext</code> methods, thus implementing the polling
 * API defined in VisitorDataHandler.
 * <p>
 * Visitor responses containing document lists should be polled for with the
 * <code>getNext</code> methods and need to be acked when processed for
 * visiting not to halt. The class is thread safe.
 *
 * @author <a href="mailto:humbe@yahoo-inc.com">Håkon Humberset</a>
 */
public class VisitorDataQueue extends VisitorDataHandler {

    final LinkedList<VisitorResponse> pendingResponses = new LinkedList<VisitorResponse>();

    /** Creates a new visitor data queue. */
    public VisitorDataQueue() {
    }

    // Inherit doc from VisitorDataHandler
    @Override
    public void reset() {
        super.reset();
        synchronized (pendingResponses) {
            pendingResponses.clear();
        }
    }

    private void appendSingleOpToPendingList(final DocumentOperation op, final AckToken token) {
        final DocumentList docList = DocumentList.create(Entry.create(op));
        final DocumentListVisitorResponse response = new DocumentListVisitorResponse(docList, token);
        synchronized (pendingResponses) {
            pendingResponses.add(response);
            pendingResponses.notifyAll();
        }
    }

    @Override
    public void onMessage(Message m, AckToken token) {
        if (m instanceof PutDocumentMessage) {
            appendSingleOpToPendingList(((PutDocumentMessage)m).getDocumentPut(), token);
        } else if (m instanceof RemoveDocumentMessage) {
            appendSingleOpToPendingList(((RemoveDocumentMessage)m).getDocumentRemove(), token);
        } else {
            throw new UnsupportedOperationException(
                    String.format("Expected put/remove message, got '%s' of type %s",
                                  m.toString(), m.getClass().toString()));
        }
    }

    /**
     * @deprecated This method is no longer called by the visitor subsystem. See onMessage instead.
     */
    @Deprecated
    public void onDocuments(DocumentList docs, AckToken token) {
        synchronized (pendingResponses) {
            pendingResponses.add(new DocumentListVisitorResponse(docs, token));
            pendingResponses.notifyAll();
        }
    }

    // Inherit doc from VisitorDataHandler
    @Override
    public VisitorResponse getNext() {
        synchronized (pendingResponses) {
            return (pendingResponses.isEmpty()
                        ? null : pendingResponses.removeFirst());
        }
    }

    // Inherit doc from VisitorDataHandler
    @Override
    public VisitorResponse getNext(int timeoutMilliseconds) throws InterruptedException {
        synchronized (pendingResponses) {
            if (pendingResponses.isEmpty()) {
                if (timeoutMilliseconds == 0) {
                    while (pendingResponses.isEmpty()) {
                        pendingResponses.wait();
                    }
                } else {
                    pendingResponses.wait(timeoutMilliseconds);
                }
            }
            return (pendingResponses.isEmpty()
                        ? null : pendingResponses.removeFirst());
        }
    }

    @Override
    public void onDone() {
        synchronized (pendingResponses) {
            pendingResponses.notifyAll();
        }
    }
}

@@ -169,129 +169,25 @@
 ent.
-BucketId;%0Aimport com.yahoo.document.DocumentOperation;%0Aimport com.yahoo.documentapi.messagebus.protocol.DocumentListEntry
+DocumentOperation
 ;%0Aim
@@ -449,26 +449,89 @@
 va.util.
-LinkedList
+concurrent.BlockingQueue;%0Aimport java.util.concurrent.LinkedBlockingQueue
 ;%0Aimport
@@ -541,19 +541,34 @@
 va.util.
-Lis
+concurrent.TimeUni
 t;%0A%0A%0A/**
@@ -985,64 +985,44 @@
 hor 
-%3Ca href=%22mailto:humbe@yahoo-inc.com%22%3EH%C3%A5kon Humberset%3C/a%3E
+H%C3%A5kon Humberset%0A * @author @vekterli
 %0A */
@@ -1092,26 +1092,29 @@
   final 
-LinkedList
+BlockingQueue
 %3CVisitor
@@ -1156,28 +1156,22 @@
 nked
-List%3CVisitorResponse
+BlockingQueue%3C
 %3E();
@@ -1366,54 +1366,8 @@
 ();%0A
-        synchronized (pendingResponses) %7B%0A    
     
@@ -1392,34 +1392,24 @@
 es.clear();%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -1690,137 +1690,39 @@
     
-synchronized (pendingResponses) %7B%0A            pendingResponses.add(response);%0A            pendingResponses.notifyAll();%0A        %7D
+pendingResponses.add(response);
 %0A   
@@ -2533,54 +2533,8 @@
 ) %7B%0A
-        synchronized (pendingResponses) %7B%0A    
     
@@ -2609,60 +2609,8 @@
 ));%0A
-            pendingResponses.notifyAll();%0A        %7D%0A
     
@@ -2712,54 +2712,8 @@
 ) %7B%0A
-        synchronized (pendingResponses) %7B%0A    
     
@@ -2715,33 +2715,32 @@
 %0A        return 
-(
 pendingResponses
@@ -2744,93 +2744,15 @@
 ses.
-isEmpty()%0A                        ? null : pendingResponses.removeFirst());%0A        %7D
+poll();
 %0A   
@@ -2914,654 +2914,87 @@
     
-synchronized (pendingResponses) %7B%0A            if (pendingResponses.isEmpty()) %7B%0A                if (timeoutMilliseconds == 0) %7B%0A                    while (pendingResponses.isEmpty()) %7B%0A                        pendingResponses.wait();%0A                    %7D%0A                %7D else %7B%0A                    pendingResponses.wait(timeoutMilliseconds);%0A                %7D%0A            %7D%0A            return (pendingResponses.isEmpty()%0A                        ? null : pendingResponses.removeFirst());%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onDone() %7B%0A        synchronized (pendingResponses) %7B%0A            pendingResponses.notifyAll();%0A   
+return pendingResponses.poll(timeoutMilliseconds, TimeUnit.MILLISECONDS);%0A
     
- 
 %7D%0A
-    %7D
 %0A%7D%0A

Replace home-brewed blocking queue with Java's built-in one