blob: 5efea9605119a7d867e23628351c78b585f9d9de (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi;
import com.yahoo.document.DocumentOperation;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.RemoveDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.UpdateDocumentMessage;
import com.yahoo.messagebus.Message;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* 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 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 Håkon Humberset
* @author vekterli
*/
public class VisitorDataQueue extends VisitorDataHandler {
private final BlockingQueue<VisitorResponse> pendingResponses = new LinkedBlockingQueue<>();
/** Creates a new visitor data queue. */
public VisitorDataQueue() {
}
// Inherit doc from VisitorDataHandler
@Override
public void reset() {
super.reset();
pendingResponses.clear();
}
private void appendSingleOpToPendingList(final DocumentOperation op, final AckToken token) {
final DocumentOpVisitorResponse response = new DocumentOpVisitorResponse(op, token);
pendingResponses.add(response);
}
@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()));
}
}
// Inherit doc from VisitorDataHandler
@Override
public VisitorResponse getNext() {
return pendingResponses.poll();
}
// Inherit doc from VisitorDataHandler
@Override
public VisitorResponse getNext(int timeoutMilliseconds) throws InterruptedException {
return pendingResponses.poll(timeoutMilliseconds, TimeUnit.MILLISECONDS);
}
}
|