-
-
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.
Refactor Citations Relations Service Layer (#11189)
* Move logic from repository to service * Refactor repositories * Update tab configuration
- Loading branch information
1 parent
f603c78
commit 8231340
Showing
14 changed files
with
514 additions
and
338 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
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
24 changes: 9 additions & 15 deletions
24
src/main/java/org/jabref/logic/citation/repository/BibEntryRelationsRepository.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,26 +1,20 @@ | ||
package org.jabref.logic.citation.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
|
||
public interface BibEntryRelationsRepository { | ||
|
||
void insertCitations(BibEntry entry, List<BibEntry> citations); | ||
|
||
List<BibEntry> readCitations(BibEntry entry); | ||
|
||
boolean containsCitations(BibEntry entry); | ||
|
||
void insertReferences(BibEntry entry, List<BibEntry> citations); | ||
|
||
List<BibEntry> readReferences(BibEntry entry); | ||
|
||
/** | ||
* Fetch citations for a bib entry and update local database. | ||
* @param entry should not be null | ||
* @deprecated fetching citations should be done by the service layer (calling code) | ||
*/ | ||
@Deprecated | ||
void forceRefreshCitations(BibEntry entry); | ||
|
||
/** | ||
* Fetch references made by a bib entry and update local database. | ||
* @param entry should not be null | ||
* @deprecated fetching references should be done by the service layer (calling code) | ||
*/ | ||
@Deprecated | ||
void forceRefreshReferences(BibEntry entry); | ||
boolean containsReferences(BibEntry entry); | ||
} |
39 changes: 29 additions & 10 deletions
39
src/main/java/org/jabref/logic/citation/repository/LRUBibEntryRelationsCache.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,38 +1,57 @@ | ||
package org.jabref.logic.citation.repository; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.eclipse.jgit.util.LRUMap; | ||
import java.util.Set; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.identifier.DOI; | ||
|
||
import org.eclipse.jgit.util.LRUMap; | ||
|
||
public class LRUBibEntryRelationsCache { | ||
private static final Integer MAX_CACHED_ENTRIES = 100; | ||
private static final Map<String, List<BibEntry>> CITATIONS_MAP = new LRUMap<>(MAX_CACHED_ENTRIES, MAX_CACHED_ENTRIES); | ||
private static final Map<String, List<BibEntry>> REFERENCES_MAP = new LRUMap<>(MAX_CACHED_ENTRIES, MAX_CACHED_ENTRIES); | ||
private static final Map<DOI, Set<BibEntry>> CITATIONS_MAP = new LRUMap<>(MAX_CACHED_ENTRIES, MAX_CACHED_ENTRIES); | ||
private static final Map<DOI, Set<BibEntry>> REFERENCES_MAP = new LRUMap<>(MAX_CACHED_ENTRIES, MAX_CACHED_ENTRIES); | ||
|
||
public List<BibEntry> getCitations(BibEntry entry) { | ||
return CITATIONS_MAP.getOrDefault(entry.getDOI().map(DOI::getDOI).orElse(""), Collections.emptyList()); | ||
return entry | ||
.getDOI() | ||
.stream() | ||
.flatMap(doi -> CITATIONS_MAP.getOrDefault(doi, Set.of()).stream()) | ||
.toList(); | ||
} | ||
|
||
public List<BibEntry> getReferences(BibEntry entry) { | ||
return REFERENCES_MAP.getOrDefault(entry.getDOI().map(DOI::getDOI).orElse(""), Collections.emptyList()); | ||
return entry | ||
.getDOI() | ||
.stream() | ||
.flatMap(doi -> REFERENCES_MAP.getOrDefault(doi, Set.of()).stream()) | ||
.toList(); | ||
} | ||
|
||
public void cacheOrMergeCitations(BibEntry entry, List<BibEntry> citations) { | ||
entry.getDOI().ifPresent(doi -> CITATIONS_MAP.put(doi.getDOI(), citations)); | ||
entry.getDOI().ifPresent(doi -> { | ||
var cachedRelations = CITATIONS_MAP.getOrDefault(doi, new LinkedHashSet<>()); | ||
cachedRelations.addAll(citations); | ||
CITATIONS_MAP.put(doi, cachedRelations); | ||
}); | ||
} | ||
|
||
public void cacheOrMergeReferences(BibEntry entry, List<BibEntry> references) { | ||
entry.getDOI().ifPresent(doi -> REFERENCES_MAP.putIfAbsent(doi.getDOI(), references)); | ||
entry.getDOI().ifPresent(doi -> { | ||
var cachedRelations = REFERENCES_MAP.getOrDefault(doi, new LinkedHashSet<>()); | ||
cachedRelations.addAll(references); | ||
REFERENCES_MAP.put(doi, cachedRelations); | ||
}); | ||
} | ||
|
||
public boolean citationsCached(BibEntry entry) { | ||
return CITATIONS_MAP.containsKey(entry.getDOI().map(DOI::getDOI).orElse("")); | ||
return entry.getDOI().map(CITATIONS_MAP::containsKey).orElse(false); | ||
} | ||
|
||
public boolean referencesCached(BibEntry entry) { | ||
return REFERENCES_MAP.containsKey(entry.getDOI().map(DOI::getDOI).orElse("")); | ||
return entry.getDOI().map(REFERENCES_MAP::containsKey).orElse(false); | ||
} | ||
} |
69 changes: 20 additions & 49 deletions
69
src/main/java/org/jabref/logic/citation/repository/LRUBibEntryRelationsRepository.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,78 +1,49 @@ | ||
package org.jabref.logic.citation.repository; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.jabref.logic.importer.fetcher.CitationFetcher; | ||
import org.jabref.logic.importer.FetcherException; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class LRUBibEntryRelationsRepository implements BibEntryRelationsRepository { | ||
private static final Logger LOGGER = LoggerFactory | ||
.getLogger(LRUBibEntryRelationsRepository.class); | ||
|
||
private final CitationFetcher fetcher; | ||
private final LRUBibEntryRelationsCache cache; | ||
|
||
public LRUBibEntryRelationsRepository(CitationFetcher fetcher, LRUBibEntryRelationsCache cache) { | ||
this.fetcher = fetcher; | ||
public LRUBibEntryRelationsRepository(LRUBibEntryRelationsCache cache) { | ||
this.cache = cache; | ||
} | ||
|
||
@Override | ||
public List<BibEntry> readCitations(BibEntry entry) { | ||
if (needToRefreshCitations(entry)) { | ||
forceRefreshCitations(entry); | ||
} | ||
|
||
return cache.getCitations(entry); | ||
public void insertCitations(BibEntry entry, List<BibEntry> citations) { | ||
cache.cacheOrMergeCitations( | ||
entry, Objects.requireNonNullElseGet(citations, List::of) | ||
); | ||
} | ||
|
||
@Override | ||
public List<BibEntry> readReferences(BibEntry entry) { | ||
if (needToRefreshReferences(entry)) { | ||
List<BibEntry> references; | ||
try { | ||
references = fetcher.searchCiting(entry); | ||
} catch (FetcherException e) { | ||
LOGGER.error("Error while fetching references", e); | ||
references = List.of(); | ||
} | ||
cache.cacheOrMergeReferences(entry, references); | ||
} | ||
|
||
return cache.getReferences(entry); | ||
public List<BibEntry> readCitations(BibEntry entry) { | ||
return cache.getCitations(entry); | ||
} | ||
|
||
@Override | ||
public void forceRefreshCitations(BibEntry entry) { | ||
try { | ||
List<BibEntry> citations = fetcher.searchCitedBy(entry); | ||
cache.cacheOrMergeCitations(entry, citations); | ||
} catch (FetcherException e) { | ||
LOGGER.error("Error while fetching citations", e); | ||
} | ||
public boolean containsCitations(BibEntry entry) { | ||
return cache.citationsCached(entry); | ||
} | ||
|
||
private boolean needToRefreshCitations(BibEntry entry) { | ||
return !cache.citationsCached(entry); | ||
@Override | ||
public void insertReferences(BibEntry entry, List<BibEntry> references) { | ||
cache.cacheOrMergeReferences( | ||
entry, Objects.requireNonNullElseGet(references, List::of) | ||
); | ||
} | ||
|
||
private boolean needToRefreshReferences(BibEntry entry) { | ||
return !cache.referencesCached(entry); | ||
@Override | ||
public List<BibEntry> readReferences(BibEntry entry) { | ||
return cache.getReferences(entry); | ||
} | ||
|
||
@Override | ||
public void forceRefreshReferences(BibEntry entry) { | ||
List<BibEntry> references; | ||
try { | ||
references = fetcher.searchCiting(entry); | ||
} catch (FetcherException e) { | ||
LOGGER.error("Error while fetching references", e); | ||
references = List.of(); | ||
} | ||
cache.cacheOrMergeReferences(entry, references); | ||
public boolean containsReferences(BibEntry entry) { | ||
return cache.referencesCached(entry); | ||
} | ||
} |
Oops, something went wrong.