-
Notifications
You must be signed in to change notification settings - Fork 74
MLE-25782 Some refactoring of BatchWriter #1858
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
Merged
Changes from all commits
Commits
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
81 changes: 81 additions & 0 deletions
81
marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/BatchWriter.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,81 @@ | ||
| /* | ||
| * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. | ||
| */ | ||
| package com.marklogic.client.datamovement.impl; | ||
|
|
||
| import com.marklogic.client.document.DocumentWriteOperation; | ||
| import com.marklogic.client.document.XMLDocumentManager; | ||
| import com.marklogic.client.io.Format; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.util.function.Consumer; | ||
|
|
||
| class BatchWriter implements Runnable { | ||
|
|
||
| private static Logger logger = LoggerFactory.getLogger(WriteBatcherImpl.class); | ||
|
|
||
| private final BatchWriteSet writeSet; | ||
|
|
||
| BatchWriter(BatchWriteSet writeSet) { | ||
| if (writeSet.getWriteSet().size() == 0) { | ||
| throw new IllegalStateException("Attempt to write an empty batch"); | ||
| } | ||
| this.writeSet = writeSet; | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| try { | ||
| logger.trace("begin write batch {} to forest on host \"{}\"", writeSet.getBatchNumber(), writeSet.getClient().getHost()); | ||
| if (writeSet.getTemporalCollection() == null) { | ||
| writeSet.getClient().newDocumentManager().write( | ||
| writeSet.getWriteSet(), writeSet.getTransform(), null | ||
| ); | ||
| } else { | ||
| // to get access to the TemporalDocumentManager write overload we need to instantiate | ||
| // a JSONDocumentManager or XMLDocumentManager, but we don't want to make assumptions about content | ||
| // format, so we'll set the default content format to unknown | ||
| XMLDocumentManager docMgr = writeSet.getClient().newXMLDocumentManager(); | ||
| docMgr.setContentFormat(Format.UNKNOWN); | ||
| docMgr.write( | ||
| writeSet.getWriteSet(), writeSet.getTransform(), null, writeSet.getTemporalCollection() | ||
| ); | ||
| } | ||
| closeAllHandles(); | ||
| Runnable onSuccess = writeSet.getOnSuccess(); | ||
| if (onSuccess != null) { | ||
| onSuccess.run(); | ||
| } | ||
| } catch (Throwable t) { | ||
| logger.trace("failed batch sent to forest on host \"{}\"", writeSet.getClient().getHost()); | ||
| Consumer<Throwable> onFailure = writeSet.getOnFailure(); | ||
| if (onFailure != null) { | ||
| onFailure.accept(t); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void closeAllHandles() throws Throwable { | ||
| Throwable lastThrowable = null; | ||
| for (DocumentWriteOperation doc : writeSet.getWriteSet()) { | ||
| try { | ||
| if (doc.getContent() instanceof Closeable) { | ||
| ((Closeable) doc.getContent()).close(); | ||
| } | ||
| if (doc.getMetadata() instanceof Closeable) { | ||
| ((Closeable) doc.getMetadata()).close(); | ||
| } | ||
| } catch (Throwable t) { | ||
| logger.error("error calling close()", t); | ||
| lastThrowable = t; | ||
| } | ||
| } | ||
| if (lastThrowable != null) throw lastThrowable; | ||
| } | ||
|
|
||
| public BatchWriteSet getWriteSet() { | ||
| return writeSet; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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 onBeforeWrite callback that was present in the original implementation has been removed. If this callback was being used elsewhere in the codebase, its removal could break existing functionality. Verify that onBeforeWrite was truly unused before removing this feature.