1

I have an Person table which contains below information.

Person table type
personId INT
Name VarChar
fatherId INT --refer to personId in same table(Person)
MotherId INT --refer to personId in same table(Person)
More columns other details

I have to implement a method similar to the below older implementation below using ASYN programming that returns a family tree.

Older Implementation

My POJO class

public class FamilyTree

    Person person;
    FamilyTree fatherFamily;
    FamilyTree motherFamily;
    
    public FamilyTree (Person person, FamilyTree father, FamilyTree mother){
        this.person = person;
        this.fatherFamily = father;
        this.motherFamily = mother;
    }
    
    public static FamilyTree buildFamilyTree(int personId){
        Person person = PersonRepository.GetPersonById(personId);
        FamilyTree fatherTree = (person.getFatherId == null || person.getFatherId(isEmpty())?        null:buildFamilyTree(person.getFatherId());
        FamilyTree motherTree = (person.getMotherId == null || person.getMotherId(isEmpty())?null:buildFamilyTree(person.getMotherId());
        return new FamilyTree(person, fatherTree, motherTree);
    }

How do I implement this with Mutiny and Quarkus reactive SQL without causing block IO exceptions?

New implementation class of what I need is:

@ApplicationScoped
public class FamilyTreeRepostiory{

    @Inject
    OraclePool client;
    
    public Uni<Person> getPersonById(int personId){
        String sql = "select * from person where personId=?";
        Tuple tuple = Tuple.of(personId);
        return client.preparedQuery(sql).execute(tuple).onItem().transform(Rowset::iterator)
           .onItem.transform(iterator-> iterator.hasNext()?Person.convertPerson(iterator.next()):null);
    }
    
    public Uni<FamilyTree> getFamilyTree(int personId){
        Uni<Person> person = getPersonById(personId);
        //help with this implementation is needed.
        return familyTree ;
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
luiza
  • 74
  • 1
  • 8
  • The trick is to use `chain(person -> ...)` and to return `Uni.createFrom().nullITem()` if the person does not have a father/mother, or return the Uni returned by the recursive call. – Clement Nov 30 '22 at 12:21

1 Answers1

0

The implementation of the getFamilyTree() method using Mutiny and Quarkus reactive SQL could look like this:

public Uni<FamilyTree> getFamilyTree(int personId){
    Uni<Person> person = getPersonById(personId);
    return person.flatMap(p -> Uni.combine().all(
        getFamilyTree(p.getFatherId()),
        getFamilyTree(p.getMotherId())
    ).map(tuple -> new FamilyTree(p, tuple.getValue1(), tuple.getValue2()))); 
}