Skip to content

Commit 4684e0b

Browse files
author
Mark Vollmary
committed
revision id handling via "If-Match" in HTTP header, not via "rev" and
"policy" params
1 parent 3f99641 commit 4684e0b

File tree

10 files changed

+108
-257
lines changed

10 files changed

+108
-257
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 70 additions & 165 deletions
Large diffs are not rendered by default.

src/main/java/com/arangodb/InternalDocumentDriver.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.List;
44

55
import com.arangodb.entity.DocumentEntity;
6-
import com.arangodb.entity.Policy;
76
import com.arangodb.impl.BaseDriverInterface;
87

98
/**
@@ -28,23 +27,20 @@ <T> DocumentEntity<T> replaceDocument(
2827
String documentHandle,
2928
T value,
3029
Long rev,
31-
Policy policy,
3230
Boolean waitForSync) throws ArangoException;
3331

3432
DocumentEntity<String> replaceDocumentRaw(
3533
String database,
3634
String documentHandle,
3735
String rawJsonString,
3836
Long rev,
39-
Policy policy,
4037
Boolean waitForSync) throws ArangoException;
4138

4239
<T> DocumentEntity<T> updateDocument(
4340
String database,
4441
String documentHandle,
4542
T value,
4643
Long rev,
47-
Policy policy,
4844
Boolean waitForSync,
4945
Boolean keepNull) throws ArangoException;
5046

@@ -53,7 +49,6 @@ DocumentEntity<String> updateDocumentRaw(
5349
String documentHandle,
5450
String rawJsonString,
5551
Long rev,
56-
Policy policy,
5752
Boolean waitForSync,
5853
Boolean keepNull) throws ArangoException;
5954

@@ -71,6 +66,5 @@ <T> DocumentEntity<T> getDocument(
7166
String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision)
7267
throws ArangoException;
7368

74-
DocumentEntity<?> deleteDocument(String database, String documentHandle, Long rev, Policy policy)
75-
throws ArangoException;
69+
DocumentEntity<?> deleteDocument(String database, String documentHandle, Long rev) throws ArangoException;
7670
}

src/main/java/com/arangodb/entity/Policy.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.List;
2020
import java.util.ListIterator;
21-
import java.util.Locale;
2221
import java.util.Map;
2322
import java.util.regex.Matcher;
2423
import java.util.regex.Pattern;
@@ -30,7 +29,6 @@
3029
import com.arangodb.entity.DefaultEntity;
3130
import com.arangodb.entity.DocumentEntity;
3231
import com.arangodb.entity.EntityFactory;
33-
import com.arangodb.entity.Policy;
3432
import com.arangodb.http.HttpManager;
3533
import com.arangodb.http.HttpResponseEntity;
3634
import com.arangodb.util.MapBuilder;
@@ -116,19 +114,13 @@ public <T> DocumentEntity<T> replaceDocument(
116114
final String documentHandle,
117115
final T value,
118116
final Long rev,
119-
final Policy policy,
120117
final Boolean waitForSync) throws ArangoException {
121118

122119
validateDocumentHandle(documentHandle);
123120

124-
Map<String, Object> header = null;
125-
if (rev != null) {
126-
final MapBuilder mapBuilder = new MapBuilder().put("If-Match", rev);
127-
header = mapBuilder.get();
128-
}
129-
130-
final HttpResponseEntity res = httpManager.doPut(createDocumentEndpointUrl(database, documentHandle), header,
131-
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), EntityFactory.toJsonString(value));
121+
final HttpResponseEntity res = httpManager.doPut(createDocumentEndpointUrl(database, documentHandle),
122+
createRevisionCheckHeader(rev), new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(),
123+
EntityFactory.toJsonString(value));
132124

133125
final DocumentEntity<T> result = createEntity(res, DocumentEntity.class);
134126
annotationHandler.updateDocumentRev(value, result.getDocumentRevision());
@@ -142,14 +134,11 @@ public DocumentEntity<String> replaceDocumentRaw(
142134
final String documentHandle,
143135
final String rawJsonString,
144136
final Long rev,
145-
final Policy policy,
146137
final Boolean waitForSync) throws ArangoException {
147138

148139
validateDocumentHandle(documentHandle);
149-
final HttpResponseEntity res = httpManager.doPut(
150-
createDocumentEndpointUrl(database, documentHandle), new MapBuilder().put("rev", rev)
151-
.put(POLICY, policy == null ? null : policy.name()).put(WAIT_FOR_SYNC, waitForSync).get(),
152-
rawJsonString);
140+
final HttpResponseEntity res = httpManager.doPut(createDocumentEndpointUrl(database, documentHandle),
141+
createRevisionCheckHeader(rev), new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), rawJsonString);
153142

154143
@SuppressWarnings("unchecked")
155144
final DocumentEntity<String> result = createEntity(res, DocumentEntity.class);
@@ -163,14 +152,13 @@ public <T> DocumentEntity<T> updateDocument(
163152
final String documentHandle,
164153
final T value,
165154
final Long rev,
166-
final Policy policy,
167155
final Boolean waitForSync,
168156
final Boolean keepNull) throws ArangoException {
169157

170158
validateDocumentHandle(documentHandle);
171159
final HttpResponseEntity res = httpManager.doPatch(createDocumentEndpointUrl(database, documentHandle),
172-
new MapBuilder().put("rev", rev).put(POLICY, policy == null ? null : policy.name())
173-
.put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(),
160+
createRevisionCheckHeader(rev),
161+
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(),
174162
EntityFactory.toJsonString(value, keepNull != null && !keepNull));
175163

176164
@SuppressWarnings("unchecked")
@@ -187,15 +175,13 @@ public DocumentEntity<String> updateDocumentRaw(
187175
final String documentHandle,
188176
final String rawJsonString,
189177
final Long rev,
190-
final Policy policy,
191178
final Boolean waitForSync,
192179
final Boolean keepNull) throws ArangoException {
193180

194181
validateDocumentHandle(documentHandle);
195182
final HttpResponseEntity res = httpManager.doPatch(createDocumentEndpointUrl(database, documentHandle),
196-
new MapBuilder().put("rev", rev).put(POLICY, policy == null ? null : policy.name())
197-
.put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(),
198-
rawJsonString);
183+
createRevisionCheckHeader(rev),
184+
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(), rawJsonString);
199185

200186
@SuppressWarnings("unchecked")
201187
final DocumentEntity<String> result = createEntity(res, DocumentEntity.class);
@@ -285,18 +271,21 @@ public String getDocumentRaw(
285271
}
286272

287273
@Override
288-
public DocumentEntity<?> deleteDocument(
289-
final String database,
290-
final String documentHandle,
291-
final Long rev,
292-
final Policy policy) throws ArangoException {
274+
public DocumentEntity<?> deleteDocument(final String database, final String documentHandle, final Long rev)
275+
throws ArangoException {
293276

294277
validateDocumentHandle(documentHandle);
295278
final HttpResponseEntity res = httpManager.doDelete(createDocumentEndpointUrl(database, documentHandle),
296-
new MapBuilder().put("rev", rev).put(POLICY, policy == null ? null : policy.name().toLowerCase(Locale.US))
297-
.get());
298-
279+
createRevisionCheckHeader(rev), null);
299280
return createEntity(res, DocumentEntity.class);
300281
}
301282

283+
private Map<String, Object> createRevisionCheckHeader(final Long rev) {
284+
Map<String, Object> header = null;
285+
if (rev != null) {
286+
final MapBuilder mapBuilder = new MapBuilder().put("If-Match", rev);
287+
header = mapBuilder.get();
288+
}
289+
return header;
290+
}
302291
}

src/test/java/com/arangodb/ArangoDriverCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public void test_getCollectionFigures_01() throws ArangoException {
425425
final DocumentEntity<TestComplexEntity01> entity = driver.createDocument(collectionName, value, true);
426426
// 1個消す
427427
if (i == 50) {
428-
driver.deleteDocument(entity.getDocumentHandle(), null, null);
428+
driver.deleteDocument(entity.getDocumentHandle());
429429
}
430430
}
431431

src/test/java/com/arangodb/ArangoDriverDocumentTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void test_replace() throws ArangoException {
207207
final Long rev = doc.getDocumentRevision();
208208

209209
final DocumentEntity<TestComplexEntity01> doc2 = driver.replaceDocument(doc.getDocumentHandle(), value, null,
210-
null, null);
210+
null);
211211

212212
assertThat(doc2.getDocumentHandle(), is(id));
213213
assertThat(doc2.getDocumentKey(), is(key));
@@ -247,7 +247,7 @@ public void test_replace_with_document_attributes() throws ArangoException {
247247
final Long rev = doc.getDocumentRevision();
248248

249249
final DocumentEntity<TestComplexEntity03> doc2 = driver.replaceDocument(doc.getDocumentHandle(), value, null,
250-
null, null);
250+
null);
251251
TestComplexEntity03 ent = doc2.getEntity();
252252

253253
assertThat(ent.getDocumentHandle(), is(id));
@@ -280,7 +280,7 @@ public void test_replace_404() throws ArangoException {
280280
final TestComplexEntity01 value = new TestComplexEntity01("test-user", "test user", 22);
281281
// 存在しないコレクションに追加しようとする
282282
try {
283-
driver.replaceDocument(collectionName404, 1, value, null, null, null);
283+
driver.replaceDocument(collectionName404, 1, value, null, null);
284284
fail("no exception was thrown");
285285
} catch (final ArangoException e) {
286286
assertThat(e.getCode(), is(404));
@@ -293,7 +293,7 @@ public void test_replace_404_2() throws ArangoException {
293293
final TestComplexEntity01 value = new TestComplexEntity01("test-user", "test user", 22);
294294
// 存在するコレクションだが、ドキュメントが存在しない
295295
try {
296-
driver.replaceDocument(collectionName, 1, value, null, null, null);
296+
driver.replaceDocument(collectionName, 1, value, null, null);
297297
fail("no exception was thrown");
298298
} catch (final ArangoException e) {
299299
assertThat(e.getCode(), is(404));
@@ -314,7 +314,7 @@ public void test_partial_update() throws ArangoException {
314314
value.setDesc("UpdatedDescription");
315315
value.setAge(15);
316316
final DocumentEntity<TestComplexEntity01> doc2 = driver.updateDocument(doc.getDocumentHandle(), value, null,
317-
null, null, null);
317+
null, null);
318318
assertThat(doc2.getStatusCode(), is(202));
319319
// Get
320320
final DocumentEntity<TestComplexEntity01> doc3 = driver.getDocument(doc2.getDocumentHandle(),
@@ -325,7 +325,7 @@ public void test_partial_update() throws ArangoException {
325325
assertThat(doc3.getEntity().getDesc(), is("UpdatedDescription"));
326326
assertThat(doc3.getEntity().getAge(), is(15));
327327
final DocumentEntity<TestComplexEntity01> doc4 = driver.updateDocument(doc.getDocumentHandle(), value, null,
328-
null, null, false);
328+
null, false);
329329
assertThat(doc4.getStatusCode(), is(202));
330330
final DocumentEntity<TestComplexEntity01> doc5 = driver.getDocument(doc2.getDocumentHandle(),
331331
TestComplexEntity01.class);
@@ -349,7 +349,7 @@ public void test_partial_update_with_document_attributes() throws ArangoExceptio
349349
value.setDesc("UpdatedDescription");
350350
value.setAge(15);
351351
final DocumentEntity<TestComplexEntity03> doc2 = driver.updateDocument(doc.getDocumentHandle(), value, null,
352-
null, null, null);
352+
null, null);
353353
assertThat(doc2.getStatusCode(), is(202));
354354

355355
final TestComplexEntity03 en1 = doc2.getEntity();
@@ -369,7 +369,7 @@ public void test_partial_update_with_document_attributes() throws ArangoExceptio
369369
assertThat(doc3.getDocumentRevision(), is(rev1));
370370

371371
final DocumentEntity<TestComplexEntity03> doc4 = driver.updateDocument(doc.getDocumentHandle(), value, null,
372-
null, null, false);
372+
null, false);
373373
assertThat(doc4.getStatusCode(), is(202));
374374
final DocumentEntity<TestComplexEntity03> doc5 = driver.getDocument(doc2.getDocumentHandle(),
375375
TestComplexEntity03.class);

src/test/java/com/arangodb/ArangoDriverReplicationTestScenario1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void test_scienario() throws ArangoException, InterruptedException {
194194
// ------------------------------------------------------------
195195
// [Master] replace document
196196
masterDriver.replaceDocument(doc2.getDocumentHandle(), new MapBuilder().put("updatedKey", "あいうえお").get(), null,
197-
null, null);
197+
null);
198198

199199
// wait
200200
TimeUnit.SECONDS.sleep(2);
@@ -213,7 +213,7 @@ public void test_scienario() throws ArangoException, InterruptedException {
213213
// ------------------------------------------------------------
214214
// [Master] update document
215215
masterDriver.updateDocument(doc2.getDocumentHandle(), new MapBuilder().put("updatedKey2", "ABCDE").get(), null,
216-
null, null, null);
216+
null, null);
217217

218218
// wait
219219
TimeUnit.SECONDS.sleep(2);

src/test/java/com/arangodb/ArangoDriverThreadSafeTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import com.arangodb.entity.CollectionEntity;
3232
import com.arangodb.entity.DocumentEntity;
33-
import com.arangodb.entity.Policy;
3433

3534
/**
3635
* @author tamtam180 - kirscheless at gmail.com
@@ -89,7 +88,7 @@ public void run() {
8988
assertThat(ret2.getEntity().getAge(), is(value.getAge()));
9089

9190
// ドキュメントを削除する
92-
final DocumentEntity<?> ret3 = driver.deleteDocument(_id, null, Policy.LAST);
91+
final DocumentEntity<?> ret3 = driver.deleteDocument(_id);
9392
assertThat(ret3.getDocumentHandle(), is(_id));
9493
assertThat(ret3.getDocumentRevision(), is(_rev));
9594

src/test/java/com/arangodb/example/document/RawDocumentExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public void ReadDocuments() {
133133

134134
x = "{\"test\":1234}";
135135
try {
136-
final DocumentEntity<String> updateDocumentRaw = driver.updateDocumentRaw(documentHandle2, x, null, null,
137-
false, true);
136+
final DocumentEntity<String> updateDocumentRaw = driver.updateDocumentRaw(documentHandle2, x, null, false,
137+
true);
138138
// print new document revision
139139
System.out.println("rev: " + updateDocumentRaw.getDocumentRevision());
140140
// show request result (you have to use getDocumentRaw to get
@@ -150,7 +150,7 @@ public void ReadDocuments() {
150150

151151
x = "{\"hund\":321,\"katze\":321,\"maus\":777}";
152152
try {
153-
final DocumentEntity<String> replaceDocumentRaw = driver.replaceDocumentRaw(documentHandle2, x, null, null,
153+
final DocumentEntity<String> replaceDocumentRaw = driver.replaceDocumentRaw(documentHandle2, x, null,
154154
false);
155155
// print new document revision
156156
System.out.println("rev: " + replaceDocumentRaw.getDocumentRevision());

src/test/java/com/arangodb/example/document/ReplaceAndUpdateDocumentExample.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.arangodb.ArangoException;
2727
import com.arangodb.ErrorNums;
2828
import com.arangodb.entity.DocumentEntity;
29-
import com.arangodb.entity.Policy;
3029

3130
public class ReplaceAndUpdateDocumentExample extends BaseExample {
3231

@@ -149,7 +148,7 @@ public void replaceAndUpdateDocument() {
149148
try {
150149
final DocumentPerson dp = new DocumentPerson("Nina", "female", 9);
151150
// wrong revision
152-
driver.replaceDocument(documentHandleExample, dp, 22L, Policy.ERROR, true);
151+
driver.replaceDocument(documentHandleExample, dp, 22L, true);
153152
Assert.fail("replaceDocument should fail here!");
154153
} catch (final ArangoException e) {
155154
Assert.assertEquals(ErrorNums.ERROR_ARANGO_CONFLICT, e.getErrorNumber());
@@ -158,7 +157,7 @@ public void replaceAndUpdateDocument() {
158157
try {
159158
final DocumentPerson dp = new DocumentPerson("Nina", "female", 9);
160159
// current revision
161-
driver.replaceDocument(documentHandleExample, dp, revision, Policy.ERROR, true);
160+
driver.replaceDocument(documentHandleExample, dp, revision, true);
162161
} catch (final ArangoException e) {
163162
Assert.fail("Failed to replace document. " + e.getMessage());
164163
}

0 commit comments

Comments
 (0)