From 97f3d0bf8d4df193545d3b3d45c796a62fb5d212 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Wed, 29 Nov 2023 05:53:39 +0000 Subject: [PATCH] feat: create result findby id Signed-off-by: Otavio Santana --- core/src/main/java/expert/os/harperdb/Template.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/expert/os/harperdb/Template.java b/core/src/main/java/expert/os/harperdb/Template.java index ca48743..cc6f761 100644 --- a/core/src/main/java/expert/os/harperdb/Template.java +++ b/core/src/main/java/expert/os/harperdb/Template.java @@ -6,6 +6,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import java.util.stream.StreamSupport; public final class Template { @@ -71,13 +72,22 @@ public boolean upsert(Iterable entities) { return server.execute(insert); } - public Optional findById(Object id, Class type) { + public Optional findById(K id, Class type) { Objects.requireNonNull(id, "id is required"); Objects.requireNonNull(type, "type is required"); var search = new SearchById(database, table(type), Collections.singleton(id), ALL_ATTRIBUTES); return server.singleResult(search, type); } + public List findById(Iterable ids, Class type) { + Objects.requireNonNull(ids, "ids is required"); + Objects.requireNonNull(type, "type is required"); + var keys = StreamSupport.stream(ids.spliterator(), false) + .collect(Collectors.toSet()); + var search = new SearchById(database, table(type), keys, ALL_ATTRIBUTES); + return server.result(search, type); + } + private String table(T entity) { return table(entity.getClass()); }