11package com .marklogic .client .ext .file ;
22
33import com .marklogic .client .DatabaseClient ;
4+ import com .marklogic .client .document .DocumentWriteOperation ;
45import com .marklogic .client .ext .batch .BatchWriter ;
56import com .marklogic .client .ext .batch .RestBatchWriter ;
67import com .marklogic .client .ext .helper .LoggingObject ;
1011import java .util .ArrayList ;
1112import java .util .Arrays ;
1213import java .util .List ;
14+ import java .util .stream .Collectors ;
1315
1416/**
1517 * Generic implementation of FileLoader. Delegates to a DocumentFileReader for reading from a set of file paths, and
1618 * delegates to a BatchWriter for writing to MarkLogic (where that BatchWriter could use XCC, the REST API, or the
1719 * Data Movement SDK in ML9).
18- *
20+ * <p>
1921 * The batchSize property defaults to null, which means all files are written in one call via the BatchWriter. Setting
2022 * this means that the List of DocumentFile objects read from the DocumentFileReader will be written in batches, each
2123 * the size of the batchSize property, except for the final one that may be less than this size.
@@ -108,15 +110,18 @@ protected void writeBatchOfDocuments(List<DocumentFile> documentFiles, final int
108110
109111 List <DocumentFile > batch = documentFiles .subList (startPosition , endPosition );
110112 if (!batch .isEmpty ()) {
111- if (logger .isInfoEnabled ()) {
113+ final boolean infoEnabled = logger .isInfoEnabled ();
114+ if (infoEnabled ) {
112115 logger .info (format ("Writing %d files" , batch .size ()));
113- if (logFileUris ) {
114- for (DocumentFile df : batch ) {
115- logger .info ("Writing: " + df .getUri ());
116- }
117- }
118116 }
119- batchWriter .write (batch );
117+ List <DocumentWriteOperation > documentWriteOperations = batch .stream ().map (file -> {
118+ if (logFileUris && infoEnabled ) {
119+ final String uri = file .getUri ();
120+ logger .info ("Writing: " + uri != null ? uri : file .getTemporalDocumentURI ());
121+ }
122+ return file .toDocumentWriteOperation ();
123+ }).collect (Collectors .toList ());
124+ batchWriter .write (documentWriteOperations );
120125 }
121126
122127 if (endPosition < documentFilesSize ) {
@@ -161,7 +166,7 @@ public void prepareAbstractDocumentFileReader(AbstractDocumentFileReader reader)
161166 FormatDocumentFileProcessor processor = reader .getFormatDocumentFileProcessor ();
162167 FormatGetter formatGetter = processor .getFormatGetter ();
163168 if (formatGetter instanceof DefaultDocumentFormatGetter ) {
164- DefaultDocumentFormatGetter ddfg = (DefaultDocumentFormatGetter )formatGetter ;
169+ DefaultDocumentFormatGetter ddfg = (DefaultDocumentFormatGetter ) formatGetter ;
165170 for (String ext : additionalBinaryExtensions ) {
166171 ddfg .getBinaryExtensions ().add (ext );
167172 }
@@ -181,7 +186,7 @@ public void prepareAbstractDocumentFileReader(AbstractDocumentFileReader reader)
181186 */
182187 protected void applyTokenReplacerOnKnownDocumentProcessors (AbstractDocumentFileReader reader ) {
183188 if (reader instanceof DefaultDocumentFileReader && tokenReplacer != null ) {
184- DefaultDocumentFileReader defaultReader = (DefaultDocumentFileReader )reader ;
189+ DefaultDocumentFileReader defaultReader = (DefaultDocumentFileReader ) reader ;
185190 CollectionsFileDocumentFileProcessor cp = defaultReader .getCollectionsFileDocumentFileProcessor ();
186191 if (cp != null ) {
187192 cp .setTokenReplacer (tokenReplacer );
0 commit comments