private List switchRowInfosToDataPrivileges()

in backend/src/main/java/org/apache/iotdb/admin/service/impl/IotDBServiceImpl.java [1266:1411]


  private List<DataPrivilegeVO> switchRowInfosToDataPrivileges(
      List<String> rowInfos, SessionPool sessionPool) throws BaseException {
    if (rowInfos == null || rowInfos.size() == 0) {
      return null;
    }
    List<String> paths = new ArrayList<>();
    List<List<String>> privilegesList = new ArrayList<>();
    List<DataPrivilegeVO> dataPrivilegeList = new ArrayList<>();
    for (String rowInfo : rowInfos) {
      String[] split = rowInfo.split("\\s:\\s");
      String[] allPrivileges = split[1].split("\\s");
      String path = split[0];
      if ("root".equals(path)) {
        handleRootPrivileges(allPrivileges, dataPrivilegeList);
        continue;
      }

      List<String> privileges = new ArrayList<>();
      for (String privilege : allPrivileges) {
        if (DATA_PRIVILEGES.contains(privilege)) {
          privileges.add(privilege);
        }
      }
      privilegesList.add(privileges);
      paths.add(path);
    }

    // Map form: {"privilege1 privilege2 privilege3..." : ["path1","path2"]} path1,path2 have the
    // same privileges.
    Map<String, List<String>> privilegeOne = new HashMap<>();
    Map<String, List<String>> privilegeTwo = new HashMap<>();
    Map<String, List<String>> privilegeThree = new HashMap<>();
    for (int i = 0; i < paths.size(); i++) {
      String path = paths.get(i);
      String privilegesStr = String.join(" ", privilegesList.get(i));
      int type = findType(sessionPool, path);
      if (type == 1) {
        if (privilegeOne.containsKey(privilegesStr)) {
          List<String> pathList = privilegeOne.get(privilegesStr);
          pathList.add(path);
          continue;
        }
        ArrayList<String> newPaths = new ArrayList();
        newPaths.add(path);
        privilegeOne.put(privilegesStr, newPaths);
        continue;
      }
      if (type == 2) {
        if (privilegeTwo.containsKey(privilegesStr)) {
          List<String> pathList = privilegeTwo.get(privilegesStr);
          pathList.add(path);
          continue;
        }
        ArrayList<String> newStr = new ArrayList();
        newStr.add(path);
        privilegeTwo.put(privilegesStr, newStr);
        continue;
      }
      if (type == 3) {
        if (privilegeThree.containsKey(privilegesStr)) {
          List<String> pathList = privilegeThree.get(privilegesStr);
          pathList.add(path);
          continue;
        }
        ArrayList<String> newStr = new ArrayList();
        newStr.add(path);
        privilegeThree.put(privilegesStr, newStr);
      }
    }

    Set<String> oneKeys = privilegeOne.keySet();
    Set<String> twoKeys = privilegeTwo.keySet();
    Set<String> threeKeys = privilegeThree.keySet();
    List<String> allGroupPaths = executeQueryOneColumn(sessionPool, "show storage group");
    List<String> allDevicePaths = executeQueryOneColumn(sessionPool, "show devices");

    for (String oneKey : oneKeys) {
      DataPrivilegeVO dataPrivilegeVO = new DataPrivilegeVO();
      List<String> groupPaths = privilegeOne.get(oneKey);
      List<String> privilegesOne = Arrays.asList(oneKey.split(" "));

      dataPrivilegeVO.setType(1);
      dataPrivilegeVO.setPrivileges(privilegesOne);
      dataPrivilegeVO.setGroupPaths(groupPaths);
      dataPrivilegeVO.setAllGroupPaths(getGroupsNodeTree(sessionPool));
      dataPrivilegeList.add(dataPrivilegeVO);
    }
    for (String twoKey : twoKeys) {
      List<String> privilegesTwo = Arrays.asList(twoKey.split(" "));
      List<String> pathsTwo = privilegeTwo.get(twoKey);

      Map<String, List<String>> groupPathsToDevicePaths = new HashMap<>();
      for (String path : pathsTwo) {
        String groupPath = getSupPath(path, allGroupPaths);
        if (!groupPathsToDevicePaths.containsKey(groupPath)) {
          groupPathsToDevicePaths.put(groupPath, Stream.of(path).collect(Collectors.toList()));
        } else {
          groupPathsToDevicePaths.get(groupPath).add(path);
        }
      }
      for (Map.Entry<String, List<String>> entry : groupPathsToDevicePaths.entrySet()) {
        DataPrivilegeVO dataPrivilegeVO = new DataPrivilegeVO();
        String groupName = entry.getKey();
        dataPrivilegeVO.setType(2);
        dataPrivilegeVO.setPrivileges(privilegesTwo);
        dataPrivilegeVO.setGroupPaths(Arrays.asList(groupName));
        dataPrivilegeVO.setDevicePaths(entry.getValue());
        dataPrivilegeVO.setAllDevicePaths(getDeviceNodeTree(sessionPool, groupName));
        dataPrivilegeVO.setAllGroupPaths(switchListToNodeList(allGroupPaths));
        dataPrivilegeList.add(dataPrivilegeVO);
      }
    }
    for (String threeKey : threeKeys) {
      List<String> privilegesThree = Arrays.asList(threeKey.split(" "));
      List<String> pathsThree = privilegeThree.get(threeKey);

      Map<String, List<String>> devicePathsTotimeseriesPaths = new HashMap<>();
      for (String path : pathsThree) {
        String devicePath = getSupPath(path, allDevicePaths);
        if (!devicePathsTotimeseriesPaths.containsKey(devicePath)) {
          devicePathsTotimeseriesPaths.put(
              devicePath, Stream.of(path).collect(Collectors.toList()));
        } else {
          devicePathsTotimeseriesPaths.get(devicePath).add(path);
        }
      }
      for (Map.Entry<String, List<String>> entry : devicePathsTotimeseriesPaths.entrySet()) {
        DataPrivilegeVO dataPrivilegeVO = new DataPrivilegeVO();
        dataPrivilegeVO.setType(3);
        dataPrivilegeVO.setPrivileges(privilegesThree);
        String device = entry.getKey();
        String group = getSupPath(device, allGroupPaths);
        dataPrivilegeVO.setGroupPaths(Arrays.asList(group));
        dataPrivilegeVO.setDevicePaths(Arrays.asList(device));
        dataPrivilegeVO.setTimeseriesPaths(entry.getValue());
        dataPrivilegeVO.setAllGroupPaths(switchListToNodeList(allGroupPaths));
        String sql = "show devices " + group;
        List<String> devicePathsOfGroup = executeQueryOneColumn(sessionPool, sql);
        dataPrivilegeVO.setAllDevicePaths(switchListToNodeList(devicePathsOfGroup));
        sql = "show timeseries " + device;
        dataPrivilegeVO.setAllTimeseriesPaths(executeQueryOneColumn(sessionPool, sql));
        dataPrivilegeList.add(dataPrivilegeVO);
      }
    }
    return dataPrivilegeList;
  }