Skip to content

Commit

Permalink
feat: create result findby id
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Nov 29, 2023
1 parent a9c024f commit 97f3d0b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/src/main/java/expert/os/harperdb/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -71,13 +72,22 @@ public <T> boolean upsert(Iterable<T> entities) {
return server.execute(insert);
}

public <T> Optional<T> findById(Object id, Class<T> type) {
public <K, T> Optional<T> findById(K id, Class<T> 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 <T> List<T> findById(Iterable<Object> ids, Class<T> 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 <T> String table(T entity) {
return table(entity.getClass());
}
Expand Down

0 comments on commit 97f3d0b

Please sign in to comment.