mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Initial commit for FilePerMap implementation.
This commit is contained in:
@@ -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 <code>key</> is the file name and the <code>value</>
|
||||
* is the contents of the file. This map will only handle <code>String</>'s.
|
||||
* <p>
|
||||
* 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<String, String> {
|
||||
private final String dir;
|
||||
private final Path dirPath;
|
||||
private final Map<File, Long> lastModifiedByProgram = new ConcurrentHashMap<>();
|
||||
private final List<Consumer<FPMEvent>> 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<String, String> {
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
|
||||
return getFiles().anyMatch(p->p.getFileName().toString().equals(key));
|
||||
}
|
||||
|
||||
@@ -87,10 +95,9 @@ public class FilePerKeyMap implements Map<String, String> {
|
||||
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<String, String> {
|
||||
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<String, String> {
|
||||
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<String, String> {
|
||||
|
||||
if (key != null ? !key.equals(fpmEntry.key) : fpmEntry.key != null) return false;
|
||||
return !(value != null ? !value.equals(fpmEntry.value) : fpmEntry.value != null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user