Skip to content

Commit 2eae18b

Browse files
committed
sort documents by URI within a batch.
1 parent 6551a1d commit 2eae18b

File tree

3 files changed

+138
-8
lines changed

3 files changed

+138
-8
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/document/DocumentWriteOperation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public enum OperationType {
4242
* <a href="http://docs.marklogic.com/guide/rest-dev/bulk#id_54554">
4343
* REST API Guide -&gt; Example: Reverting to System Default Metadata</a>
4444
*/
45+
@Deprecated
4546
DISABLE_METADATA_DEFAULT,
4647
/** This write operation (REST API mime part) creates or overwrites
4748
* one document and/or document metadata.

marklogic-client-api/src/main/java/com/marklogic/client/impl/DocumentWriteOperationImpl.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.marklogic.client.io.marker.AbstractWriteHandle;
2121
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
2222

23-
public class DocumentWriteOperationImpl implements DocumentWriteOperation {
23+
public class DocumentWriteOperationImpl implements DocumentWriteOperation,Comparable<DocumentWriteOperation> {
2424
private OperationType operationType;
2525
private String uri;
2626
private String temporalDocumentURI;
@@ -30,20 +30,31 @@ public class DocumentWriteOperationImpl implements DocumentWriteOperation {
3030
public DocumentWriteOperationImpl(OperationType type, String uri,
3131
DocumentMetadataWriteHandle metadata, AbstractWriteHandle content)
3232
{
33-
this.operationType = type;
34-
this.uri = uri;
35-
this.metadata = metadata;
36-
this.content = content;
33+
this(uri, type, metadata, content);
3734
this.temporalDocumentURI = null;
3835
}
3936

4037
public DocumentWriteOperationImpl(OperationType type, String uri,
4138
DocumentMetadataWriteHandle metadata, AbstractWriteHandle content, String temporalDocumentURI) {
39+
this(uri,type,metadata, content);
40+
this.temporalDocumentURI = temporalDocumentURI;
41+
}
42+
43+
private DocumentWriteOperationImpl(String uri, OperationType type,
44+
DocumentMetadataWriteHandle metadata, AbstractWriteHandle content) {
45+
46+
47+
if(type == OperationType.DOCUMENT_WRITE && uri == null) {
48+
throw new IllegalArgumentException("Uri cannot be null when Operation Type is DOCUMENT_WRITE.");
49+
}
50+
if(!(type == OperationType.DOCUMENT_WRITE) && uri != null) {
51+
throw new IllegalArgumentException("Operation Type should be DOCUMENT_WRITE when uri is not null");
52+
}
53+
4254
this.operationType = type;
4355
this.uri = uri;
4456
this.metadata = metadata;
4557
this.content = content;
46-
this.temporalDocumentURI = temporalDocumentURI;
4758
}
4859

4960
@Override
@@ -70,4 +81,37 @@ public DocumentMetadataWriteHandle getMetadata() {
7081
public AbstractWriteHandle getContent() {
7182
return content;
7283
}
84+
85+
@Override
86+
public int compareTo(DocumentWriteOperation o) {
87+
if(o == null)
88+
throw new NullPointerException("DocumentWriteOperation cannot be null");
89+
90+
if(this.getUri() instanceof String && o.getUri() instanceof String)
91+
return getUri().compareTo(o.getUri());
92+
93+
if(this.getUri() == null && o.getUri() instanceof String)
94+
return -1;
95+
96+
if(this.getUri() instanceof String && o.getUri()==null)
97+
return 1;
98+
99+
if(this.getUri() == null && o.getUri() == null)
100+
super.hashCode();
101+
return 0;
102+
}
103+
104+
@Override
105+
public int hashCode() {
106+
if(this.getUri()!=null)
107+
return this.getUri().hashCode();
108+
return super.hashCode();
109+
}
110+
111+
@Override
112+
public boolean equals(Object o){
113+
if(this.getUri()!=null)
114+
return this.getUri().equals(o);
115+
return super.equals(o);
116+
}
73117
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/DocumentWriteSetImpl.java

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@
2626
import com.marklogic.client.io.marker.ContentHandle;
2727
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
2828

29-
import java.util.LinkedHashSet;
29+
import java.util.*;
3030

31-
public class DocumentWriteSetImpl extends LinkedHashSet<DocumentWriteOperation> implements DocumentWriteSet {
31+
public class DocumentWriteSetImpl implements Set<DocumentWriteOperation>,DocumentWriteSet {
32+
33+
final private static String SET_TYPE = System.getProperty("com.marklogic.client.DocumentWriteSet.type", "sorted");
34+
private Set<DocumentWriteOperation> operations;
35+
36+
DocumentWriteSetImpl(){
37+
setOperations(SET_TYPE);
38+
39+
}
3240
@Override
3341
public DocumentWriteSet addDefault(DocumentMetadataWriteHandle metadataHandle) {
3442
add(new DocumentWriteOperationImpl(OperationType.METADATA_DEFAULT,
@@ -133,4 +141,81 @@ public DocumentWriteSet add(DocumentDescriptor desc, DocumentMetadataWriteHandle
133141
temporalDocumentURI));
134142
return this;
135143
}
144+
145+
@Override
146+
public int size() {
147+
return operations.size();
148+
}
149+
150+
@Override
151+
public boolean isEmpty() {
152+
return operations.isEmpty();
153+
}
154+
155+
@Override
156+
public boolean contains(Object o) {
157+
return operations.contains(o);
158+
}
159+
160+
@Override
161+
public Iterator<DocumentWriteOperation> iterator() {
162+
return operations.iterator();
163+
}
164+
165+
@Override
166+
public Object[] toArray() {
167+
return operations.toArray();
168+
}
169+
170+
@Override
171+
public <T> T[] toArray(T[] a) {
172+
return operations.toArray(a);
173+
}
174+
175+
@Override
176+
public boolean add(DocumentWriteOperation documentWriteOperation) {
177+
System.out.println(operations);
178+
return operations.add(documentWriteOperation);
179+
}
180+
181+
@Override
182+
public boolean remove(Object o) {
183+
return operations.remove(o);
184+
}
185+
186+
@Override
187+
public boolean containsAll(Collection<?> c) {
188+
return operations.containsAll(c);
189+
}
190+
191+
@Override
192+
public boolean addAll(Collection<? extends DocumentWriteOperation> c) {
193+
return operations.addAll(c);
194+
}
195+
196+
@Override
197+
public boolean retainAll(Collection<?> c) {
198+
return operations.retainAll(c);
199+
}
200+
201+
@Override
202+
public boolean removeAll(Collection<?> c) {
203+
return operations.removeAll(c);
204+
}
205+
206+
@Override
207+
public void clear() {
208+
operations.clear();
209+
}
210+
211+
private void setOperations(String SET_TYPE) {
212+
213+
if(SET_TYPE.equals("sorted")) {
214+
this.operations = new TreeSet<DocumentWriteOperation>();
215+
} else if(SET_TYPE.equals("unsorted")) {
216+
this.operations = new LinkedHashSet<DocumentWriteOperation>();
217+
} else {
218+
throw new IllegalStateException("SET_TYPE should be either sorted or unsorted");
219+
}
220+
}
136221
}

0 commit comments

Comments
 (0)