Skip to content

Commit

Permalink
Keep track of objects per file
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Jul 9, 2023
1 parent e193512 commit 330b8d7
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class YamlModelRepository implements WatchService.WatchEventListener {
private final ObjectMapper yamlReader;

private final Map<String, List<YamlModelListener<?>>> listeners = new ConcurrentHashMap<>();
private final Map<String, List<? extends YamlElement>> objects = new ConcurrentHashMap<>();
private final Map<Path, List<? extends YamlElement>> objects = new ConcurrentHashMap<>();

@Activate
public YamlModelRepository(@Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService) {
Expand Down Expand Up @@ -87,12 +87,14 @@ public synchronized void processWatchEvent(Kind kind, Path path) {
}

private void processWatchEvent(String dirName, Kind kind, Path fullPath, YamlModelListener<?> listener) {
logger.debug("processWatchEvent dirName={} kind={} fullPath={} listener={}", dirName, kind, fullPath,
listener.getClass().getSimpleName());
Map<String, ? extends YamlElement> oldObjects;
Map<String, ? extends YamlElement> newObjects;
if (kind == WatchService.Kind.DELETE) {
newObjects = Map.of();

List<? extends YamlElement> oldListObjects = objects.remove(dirName);
List<? extends YamlElement> oldListObjects = objects.remove(fullPath);
if (oldListObjects == null) {
oldListObjects = List.of();
}
Expand All @@ -109,13 +111,13 @@ private void processWatchEvent(String dirName, Kind kind, Path fullPath, YamlMod
List<? extends YamlElement> newListObjects = yamlData.getElements();
newObjects = newListObjects.stream().collect(Collectors.toMap(YamlElement::getId, obj -> obj));

List<? extends YamlElement> oldListObjects = objects.get(dirName);
List<? extends YamlElement> oldListObjects = objects.get(fullPath);
if (oldListObjects == null) {
oldListObjects = List.of();
}
oldObjects = oldListObjects.stream().collect(Collectors.toMap(YamlElement::getId, obj -> obj));

objects.put(dirName, newListObjects);
objects.put(fullPath, newListObjects);
}

String modelName = fullPath.toFile().getName();
Expand Down Expand Up @@ -167,7 +169,11 @@ protected void addYamlModelListener(YamlModelListener<?> listener) {

// Load all existing YAML files
try (Stream<Path> stream = Files.walk(watchPath.resolve(dirName))) {
stream.forEach(path -> processWatchEvent(dirName, Kind.CREATE, path, listener));
stream.forEach(path -> {
if (!Files.isDirectory(path) && !path.toFile().isHidden() && path.toString().endsWith(".yaml")) {
processWatchEvent(dirName, Kind.CREATE, path, listener);
}
});
} catch (IOException ignored) {
}
}
Expand Down

0 comments on commit 330b8d7

Please sign in to comment.