|
1 | 1 | package org.radarcns;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * Created by joris on 13/04/2017. |
5 |
| - */ |
6 |
| -public class FileCache { |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.Closeable; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.Flushable; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.Writer; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
7 | 16 |
|
| 17 | +public class FileCache implements Flushable, Closeable { |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(FileCache.class); |
| 19 | + |
| 20 | + private final int maxFiles; |
| 21 | + private final Map<File, SingleFileCache> caches; |
| 22 | + |
| 23 | + public FileCache(int maxFiles) { |
| 24 | + this.maxFiles = maxFiles; |
| 25 | + this.caches = new HashMap<>(); |
| 26 | + } |
| 27 | + |
| 28 | + public void appendLine(File file, String data) throws IOException { |
| 29 | + SingleFileCache cache = caches.get(file); |
| 30 | + if (cache == null) { |
| 31 | + if (caches.size() == maxFiles) { |
| 32 | + ArrayList<SingleFileCache> cacheList = new ArrayList<>(caches.values()); |
| 33 | + Collections.sort(cacheList); |
| 34 | + for (int i = 0; i < cacheList.size() / 2; i++) { |
| 35 | + SingleFileCache rmCache = cacheList.get(i); |
| 36 | + caches.remove(rmCache.getFile()); |
| 37 | + rmCache.close(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + File dir = file.getParentFile(); |
| 42 | + if (!file.getParentFile().exists()){ |
| 43 | + if (dir.mkdirs()) { |
| 44 | + logger.info("Created directory: {}", dir.getAbsolutePath()); |
| 45 | + } else { |
| 46 | + logger.warn("FAILED to create directory: " + dir.getAbsolutePath()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + cache = new SingleFileCache(file); |
| 51 | + caches.put(file, cache); |
| 52 | + } |
| 53 | + cache.appendLine(data); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void flush() throws IOException { |
| 58 | + for (SingleFileCache cache : caches.values()) { |
| 59 | + cache.flush(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void close() throws IOException { |
| 65 | + try { |
| 66 | + for (SingleFileCache cache : caches.values()) { |
| 67 | + cache.close(); |
| 68 | + } |
| 69 | + } finally { |
| 70 | + caches.clear(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static class SingleFileCache implements Closeable, Flushable, Comparable<SingleFileCache> { |
| 75 | + private final BufferedWriter bufferedWriter; |
| 76 | + private final Writer fileWriter; |
| 77 | + private final File file; |
| 78 | + private long lastUse; |
| 79 | + |
| 80 | + private SingleFileCache(File file) throws IOException { |
| 81 | + this.file = file; |
| 82 | + this.fileWriter = new FileWriter(file, true); |
| 83 | + this.bufferedWriter = new BufferedWriter(fileWriter); |
| 84 | + } |
| 85 | + |
| 86 | + private void appendLine(String data) throws IOException { |
| 87 | + bufferedWriter.write(data); |
| 88 | + bufferedWriter.write('\n'); |
| 89 | + lastUse = System.nanoTime(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void close() throws IOException { |
| 94 | + bufferedWriter.close(); |
| 95 | + fileWriter.close(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void flush() throws IOException { |
| 100 | + bufferedWriter.flush(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int compareTo(SingleFileCache other) { |
| 105 | + return Long.compare(lastUse, other.lastUse); |
| 106 | + } |
| 107 | + |
| 108 | + private File getFile() { |
| 109 | + return file; |
| 110 | + } |
| 111 | + } |
8 | 112 | }
|
0 commit comments