Skip to content

Commit

Permalink
Fix Formatters optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Jan 8, 2024
1 parent 0cce22c commit 9672c48
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
public class Formatters {
private static final Pattern TRUNCATE_PATTERN = Pattern.compile("\\Atruncate\\d+\\z");

private static Map<String, Formatter> nameToFormatterMap;
private static Map<String, Formatter> keyToFormatterMap;

static {
nameToFormatterMap = getAll().stream().collect(Collectors.toMap(Formatter::getName, f -> f));
keyToFormatterMap = getAll().stream().collect(Collectors.toMap(Formatter::getKey, f -> f));
}

private Formatters() {
Expand Down Expand Up @@ -102,7 +102,7 @@ public static List<Formatter> getAll() {

public static Optional<Formatter> getFormatterForName(String name) {
Objects.requireNonNull(name);
return nameToFormatterMap.containsKey(name) ? Optional.of(nameToFormatterMap.get(name)) : Optional.empty();
return keyToFormatterMap.containsKey(name) ? Optional.of(keyToFormatterMap.get(name)) : Optional.empty();
}

public static Optional<Formatter> getFormatterForModifier(String modifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ private void parseJabRefComment(Map<String, String> meta) {
return;
}

// We remove all line breaks in the metadata
// These have been inserted to prevent too long lines when the file was saved, and are not part of the data.
String comment = buffer.toString().replaceAll("[\\x0d\\x0a]", "");
if (comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG.length())).equals(MetaData.META_FLAG)) {
if (comment.startsWith(MetaData.META_FLAG)) {
Expand All @@ -309,10 +311,6 @@ private void parseJabRefComment(Map<String, String> meta) {
int pos = rest.indexOf(':');

if (pos > 0) {
// We remove all line breaks in the metadata - these
// will have been inserted
// to prevent too long lines when the file was
// saved, and are not part of the data.
meta.put(rest.substring(0, pos), rest.substring(pos + 1));

// meta comments are always re-written by JabRef and not stored in the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public MetaData parse(Map<String, String> data, Character keywordSeparator) thro

/**
* Parses the data map and changes the given {@link MetaData} instance respectively.
*
* @return the given metaData instance (which is modified, too)
*/
public MetaData parse(MetaData metaData, Map<String, String> data, Character keywordSeparator) throws ParseException {
List<String> defaultCiteKeyPattern = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/pdf/search/PdfSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PdfSearchResults search(final String searchString, final int maxHits) thr
return new PdfSearchResults();
}
if (maxHits <= 0) {
throw new IllegalArgumentException("Must be called with at least 1 maxHits, was" + maxHits);
throw new IllegalArgumentException("Must be called with at least 1 maxHits, was " + maxHits);
}

List<SearchResult> resultDocs = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.jabref.logic.formatter.IdentityFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeAmpersandsFormatter;
Expand All @@ -24,6 +25,9 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -73,7 +77,7 @@ public void checkLowerCaseSaveAction() {
FieldFormatterCleanups actions = new FieldFormatterCleanups(true, FieldFormatterCleanups.parse("title[lower_case]"));

FieldFormatterCleanup lowerCaseTitle = new FieldFormatterCleanup(StandardField.TITLE, new LowerCaseFormatter());
assertEquals(Collections.singletonList(lowerCaseTitle), actions.getConfiguredActions());
assertEquals(List.of(lowerCaseTitle), actions.getConfiguredActions());

actions.applySaveActions(entry);

Expand Down Expand Up @@ -348,8 +352,16 @@ void parsingOfDefaultSaveActions() {
"""));
}

@Test
void formatterFromString() {
assertEquals(new ReplaceUnicodeLigaturesFormatter(), FieldFormatterCleanups.getFormatterFromString("replace_unicode_ligatures"));
public static Stream<Arguments> formatterFromString() {
return Stream.of(
Arguments.of(new ReplaceUnicodeLigaturesFormatter(), "replace_unicode_ligatures"),
Arguments.of(new LowerCaseFormatter(), "lower_case")
);
}

@ParameterizedTest
@MethodSource
void formatterFromString(Formatter expected, String input) {
assertEquals(expected, FieldFormatterCleanups.getFormatterFromString(input));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package org.jabref.logic.importer.util;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.exporter.MetaDataSerializerTest;
import org.jabref.logic.formatter.casechanger.LowerCaseFormatter;
import org.jabref.model.entry.BibEntryTypeBuilder;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.UnknownEntryType;
import org.jabref.model.metadata.MetaData;
import org.jabref.model.util.DummyFileUpdateMonitor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -56,4 +65,16 @@ public static Stream<Arguments> parseCustomizedEntryType() {
void parseCustomizedEntryType(BibEntryTypeBuilder expected, String source) {
assertEquals(Optional.of(expected.build()), MetaDataParser.parseCustomEntryType(source));
}

@Test
public void saveActions() throws Exception {
Map<String, String> data = Map.of("saveActions", "enabled;title[lower_case]");
MetaDataParser metaDataParser = new MetaDataParser(new DummyFileUpdateMonitor());
MetaData parsed = metaDataParser.parse(new MetaData(), data, ',');

MetaData expected = new MetaData();
FieldFormatterCleanups fieldFormatterCleanups = new FieldFormatterCleanups(true, List.of(new FieldFormatterCleanup(StandardField.TITLE, new LowerCaseFormatter())));
expected.setSaveActions(fieldFormatterCleanups);
assertEquals(expected, parsed);
}
}
29 changes: 18 additions & 11 deletions src/test/java/org/jabref/logic/pdf/search/PdfSearcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabase;
Expand All @@ -11,6 +12,7 @@
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.pdf.search.PdfSearchResults;
import org.jabref.model.pdf.search.SearchResult;
import org.jabref.preferences.FilePreferences;

import org.apache.lucene.queryparser.classic.ParseException;
Expand All @@ -31,25 +33,27 @@ public class PdfSearcherTest {
@BeforeEach
public void setUp(@TempDir Path indexDir) throws IOException {
FilePreferences filePreferences = mock(FilePreferences.class);
// given

BibDatabase database = new BibDatabase();

BibDatabaseContext context = mock(BibDatabaseContext.class);
when(context.getFileDirectories(Mockito.any())).thenReturn(Collections.singletonList(Path.of("src/test/resources/pdfs")));
when(context.getFulltextIndexPath()).thenReturn(indexDir);
when(context.getDatabase()).thenReturn(database);
when(context.getEntries()).thenReturn(database.getEntries());
BibEntry examplePdf = new BibEntry(StandardEntryType.Article);
examplePdf.setFiles(Collections.singletonList(new LinkedFile("Example Entry", "example.pdf", StandardFileType.PDF.getName())));

BibEntry examplePdf = new BibEntry(StandardEntryType.Article)
.withFiles(Collections.singletonList(new LinkedFile("Example Entry", "example.pdf", StandardFileType.PDF.getName())));
database.insertEntry(examplePdf);

BibEntry metaDataEntry = new BibEntry(StandardEntryType.Article);
metaDataEntry.setFiles(Collections.singletonList(new LinkedFile("Metadata Entry", "metaData.pdf", StandardFileType.PDF.getName())));
metaDataEntry.setCitationKey("MetaData2017");
BibEntry metaDataEntry = new BibEntry(StandardEntryType.Article)
.withCitationKey("MetaData2017")
.withFiles(Collections.singletonList(new LinkedFile("Metadata Entry", "metaData.pdf", StandardFileType.PDF.getName())));
database.insertEntry(metaDataEntry);

BibEntry exampleThesis = new BibEntry(StandardEntryType.PhdThesis);
exampleThesis.setFiles(Collections.singletonList(new LinkedFile("Example Thesis", "thesis-example.pdf", StandardFileType.PDF.getName())));
exampleThesis.setCitationKey("ExampleThesis");
BibEntry exampleThesis = new BibEntry(StandardEntryType.PhdThesis)
.withCitationKey("ExampleThesis")
.withFiles(Collections.singletonList(new LinkedFile("Example Thesis", "thesis-example.pdf", StandardFileType.PDF.getName())));
database.insertEntry(exampleThesis);

PdfIndexer indexer = PdfIndexer.of(context, filePreferences);
Expand All @@ -61,13 +65,16 @@ public void setUp(@TempDir Path indexDir) throws IOException {
@Test
public void searchForTest() throws IOException, ParseException {
PdfSearchResults result = search.search("test", 10);
assertEquals(8, result.numSearchResults());
assertEquals(10, result.numSearchResults());
}

@Test
public void searchForUniversity() throws IOException, ParseException {
PdfSearchResults result = search.search("University", 10);
assertEquals(1, result.numSearchResults());
assertEquals(2, result.numSearchResults());
List<SearchResult> searchResults = result.getSearchResults();
assertEquals(0, searchResults.get(0).getPageNumber());
assertEquals(9, searchResults.get(1).getPageNumber());
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jabref/logic/search/SearchQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class SearchQueryTest {

@Test
public void testToString() {
assertEquals("\"asdf\" (case sensitive, regular expression)", new SearchQuery("asdf", EnumSet.of(SearchRules.SearchFlags.CASE_SENSITIVE, SearchRules.SearchFlags.REGULAR_EXPRESSION)).toString());
assertEquals("\"asdf\" (case insensitive, plain text)", new SearchQuery("asdf", EnumSet.noneOf(SearchFlags.class)).toString());
assertEquals("\"asdf\" (case sensitive, regular expression) [CASE_SENSITIVE, REGULAR_EXPRESSION]", new SearchQuery("asdf", EnumSet.of(SearchRules.SearchFlags.CASE_SENSITIVE, SearchRules.SearchFlags.REGULAR_EXPRESSION)).toString());
assertEquals("\"asdf\" (case insensitive, plain text) []", new SearchQuery("asdf", EnumSet.noneOf(SearchFlags.class)).toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -150,9 +151,11 @@ void testGetFullTextIndexPathWhenPathIsNotNull() {
BibDatabaseContext bibDatabaseContext = new BibDatabaseContext();
bibDatabaseContext.setDatabasePath(existingPath);

Path expectedPath = OS.getNativeDesktop().getFulltextIndexBaseDirectory().resolve(existingPath.hashCode() + "");
Path actualPath = bibDatabaseContext.getFulltextIndexPath();
assertNotNull(actualPath);

assertEquals(expectedPath, actualPath);
String fulltextIndexBaseDirectory = OS.getNativeDesktop().getFulltextIndexBaseDirectory().toString();
String actualPathStart = actualPath.toString().substring(0, fulltextIndexBaseDirectory.length());
assertEquals(fulltextIndexBaseDirectory, actualPathStart);
}
}

0 comments on commit 9672c48

Please sign in to comment.