|
| 1 | +package com.coveo.pushapiclient; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import org.apache.logging.log4j.LogManager; |
| 6 | +import org.apache.logging.log4j.Logger; |
| 7 | + |
| 8 | +public class StreamDocumentUploadQueue extends DocumentUploadQueue { |
| 9 | + |
| 10 | + private static final Logger logger = LogManager.getLogger(StreamDocumentUploadQueue.class); |
| 11 | + protected ArrayList<PartialUpdateDocument> documentToPartiallyUpdateList; |
| 12 | + |
| 13 | + public StreamDocumentUploadQueue(UploadStrategy uploader) { |
| 14 | + super(uploader); |
| 15 | + this.documentToPartiallyUpdateList = new ArrayList<>(); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Flushes the accumulated documents by applying the upload strategy. |
| 20 | + * |
| 21 | + * @throws IOException If an I/O error occurs during the upload. |
| 22 | + * @throws InterruptedException If the upload process is interrupted. |
| 23 | + */ |
| 24 | + @Override |
| 25 | + public void flush() throws IOException, InterruptedException { |
| 26 | + if (this.isEmpty()) { |
| 27 | + logger.debug("Empty batch. Skipping upload"); |
| 28 | + return; |
| 29 | + } |
| 30 | + // TODO: LENS-871: support concurrent requests |
| 31 | + StreamUpdate stream = this.getStream(); |
| 32 | + logger.info("Uploading document Stream"); |
| 33 | + this.uploader.apply(stream); |
| 34 | + |
| 35 | + this.size = 0; |
| 36 | + this.documentToAddList.clear(); |
| 37 | + this.documentToDeleteList.clear(); |
| 38 | + this.documentToPartiallyUpdateList.clear(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Adds the {@link PartialUpdateDocument} to the upload queue and flushes the queue if it exceeds |
| 43 | + * the maximum content length. See {@link PartialUpdateDocument#flush}. |
| 44 | + * |
| 45 | + * @param document The document to be deleted from the index. |
| 46 | + * @throws IOException If an I/O error occurs during the upload. |
| 47 | + * @throws InterruptedException If the upload process is interrupted. |
| 48 | + */ |
| 49 | + public void add(PartialUpdateDocument document) throws IOException, InterruptedException { |
| 50 | + if (document == null) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + final int sizeOfDoc = document.marshalJsonObject().toString().getBytes().length; |
| 55 | + if (this.size + sizeOfDoc >= this.maxQueueSize) { |
| 56 | + this.flush(); |
| 57 | + } |
| 58 | + documentToPartiallyUpdateList.add(document); |
| 59 | + logger.info("Adding document to batch: " + document.documentId); |
| 60 | + this.size += sizeOfDoc; |
| 61 | + } |
| 62 | + |
| 63 | + public StreamUpdate getStream() { |
| 64 | + return new StreamUpdate( |
| 65 | + new ArrayList<>(this.documentToAddList), |
| 66 | + new ArrayList<>(this.documentToDeleteList), |
| 67 | + new ArrayList<>(this.documentToPartiallyUpdateList)); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public BatchUpdate getBatch() { |
| 72 | + throw new UnsupportedOperationException("StreamDocumentUploadQueue does not support getBatch"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean isEmpty() { |
| 77 | + return super.isEmpty() && documentToPartiallyUpdateList.isEmpty(); |
| 78 | + } |
| 79 | +} |
0 commit comments