Skip to content

Commit 0acc53b

Browse files
author
mpv1989
committed
Add replace-insert support: DocumentCreateOptions.overwrite(Boolean)
1 parent 266a452 commit 0acc53b

File tree

5 files changed

+188
-117
lines changed

5 files changed

+188
-117
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
v4.5.0 (xxxx-xx-xx)
22
---------------------------
3+
* added replace-insert support: DocumentCreateOptions.overwrite(Boolean)
34
* added support for satellite collections: CollectionCreateOptions.satellite(Boolean)
45
* added AqlQueryOptions.stream(boolean) for Streaming AQL Cursors
56
* added ArangoDatabase.create()
Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,65 @@
1-
/*
2-
* DISCLAIMER
3-
*
4-
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5-
*
6-
* Licensed under the Apache License, Version 2.0 (the "License");
7-
* you may not use this file except in compliance with the License.
8-
* You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing, software
13-
* distributed under the License is distributed on an "AS IS" BASIS,
14-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
16-
* limitations under the License.
17-
*
18-
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19-
*/
20-
21-
package com.arangodb.entity;
22-
23-
import com.arangodb.velocypack.annotations.Expose;
24-
25-
/**
26-
* @author Mark Vollmary
27-
*
28-
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#create-document">API
29-
* Documentation</a>
30-
*/
31-
public class DocumentCreateEntity<T> extends DocumentEntity {
32-
33-
@Expose(deserialize = false)
34-
private T newDocument;
35-
36-
public DocumentCreateEntity() {
37-
super();
38-
}
39-
40-
/**
41-
* @return If the query parameter returnNew is true, then the complete new document is returned.
42-
*/
43-
public T getNew() {
44-
return newDocument;
45-
}
46-
47-
public void setNew(final T newDocument) {
48-
this.newDocument = newDocument;
49-
}
50-
51-
}
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity;
22+
23+
import com.arangodb.velocypack.annotations.Expose;
24+
25+
/**
26+
* @author Mark Vollmary
27+
*
28+
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#create-document">API
29+
* Documentation</a>
30+
*/
31+
public class DocumentCreateEntity<T> extends DocumentEntity {
32+
33+
@Expose(deserialize = false)
34+
private T newDocument;
35+
@Expose(deserialize = false)
36+
private T oldDocument;
37+
38+
public DocumentCreateEntity() {
39+
super();
40+
}
41+
42+
/**
43+
* @return If the query parameter returnNew is true, then the complete new document is returned.
44+
*/
45+
public T getNew() {
46+
return newDocument;
47+
}
48+
49+
public void setNew(final T newDocument) {
50+
this.newDocument = newDocument;
51+
}
52+
53+
/**
54+
* @return If the query parameter returnOld is true, then the complete previous revision of the document is
55+
* returned.
56+
*/
57+
public T getOld() {
58+
return oldDocument;
59+
}
60+
61+
public void setOld(final T oldDocument) {
62+
this.oldDocument = oldDocument;
63+
}
64+
65+
}

