Skip to content

Commit

Permalink
feat: create find all by 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 3747de0 commit b118143
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/expert/os/harperdb/SearchById.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Represents the "search by ID" operation in HarperDB.
* This operation is used to retrieve records from a specific table in a given database based on their IDs.
*/
final class SearchById extends Operation {
final class SearchById<K> extends Operation {


@JsonProperty
Expand All @@ -19,7 +19,7 @@ final class SearchById extends Operation {
private final String table;

@JsonProperty
private final Set<Object> ids;
private final Set<K> ids;

@JsonProperty("get_attributes")
private final Set<String> attributes;
Expand All @@ -32,7 +32,7 @@ final class SearchById extends Operation {
* @param ids The set of IDs used to identify and retrieve specific records.
* @param attributes The set of attributes to retrieve for each record.
*/
SearchById(String database, String table, Set<Object> ids, Set<String> attributes) {
SearchById(String database, String table, Set<K> ids, Set<String> attributes) {
super(OperationType.SEARCH_BY_ID);
this.database = database;
this.table = table;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/expert/os/harperdb/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ public <T> boolean upsert(Iterable<T> entities) {
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);
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) {
public <K, T> List<T> findAllById(Iterable<K> 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);
var search = new SearchById<>(database, table(type), keys, ALL_ATTRIBUTES);
return server.result(search, type);
}

Expand Down
16 changes: 16 additions & 0 deletions core/src/test/java/expert/os/harperdb/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ void shouldFindById(Animal animal){
});
}

@ParameterizedTest
@MethodSource("animal")
void shouldReturnEmptyOptionalWhenNotFound(Animal animal){
Optional<Animal> optional = template.findById(animal, Animal.class);
Assertions.assertThat(optional).isEmpty();
}

@ParameterizedTest
@MethodSource("animals")
void shouldFindAllById(List<Animal> animals){
template.insert(animals);
var ids = animals.stream().map(Animal::id).toList();
List<Animal> entities = template.findAllById(ids, Animal.class);
Assertions.assertThat(entities).hasSize(animals.size());
}

static Stream<Arguments> animal(){
return Stream.of(Arguments.of(new Animal(FAKER.idNumber().valid(), FAKER.animal().name())));
}
Expand Down

0 comments on commit b118143

Please sign in to comment.