|
| 1 | +package com.coveo.pushapiclient; |
| 2 | + |
| 3 | +import com.coveo.pushapiclient.exceptions.NoOpenFileContainerException; |
| 4 | +import com.google.gson.Gson; |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.http.HttpResponse; |
| 7 | +import org.apache.logging.log4j.LogManager; |
| 8 | +import org.apache.logging.log4j.Logger; |
| 9 | + |
| 10 | +public class UpdateStreamService { |
| 11 | + |
| 12 | + private final PlatformClient platformClient; |
| 13 | + private final UpdateStreamServiceInternal updateStreamServiceInternal; |
| 14 | + |
| 15 | + private FileContainer fileContainer; |
| 16 | + |
| 17 | + /** |
| 18 | + * Creates a service to stream your documents to the provided source by interacting with the |
| 19 | + * Stream API. This provides the ability to incrementally add, update, or delete documents via a |
| 20 | + * stream. |
| 21 | + * |
| 22 | + * <p>To perform <a href="https://docs.coveo.com/en/lb4a0344">a full source rebuild</a>, use the |
| 23 | + * {@StreamService} |
| 24 | + * |
| 25 | + * @param source The source to which you want to send your documents. |
| 26 | + */ |
| 27 | + public UpdateStreamService(StreamEnabledSource source) { |
| 28 | + this(source, new BackoffOptionsBuilder().build()); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Creates a service to stream your documents to the provided source by interacting with the |
| 33 | + * Stream API. This provides the ability to incrementally add, update, or delete documents via a |
| 34 | + * stream. |
| 35 | + * |
| 36 | + * <p>To perform <a href="https://docs.coveo.com/en/lb4a0344">a full source rebuild</a>, use the |
| 37 | + * {@StreamService} |
| 38 | + * |
| 39 | + * @param source The source to which you want to send your documents. |
| 40 | + * @param options The configuration options for exponential backoff. |
| 41 | + */ |
| 42 | + public UpdateStreamService(StreamEnabledSource source, BackoffOptions options) { |
| 43 | + Logger logger = LogManager.getLogger(UpdateStreamService.class); |
| 44 | + this.platformClient = |
| 45 | + new PlatformClient( |
| 46 | + source.getApiKey(), source.getOrganizationId(), source.getPlatformUrl(), options); |
| 47 | + this.updateStreamServiceInternal = |
| 48 | + new UpdateStreamServiceInternal( |
| 49 | + source, new DocumentUploadQueue(this.getUploadStrategy()), this.platformClient, logger); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Adds documents to an open file container be created or updated. If there is no file container |
| 54 | + * open to receive the documents, this function will open a file container before uploading |
| 55 | + * documents into it. |
| 56 | + * |
| 57 | + * <p>If called several times, the service will automatically batch documents and create new |
| 58 | + * stream chunks whenever the data payload exceeds the <a |
| 59 | + * href="https://docs.coveo.com/en/lb4a0344#stream-api-limits">batch size limit</a> set for the |
| 60 | + * Stream API. |
| 61 | + * |
| 62 | + * <p>Once there are no more documents to add, it is important to call the {@link |
| 63 | + * UpdateStreamService#close} function in order to send any buffered documents and push the file |
| 64 | + * container. Otherwise, changes will not be reflected in the index. |
| 65 | + * |
| 66 | + * <p> |
| 67 | + * |
| 68 | + * <pre>{@code |
| 69 | + * //... |
| 70 | + * UpdateStreamService service = new UpdateStreamService(source)); |
| 71 | + * for (DocumentBuilder document : fictionalDocumentList) { |
| 72 | + * service.addOrUpdate(document); |
| 73 | + * } |
| 74 | + * service.close(document); |
| 75 | + * }</pre> |
| 76 | + * |
| 77 | + * <p>For more code samples, @see `samples/UpdateStreamDocuments.java` |
| 78 | + * |
| 79 | + * @param document The documentBuilder to push to your file container |
| 80 | + * @throws InterruptedException If the creation of the file container or adding the document is |
| 81 | + * interrupted. |
| 82 | + * @throws IOException If the creation of the file container or adding the document fails. |
| 83 | + */ |
| 84 | + public void addOrUpdate(DocumentBuilder document) throws IOException, InterruptedException { |
| 85 | + fileContainer = updateStreamServiceInternal.addOrUpdate(document); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Adds documents to an open file container be deleted. If there is no file container open to |
| 90 | + * receive the documents, this function will open a file container before uploading documents into |
| 91 | + * it. |
| 92 | + * |
| 93 | + * <p>If called several times, the service will automatically batch documents and create new |
| 94 | + * stream chunks whenever the data payload exceeds the <a |
| 95 | + * href="https://docs.coveo.com/en/lb4a0344#stream-api-limits">batch size limit</a> set for the |
| 96 | + * Stream API. |
| 97 | + * |
| 98 | + * <p>Once there are no more documents to add, it is important to call the {@link |
| 99 | + * UpdateStreamService#close} function in order to send any buffered documents and push the file |
| 100 | + * container. Otherwise, changes will not be reflected in the index. |
| 101 | + * |
| 102 | + * <p> |
| 103 | + * |
| 104 | + * <pre>{@code |
| 105 | + * //... |
| 106 | + * UpdateStreamService service = new UpdateStreamService(source)); |
| 107 | + * for (DeleteDocument document : fictionalDocumentList) { |
| 108 | + * service.delete(document); |
| 109 | + * } |
| 110 | + * service.close(document); |
| 111 | + * }</pre> |
| 112 | + * |
| 113 | + * <p>For more code samples, @see `samples/UpdateStreamDocuments.java` |
| 114 | + * |
| 115 | + * @param document The deleteDocument to push to your file container |
| 116 | + * @throws InterruptedException If the creation of the file container or adding the document is |
| 117 | + * interrupted. |
| 118 | + * @throws IOException If the creation of the file container or adding the document fails. |
| 119 | + */ |
| 120 | + public void delete(DeleteDocument document) throws IOException, InterruptedException { |
| 121 | + fileContainer = updateStreamServiceInternal.delete(document); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Sends any buffered documents and <a |
| 126 | + * href="https://docs.coveo.com/en/l62e0540/how-to-update-your-catalog#step-3-send-the-file-container-to-update-your-catalog">pushes |
| 127 | + * the file container</a>. |
| 128 | + * |
| 129 | + * <p>Upon invoking this method, any documents added to the file container will be pushed and |
| 130 | + * indexed. |
| 131 | + * |
| 132 | + * @return The HttpResponse from the platform. |
| 133 | + * @throws IOException If the pushing file container failed. |
| 134 | + * @throws InterruptedException If the pushing file container is interrupted. |
| 135 | + * @throws NoOpenFileContainerException If there is no open file container to push. |
| 136 | + */ |
| 137 | + public HttpResponse<String> close() |
| 138 | + throws IOException, InterruptedException, NoOpenFileContainerException { |
| 139 | + return updateStreamServiceInternal.close(); |
| 140 | + } |
| 141 | + |
| 142 | + private UploadStrategy getUploadStrategy() { |
| 143 | + return (batchUpdate) -> { |
| 144 | + String batchUpdateJson = new Gson().toJson(batchUpdate.marshal()); |
| 145 | + return this.platformClient.uploadContentToFileContainer(fileContainer, batchUpdateJson); |
| 146 | + }; |
| 147 | + } |
| 148 | +} |
0 commit comments