init the indexAsTree to visual tree @return tree Id
()
| 41 | * @return tree Id |
| 42 | */ |
| 43 | private String writeTree() { |
| 44 | // read the index dict |
| 45 | final String indexContent = FileUtil.getFileAsString(ConstantVal.INDEX, ConstantVal.NONE); |
| 46 | Map<String, String> indexItems = new Gson().fromJson(indexContent, new TypeToken<Map<String, String>>(){}.getType()); |
| 47 | |
| 48 | |
| 49 | /* construct the tree map called indexAsTree like the below |
| 50 | { |
| 51 | java: { => val is map => tree |
| 52 | com: { => val is map => tree |
| 53 | file1: objectId => val is string => object |
| 54 | club: { => val is map => tree |
| 55 | file2: objectId => val is string => object |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | */ |
| 61 | Map<String, Object> indexAsTree = new HashMap<>(); |
| 62 | for (Map.Entry<String, String> pathObjectId : indexItems.entrySet()) { |
| 63 | String path = pathObjectId.getKey(); |
| 64 | String objectId = pathObjectId.getValue(); |
| 65 | List<String> pathAndFile = Arrays.asList(path.split("/")); |
| 66 | List<String> dirPaths = pathAndFile.subList(0, pathAndFile.size() - 1); |
| 67 | String fileName = pathAndFile.get(pathAndFile.size() - 1); |
| 68 | |
| 69 | Map<String, Object> current = indexAsTree; |
| 70 | for (String dirPath : dirPaths) { |
| 71 | current = (Map<String, Object>) current.computeIfAbsent(dirPath, e -> new HashMap<>()); |
| 72 | } |
| 73 | current.put(fileName, objectId); |
| 74 | } |
| 75 | |
| 76 | // write objects into zit repository with the reference of above tree |
| 77 | return writeTreeRecursive(indexAsTree); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * this is an easy dfs for writing objects and trees |
no test coverage detected