-
Notifications
You must be signed in to change notification settings - Fork 74
MLE-26427 Namespace support for exclusions #1885
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
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
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 |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
|
|
||
|
|
@@ -51,6 +52,7 @@ public static class Builder { | |
| private Consumer<DocumentWriteOperation[]> skippedDocumentsConsumer; | ||
| private String[] jsonExclusions; | ||
| private String[] xmlExclusions; | ||
| private Map<String, String> xmlNamespaces; | ||
|
|
||
| /** | ||
| * @param keyName the name of the MarkLogic metadata key that will hold the hash value; defaults to "incrementalWriteHash". | ||
|
|
@@ -117,13 +119,22 @@ public Builder xmlExclusions(String... xpathExpressions) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @param namespaces a map of namespace prefixes to URIs for use in XPath exclusion expressions. | ||
| * For example, Map.of("ns", "http://example.com/ns") allows XPath like "//ns:timestamp". | ||
|
Comment on lines
+123
to
+124
|
||
| */ | ||
| public Builder xmlNamespaces(Map<String, String> namespaces) { | ||
| this.xmlNamespaces = namespaces; | ||
| return this; | ||
| } | ||
|
|
||
| public IncrementalWriteFilter build() { | ||
| validateJsonExclusions(); | ||
| validateXmlExclusions(); | ||
| if (useEvalQuery) { | ||
| return new IncrementalWriteEvalFilter(hashKeyName, timestampKeyName, canonicalizeJson, skippedDocumentsConsumer, jsonExclusions, xmlExclusions); | ||
| return new IncrementalWriteEvalFilter(hashKeyName, timestampKeyName, canonicalizeJson, skippedDocumentsConsumer, jsonExclusions, xmlExclusions, xmlNamespaces); | ||
| } | ||
| return new IncrementalWriteOpticFilter(hashKeyName, timestampKeyName, canonicalizeJson, skippedDocumentsConsumer, jsonExclusions, xmlExclusions); | ||
| return new IncrementalWriteOpticFilter(hashKeyName, timestampKeyName, canonicalizeJson, skippedDocumentsConsumer, jsonExclusions, xmlExclusions, xmlNamespaces); | ||
| } | ||
|
|
||
| private void validateJsonExclusions() { | ||
|
|
@@ -151,6 +162,9 @@ private void validateXmlExclusions() { | |
| return; | ||
| } | ||
| XPath xpath = XmlFactories.getXPathFactory().newXPath(); | ||
| if (xmlNamespaces != null && !xmlNamespaces.isEmpty()) { | ||
| xpath.setNamespaceContext(new SimpleNamespaceContext(xmlNamespaces)); | ||
| } | ||
| for (String xpathExpression : xmlExclusions) { | ||
| if (xpathExpression == null || xpathExpression.trim().isEmpty()) { | ||
| throw new IllegalArgumentException( | ||
|
|
@@ -173,18 +187,20 @@ private void validateXmlExclusions() { | |
| private final Consumer<DocumentWriteOperation[]> skippedDocumentsConsumer; | ||
| private final String[] jsonExclusions; | ||
| private final String[] xmlExclusions; | ||
| private final Map<String, String> xmlNamespaces; | ||
|
|
||
| // Hardcoding this for now, with a good general purpose hashing function. | ||
| // See https://xxhash.com for benchmarks. | ||
| private final LongHashFunction hashFunction = LongHashFunction.xx3(); | ||
|
|
||
| public IncrementalWriteFilter(String hashKeyName, String timestampKeyName, boolean canonicalizeJson, Consumer<DocumentWriteOperation[]> skippedDocumentsConsumer, String[] jsonExclusions, String[] xmlExclusions) { | ||
| public IncrementalWriteFilter(String hashKeyName, String timestampKeyName, boolean canonicalizeJson, Consumer<DocumentWriteOperation[]> skippedDocumentsConsumer, String[] jsonExclusions, String[] xmlExclusions, Map<String, String> xmlNamespaces) { | ||
| this.hashKeyName = hashKeyName; | ||
| this.timestampKeyName = timestampKeyName; | ||
| this.canonicalizeJson = canonicalizeJson; | ||
| this.skippedDocumentsConsumer = skippedDocumentsConsumer; | ||
| this.jsonExclusions = jsonExclusions; | ||
| this.xmlExclusions = xmlExclusions; | ||
| this.xmlNamespaces = xmlNamespaces; | ||
| } | ||
|
|
||
| protected final DocumentWriteSet filterDocuments(Context context, Function<String, String> hashRetriever) { | ||
|
|
@@ -260,7 +276,7 @@ private String serializeContent(DocumentWriteOperation doc) { | |
| } | ||
| } else if (xmlExclusions != null && xmlExclusions.length > 0) { | ||
| try { | ||
| content = ContentExclusionUtil.applyXmlExclusions(doc.getUri(), content, xmlExclusions); | ||
| content = ContentExclusionUtil.applyXmlExclusions(doc.getUri(), content, xmlNamespaces, xmlExclusions); | ||
| } catch (Exception e) { | ||
| logger.warn("Unable to apply XML exclusions for URI {}, using original content for hashing; cause: {}", | ||
| doc.getUri(), e.getMessage()); | ||
|
|
||
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
46 changes: 46 additions & 0 deletions
46
...nt-api/src/main/java/com/marklogic/client/datamovement/filter/SimpleNamespaceContext.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,46 @@ | ||
| /* | ||
| * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. | ||
| */ | ||
| package com.marklogic.client.datamovement.filter; | ||
|
|
||
| import javax.xml.namespace.NamespaceContext; | ||
| import java.util.Iterator; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A simple implementation of {@link NamespaceContext} backed by a Map of prefix to namespace URI mappings. | ||
| * Used for XPath evaluation with namespace-qualified expressions. | ||
| * | ||
| * @since 8.1.0 | ||
| */ | ||
| class SimpleNamespaceContext implements NamespaceContext { | ||
|
|
||
| private final Map<String, String> prefixToNamespaceUri; | ||
|
|
||
| SimpleNamespaceContext(Map<String, String> prefixToNamespaceUri) { | ||
| this.prefixToNamespaceUri = prefixToNamespaceUri; | ||
| } | ||
|
|
||
| @Override | ||
| public String getNamespaceURI(String prefix) { | ||
| return prefixToNamespaceUri.get(prefix); | ||
| } | ||
|
|
||
| @Override | ||
| public String getPrefix(String namespaceURI) { | ||
| for (Map.Entry<String, String> entry : prefixToNamespaceUri.entrySet()) { | ||
| if (entry.getValue().equals(namespaceURI)) { | ||
| return entry.getKey(); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<String> getPrefixes(String namespaceURI) { | ||
| return prefixToNamespaceUri.entrySet().stream() | ||
| .filter(entry -> entry.getValue().equals(namespaceURI)) | ||
| .map(Map.Entry::getKey) | ||
| .iterator(); | ||
| } | ||
| } |
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.
Remove the trailing whitespace on this blank line to maintain consistent code formatting.