From 2ea5a640392dcf6eab03b8a72503ff1e2ff2f184 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Wed, 15 Jan 2025 00:37:36 +0200 Subject: [PATCH] Speed up method remove o^2 loop --- .../net/osmand/plus/backup/BackupUtils.java | 69 ++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/backup/BackupUtils.java b/OsmAnd/src/net/osmand/plus/backup/BackupUtils.java index 627196b1dec..83c82d0638b 100644 --- a/OsmAnd/src/net/osmand/plus/backup/BackupUtils.java +++ b/OsmAnd/src/net/osmand/plus/backup/BackupUtils.java @@ -91,24 +91,48 @@ public static Map getItemsMapForRestore(@Nullable Back @NonNull public static Map getRemoteFilesSettingsItems(@NonNull List items, - @NonNull List remoteFiles, - boolean infoFiles) { + @NonNull List remoteFiles, boolean infoFiles) { Map res = new HashMap<>(); - List files = new ArrayList<>(remoteFiles); + Map settingsItemMap = new HashMap<>(); + List subtypeFolders = new ArrayList<>(); + String DELIMETER = "___"; for (SettingsItem item : items) { - List processedFiles = new ArrayList<>(); - for (RemoteFile file : files) { - String type = file.getType(); - String name = file.getName(); - if (infoFiles && name.endsWith(BackupHelper.INFO_EXT)) { - name = name.substring(0, name.length() - BackupHelper.INFO_EXT.length()); + String itemFileName = getItemFileName(item); + settingsItemMap.put(item.getType().name() + DELIMETER + itemFileName, item); + // Commits FileSettingsItem introduced likely are related to TTS / Voice configuration (folders) + // https://github.com/osmandapp/OsmAnd/commit/ba750f9df87057da268b36e0d32b8c1996f4023a + // https://github.com/osmandapp/OsmAnd/commit/bf93162bd13ef7ab16622bb662c953e931c34a21 + if (item instanceof FileSettingsItem fileItem) { + String subtypeFolder = fileItem.getSubtype().getSubtypeFolder(); + if (subtypeFolder != null && fileItem.getFile().isDirectory()) { + subtypeFolders.add(fileItem); } - if (applyItem(item, type, name)) { - res.put(file, item); - processedFiles.add(file); + } + } + for (RemoteFile file : remoteFiles) { + String type = file.getType(); + String name = file.getName(); + if (infoFiles && name.endsWith(BackupHelper.INFO_EXT)) { + name = name.substring(0, name.length() - BackupHelper.INFO_EXT.length()); + } + SettingsItem item = settingsItemMap.get(type + DELIMETER + name); + if (item != null) { + res.put(file, item); + } else { + for (FileSettingsItem fileItem : subtypeFolders) { + String itemFileName = getItemFileName(fileItem); + boolean found = false; + if (!itemFileName.endsWith("/")) { + found = name.startsWith(itemFileName + "/"); + } else { + found = name.startsWith(itemFileName); + } + if (found) { + res.put(file, fileItem); + break; + } } } - files.removeAll(processedFiles); } return res; } @@ -121,25 +145,6 @@ public static CommonPreference getVersionHistoryTypePref(@NonNull Osman return app.getSettings().registerBooleanPreference(VERSION_HISTORY_PREFIX + exportType.name(), true).makeGlobal().makeShared(); } - public static boolean applyItem(@NonNull SettingsItem item, @NonNull String type, @NonNull String name) { - String itemFileName = getItemFileName(item); - if (item.getType().name().equals(type)) { - if (name.equals(itemFileName)) { - return true; - } else if (item instanceof FileSettingsItem fileItem) { - String subtypeFolder = fileItem.getSubtype().getSubtypeFolder(); - if (subtypeFolder != null && name.startsWith(subtypeFolder)) { - if (fileItem.getFile().isDirectory() && !itemFileName.endsWith("/")) { - return name.startsWith(itemFileName + "/"); - } else { - return name.startsWith(itemFileName); - } - } - } - } - return false; - } - @NonNull public static String getItemFileName(@NonNull SettingsItem item) { String fileName;