8000 [DE-539] return entity from ArangoOperations.repsert() by rashtao · Pull Request #285 · arangodb/spring-data · GitHub
[go: up one dir, main page]

Skip to content

[DE-539] return entity from ArangoOperations.repsert() #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ <T> MultiDocumentEntity<DocumentCreateEntity<?>> insertAll(Iterable<? extends T>
* @throws DataAccessException
* @since ArangoDB 3.4
*/
<T> void repsert(T value) throws DataAccessException;
<T> T repsert(T value) throws DataAccessException;

/**
* Creates new documents from the given documents, unless there already exists. In that case it replaces the
Expand All @@ -431,7 +431,7 @@ <T> MultiDocumentEntity<DocumentCreateEntity<?>> insertAll(Iterable<? extends T>
* @throws DataAccessException
* @since ArangoDB 3.4
*/
<T> void repsert(Iterable<? extends T> values, Class<T> entityClass) throws DataAccessException;
<T> Iterable<T> repsertAll(Iterable<T> values, Class<? super T> entityClass) throws DataAccessException;

/**
* Checks whether the document exists by reading a single document head
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ public DocumentCreateEntity<?> insert(final Object value) throws DataAccessExcep
}

@Override
public <T> void repsert(final T value) throws DataAccessException {
public <T> T repsert(final T value) throws DataAccessException {
@SuppressWarnings("unchecked") final Class<T> clazz = (Class<T>) value.getClass();
final String collectionName = _collection(clazz).name();

Expand All @@ -563,12 +563,14 @@ public <T> void repsert(final T value) throws DataAccessException {

updateDBFieldsFromObject(value, result);
potentiallyEmitEvent(new AfterSaveEvent<>(result));
return value;
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public <T> void repsert(final Iterable<? extends T> values, final Class<T> entityClass) throws DataAccessException {
public <T> Iterable<T> repsertAll(final Iterable<T> values, final Class<? super T> entityClass) throws DataAccessException {
if (!values.iterator().hasNext()) {
return;
return Collections.emptyList();
}

final String collectionName = _collection(entityClass).name();
Expand All @@ -578,7 +580,7 @@ public <T> void repsert(final Iterable<? extends T> values, final Class<T> entit
bindVars.put("@col", collectionName);
bindVars.put("docs", values);

final Iterable<? extends T> result;
List result;
try {
result = query(
REPSERT_MANY_QUERY,
Expand All @@ -591,6 +593,7 @@ public <T> void repsert(final Iterable<? extends T> values, final Class<T> entit

updateDBFieldsFromObjects(values, result);
result.forEach(it -> potentiallyEmitEvent(new AfterSaveEvent<>(it)));
return values;
}

private void updateDBFieldsFromObjects(final Iterable<?> values, final Iterable<?> res) {
Expand Down
8000
Original file line numberDiff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public SimpleArangoRepository(final ArangoOperations arangoOperations, final Cla
*/
@Override
public <S extends T> S save(final S entity) {
arangoOperations.repsert(entity);
return entity;
return arangoOperations.repsert(entity);
}

/**
Expand All @@ -87,8 +86,7 @@ public <S extends T> S save(final S entity) {
*/
@Override
public <S extends T> Iterable<S> saveAll(final Iterable<S> entities) {
arangoOperations.repsert(entities, domainClass);
return entities;
return arangoOperations.repsertAll(entities, domainClass);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,64 @@ public void insertDocumentsReturnNew() {

@Test
public void repsertDocument() {
String id = "id-" + UUID.randomUUID();
Customer customer = new Customer("John", "Doe", 30);
customer.setId(id);
template.repsert(customer);
template.repsert(customer);
Customer read = template.find(id, Customer.class).orElseThrow();
Customer repsert1 = template.repsert(customer);
Customer repsert2 = template.repsert(customer);

// in place updates
assertThat(repsert1, sameInstance(customer));
assertThat(repsert2, sameInstance(customer));

Customer read = template.find(repsert1.getId(), Customer.class).orElseThrow();
assertThat(read.getId(), is(customer.getId()));
assertThat(read.getName(), is(customer.getName()));
assertThat(read.getSurname(), is(customer.getSurname()));
assertThat(read.getAge(), is(customer.getAge()));
}

@Test
public void repsertDocumentRevConflict() {
String id = "id-" + UUID.randomUUID();
Customer customer = new Customer("John", "Doe", 30);
customer.setId(id);
template.repsert(customer);
customer.setRev("foo");
assertThrows(OptimisticLockingFailureException.class, () -> template.repsert(customer));
}
@Test
public void repsertDocuments() {
Customer c1 = new Customer("John", "Doe", 11);
Customer c2 = new Customer("John2", "Doe2", 22);
Iterable<Customer> repsert1 = template.repsertAll(List.of(c1, c2), Customer.class);
Iterable<Customer> repsert2 = template.repsertAll(List.of(c1, c2), Customer.class);

Iterator<Customer> rit1 = repsert1.iterator();
Iterator<Customer> rit2 = repsert2.iterator();

// in place updates
assertThat(rit1.next(), sameInstance(c1));
assertThat(rit2.next(), sameInstance(c1));
assertThat(rit1.next(), sameInstance(c2));
assertThat(rit2.next(), sameInstance(c2));

List<String> ids = StreamSupport.stream(repsert1.spliterator(), false).map(Customer::getId).toList();

Iterable<Customer> read = template.findAll(ids, Customer.class);
Iterator<Customer> rit = read.iterator();

Customer read1 = rit.next();
assertThat(read1.getId(), is(c1.getId()));
assertThat(read1.getName(), is(c1.getName()));
assertThat(read1.getSurname(), is(c1.getSurname()));
assertThat(read1.getAge(), is(c1.getAge()));

Customer read2 = rit.next();
assertThat(read2.getId(), is(c2.getId()));
assertThat(read2.getName(), is(c2.getName()));
assertThat(read2.getSurname(), is(c2.getSurname()));
assertThat(read2.getAge(), is(c2.getAge()));
}

@Test
public void repsertDocumentRevConflict() {
String id = "id-" + UUID.randomUUID();
Customer customer = new Customer("John", "Doe", 30);
customer.setId(id);
template.repsert(customer);
customer.setRev("foo");
assertThrows(OptimisticLockingFailureException.class, () -> template.repsert(customer));
}

@Test
public void getDocument() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.*;

/**
* @author Michele Rastelli
Expand Down Expand Up @@ -206,4 +205,33 @@ public void replaceAllVariance() {
assertThat(res.getDocuments().stream().map(DocumentUpdateEntity::getOld).toList().containsAll(dogs), is(true));
}

@Test
public void repsertVariance() {
Dog dog = new Dog();
dog.setId("1");
dog.setName("dog");
dog.setTeeths(11);

Dog res = template.repsert(dog);
assertThat(res, sameInstance(dog));
assertThat(col().documentExists(dog.getId()), is(true));
}

@Test
public void repsertAllVariance() {
Dog dog1 = new Dog();
dog1.setId("1");
dog1.setName("dog1");
dog1.setTeeths(11);

Dog dog2 = new Dog();
dog1.setId("2");
dog1.setName("dog2");
dog1.setTeeths(22);

List<Dog> dogs = List.of(dog1, dog2);
Iterable<Dog> res = template.repsertAll(dogs, Animal.class);
assertThat(res, containsInRelativeOrder(dog1, dog2));
}

}
0