|
1 | 1 | package stroom.analytics.impl;
|
2 | 2 |
|
3 |
| -import stroom.analytics.shared.AbstractAnalyticRuleDoc; |
| 3 | +import stroom.analytics.shared.AnalyticRuleDoc; |
4 | 4 | import stroom.lmdb2.LmdbEnvDir;
|
5 | 5 | import stroom.lmdb2.LmdbEnvDirFactory;
|
6 | 6 | import stroom.query.common.v2.DuplicateCheckStoreConfig;
|
| 7 | +import stroom.util.NullSafe; |
7 | 8 | import stroom.util.logging.LambdaLogger;
|
8 | 9 | import stroom.util.logging.LambdaLoggerFactory;
|
| 10 | +import stroom.util.logging.LogUtil; |
9 | 11 |
|
10 | 12 | import jakarta.inject.Inject;
|
11 | 13 |
|
12 | 14 | import java.io.IOException;
|
13 | 15 | import java.nio.file.Files;
|
14 | 16 | import java.nio.file.Path;
|
15 | 17 | import java.util.ArrayList;
|
16 |
| -import java.util.HashSet; |
17 | 18 | import java.util.List;
|
| 19 | +import java.util.Objects; |
| 20 | +import java.util.Optional; |
18 | 21 | import java.util.Set;
|
| 22 | +import java.util.stream.Collectors; |
19 | 23 | import java.util.stream.Stream;
|
20 | 24 |
|
21 | 25 | public class DuplicateCheckDirs {
|
@@ -68,24 +72,61 @@ public List<String> getAnalyticRuleUUIDList() {
|
68 | 72 | return uuidList;
|
69 | 73 | }
|
70 | 74 |
|
71 |
| - public <T extends AbstractAnalyticRuleDoc> void deleteUnused( |
72 |
| - final List<String> duplicateStoreDirs, |
73 |
| - final List<T> analytics) { |
| 75 | + public List<String> deleteUnused( |
| 76 | + final List<String> duplicateStoreUuids, |
| 77 | + final List<AnalyticRuleDoc> analytics) { |
| 78 | + final List<String> deletedUuids = new ArrayList<>(); |
74 | 79 | try {
|
75 |
| - // Delete unused duplicate stores. |
76 |
| - final Set<String> remaining = new HashSet<>(duplicateStoreDirs); |
77 |
| - for (final T analyticRuleDoc : analytics) { |
78 |
| - remaining.remove(analyticRuleDoc.getUuid()); |
79 |
| - } |
80 |
| - for (final String uuid : remaining) { |
81 |
| - try { |
82 |
| - getDir(uuid).delete(); |
83 |
| - } catch (final RuntimeException e) { |
84 |
| - LOGGER.error(e::getMessage, e); |
| 80 | + LOGGER.debug(() -> LogUtil.message( |
| 81 | + "deleteUnused() - duplicateStoreUuids.size: {}, analytics.size: {}", |
| 82 | + NullSafe.size(duplicateStoreUuids), NullSafe.size(analytics))); |
| 83 | + if (NullSafe.hasItems(duplicateStoreUuids)) { |
| 84 | + final List<String> redundantDupStoreUuids; |
| 85 | + if (NullSafe.hasItems(analytics)) { |
| 86 | + final Set<String> analyticUuids = analytics.stream() |
| 87 | + .filter(Objects::nonNull) |
| 88 | + .map(AnalyticRuleDoc::getUuid) |
| 89 | + .filter(Objects::nonNull) |
| 90 | + .collect(Collectors.toSet()); |
| 91 | + // Find dup stores with no corresponding analytic |
| 92 | + redundantDupStoreUuids = duplicateStoreUuids.stream() |
| 93 | + .filter(uuid -> !analyticUuids.contains(uuid)) |
| 94 | + .toList(); |
| 95 | + } else { |
| 96 | + // No analytics so all redundant |
| 97 | + redundantDupStoreUuids = duplicateStoreUuids; |
| 98 | + } |
| 99 | + |
| 100 | + // Delete unused duplicate stores. |
| 101 | + redundantDupStoreUuids.stream() |
| 102 | + .map(this::deleteDuplicateStore) |
| 103 | + .filter(Optional::isPresent) |
| 104 | + .map(Optional::get) |
| 105 | + .forEach(deletedUuids::add); |
| 106 | + |
| 107 | + if (!deletedUuids.isEmpty()) { |
| 108 | + LOGGER.info("Deleted {} redundant duplicate check stores", deletedUuids.size()); |
85 | 109 | }
|
86 | 110 | }
|
87 | 111 | } catch (final RuntimeException e) {
|
88 | 112 | LOGGER.error(e::getMessage, e);
|
89 | 113 | }
|
| 114 | + // Return this to ease testing |
| 115 | + return deletedUuids; |
| 116 | + } |
| 117 | + |
| 118 | + private Optional<String> deleteDuplicateStore(final String uuid) { |
| 119 | + try { |
| 120 | + final LmdbEnvDir lmdbEnvDir = getDir(uuid); |
| 121 | + lmdbEnvDir.delete(); |
| 122 | + LOGGER.info("Deleted redundant duplicate check store with UUID: {}, path: {}", |
| 123 | + uuid, LogUtil.path(lmdbEnvDir.getEnvDir())); |
| 124 | + return Optional.of(uuid); |
| 125 | + } catch (final RuntimeException e) { |
| 126 | + LOGGER.error(() -> LogUtil.message( |
| 127 | + "Error deleting duplicateStore with UUID {}: {}", |
| 128 | + uuid, LogUtil.exceptionMessage(e), e)); |
| 129 | + return Optional.empty(); |
| 130 | + } |
90 | 131 | }
|
91 | 132 | }
|
0 commit comments