Skip to content

Commit a0cc539

Browse files
committed
Prepare linked files tables
1 parent 72b26e0 commit a0cc539

File tree

5 files changed

+116
-8
lines changed

5 files changed

+116
-8
lines changed

src/main/java/org/jabref/logic/search/IndexManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import javafx.beans.value.ChangeListener;
99

1010
import org.jabref.logic.preferences.CliPreferences;
11-
import org.jabref.logic.search.indexing.DefaultLinkedFilesIndexer;
1211
import org.jabref.logic.search.indexing.BibFieldsIndexer;
12+
import org.jabref.logic.search.indexing.DefaultLinkedFilesIndexer;
13+
import org.jabref.logic.search.indexing.PostgresLinkedFilesIndexer;
1314
import org.jabref.logic.search.indexing.ReadOnlyLinkedFilesIndexer;
1415
import org.jabref.logic.search.retrieval.BibFieldsSearcher;
1516
import org.jabref.logic.search.retrieval.LinkedFilesSearcher;
@@ -42,6 +43,7 @@ public class IndexManager {
4243
private final BibFieldsIndexer bibFieldsIndexer;
4344
private final LuceneIndexer linkedFilesIndexer;
4445
private final BibFieldsSearcher bibFieldsSearcher;
46+
private final PostgresLinkedFilesIndexer postgresLinkedFilesIndexer;
4547
private final LinkedFilesSearcher linkedFilesSearcher;
4648

4749
public IndexManager(BibDatabaseContext databaseContext, TaskExecutor executor, CliPreferences preferences) {
@@ -53,6 +55,7 @@ public IndexManager(BibDatabaseContext databaseContext, TaskExecutor executor, C
5355

5456
PostgreServer postgreServer = Injector.instantiateModelOrService(PostgreServer.class);
5557
bibFieldsIndexer = new BibFieldsIndexer(preferences.getBibEntryPreferences(), databaseContext, postgreServer.getConnection());
58+
postgresLinkedFilesIndexer = new PostgresLinkedFilesIndexer(databaseContext, preferences.getFilePreferences(), postgreServer.getConnection());
5659

5760
LuceneIndexer indexer;
5861
try {

src/main/java/org/jabref/logic/search/indexing/BibFieldsIndexer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import static org.jabref.model.search.PostgreConstants.FIELD_NAME;
3232
import static org.jabref.model.search.PostgreConstants.FIELD_VALUE_LITERAL;
3333
import static org.jabref.model.search.PostgreConstants.FIELD_VALUE_TRANSFORMED;
34-
import static org.jabref.model.search.PostgreConstants.TABLE_NAME_SUFFIX;
34+
import static org.jabref.model.search.PostgreConstants.SPLIT_TABLE_SUFFIX;
3535

3636
public class BibFieldsIndexer {
3737
private static final Logger LOGGER = LoggerFactory.getLogger(BibFieldsIndexer.class);
@@ -54,7 +54,7 @@ public BibFieldsIndexer(BibEntryPreferences bibEntryPreferences, BibDatabaseCont
5454
this.libraryName = databaseContext.getDatabasePath().map(path -> path.getFileName().toString()).orElse("unsaved");
5555

5656
this.mainTable = databaseContext.getUniqueName();
57-
this.splitValuesTable = mainTable + TABLE_NAME_SUFFIX;
57+
this.splitValuesTable = mainTable + SPLIT_TABLE_SUFFIX;
5858

5959
this.schemaMainTableReference = """
6060
"%s"."%s"
@@ -97,7 +97,7 @@ PRIMARY KEY (%s, %s)
9797
%s TEXT NOT NULL,
9898
%s TEXT,
9999
%s TEXT
100-
)
100+
)
101101
""".formatted(
102102
schemaSplitValuesTableReference,
103103
ENTRY_ID,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.jabref.logic.search.indexing;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
import org.jabref.logic.FilePreferences;
7+
import org.jabref.model.database.BibDatabaseContext;
8+
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import static org.jabref.model.search.PostgreConstants.CONTENT_TABLE_SUFFIX;
13+
import static org.jabref.model.search.PostgreConstants.FILE_MODIFIED;
14+
import static org.jabref.model.search.PostgreConstants.FILE_PATH;
15+
import static org.jabref.model.search.PostgreConstants.LINKED_ENTRIES;
16+
import static org.jabref.model.search.PostgreConstants.LINKED_FILES_SCHEME;
17+
import static org.jabref.model.search.PostgreConstants.PAGE_ANNOTATIONS;
18+
import static org.jabref.model.search.PostgreConstants.PAGE_CONTENT;
19+
import static org.jabref.model.search.PostgreConstants.PAGE_NUMBER;
20+
21+
public class PostgresLinkedFilesIndexer {
22+
private static final Logger LOGGER = LoggerFactory.getLogger(PostgresLinkedFilesIndexer.class);
23+
24+
private final BibDatabaseContext databaseContext;
25+
private final FilePreferences filePreferences;
26+
private final Connection connection;
27+
private final String libraryName;
28+
private final String mainTable;
29+
private final String contentTable;
30+
private final String schemaMainTableReference;
31+
private final String schemaContentTableReference;
32+
33+
public PostgresLinkedFilesIndexer(BibDatabaseContext databaseContext, FilePreferences filePreferences, Connection connection) {
34+
this.databaseContext = databaseContext;
35+
this.filePreferences = filePreferences;
36+
this.connection = connection;
37+
this.libraryName = databaseContext.getDatabasePath().map(path -> path.getFileName().toString()).orElse("unsaved");
38+
this.mainTable = databaseContext.getUniqueName();
39+
this.contentTable = mainTable + CONTENT_TABLE_SUFFIX;
40+
this.schemaMainTableReference = """
41+
"%s"."%s"
42+
""".formatted(LINKED_FILES_SCHEME, mainTable);
43+
this.schemaContentTableReference = """
44+
"%s"."%s"
45+
""".formatted(LINKED_FILES_SCHEME, contentTable);
46+
setup();
47+
}
48+
49+
private void setup() {
50+
try {
51+
connection.createStatement().executeUpdate("""
52+
CREATE TABLE IF NOT EXISTS %s (
53+
%s TEXT NOT NULL,
54+
%s TEXT NOT NULL,
55+
%s TEXT NOT NULL,
56+
PRIMARY KEY (%s)
57+
)
58+
""".formatted(
59+
schemaMainTableReference,
60+
FILE_PATH,
61+
FILE_MODIFIED,
62+
LINKED_ENTRIES,
63+
FILE_PATH));
64+
65+
connection.createStatement().executeUpdate("""
66+
CREATE TABLE IF NOT EXISTS %s (
67+
%s TEXT NOT NULL REFERENCES %s(%s),
68+
%s INT NOT NULL,
69+
%s TEXT,
70+
%s TEXT,
71+
PRIMARY KEY (%s, %s)
72+
)
73+
""".formatted(
74+
schemaContentTableReference,
75+
FILE_PATH,
76+
schemaMainTableReference, FILE_PATH,
77+
PAGE_NUMBER,
78+
PAGE_CONTENT,
79+
PAGE_ANNOTATIONS,
80+
FILE_PATH, PAGE_NUMBER));
81+
82+
// full text index on page content, annotations
83+
connection.createStatement().executeUpdate("""
84+
CREATE INDEX IF NOT EXISTS "%s_content_idx"
85+
ON %s
86+
USING GIN (to_tsvector('english', %s || ' ' || %s))
87+
""".formatted(
88+
contentTable,
89+
schemaContentTableReference,
90+
PAGE_CONTENT, PAGE_ANNOTATIONS));
91+
92+
LOGGER.debug("Created full-text tables for library: {}", libraryName);
93+
} catch (SQLException e) {
94+
LOGGER.error("Could not create full-text tables for library: {}", libraryName, e);
95+
}
96+
}
97+
}

src/main/java/org/jabref/logic/search/query/SearchToSqlVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import static org.jabref.model.search.PostgreConstants.FIELD_NAME;
1919
import static org.jabref.model.search.PostgreConstants.FIELD_VALUE_LITERAL;
2020
import static org.jabref.model.search.PostgreConstants.FIELD_VALUE_TRANSFORMED;
21-
import static org.jabref.model.search.PostgreConstants.TABLE_NAME_SUFFIX;
21+
import static org.jabref.model.search.PostgreConstants.SPLIT_TABLE_SUFFIX;
2222

2323
/**
2424
* Converts to a query processable by the scheme created by {@link BibFieldsIndexer}.
@@ -40,7 +40,7 @@ public class SearchToSqlVisitor extends SearchBaseVisitor<String> {
4040

4141
public SearchToSqlVisitor(String mainTableName) {
4242
this.mainTableName = mainTableName;
43-
this.splitValuesTableName = mainTableName + TABLE_NAME_SUFFIX;
43+
this.splitValuesTableName = mainTableName + SPLIT_TABLE_SUFFIX;
4444
}
4545

4646
private enum SearchTermFlag {

src/main/java/org/jabref/model/search/PostgreConstants.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
public enum PostgreConstants {
44
BIB_FIELDS_SCHEME("bib_fields"),
5-
LINKED_FILES_SCHEME("linked_files"),
5+
SPLIT_TABLE_SUFFIX("_split_values"),
66
ENTRY_ID("entry_id"),
77
FIELD_NAME("field_name"),
88
FIELD_VALUE_LITERAL("field_value_literal"), // contains the value as-is
99
FIELD_VALUE_TRANSFORMED("field_value_transformed"), // contains the value transformed for better querying
10-
TABLE_NAME_SUFFIX("_split_values");
10+
LINKED_FILES_SCHEME("linked_files"),
11+
CONTENT_TABLE_SUFFIX("_file_content"),
12+
FILE_PATH("path"),
13+
FILE_MODIFIED("modified"),
14+
LINKED_ENTRIES("entries"),
15+
PAGE_NUMBER("pageNumber"),
16+
PAGE_CONTENT("content"),
17+
PAGE_ANNOTATIONS("annotations");
18+
1119
private final String value;
1220

1321
PostgreConstants(String value) {

0 commit comments

Comments
 (0)