-
Notifications
You must be signed in to change notification settings - Fork 4
feat: implement DocumentUploadQueue class
#33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
e0296ef
create `PushSource` and `catalogSource` classes
y-lakhdar 419d177
add boilerplate for stream service
y-lakhdar 4e8528c
Merge branch 'main' of github.com:coveo/push-api-client.java into LEN…
y-lakhdar 1c5adec
revert master merge
y-lakhdar f5a2557
add required POST calls
y-lakhdar 673b76a
update streamService
y-lakhdar 5ef6772
apply ApiUrl corrections
y-lakhdar 93c9867
Merge branch 'LEN-839' of github.com:coveo/push-api-client.java into …
y-lakhdar 8e1e3e7
document StreamService
y-lakhdar de36933
Merge branch 'main' of github.com:coveo/push-api-client.java into LEN…
y-lakhdar 7f9ad5e
Update src/main/java/com/coveo/pushapiclient/StreamService.java
y-lakhdar 3499404
Update src/main/java/com/coveo/pushapiclient/StreamService.java
y-lakhdar f3ec845
Apply suggestions from code review
y-lakhdar 508564e
refactor unit tests
y-lakhdar 954ad44
Merge branch 'LEN-838' of github.com:coveo/push-api-client.java into …
y-lakhdar 0e9acac
Merge branch 'main' into LEN-838
y-lakhdar b121952
create batch update accumulator
y-lakhdar 476a09d
rework queue
y-lakhdar 141889d
complete unit tests
y-lakhdar aa68240
remove comments
y-lakhdar 369525a
update comment
y-lakhdar c90446c
apply corrections
y-lakhdar cd43eb9
Merge branch 'LEN-838' of github.com:coveo/push-api-client.java into …
y-lakhdar 9a7c88b
Merge branch 'LEN-838' of github.com:coveo/push-api-client.java into …
y-lakhdar cad5aac
add gitignore
y-lakhdar 0b5baa1
fix merge conflict
y-lakhdar 4915bfe
Merge branch 'main' of github.com:coveo/push-api-client.java into LEN…
y-lakhdar e03938c
Merge branch 'main' into LENS-856
y-lakhdar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /target/ | ||
| .env | ||
| /.idea/ | ||
| .vscode |
90 changes: 87 additions & 3 deletions
90
src/main/java/com/coveo/pushapiclient/DocumentUploadQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,105 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
|
|
||
| // TODO: LENS-851 - Make public | ||
| /** | ||
| * Represents a queue for uploading documents using a specified upload strategy | ||
| */ | ||
| class DocumentUploadQueue { | ||
| private final UploadStrategy uploader; | ||
| private final int maxQueueSize = 5 * 1024 * 1024; | ||
| private ArrayList<DocumentBuilder> documentToAddList; | ||
| private ArrayList<DeleteDocument> documentToDeleteList; | ||
| private int size; | ||
|
|
||
| /** | ||
| * Constructs a new DocumentUploadQueue object with a default maximum queue size | ||
| * limit of 5MB. | ||
| * | ||
| * @param uploader The upload strategy to be used for document uploads. | ||
| */ | ||
| public DocumentUploadQueue(UploadStrategy uploader) { | ||
| this.documentToAddList = new ArrayList<>(); | ||
| this.documentToDeleteList = new ArrayList<>(); | ||
| this.uploader = uploader; | ||
| } | ||
|
|
||
| /** | ||
| * Flushes the accumulated documents by applying the upload strategy. | ||
| * | ||
| * @throws IOException If an I/O error occurs during the upload. | ||
| * @throws InterruptedException If the upload process is interrupted. | ||
| */ | ||
| public void flush() throws IOException, InterruptedException { | ||
| throw new UnsupportedOperationException("Unimplemented method (TODO: LENS-856)"); | ||
| if (this.isEmpty()) { | ||
| return; | ||
| } | ||
| BatchUpdate batch = this.getBatch(); | ||
| // TODO: LENS-871: support concurrent requests | ||
| this.uploader.apply(batch); | ||
| this.size = 0; | ||
| this.documentToAddList.clear(); | ||
| this.documentToDeleteList.clear(); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a {@link DocumentBuilder} to the upload queue and flushes the queue if | ||
| * it exceeds the maximum content length. | ||
| * See {@link DocumentUploadQueue#flush}. | ||
| * | ||
| * @param document The document to be added to the index. | ||
| * @throws IOException If an I/O error occurs during the upload. | ||
| * @throws InterruptedException If the upload process is interrupted. | ||
| */ | ||
| public void add(DocumentBuilder document) throws IOException, InterruptedException { | ||
| throw new UnsupportedOperationException("Unimplemented method (TODO: LENS-856)"); | ||
| if (document == null) { | ||
| return; | ||
| } | ||
|
|
||
| final int sizeOfDoc = document.marshal().getBytes().length; | ||
|
y-lakhdar marked this conversation as resolved.
|
||
| if (this.size + sizeOfDoc >= this.maxQueueSize) { | ||
| this.flush(); | ||
| } | ||
| if (document != null) { | ||
| documentToAddList.add(document); | ||
| this.size += sizeOfDoc; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds a {@link DeleteDocument} to the upload queue and flushes the queue if | ||
| * it exceeds the maximum content length. | ||
| * See {@link DocumentUploadQueue#flush}. | ||
| * | ||
| * @param document The document to be delete from the index. | ||
| * @throws IOException If an I/O error occurs during the upload. | ||
| * @throws InterruptedException If the upload process is interrupted. | ||
| */ | ||
| public void add(DeleteDocument document) throws IOException, InterruptedException { | ||
| if (document == null) { | ||
| return; | ||
| } | ||
|
|
||
| final int sizeOfDoc = document.marshalJsonObject().toString().getBytes().length; | ||
| if (this.size + sizeOfDoc >= this.maxQueueSize) { | ||
| this.flush(); | ||
| } | ||
| if (document != null) { | ||
| documentToDeleteList.add(document); | ||
| this.size += sizeOfDoc; | ||
|
y-lakhdar marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| public BatchUpdate getBatch() { | ||
| return new BatchUpdate( | ||
| new ArrayList<DocumentBuilder>(this.documentToAddList), | ||
| new ArrayList<DeleteDocument>(this.documentToDeleteList)); | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| // TODO: LENS-843: include partial document updates | ||
| return documentToAddList.isEmpty() && documentToDeleteList.isEmpty(); | ||
| } | ||
|
|
||
| } | ||
192 changes: 192 additions & 0 deletions
192
src/test/java/com/coveo/pushapiclient/DocumentUploadQueueTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
|
|
||
| public class DocumentUploadQueueTest { | ||
|
|
||
| @Mock | ||
| private UploadStrategy uploadStrategy; | ||
|
|
||
| @InjectMocks | ||
| private DocumentUploadQueue queue; | ||
|
|
||
| private AutoCloseable closeable; | ||
| private DocumentBuilder documentToAdd; | ||
| private DeleteDocument documentToDelete; | ||
|
|
||
| private int oneMegaByte = 1 * 1024 * 1024; | ||
|
|
||
| private String generateStringFromBytes(int numBytes) { | ||
| // Check if the number of bytes is valid | ||
| if (numBytes <= 0) { | ||
| return ""; | ||
| } | ||
|
|
||
| // Create a byte array with the specified length | ||
| byte[] bytes = new byte[numBytes]; | ||
|
|
||
| // Fill the byte array with a pattern of ASCII characters | ||
| byte pattern = 65; // ASCII value for 'A' | ||
| for (int i = 0; i < numBytes; i++) { | ||
| bytes[i] = pattern; | ||
| } | ||
|
|
||
| return new String(bytes); | ||
| } | ||
|
|
||
| private DocumentBuilder generateDocumentFromSize(int numBytes) { | ||
| return new DocumentBuilder("https://my.document.uri?ref=1", | ||
| "My bulky document") | ||
| .withData(generateStringFromBytes(numBytes)); | ||
| } | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| String twoMegaByteData = generateStringFromBytes(2 * oneMegaByte); | ||
|
|
||
| documentToAdd = new DocumentBuilder( | ||
| "https://my.document.uri?ref=1", | ||
| "My new document") | ||
| .withData(twoMegaByteData); | ||
|
|
||
| documentToDelete = new DeleteDocument("https://my.document.uri?ref=3"); | ||
|
|
||
| closeable = MockitoAnnotations.openMocks(this); | ||
| } | ||
|
|
||
| @After | ||
| public void closeService() throws Exception { | ||
| closeable.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsEmpty() throws IOException, InterruptedException { | ||
| assertTrue(queue.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsNotEmpty() throws IOException, InterruptedException { | ||
| queue.add(documentToAdd); | ||
| assertFalse(queue.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testShouldReturnBatch() throws IOException, InterruptedException { | ||
| BatchUpdate batchUpdate = new BatchUpdate( | ||
| new ArrayList<>() { | ||
| { | ||
| add(documentToAdd); | ||
| } | ||
| }, new ArrayList<>() { | ||
| { | ||
| add(documentToDelete); | ||
| } | ||
| }); | ||
| queue.add(documentToAdd); | ||
| queue.add(documentToDelete); | ||
|
|
||
| assertEquals(batchUpdate, queue.getBatch()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlushShouldNotUploadDocumentaWhenRequiredSizeIsNotMet() throws IOException, InterruptedException { | ||
| queue.add(documentToAdd); | ||
| queue.add(documentToDelete); | ||
|
|
||
| verify(uploadStrategy, times(0)).apply(any(BatchUpdate.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testShouldAutomaticallyFlushAccumulatedDocuments() throws IOException, InterruptedException { | ||
| DocumentBuilder firstBulkyDocument = generateDocumentFromSize(2 * oneMegaByte); | ||
| DocumentBuilder secondBulkyDocument = generateDocumentFromSize(2 * oneMegaByte); | ||
| DocumentBuilder thirdBulkyDocument = generateDocumentFromSize(2 * oneMegaByte); | ||
| ArrayList<DeleteDocument> emptyList = new ArrayList<>(); | ||
| BatchUpdate firstBatch = new BatchUpdate( | ||
| new ArrayList<>() { | ||
| { | ||
| add(firstBulkyDocument); | ||
| add(secondBulkyDocument); | ||
| } | ||
| }, emptyList); | ||
|
|
||
| // Adding 3 documents of 2MB to the queue. After adding the first 2 documents, | ||
| // the queue size will reach 6MB, which exceeds the maximum queue size | ||
| // limit. Therefore, the 2 first added documents will automatically be uploaded | ||
| // to the source. | ||
| queue.add(firstBulkyDocument); | ||
| queue.add(secondBulkyDocument); | ||
|
|
||
| // The 3rd document added to the queue will be included in a separate batch, | ||
| // which will not be uploaded unless the `flush()` method is called or until the | ||
| // queue size limit has been reached | ||
| queue.add(thirdBulkyDocument); | ||
|
|
||
| verify(uploadStrategy, times(1)).apply(any(BatchUpdate.class)); | ||
| verify(uploadStrategy, times(1)).apply(firstBatch); | ||
| } | ||
|
|
||
| @Test | ||
| public void testShouldManuallyFlushAccumulatedDocuments() throws IOException, InterruptedException { | ||
| DocumentBuilder firstBulkyDocument = generateDocumentFromSize(2 * oneMegaByte); | ||
| DocumentBuilder secondBulkyDocument = generateDocumentFromSize(2 * oneMegaByte); | ||
| DocumentBuilder thirdBulkyDocument = generateDocumentFromSize(2 * oneMegaByte); | ||
| ArrayList<DeleteDocument> emptyList = new ArrayList<>(); | ||
| BatchUpdate firstBatch = new BatchUpdate( | ||
| new ArrayList<>() { | ||
| { | ||
| add(firstBulkyDocument); | ||
| add(secondBulkyDocument); | ||
| } | ||
| }, emptyList); | ||
|
|
||
| BatchUpdate secondBatch = new BatchUpdate( | ||
| new ArrayList<>() { | ||
| { | ||
| add(thirdBulkyDocument); | ||
| } | ||
| }, emptyList); | ||
|
|
||
| // Adding 3 documents of 2MB to the queue. After adding the first 2 documents, | ||
| // the queue size will reach 6MB, which exceeds the maximum queue size | ||
| // limit. Therefore, the 2 first added documents will automatically be uploaded | ||
| // to the source. | ||
| queue.add(firstBulkyDocument); | ||
| queue.add(secondBulkyDocument); | ||
| queue.add(thirdBulkyDocument); | ||
|
|
||
| queue.flush(); | ||
|
|
||
| // Additional flush will have no effect if documents where already flushed | ||
| queue.flush(); | ||
|
|
||
| verify(uploadStrategy, times(2)).apply(any(BatchUpdate.class)); | ||
| verify(uploadStrategy, times(1)).apply(firstBatch); | ||
| verify(uploadStrategy, times(1)).apply(secondBatch); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAddingEmptyDocument() throws IOException, InterruptedException { | ||
| DocumentBuilder nullDocument = null; | ||
|
|
||
| queue.add(nullDocument); | ||
| queue.flush(); | ||
|
|
||
| verify(uploadStrategy, times(0)).apply(any(BatchUpdate.class)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.