diff --git a/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java b/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java index 87f74cb44..67cb456d2 100644 --- a/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java +++ b/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java @@ -8,6 +8,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.*; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -19,6 +20,7 @@ import java.util.stream.Stream; public class FilePerKeyMap implements ChronicleMap { private String dir; private Path dirPath; + private Map lastModifiedByProgram = new ConcurrentHashMap<>(); public FilePerKeyMap(String dir) { try { @@ -60,13 +62,27 @@ public class FilePerKeyMap implements ChronicleMap { } else if (kind == StandardWatchEventKinds.ENTRY_CREATE) { Path p = dirPath.resolve(fileName); String mapVal = getFileContents(p); - System.out.println("created " + mapKey + " : " + mapVal); + if(isProgrammaticUpdate(p.toFile())){ + System.out.println("Programmatic create to " + mapKey + " : " + mapVal); + }else { + System.out.println("created " + mapKey + " : " + mapVal); + } } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) { - System.out.println("deleted " + mapKey); + Path p = dirPath.resolve(fileName); + if(isProgrammaticUpdate(p.toFile())){ + System.out.println("Programmatic delete to " + mapKey); + lastModifiedByProgram.remove(p.toFile()); + }else { + System.out.println("User delete " + mapKey); + } } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { Path p = dirPath.resolve(fileName); String mapVal = getFileContents(p); - System.out.println("modified " + mapKey + " : " + mapVal); + if(isProgrammaticUpdate(p.toFile())){ + System.out.println("Programmatic update to " + mapKey + " : " + mapVal); + }else { + System.out.println("User modified " + mapKey + " : " + mapVal); + } } } @@ -89,6 +105,14 @@ public class FilePerKeyMap implements ChronicleMap { } } + private boolean isProgrammaticUpdate(File file){ + if(lastModifiedByProgram.containsKey(file)){ + return file.lastModified() == lastModifiedByProgram.get(file) ? true : false; + }else{ + return false; + } + } + @Override public int size() { return (int)getFiles(dirPath).count(); @@ -301,6 +325,7 @@ public class FilePerKeyMap implements ChronicleMap { Files.write(path, value.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); + lastModifiedByProgram.put(path.toFile(), path.toFile().lastModified()); } catch (IOException e) { throw new AssertionError(e); } @@ -309,6 +334,7 @@ public class FilePerKeyMap implements ChronicleMap { private void deleteFile(Path path){ try { Files.delete(path); + lastModifiedByProgram.put(path.toFile(), path.toFile().lastModified()); } catch (IOException e) { throw new AssertionError(e); } diff --git a/src/test/java/net/openhft/chronicle/map/FilePerMapKeyTest.java b/src/test/java/net/openhft/chronicle/map/FilePerMapKeyTest.java index f141d8dde..5e7228d3b 100644 --- a/src/test/java/net/openhft/chronicle/map/FilePerMapKeyTest.java +++ b/src/test/java/net/openhft/chronicle/map/FilePerMapKeyTest.java @@ -65,7 +65,6 @@ public class FilePerMapKeyTest { map.put("one", "test1"); map.put("two", "test2"); map.put("three", "test3"); - assertEquals(3, map.size()); val = map.remove("two"); @@ -112,10 +111,11 @@ public class FilePerMapKeyTest { assertTrue(false); } } + try { - Thread.sleep(2000); + Thread.sleep(3000); } catch (InterruptedException e) { - e.printStackTrace(); + e.printStackTrace(); } }