Ok, I checked several drivers and yes.
Usually, they return as Iterable.
I have on my mind two solutions both it will change the result to all search results.
We can change the result to Iterable itself, e.g.:
Iterable<DocumentEntity> query(String query);
What I propose it to have an interface to represents this result as iterable, therefore:
- We can have a Stream default method to make easier to the final user.
- We can create a default supplier where give the entities result from database and a converter, to translate the entities to Jakarta NoSQL API, it will create a default Result to the drives, so make to driver support as well.
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public interface Result<T> extends Iterable<T> {
default Stream<T> toStream() {
return StreamSupport.stream(this.spliterator(), false);
}
static <E, T> Result<T> of(Iterable<E> entities, Function<E, T> converter) {
Objects.requireNonNull(converter, "converter is required");
Objects.requireNonNull(entities, "entities is required");
final ResultSupplier<T, E> supplier = ServiceLoaderProvider.get(ResultSupplier.class);
return supplier.apply(converter, entities);
}
interface ResultSupplier<T, E> extends BiFunction<Function<E, T>, Iterable<E>, Result<T>> {
}
}
The new return:
Result<DocumentEntity> query(String query);
So:
Iterable<DatabaseEntity> entities = ...;
Function<DatabaseEntity, ColumnEntity> converter = ...;
final Result<ColumnEntity> result = Result.of(entities, converter);
for (ColumnEntity columnEntity : result) {
System.out.println("");
}
final Stream<ColumnEntity> columnEntityStream = result.toStream();