Skip to content
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

Use Hibernate Reactive's persistAll in Panache #21840

Merged
merged 1 commit into from
Dec 1, 2021
Merged
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
@@ -1,6 +1,7 @@
package io.quarkus.hibernate.reactive.panache.common.runtime;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -10,9 +11,7 @@
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import javax.enterprise.inject.spi.Bean;
import javax.persistence.LockModeType;
Expand All @@ -33,6 +32,7 @@ public abstract class AbstractJpaOperations<PanacheQueryType> {

// FIXME: make it configurable?
static final long TIMEOUT_MS = 5000;
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];

private static void executeInVertxEventLoop(Runnable runnable) {
Vertx vertx = Arc.container().instance(Vertx.class).get();
Expand Down Expand Up @@ -80,27 +80,28 @@ public Uni<Void> persist(Uni<Mutiny.Session> sessionUni, Object entity) {
});
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public Uni<Void> persist(Iterable<?> entities) {
return persist(StreamSupport.stream(entities.spliterator(), false));
List list = new ArrayList();
for (Object entity : entities) {
list.add(entity);
}
return persist(list.toArray(EMPTY_OBJECT_ARRAY));
}

public Uni<Void> persist(Object firstEntity, Object... entities) {
List<Object> array = new ArrayList<>(entities.length + 1);
array.add(firstEntity);
for (Object entity : entities) {
array.add(entity);
}
return persist(array.stream());
List<Object> list = new ArrayList<>(entities.length + 1);
list.add(firstEntity);
Collections.addAll(list, entities);
return persist(list.toArray(EMPTY_OBJECT_ARRAY));
}

public Uni<Void> persist(Stream<?> entities) {
Uni<Mutiny.Session> session = getSession();
List<Uni<Void>> uniList = entities.map(entity -> persist(session, entity)).collect(Collectors.toList());
return Uni.combine().all().unis(uniList).discardItems();
// this should work, but doesn't
// return Multi.createFrom().items(entities)
// .map(entity -> persist(session, entity))
// .onItem().ignoreAsUni();
return persist(entities.toArray());
}

public Uni<Void> persist(Object... entities) {
return getSession().chain(session -> session.persistAll(entities));
}

public Uni<Void> delete(Object entity) {
Expand Down Expand Up @@ -465,4 +466,4 @@ public Uni<Integer> update(Class<?> entityClass, String query, Parameters params
public Uni<Integer> update(Class<?> entityClass, String query, Object... params) {
return executeUpdate(entityClass, query, params);
}
}
}