src/main/java/com/arangodb/internal/InternalArangoCollection.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ protected <T> Request insertDocumentRequest(final T value, final DocumentCreateO
9292
final DocumentCreateOptions params = (options != null ? options : new DocumentCreateOptions());
9393
request.putQueryParam(ArangoDBConstants.WAIT_FOR_SYNC, params.getWaitForSync());
9494
request.putQueryParam(ArangoDBConstants.RETURN_NEW, params.getReturnNew());
95+
request.putQueryParam(ArangoDBConstants.RETURN_OLD, params.getReturnOld());
96+
request.putQueryParam(ArangoDBConstants.OVERWRITE, params.getOverwrite());
9597
request.setBody(util().serialize(value));
9698
return request;
9799
}
@@ -107,6 +109,10 @@ public DocumentCreateEntity<T> deserialize(final Response response) throws VPack
107109
if (newDoc.isObject()) {
108110
doc.setNew((T) util().deserialize(newDoc, value.getClass()));
109111
}
112+
final VPackSlice oldDoc = body.get(ArangoDBConstants.OLD);
113+
if (oldDoc.isObject()) {
114+
doc.setOld((T) util().deserialize(oldDoc, value.getClass()));
115+
}
110116
final Map<DocumentField.Type, String> values = new HashMap<DocumentField.Type, String>();
111117
values.put(DocumentField.Type.ID, doc.getId());
112118
values.put(DocumentField.Type.KEY, doc.getKey());
Lines changed: 101 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,101 @@
1-
/*
2-
* DISCLAIMER
3-
*
4-
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5-
*
6-
* Licensed under the Apache License, Version 2.0 (the "License");
7-
* you may not use this file except in compliance with the License.
8-
* You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing, software
13-
* distributed under the License is distributed on an "AS IS" BASIS,
14-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
16-
* limitations under the License.
17-
*
18-
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19-
*/
20-
21-
package com.arangodb.model;
22-
23-
/**
24-
* @author Mark Vollmary
25-
*
26-
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#create-document">API
27-
* Documentation</a>
28-
*/
29-
public class DocumentCreateOptions {
30-
31-
private Boolean waitForSync;
32-
private Boolean returnNew;
33-
34-
public DocumentCreateOptions() {
35-
super();
36-
}
37-
38-
public Boolean getWaitForSync() {
39-
return waitForSync;
40-
}
41-
42-
/**
43-
* @param waitForSync
44-
* Wait until document has been synced to disk.
45-
* @return options
46-
*/
47-
public DocumentCreateOptions waitForSync(final Boolean waitForSync) {
48-
this.waitForSync = waitForSync;
49-
return this;
50-
}
51-
52-
public Boolean getReturnNew() {
53-
return returnNew;
54-
}
55-
56-
/**
57-
* @param returnNew
58-
* Return additionally the complete new document under the attribute new in the result.
59-
* @return options
60-
*/
61-
public DocumentCreateOptions returnNew(final Boolean returnNew) {
62-
this.returnNew = returnNew;
63-
return this;
64-
}
65-
66-
}
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.model;
22+
23+
/**
24+
* @author Mark Vollmary
25+
*
26+
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#create-document">API
27+
* Documentation</a>
28+
*/
29+
public class DocumentCreateOptions {
30+
31+
private Boolean waitForSync;
32+
private Boolean returnNew;
33+
private Boolean returnOld;
34+
private Boolean overwrite;
35+
36+
public DocumentCreateOptions() {
37+
super();
38+
}
39+
40+
public Boolean getWaitForSync() {
41+
return waitForSync;
42+
}
43+
44+
/**
45+
* @param waitForSync
46+
* Wait until document has been synced to disk.
47+
* @return options
48+
*/
49+
public DocumentCreateOptions waitForSync(final Boolean waitForSync) {
50+
this.waitForSync = waitForSync;
51+
return this;
52+
}
53+
54+
public Boolean getReturnNew() {
55+
return returnNew;
56+
}
57+
58+
/**
59+
* @param returnNew
60+
* Return additionally the complete new document under the attribute new in the result.
61+
* @return options
62+
*/
63+
public DocumentCreateOptions returnNew(final Boolean returnNew) {
64+
this.returnNew = returnNew;
65+
return this;
66+
}
67+
68+
public Boolean getReturnOld() {
69+
return returnOld;
70+
}
71+
72+
/**
73+
* @param returnOld
74+
* Additionally return the complete old document under the attribute old in the result. Only available if
75+
* the <code>overwrite</code> option is used.
76+
* @since ArangoDB 3.4
77+
* @return options
78+
*/
79+
public DocumentCreateOptions returnOld(final Boolean returnOld) {
80+
this.returnOld = returnOld;
81+
return this;
82+
}
83+
84+
public Boolean getOverwrite() {
85+
return overwrite;
86+
}
87+
88+
/**
89+
* @param overwrite
90+
* If set to true, the insert becomes a replace-insert. If a document with the same <code>_key</code>
91+
* already exists the new document is not rejected with unique constraint violated but will replace the
92+
* old document.
93+
* @since ArangoDB 3.4
94+
* @return options
95+
*/
96+
public DocumentCreateOptions overwrite(final Boolean overwrite) {
97+
this.overwrite = overwrite;
98+
return this;
99+
}
100+
101+
}

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ public void insertDocumentReturnNew() {
137137
assertThat(doc.getNew(), is(notNullValue()));
138138
}
139139

140+
@Test
141+
public void insertDocumentOverwriteReturnOld() {
142+
final BaseDocument doc = new BaseDocument();
143+
doc.addAttribute("value", "a");
144+
final DocumentCreateEntity<BaseDocument> meta = db.collection(COLLECTION_NAME).insertDocument(doc);
145+
doc.addAttribute("value", "b");
146+
final DocumentCreateEntity<BaseDocument> repsert = db.collection(COLLECTION_NAME).insertDocument(doc,
147+
new DocumentCreateOptions().overwrite(true).returnOld(true).returnNew(true));
148+
assertThat(repsert, is(notNullValue()));
149+
assertThat(repsert.getRev(), is(not(meta.getRev())));
150+
assertThat(repsert.getOld().getAttribute("value").toString(), is("a"));
151+
assertThat(repsert.getNew().getAttribute("value").toString(), is("b"));
152+
assertThat(db.collection(COLLECTION_NAME).count().getCount(), is(1L));
153+
}
154+
140155
@Test
141156
public void insertDocumentWaitForSync() {
142157
final DocumentCreateOptions options = new DocumentCreateOptions().waitForSync(true);

0 commit comments

Comments
 (0)