From 304d4c9acf2f9b4ffeb2c6cb791f12253541ea89 Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Wed, 17 Jan 2024 10:56:16 +0100 Subject: [PATCH] (control) Fix result ordering in the file storage listing view In some scenarios, such as when restoring storage items from json-manifest on db failure, the file storage view would present the items in a non-chronological order. Added a sort() operation to mitigate this. --- .../control/node/svc/ControlNodeService.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/code/services-core/control-service/src/main/java/nu/marginalia/control/node/svc/ControlNodeService.java b/code/services-core/control-service/src/main/java/nu/marginalia/control/node/svc/ControlNodeService.java index 6883d70a..66a2b485 100644 --- a/code/services-core/control-service/src/main/java/nu/marginalia/control/node/svc/ControlNodeService.java +++ b/code/services-core/control-service/src/main/java/nu/marginalia/control/node/svc/ControlNodeService.java @@ -360,22 +360,29 @@ public class ControlNodeService { } private List makeFileStorageBaseWithStorage(List storageIds) throws SQLException { - Map fileStorageBaseByBaseId = new HashMap<>(); - Map> fileStoragByBaseId = new HashMap<>(); + Map> fileStorageByBaseId = new HashMap<>(); for (var id : storageIds) { var storage = fileStorageService.getStorage(id); fileStorageBaseByBaseId.computeIfAbsent(storage.base().id(), k -> storage.base()); - fileStoragByBaseId.computeIfAbsent(storage.base().id(), k -> new ArrayList<>()).add(new FileStorageWithActions(storage)); + fileStorageByBaseId.computeIfAbsent(storage.base().id(), k -> new ArrayList<>()).add(new FileStorageWithActions(storage)); } List result = new ArrayList<>(); - for (var baseId : fileStorageBaseByBaseId.keySet()) { - result.add(new FileStorageBaseWithStorage(fileStorageBaseByBaseId.get(baseId), - fileStoragByBaseId.get(baseId) - )); + for (var baseId : fileStorageBaseByBaseId.keySet()) { + var base = fileStorageBaseByBaseId.get(baseId); + var items = fileStorageByBaseId.get(baseId); + + // Sort by timestamp, then by relPath + // this ensures that the newest file is listed last + items.sort(Comparator + .comparing(FileStorageWithActions::getTimestamp) + .thenComparing(FileStorageWithActions::getRelPath) + ); + + result.add(new FileStorageBaseWithStorage(base, items)); } return result;