Skip to content

Commit 01898fd

Browse files
committed
#80 make count and deleteAll transactional
1 parent 23d40a9 commit 01898fd

File tree

4 files changed

+46
-35
lines changed

4 files changed

+46
-35
lines changed

src/main/java/com/arangodb/springframework/core/CollectionOperations.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,34 @@ public interface CollectionOperations {
5757
*
5858
* @throws DataAccessException
5959
*/
60-
void truncate() throws DataAccessException;
60+
void truncate(CollectionTruncateOptions options) throws DataAccessException;
61+
62+
/**
63+
* Removes all documents from the collection, but leaves the indexes intact
64+
*
65+
* @throws DataAccessException
66+
*/
67+
default void truncate() throws DataAccessException {
68+
truncate(new CollectionTruncateOptions());
69+
}
70+
71+
/**
72+
* Counts the documents in a collection
73+
*
74+
* @return number of
75+
* @throws DataAccessException
76+
*/
77+
long count(CollectionCountOptions options) throws DataAccessException;
6178

6279
/**
6380
* Counts the documents in a collection
6481
*
6582
* @return number of
6683
* @throws DataAccessException
6784
*/
68-
long count() throws DataAccessException;
85+
default long count() throws DataAccessException {
86+
return count(new CollectionCountOptions());
87+
}
6988

7089
/**
7190
* Reads the properties of the specified collection

src/main/java/com/arangodb/springframework/core/template/DefaultCollectionOperations.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ public void drop() throws DataAccessException {
6969
}
7070

7171
@Override
72-
public void truncate() throws DataAccessException {
72+
public void truncate(CollectionTruncateOptions options) throws DataAccessException {
7373
try {
74-
collection.truncate();
74+
collection.truncate(options);
7575
} catch (final ArangoDBException e) {
7676
throw translateException(e);
7777
}
7878
}
7979

8080
@Override
81-
public long count() throws DataAccessException {
81+
public long count(CollectionCountOptions options) throws DataAccessException {
8282
try {
83-
final Long count = collection.count().getCount();
83+
final Long count = collection.count(options).getCount();
8484
return count != null ? count : -1L;
8585
} catch (final ArangoDBException e) {
8686
throw translateException(e);

src/main/java/com/arangodb/springframework/repository/SimpleArangoRepository.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import com.arangodb.entity.DocumentDeleteEntity;
2626
import com.arangodb.entity.ErrorEntity;
2727
import com.arangodb.entity.MultiDocumentEntity;
28-
import com.arangodb.model.AqlQueryOptions;
29-
import com.arangodb.model.DocumentDeleteOptions;
30-
import com.arangodb.model.DocumentExistsOptions;
31-
import com.arangodb.model.DocumentReadOptions;
28+
import com.arangodb.model.*;
3229
import com.arangodb.springframework.core.DocumentNotFoundException;
3330
import com.arangodb.springframework.core.convert.ArangoConverter;
3431
import com.arangodb.springframework.core.mapping.ArangoMappingContext;
@@ -160,7 +157,7 @@ public Iterable<T> findAllById(final Iterable<ID> ids) {
160157
*/
161158
@Override
162159
public long count() {
163-
return arangoTemplate.collection(domainClass).count();
160+
return arangoTemplate.collection(domainClass).count(defaultCountOptions());
164161
}
165162

166163
/**
@@ -228,7 +225,7 @@ public void deleteAll(final Iterable<? extends T> entities) {
228225
*/
229226
@Override
230227
public void deleteAll() {
231-
arangoTemplate.collection(domainClass).truncate();
228+
arangoTemplate.collection(domainClass).truncate(defaultTruncateOptions());
232229
}
233230

234231
/**
@@ -406,35 +403,33 @@ private String buildSortClause(final Sort sort, final String varName) {
406403
}
407404

408405
private DocumentReadOptions defaultReadOptions() {
409-
DocumentReadOptions options = new DocumentReadOptions();
410-
if (transactionBridge != null) {
411-
options.streamTransactionId(transactionBridge.getCurrentTransaction(Collections.singleton(getCollectionName())));
412-
}
413-
return options;
406+
return defaultTransactionalOptions(new DocumentReadOptions());
414407
}
415408

416409
private AqlQueryOptions defaultQueryOptions() {
417-
AqlQueryOptions options = new AqlQueryOptions();
418-
if (transactionBridge != null) {
419-
options.streamTransactionId(transactionBridge.getCurrentTransaction(Collections.singleton(getCollectionName())));
420-
}
421-
return options;
410+
return defaultTransactionalOptions(new AqlQueryOptions());
422411
}
423412

424413
private DocumentExistsOptions defaultExistsOptions() {
425-
DocumentExistsOptions options = new DocumentExistsOptions();
426-
if (transactionBridge != null) {
427-
options.streamTransactionId(transactionBridge.getCurrentTransaction(Collections.singleton(getCollectionName())));
428-
}
429-
return options;
414+
return defaultTransactionalOptions(new DocumentExistsOptions());
430415
}
431416

432417
private DocumentDeleteOptions defaultDeleteOptions() {
433-
DocumentDeleteOptions options = new DocumentDeleteOptions();
418+
return defaultTransactionalOptions(new DocumentDeleteOptions());
419+
}
420+
421+
private CollectionTruncateOptions defaultTruncateOptions() {
422+
return defaultTransactionalOptions(new CollectionTruncateOptions());
423+
}
424+
425+
private CollectionCountOptions defaultCountOptions() {
426+
return defaultTransactionalOptions(new CollectionCountOptions());
427+
}
428+
429+
private <O extends TransactionalOptions<O>> O defaultTransactionalOptions(O options) {
434430
if (transactionBridge != null) {
435431
options.streamTransactionId(transactionBridge.getCurrentTransaction(Collections.singleton(getCollectionName())));
436432
}
437433
return options;
438434
}
439-
440435
}

src/test/java/com/arangodb/springframework/transaction/ArangoTransactionManagerRepositoryTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.arangodb.springframework.repository.ActorRepository;
66
import com.arangodb.springframework.repository.MovieRepository;
77
import com.arangodb.springframework.testdata.Movie;
8-
import org.junit.jupiter.api.Disabled;
98
import org.junit.jupiter.api.Test;
109
import org.springframework.beans.factory.annotation.Autowired;
1110
import org.springframework.data.util.Streamable;
@@ -84,7 +83,7 @@ public void shouldFindSavedEntityWithinTransaction_findAllById() {
8483
List<Movie> list = Streamable.of(movieRepository.findAllById(List.of(saved.getId()))).toList();
8584

8685
assertThat(list).hasSize(1);
87-
assertThat(list.getFirst().getId()).isEqualTo(saved.getId());
86+
assertThat(list.get(0).getId()).isEqualTo(saved.getId());
8887
}
8988

9089
@Test
@@ -110,7 +109,7 @@ public void shouldFindSavedEntityWithinTransaction_existsById() {
110109
@Test
111110
@Transactional
112111
public void shouldFindSavedEntityWithinTransaction_saveAll() {
113-
Movie saved = Streamable.of(movieRepository.saveAll(List.of(starWars))).stream().toList().getFirst();
112+
Movie saved = Streamable.of(movieRepository.saveAll(List.of(starWars))).stream().toList().get(0);
114113

115114
Boolean exists = movieRepository.existsById(saved.getId());
116115

@@ -125,12 +124,11 @@ public void shouldFindSavedEntityWithinTransaction_findAll() {
125124
List<Movie> list = Streamable.of(movieRepository.findAll()).toList();
126125

127126
assertThat(list).hasSize(1);
128-
assertThat(list.getFirst().getId()).isEqualTo(saved.getId());
127+
assertThat(list.get(0).getId()).isEqualTo(saved.getId());
129128
}
130129

131130
@Test
132131
@Transactional
133-
@Disabled("count is not transactional")
134132
public void shouldFindSavedEntityWithinTransaction_count() {
135133
movieRepository.save(starWars);
136134

@@ -185,7 +183,6 @@ public void shouldFindSavedEntityWithinTransaction_deleteAll() {
185183

186184
@Test
187185
@Transactional
188-
@Disabled("delete all is not transactional")
189186
public void shouldFindSavedEntityWithinTransaction_deleteAllNoArg() {
190187
Movie saved = movieRepository.save(starWars);
191188

0 commit comments

Comments
 (0)