|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.client.datamovement.filter; |
| 5 | + |
| 6 | +import com.marklogic.client.FailedRequestException; |
| 7 | +import com.marklogic.client.document.DocumentWriteOperation; |
| 8 | +import com.marklogic.client.document.DocumentWriteSet; |
| 9 | +import com.marklogic.client.row.RowTemplate; |
| 10 | + |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.function.Consumer; |
| 14 | + |
| 15 | +/** |
| 16 | + * Uses an Optic query with fromView to get the existing hash values for a set of URIs from a TDE view. |
| 17 | + * This implementation requires a TDE template to be deployed that extracts the URI and hash metadata. |
| 18 | + * |
| 19 | + * @since 8.1.0 |
| 20 | + */ |
| 21 | +class IncrementalWriteViewFilter extends IncrementalWriteFilter { |
| 22 | + |
| 23 | + private final String schemaName; |
| 24 | + private final String viewName; |
| 25 | + |
| 26 | + IncrementalWriteViewFilter(String hashKeyName, String timestampKeyName, boolean canonicalizeJson, |
| 27 | + Consumer<DocumentWriteOperation[]> skippedDocumentsConsumer, String[] jsonExclusions, |
| 28 | + String[] xmlExclusions, Map<String, String> xmlNamespaces, |
| 29 | + String schemaName, String viewName) { |
| 30 | + super(hashKeyName, timestampKeyName, canonicalizeJson, skippedDocumentsConsumer, jsonExclusions, xmlExclusions, xmlNamespaces); |
| 31 | + this.schemaName = schemaName; |
| 32 | + this.viewName = viewName; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public DocumentWriteSet apply(Context context) { |
| 37 | + final String[] uris = context.getDocumentWriteSet().stream() |
| 38 | + .filter(op -> DocumentWriteOperation.OperationType.DOCUMENT_WRITE.equals(op.getOperationType())) |
| 39 | + .map(DocumentWriteOperation::getUri) |
| 40 | + .toArray(String[]::new); |
| 41 | + |
| 42 | + RowTemplate rowTemplate = new RowTemplate(context.getDatabaseClient()); |
| 43 | + |
| 44 | + try { |
| 45 | + Map<String, String> existingHashes = rowTemplate.query(op -> |
| 46 | + op.fromView(schemaName, viewName) |
| 47 | + .where(op.in(op.col("uri"), op.xs.stringSeq(uris))), |
| 48 | + |
| 49 | + rows -> { |
| 50 | + Map<String, String> map = new HashMap<>(); |
| 51 | + rows.forEach(row -> { |
| 52 | + String uri = row.getString("uri"); |
| 53 | + String existingHash = row.getString("hash"); |
| 54 | + map.put(uri, existingHash); |
| 55 | + }); |
| 56 | + return map; |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + return filterDocuments(context, uri -> existingHashes.get(uri)); |
| 61 | + } catch (FailedRequestException e) { |
| 62 | + String message = "Unable to query for existing incremental write hashes from view " + schemaName + "." + viewName + "; cause: " + e.getMessage(); |
| 63 | + throw new FailedRequestException(message, e.getFailedRequest()); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments