-
Notifications
You must be signed in to change notification settings - Fork 74
MLE-26420 Added DocumentWriteSetFilter #1867
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. | ||
| */ | ||
| package com.marklogic.client.datamovement; | ||
|
|
||
| import com.marklogic.client.DatabaseClient; | ||
| import com.marklogic.client.document.DocumentWriteSet; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * A filter that can modify a DocumentWriteSet before it is written to the database. | ||
| * | ||
| * @since 8.1.0 | ||
| */ | ||
| public interface DocumentWriteSetFilter extends Function<DocumentWriteSetFilter.Context, DocumentWriteSet> { | ||
|
|
||
| interface Context { | ||
| /** | ||
| * @return the DocumentWriteSet to be written | ||
| */ | ||
| DocumentWriteSet getDocumentWriteSet(); | ||
|
|
||
| /** | ||
| * @return the batch number | ||
| */ | ||
| long getBatchNumber(); | ||
|
|
||
| /** | ||
| * @return the DatabaseClient being used for this batch | ||
| */ | ||
| DatabaseClient getDatabaseClient(); | ||
|
|
||
| /** | ||
| * @return the temporal collection name, or null if not writing to a temporal collection | ||
| */ | ||
| String getTemporalCollection(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| */ | ||
| package com.marklogic.client.datamovement.impl; | ||
|
|
||
| import com.marklogic.client.datamovement.DocumentWriteSetFilter; | ||
| import com.marklogic.client.document.DocumentWriteOperation; | ||
| import com.marklogic.client.document.DocumentWriteSet; | ||
| import com.marklogic.client.document.XMLDocumentManager; | ||
|
|
@@ -13,7 +14,7 @@ | |
| import java.io.Closeable; | ||
| import java.util.function.Consumer; | ||
|
|
||
| record BatchWriter(BatchWriteSet batchWriteSet) implements Runnable { | ||
| record BatchWriter(BatchWriteSet batchWriteSet, DocumentWriteSetFilter filter) implements Runnable { | ||
|
|
||
| private static Logger logger = LoggerFactory.getLogger(WriteBatcherImpl.class); | ||
|
|
||
|
|
@@ -28,6 +29,16 @@ public void run() { | |
| logger.trace("Begin write batch {} to forest on host '{}'", batchWriteSet.getBatchNumber(), batchWriteSet.getClient().getHost()); | ||
|
|
||
| DocumentWriteSet documentWriteSet = batchWriteSet.getDocumentWriteSet(); | ||
| if (filter != null) { | ||
| documentWriteSet = filter.apply(batchWriteSet); | ||
| if (documentWriteSet == null || documentWriteSet.isEmpty()) { | ||
| logger.debug("Filter returned empty write set for batch {}, skipping write", batchWriteSet.getBatchNumber()); | ||
| closeAllHandles(); | ||
|
||
| return; | ||
| } | ||
| batchWriteSet.updateWithFilteredDocumentWriteSet(documentWriteSet); | ||
| } | ||
|
|
||
| writeDocuments(documentWriteSet); | ||
|
|
||
| // This seems like it should be part of a finally block - but it's able to throw an exception. Which implies | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. | ||
| */ | ||
| package com.marklogic.client.datamovement.filter; | ||
|
|
||
| import com.marklogic.client.io.DocumentMetadataHandle; | ||
| import com.marklogic.client.io.StringHandle; | ||
| import com.marklogic.client.test.AbstractClientTest; | ||
| import com.marklogic.client.test.Common; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class RemoveAllDocumentsFilterTest extends AbstractClientTest { | ||
|
|
||
| private static final DocumentMetadataHandle METADATA = new DocumentMetadataHandle() | ||
| .withCollections("incremental-test") | ||
| .withPermission("rest-reader", DocumentMetadataHandle.Capability.READ, DocumentMetadataHandle.Capability.UPDATE); | ||
|
|
||
| AtomicInteger writtenCount = new AtomicInteger(); | ||
|
|
||
| @Test | ||
| void filterRemovesAllDocuments() { | ||
| new WriteBatcherTemplate(Common.newClient()).runWriteJob( | ||
| writeBatcher -> writeBatcher | ||
| .withDocumentWriteSetFilter(context -> context.getDatabaseClient().newDocumentManager().newWriteSet()) | ||
| .onBatchSuccess(batch -> writtenCount.addAndGet(batch.getItems().length)), | ||
|
|
||
| writeBatcher -> { | ||
| for (int i = 1; i <= 10; i++) { | ||
| writeBatcher.add("/incremental/test/doc-" + i + ".xml", METADATA, new StringHandle("<doc/>")); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| assertEquals(0, writtenCount.get(), "No documents should have been written since the filter removed them all. " + | ||
| "This test is verifying that no error will occur either when the filter doesn't return any documents."); | ||
| assertCollectionSize("incremental-test", 0); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||
| /* | ||||||
| * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. | ||||||
| */ | ||||||
| package com.marklogic.client.datamovement.filter; | ||||||
|
|
||||||
| import com.marklogic.client.DatabaseClient; | ||||||
| import com.marklogic.client.datamovement.DataMovementManager; | ||||||
| import com.marklogic.client.datamovement.WriteBatcher; | ||||||
|
|
||||||
| import java.util.function.Consumer; | ||||||
|
|
||||||
| // Experimenting with a template that gets rid of some annoying DMSDK boilerplate. | ||||||
| record WriteBatcherTemplate(DatabaseClient databaseClient) { | ||||||
|
||||||
| record WriteBatcherTemplate(DatabaseClient databaseClient) { | |
| public record WriteBatcherTemplate(DatabaseClient databaseClient) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment 'Can be overridden after creation' is misleading as this is a private field, not an overridable method. Consider revising to 'Can be replaced after creation via updateWithFilteredDocumentWriteSet()' to more accurately describe the intended usage pattern.