15
15
*/
16
16
package com .marklogic .client .impl ;
17
17
18
+ import java .util .ArrayList ;
18
19
import java .util .HashSet ;
19
20
import java .util .Set ;
20
21
21
22
import org .slf4j .Logger ;
22
23
import org .slf4j .LoggerFactory ;
23
24
25
+ import com .fasterxml .jackson .databind .JsonNode ;
26
+
24
27
import com .marklogic .client .DatabaseClientFactory .HandleFactoryRegistry ;
25
28
import com .marklogic .client .FailedRequestException ;
26
29
import com .marklogic .client .ForbiddenUserException ;
30
33
import com .marklogic .client .document .DocumentManager ;
31
34
import com .marklogic .client .document .DocumentMetadataPatchBuilder ;
32
35
import com .marklogic .client .document .DocumentUriTemplate ;
36
+ import com .marklogic .client .document .DocumentPage ;
37
+ import com .marklogic .client .document .DocumentRecord ;
38
+ import com .marklogic .client .document .DocumentWriteOperation ;
39
+ import com .marklogic .client .document .DocumentWriteSet ;
33
40
import com .marklogic .client .document .ServerTransform ;
34
41
import com .marklogic .client .impl .DocumentMetadataPatchBuilderImpl .DocumentPatchHandleImpl ;
35
42
import com .marklogic .client .io .Format ;
39
46
import com .marklogic .client .io .marker .DocumentMetadataReadHandle ;
40
47
import com .marklogic .client .io .marker .DocumentMetadataWriteHandle ;
41
48
import com .marklogic .client .io .marker .DocumentPatchHandle ;
49
+ import com .marklogic .client .io .marker .SearchReadHandle ;
50
+ import com .marklogic .client .io .JacksonHandle ;
51
+ import com .marklogic .client .io .SearchHandle ;
52
+ import com .marklogic .client .query .QueryDefinition ;
53
+ import com .marklogic .client .query .QueryManager .QueryView ;
42
54
import com .marklogic .client .util .RequestParameters ;
43
55
44
56
abstract class DocumentManagerImpl <R extends AbstractReadHandle , W extends AbstractWriteHandle >
45
57
extends AbstractLoggingManager
46
58
implements DocumentManager <R , W >
47
59
{
60
+ static final private long DEFAULT_PAGE_LENGTH = 50 ;
61
+
48
62
static final private Logger logger = LoggerFactory .getLogger (DocumentManagerImpl .class );
49
63
50
- final private Set <Metadata > processedMetadata ;
64
+ private boolean isProcessedMetadataModified = false ;
65
+ final private Set <Metadata > processedMetadata = new HashSet <Metadata >() {
66
+ public boolean add (Metadata e ) {
67
+ isProcessedMetadataModified = true ;
68
+ return super .add (e );
69
+ }
70
+ };
71
+ {
72
+ processedMetadata .add (Metadata .ALL );
73
+ // we need to know if the user modifies after us
74
+ isProcessedMetadataModified = false ;
75
+ }
76
+
51
77
52
78
private RESTServices services ;
53
79
private Format contentFormat ;
54
80
private HandleFactoryRegistry handleRegistry ;
55
81
private ServerTransform readTransform ;
56
82
private ServerTransform writeTransform ;
57
83
private String forestName ;
84
+ private long pageLength = DEFAULT_PAGE_LENGTH ;
85
+ private QueryView searchView = QueryView .RESULTS ;
86
+ private Format responseFormat = Format .XML ;
58
87
59
88
DocumentManagerImpl (RESTServices services , Format contentFormat ) {
60
89
super ();
@@ -82,15 +111,6 @@ public Format getContentFormat() {
82
111
}
83
112
84
113
// select categories of metadata to read, write, or reset
85
- {
86
- HashSet <Metadata > metadata = new HashSet <Metadata >();
87
- metadata .add (Metadata .ALL );
88
- processedMetadata = metadata ;
89
- }
90
- @ Override
91
- public Set <Metadata > getMetadataCategories () {
92
- return processedMetadata ;
93
- }
94
114
@ Override
95
115
public void setMetadataCategories (Set <Metadata > categories ) {
96
116
clearMetadataCategories ();
@@ -103,6 +123,10 @@ public void setMetadataCategories(Metadata... categories) {
103
123
processedMetadata .add (category );
104
124
}
105
125
@ Override
126
+ public Set <Metadata > getMetadataCategories () {
127
+ return processedMetadata ;
128
+ }
129
+ @ Override
106
130
public void clearMetadataCategories () {
107
131
processedMetadata .clear ();
108
132
}
@@ -250,7 +274,7 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
250
274
public <T extends R > T read (DocumentDescriptor desc , DocumentMetadataReadHandle metadataHandle , T contentHandle , ServerTransform transform , Transaction transaction , RequestParameters extraParams )
251
275
throws ResourceNotFoundException , ForbiddenUserException , FailedRequestException {
252
276
if (desc == null )
253
- throw new IllegalArgumentException ("Reading document with null identifier " );
277
+ throw new IllegalArgumentException ("Attempt to call read with null DocumentDescriptor " );
254
278
255
279
if (logger .isInfoEnabled ())
256
280
logger .info ("Reading metadata and content for {}" , desc .getUri ());
@@ -285,6 +309,130 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
285
309
return wasModified ? contentHandle : null ;
286
310
}
287
311
312
+ public DocumentPage read (String ... uris ) {
313
+ return read (null , null , uris );
314
+ }
315
+
316
+ public DocumentPage read (Transaction transaction , String ... uris ) {
317
+ return read (null , transaction , uris );
318
+ }
319
+
320
+ public DocumentPage read (ServerTransform transform , String ... uris ) {
321
+ return read (transform , null , uris );
322
+ }
323
+
324
+ public DocumentPage read (ServerTransform transform , Transaction transaction , String ... uris ) {
325
+ if (uris == null || uris .length == 0 )
326
+ throw new IllegalArgumentException ("Attempt to call read with no uris" );
327
+
328
+ if (logger .isInfoEnabled ())
329
+ logger .info ("Reading metadata and content for multiple uris beginning with {}" , uris [0 ]);
330
+
331
+ return services .getBulkDocuments (
332
+ requestLogger ,
333
+ (transaction == null ) ? null : transaction .getTransactionId (),
334
+ // the default for bulk is no metadata, which differs from the normal default of ALL
335
+ isProcessedMetadataModified ? processedMetadata : null ,
336
+ responseFormat ,
337
+ null ,
338
+ uris );
339
+ }
340
+
341
+ public DocumentPage search (QueryDefinition querydef , long start ) {
342
+ return search (querydef , start , null , null );
343
+ }
344
+
345
+ public DocumentPage search (QueryDefinition querydef , long start , SearchReadHandle searchHandle ) {
346
+ return search (querydef , start , searchHandle , null );
347
+ }
348
+
349
+ public DocumentPage search (QueryDefinition querydef , long start , Transaction transaction ) {
350
+ return search (querydef , start , null , transaction );
351
+ }
352
+
353
+ public DocumentPage search (QueryDefinition querydef , long start , SearchReadHandle searchHandle , Transaction transaction ) {
354
+
355
+ if ( searchHandle != null ) {
356
+ HandleImplementation searchBase = HandleAccessor .checkHandle (searchHandle , "search" );
357
+ if (searchHandle instanceof SearchHandle ) {
358
+ SearchHandle responseHandle = (SearchHandle ) searchHandle ;
359
+ responseHandle .setHandleRegistry (getHandleRegistry ());
360
+ responseHandle .setQueryCriteria (querydef );
361
+ }
362
+ if ( responseFormat != searchBase .getFormat () ) {
363
+ throw new UnsupportedOperationException ("The format supported by your handle:[" +
364
+ searchBase .getFormat () + "] does not match your setResponseFormat:[" + responseFormat + "]" );
365
+ }
366
+ }
367
+
368
+ String tid = transaction == null ? null : transaction .getTransactionId ();
369
+ // the default for bulk is no metadata, which differs from the normal default of ALL
370
+ Set <Metadata > metadata = isProcessedMetadataModified ? processedMetadata : null ;
371
+ return services .getBulkDocuments ( requestLogger , querydef , start , getPageLength (),
372
+ tid , searchHandle , searchView , metadata , responseFormat , null );
373
+ }
374
+
375
+ public long getPageLength () {
376
+ return pageLength ;
377
+ }
378
+
379
+ public void setPageLength (long length ) {
380
+ this .pageLength = length ;
381
+ }
382
+
383
+ public QueryView getSearchView () {
384
+ return searchView ;
385
+ }
386
+
387
+ public void setSearchView (QueryView view ) {
388
+ this .searchView = view ;
389
+ }
390
+
391
+ public Format getResponseFormat () {
392
+ return responseFormat ;
393
+ }
394
+
395
+ public void setResponseFormat (Format responseFormat ) {
396
+ if ( responseFormat != Format .XML && responseFormat != Format .JSON ) {
397
+ throw new UnsupportedOperationException ("Only XML and JSON are valid response formats. You specified:[" +
398
+ responseFormat + "]" );
399
+ }
400
+ this .responseFormat = responseFormat ;
401
+ }
402
+
403
+ public DocumentWriteSet newWriteSet () {
404
+ return new DocumentWriteSetImpl ();
405
+ }
406
+
407
+ public void write (DocumentWriteSet writeSet ) {
408
+ write (writeSet , null , null );
409
+ }
410
+
411
+ public void write (DocumentWriteSet writeSet , ServerTransform transform ) {
412
+ write (writeSet , transform , null );
413
+ }
414
+
415
+ public void write (DocumentWriteSet writeSet , Transaction transaction ) {
416
+ write (writeSet , null , transaction );
417
+ }
418
+
419
+ public void write (DocumentWriteSet writeSet , ServerTransform transform , Transaction transaction ) {
420
+ JacksonHandle jacksonHandle = new JacksonHandle ();
421
+ jacksonHandle = services .postBulkDocuments (
422
+ requestLogger ,
423
+ writeSet ,
424
+ (transform != null ) ? transform : getWriteTransform (),
425
+ (transaction == null ) ? null : transaction .getTransactionId (),
426
+ jacksonHandle );
427
+ JsonNode root = jacksonHandle .get ();
428
+ for (JsonNode item : root .get ("documents" )) {
429
+ String uri = item .get ("uri" ).asText ();
430
+ String mimetype = item .get ("mime-type" ).asText ();
431
+ String category = item .get ("category" ).get (0 ).asText ();
432
+ }
433
+
434
+ }
435
+
288
436
// shortcut writers
289
437
@ Override
290
438
public void writeAs (String uri , Object content )
0 commit comments