From 4743f9b870276f7b2452b33162a6f79f5a894242 Mon Sep 17 00:00:00 2001 From: danielshaya Date: Mon, 27 Apr 2015 10:30:15 +0100 Subject: [PATCH] Initial commit for FilePerMap implementation. --- .../openhft/chronicle/map/FilePerKeyMap.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java b/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java index 592c67ab4..b38f6e854 100644 --- a/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java +++ b/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java @@ -13,17 +13,26 @@ import java.util.stream.Collectors; import java.util.stream.Stream; /** - * + * A {@link Map} implementation that stores each entry as a file in a + * directory. The key is the file name and the value + * is the contents of the file. This map will only handle String's. + *

+ * The class is effectively an abstraction over a directory in the file system. + * Therefore when the underlying files are changed an event will be fired to those + * registered for notifications. + * Since every write to this map will cause a change to underlying + * file system the event will distinguish between a programmatic event + * (i.e one caused my the actions of the map itself) and an event that + * has been triggered as a direct result of a file being manipulated outside + * this class. */ public class FilePerKeyMap implements Map { - private final String dir; private final Path dirPath; private final Map lastModifiedByProgram = new ConcurrentHashMap<>(); private final List> listeners = new ArrayList<>(); private final FPMWatcher fileFpmWatcher = new FPMWatcher(); public FilePerKeyMap(String dir){ - this.dir = dir; this.dirPath = Paths.get(dir); try { Files.createDirectories(dirPath); @@ -59,7 +68,6 @@ public class FilePerKeyMap implements Map { @Override public boolean containsKey(Object key) { - return getFiles().anyMatch(p->p.getFileName().toString().equals(key)); } @@ -87,10 +95,9 @@ public class FilePerKeyMap implements Map { public String remove(Object key) { String existing = get(key); if(existing != null){ - deleteFile(Paths.get(dir, (String)key)); + deleteFile(dirPath.resolve((String) key)); } return existing; - } @Override @@ -132,7 +139,7 @@ public class FilePerKeyMap implements Map { try { return Files.walk(dirPath).filter(p -> !Files.isDirectory(p)); } catch (IOException e) { - throw new AssertionError(e); + throw new RuntimeException(e); } } @@ -142,7 +149,7 @@ public class FilePerKeyMap implements Map { try { existingValue = Files.readAllLines(path).stream().collect(Collectors.joining()); } catch (IOException e) { - throw new AssertionError(e); + throw new RuntimeException(e); } } return existingValue; @@ -208,7 +215,6 @@ public class FilePerKeyMap implements Map { if (key != null ? !key.equals(fpmEntry.key) : fpmEntry.key != null) return false; return !(value != null ? !value.equals(fpmEntry.value) : fpmEntry.value != null); - } @Override