55
66import com .fasterxml .jackson .databind .ObjectMapper ;
77import com .fasterxml .jackson .databind .node .ObjectNode ;
8- import com .marklogic .client .document .DocumentWriteOperation ;
8+ import com .marklogic .client .document .* ;
99import com .marklogic .client .impl .DocumentWriteOperationImpl ;
1010import com .marklogic .client .io .DocumentMetadataHandle ;
1111import com .marklogic .client .io .Format ;
@@ -150,8 +150,40 @@ void invalidJsonWithFormat() {
150150 "Expecting the server to throw an error. Actual message: " + message );
151151 }
152152
153+ @ Test
154+ void noRangeIndexForField () {
155+ filter = IncrementalWriteFilter .newBuilder ()
156+ .fieldName ("non-existent-field" )
157+ .build ();
158+
159+ writeTenDocuments ();
160+
161+ assertNotNull (batchFailure .get ());
162+ String message = batchFailure .get ().getMessage ();
163+ assertTrue (message .contains ("Unable to query for existing incremental write hashes" ) && message .contains ("XDMP-FIELDRIDXNOTFOUND" ),
164+ "When the user tries to use the incremental write feature without the required range index, we should " +
165+ "fail with a clear error message. Actual message: " + message );
166+ }
167+
168+ @ Test
169+ void noRangeIndexForFieldWithEval () {
170+ filter = IncrementalWriteFilter .newBuilder ()
171+ .fieldName ("non-existent-field" )
172+ .useEvalQuery (true )
173+ .build ();
174+
175+ writeTenDocuments ();
176+
177+ assertNotNull (batchFailure .get ());
178+ String message = batchFailure .get ().getMessage ();
179+ assertTrue (message .contains ("Unable to query for existing incremental write hashes" ) && message .contains ("XDMP-FIELDRIDXNOTFOUND" ),
180+ "When the user tries to use the incremental write feature without the required range index, we should " +
181+ "fail with a helpful error message. Actual message: " + message );
182+ }
183+
153184 private void verifyIncrementalWriteWorks () {
154185 writeTenDocuments ();
186+ verifyDocumentsHasHashInMetadataKey ();
155187 assertEquals (10 , writtenCount .get ());
156188 assertEquals (0 , skippedCount .get (), "No docs should have been skipped on the first write." );
157189
@@ -169,36 +201,44 @@ private void verifyIncrementalWriteWorks() {
169201 }
170202
171203 private void writeTenDocuments () {
172- new WriteBatcherTemplate (Common .client ).runWriteJob (writeBatcher -> writeBatcher
173- .withThreadCount (1 ).withBatchSize (5 )
174- .onBatchSuccess (batch -> writtenCount .addAndGet (batch .getItems ().length ))
175- .withDocumentWriteSetFilter (filter ),
176-
177- writeBatcher -> {
178- for (int i = 1 ; i <= 10 ; i ++) {
179- // Consistent URIs are required for incremental writes to work.
180- String uri = "/incremental/test/doc-" + i + ".xml" ;
181- String content = "<doc>This is document number " + i + "</doc>" ;
182- writeBatcher .add (uri , METADATA , new StringHandle (content ));
183- }
204+ docs = new ArrayList <>();
205+ for (int i = 1 ; i <= 10 ; i ++) {
206+ // Consistent URIs are required for incremental writes to work.
207+ String uri = "/incremental/test/doc-" + i + ".xml" ;
208+ String content = "<doc>This is document number " + i + "</doc>" ;
209+ docs .add (new DocumentWriteOperationImpl (uri , METADATA , new StringHandle (content )));
210+ }
211+ writeDocs (docs );
212+ }
213+
214+ private void verifyDocumentsHasHashInMetadataKey () {
215+ GenericDocumentManager mgr = Common .client .newDocumentManager ();
216+ mgr .setMetadataCategories (DocumentManager .Metadata .METADATAVALUES );
217+ DocumentPage page = mgr .search (Common .client .newQueryManager ().newStructuredQueryBuilder ().collection ("incremental-test" ), 1 );
218+ while (page .hasNext ()) {
219+ DocumentRecord doc = page .next ();
220+ DocumentMetadataHandle metadata = doc .getMetadata (new DocumentMetadataHandle ());
221+ assertTrue (metadata .getMetadataValues ().containsKey ("incrementalWriteHash" ),
222+ "Document " + doc .getUri () + " should have an incrementalWriteHash in its metadata values." );
223+
224+ String hash = metadata .getMetadataValues ().get ("incrementalWriteHash" );
225+ try {
226+ // Can use Java's support for parsing unsigned longs in base 16 to verify the hash is valid.
227+ Long .parseUnsignedLong (hash , 16 );
228+ } catch (NumberFormatException e ) {
229+ fail ("Document " + doc .getUri () + " has an invalid incrementalWriteHash value: " + hash );
184230 }
185- );
231+ }
186232 }
187233
188234 private void modifyFiveDocuments () {
189- new WriteBatcherTemplate (Common .client ).runWriteJob (writeBatcher -> writeBatcher
190- .withThreadCount (1 ).withBatchSize (5 )
191- .onBatchSuccess (batch -> writtenCount .addAndGet (batch .getItems ().length ))
192- .withDocumentWriteSetFilter (filter ),
193-
194- writeBatcher -> {
195- for (int i = 6 ; i <= 10 ; i ++) {
196- String uri = "/incremental/test/doc-" + i + ".xml" ;
197- String content = "<doc>This is modified content</doc>" ;
198- writeBatcher .add (uri , METADATA , new StringHandle (content ));
199- }
200- }
201- );
235+ docs = new ArrayList <>();
236+ for (int i = 6 ; i <= 10 ; i ++) {
237+ String uri = "/incremental/test/doc-" + i + ".xml" ;
238+ String content = "<doc>This is modified content</doc>" ;
239+ docs .add (new DocumentWriteOperationImpl (uri , METADATA , new StringHandle (content )));
240+ }
241+ writeDocs (docs );
202242 }
203243
204244 private void writeDocs (List <DocumentWriteOperation > docs ) {
0 commit comments