Skip to content

Commit

Permalink
add file-based caching for annotations - was taking waaaaay too long to
Browse files Browse the repository at this point in the history
look them up from the hosting MN each time they were read. cache will be
cleared when the app container (typically tomcat) is restarted.
  • Loading branch information
leinfelder committed Oct 17, 2016
1 parent c21320e commit 7034ca8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/main/java/org/dataone/annotator/store/FileBasedCache.java
@@ -0,0 +1,52 @@
package org.dataone.annotator.store;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;

public class FileBasedCache {

private static Map<String, File> cache = new HashMap<String, File>();

public static void cache(String id, InputStream is) throws IOException {
if (!cache.containsKey(id)) {
File file = File.createTempFile("annotator_cache", ".dat");
file.deleteOnExit();
IOUtils.copy(is, new FileOutputStream(file));
cache.put(id, file);
}
}

public static InputStream get(String id) throws FileNotFoundException {

if (cache.containsKey(id)) {
File file = cache.get(id);
return new FileInputStream(file);
}

return null;
}

public static boolean remove(String id) {
if (cache.containsKey(id)) {
File file = cache.get(id);
file.delete();
return true;
}
return false;
}

public static void clear() {
for (File file: cache.values()) {
file.delete();
}
cache.clear();
}
}
Expand Up @@ -204,7 +204,13 @@ public String read(String id) throws Exception {
// read the annotation out as a String object
Identifier sid = new Identifier();
sid.setValue(id);
InputStream object = storageNode.get(session, sid);
InputStream object = FileBasedCache.get(id);
if (object == null) {
object = storageNode.get(session, sid);
FileBasedCache.cache(id, object);
object = FileBasedCache.get(id);
}

return IOUtils.toString(object, "UTF-8");
}

Expand Down Expand Up @@ -255,6 +261,7 @@ public String update(String id, String partialAnnotationContent) throws Exceptio
// update it on the node
InputStream object = new ByteArrayInputStream(annotation.toJSONString().getBytes(DEFAULT_ENCODING));
storageNode.update(session, originalPid, object, pid, sysmeta);
FileBasedCache.remove(id);

return annotation.toJSONString();

Expand All @@ -270,6 +277,9 @@ public void delete(String id) throws Exception {
sid.setValue(id);

storageNode.archive(session, sid);

FileBasedCache.remove(id);

//TODO: allow deletes by anyone, not just the node admin
//storageNode.delete(session, sid);

Expand Down

0 comments on commit 7034ca8

Please sign in to comment.