use of com.stnetix.ariaddna.localstoragemanager.event.FileSystemWatchEvent in project ariADDna by StnetixDevTeam.
the class Consumer method checkOtherEvents.
/**
* find MOVE, CREATE, DELETE events
* bufferQueue processing
* wen we delete file the fileSystem generate ENTRY_DELETE and ENTRY_CREATE events but not in a strict order
* therefore we take a event from bufferQueue and fid a pair, the remove these events from queue
* if we did not find a pair
*/
private void checkOtherEvents() {
// collection for prepared events
List<Pair<Path, WatchEvent<?>>> doneList = new ArrayList<>();
for (Pair<Path, WatchEvent<?>> p : bufferQueue) {
if (p != null && !doneList.contains(p)) {
@SuppressWarnings("unchecked") WatchEvent<Path> event = (WatchEvent<Path>) p.getValue();
Path name = event.context();
final boolean[] isMoveEvent = { false };
WatchEvent.Kind<Path> from = event.kind();
WatchEvent.Kind<Path> to = from.equals(ENTRY_DELETE) ? ENTRY_CREATE : ENTRY_DELETE;
bufferQueue.forEach(e -> {
if (!doneList.contains(e)) {
WatchEvent<Path> ev = (WatchEvent<Path>) e.getValue();
Path newName = ev.context();
if (ev.kind().equals(to) && newName.equals(name)) {
if (from.equals(ENTRY_DELETE)) {
LOGGER.debug("defined MOVE event from {} to {}", p.getKey().resolve(name), e.getKey().resolve(name));
service.runEvents(new FileSystemWatchEvent(FileSystemWatchEvent.Type.MOVE, p.getKey().resolve(name), e.getKey().resolve(name)));
} else {
LOGGER.debug("defined MOVE event from {} to {}", e.getKey().resolve(name), p.getKey().resolve(name));
service.runEvents(new FileSystemWatchEvent(FileSystemWatchEvent.Type.MOVE, e.getKey().resolve(name), p.getKey().resolve(name)));
}
bufferQueue.remove(e);
doneList.add(e);
isMoveEvent[0] = true;
}
}
});
if (!isMoveEvent[0]) {
FileSystemWatchEvent.Type eventType = from.equals(ENTRY_CREATE) ? FileSystemWatchEvent.Type.CREATE : FileSystemWatchEvent.Type.DELETE;
if (from == ENTRY_CREATE) {
LOGGER.debug("defined CREATE event from {}", p.getKey().resolve(name));
try {
if (Files.isDirectory(p.getKey().resolve(name))) {
service.registerDirectories(p.getKey().resolve(name));
}
} catch (IOException x) {
LOGGER.trace("Cant register folder {}", x.getMessage());
}
} else if (from == ENTRY_DELETE) {
LOGGER.debug("defined DELETE event from {}", p.getKey().resolve(name));
}
service.runEvents(new FileSystemWatchEvent(eventType, p.getKey().resolve(name)));
isMoveEvent[0] = false;
}
}
bufferQueue.remove(p);
doneList.add(p);
}
// queue.clear();
bufferQueue.clear();
}
Aggregations