class UnitGraphInfoLoader {

    public Map<String, UnitGraphInfo> selectAllUnitGraphByIds(MetaDataClient metaDataClient, Set<String> unitIds)
        throws VitamDBException, InvalidParseOperationException, MetaDataExecutionException,
        MetaDataDocumentSizeException, MetaDataClientServerException, InvalidCreateOperationException {

        // Result map
        Map<String, UnitGraphInfo> unitGraphById = new HashMap<>();

        // The remaining units to load
        Set<String> unitsToLoad = new HashSet<>(unitIds);

        while (!unitsToLoad.isEmpty()) {

            // Load units by bulk (ES $in query size is limited)
            Set<String> bulkIds = unitsToLoad.stream()
                .limit(MAX_ELASTIC_SEARCH_IN_REQUEST_SIZE)
                .collect(Collectors.toSet());

            // Pre-fill with null values (in case entry not found)
            for (String bulkId : bulkIds) {
                unitGraphById.put(bulkId, null);
            }

            // Load bulk units
            List<UnitGraphInfo> loadedUnits = loadBulkUnitGraph(metaDataClient, bulkIds);
            for (UnitGraphInfo loadedUnit : loadedUnits) {
                unitGraphById.put(loadedUnit.getId(), loadedUnit);
                for (String up : loadedUnit.getUp()) {
                    if (!unitGraphById.containsKey(up)) {
                        unitsToLoad.add(up);
                    }
                }
            }

            unitsToLoad.removeAll(bulkIds);
        }

        return unitGraphById;
    }

}