-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Refactored around the FileAnnotationCache. (#2557)
* Refactored quite much around the FileAnnotationCache. The Cache now works as intended, i.e., fully automatically instead of manual maintenance. * Reverted an incorrect renaming. * Reverted an incorrect renaming to the original * Fixed wrong locale message * fixing @koppor's remarks * fixed imports, consitent naming * minor change
- Loading branch information
1 parent
43e6138
commit 41d1551
Showing
8 changed files
with
330 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 140 additions & 116 deletions
256
src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Large diffs are not rendered by default.
Oops, something went wrong.
224 changes: 96 additions & 128 deletions
224
src/main/java/org/jabref/gui/entryeditor/FileAnnotationTab.java
Large diffs are not rendered by default.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/java/org/jabref/logic/pdf/AnnotationImporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.jabref.logic.pdf; | ||
|
||
import java.util.List; | ||
|
||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.pdf.FileAnnotation; | ||
|
||
public interface AnnotationImporter { | ||
|
||
List<FileAnnotation> importAnnotations(final String path, final BibDatabaseContext context); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/org/jabref/logic/pdf/EntryAnnotationImporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.jabref.logic.pdf; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.FieldName; | ||
import org.jabref.model.entry.FileField; | ||
import org.jabref.model.entry.ParsedFileField; | ||
import org.jabref.model.pdf.FileAnnotation; | ||
|
||
|
||
/** | ||
* Here all PDF files attached to a BibEntry are scanned for annotations using a PdfAnnotationImporter. | ||
*/ | ||
public class EntryAnnotationImporter { | ||
|
||
private final BibEntry entry; | ||
|
||
/** | ||
* @param entry The BibEntry whose attached files are scanned for annotations. | ||
*/ | ||
public EntryAnnotationImporter(BibEntry entry) { | ||
this.entry = entry; | ||
} | ||
|
||
/** | ||
* Filter files with a web address containing "www." | ||
* | ||
* @return a list of file parsed files | ||
*/ | ||
public List<ParsedFileField> getFilteredFileList() { | ||
return FileField.parse(this.entry.getField(FieldName.FILE).get()).stream() | ||
.filter(parsedFileField -> parsedFileField.getLink().toLowerCase().endsWith(".pdf")) | ||
.filter(parsedFileField -> !parsedFileField.getLink().contains("www.")).collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Reads the annotations from the files that are attached to a BibEntry. | ||
* | ||
* @param context The context is needed for the importer. | ||
* @return Map from each PDF to a list of file annotations | ||
*/ | ||
public Map<String, List<FileAnnotation>> importAnnotationsFromFiles(BibDatabaseContext context) { | ||
Map<String, List<FileAnnotation>> annotations = new HashMap<>(); | ||
AnnotationImporter importer = new PdfAnnotationImporter(); | ||
//import annotationsOfFiles if the selected files are valid which is checked in getFilteredFileList() | ||
this.getFilteredFileList().forEach(parsedFileField -> annotations.put(parsedFileField.getLink(), importer.importAnnotations(parsedFileField.getLink(), context))); | ||
return annotations; | ||
} | ||
} |
27 changes: 7 additions & 20 deletions
27
src/main/java/org/jabref/logic/pdf/FileAnnotationCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,41 @@ | ||
package org.jabref.logic.pdf; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.pdf.FileAnnotation; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
|
||
|
||
public class FileAnnotationCache { | ||
|
||
//cache size in entries | ||
private final static int CACHE_SIZE = 10; | ||
//the inner list holds the annotations per file, the outer collection maps this to a BibEntry. | ||
private LoadingCache<BibEntry, Map<String, List<FileAnnotation>>> annotationCache; | ||
|
||
public FileAnnotationCache() { | ||
public FileAnnotationCache(BibDatabaseContext context) { | ||
annotationCache = CacheBuilder.newBuilder().maximumSize(CACHE_SIZE).build(new CacheLoader<BibEntry, Map<String, List<FileAnnotation>>>() { | ||
@Override | ||
public Map<String, List<FileAnnotation>> load(BibEntry notUsed) throws Exception { | ||
// Automated reloading of entries is not supported. | ||
return new HashMap<>(); | ||
public Map<String, List<FileAnnotation>> load(BibEntry entry) throws Exception { | ||
return new EntryAnnotationImporter(entry).importAnnotationsFromFiles(context); | ||
} | ||
}); | ||
} | ||
|
||
public void addToCache(BibEntry entry, final Map<String, List<FileAnnotation>> annotations) { | ||
annotationCache.put(entry, annotations); | ||
} | ||
|
||
/** | ||
* Note that entry becomes the most recent entry in the cache | ||
* | ||
* @param entry entry for which to get the annotations | ||
* @return Map containing a list of annotations in a list for each file | ||
*/ | ||
public Optional<Map<String, List<FileAnnotation>>> getFromCache(Optional<BibEntry> entry) { | ||
Optional<Map<String, List<FileAnnotation>>> emptyAnnotation = Optional.empty(); | ||
try { | ||
if (entry.isPresent() && annotationCache.get(entry.get()).size() > 0) { | ||
return Optional.of(annotationCache.get(entry.get())); | ||
} | ||
} catch (ExecutionException failure) { | ||
return emptyAnnotation; | ||
} | ||
return emptyAnnotation; | ||
public Map<String, List<FileAnnotation>> getFromCache(BibEntry entry) throws ExecutionException { | ||
return annotationCache.get(entry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters