-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
DatabaseLegacyReindexer.java
189 lines (149 loc) · 5.46 KB
/
DatabaseLegacyReindexer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.storage.rdbms;
import java.sql.Connection;
import java.util.Arrays;
import java.util.List;
import org.dspace.browse.IndexBrowse;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.search.DSIndexer;
import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.api.callback.FlywayCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This is a FlywayCallback class which automatically reindexes Database
* contents into your Legacy search/browse engine of choice. It is NOT needed
* for Solr, but is necessary for Lucene / RDBMS indexes.
* <P>
* Reindexing is performed AFTER any database migration or repair. This
* ensures that your search/browse indexes are auto-updated post-upgrade.
*
* @author Tim Donohue
*/
public class DatabaseLegacyReindexer implements FlywayCallback
{
/** logging category */
private static final Logger log = LoggerFactory.getLogger(DatabaseLegacyReindexer.class);
/**
* Method to actually reindex all database contents. This method is "smart"
* in that it determines which indexing consumer(s) you have enabled,
* and then ensures each is reindexed appropriately.
* <P>
* NOTE: However, because Solr is never running when the Database is initialized,
* this reindexer only really works for Lucene / DBMS. Once those are obsolete,
* this can be safely removed, along with the reference to it in
* DatabaseUtils.setupFlyway()
*/
private void reindex()
{
Context context = null;
try
{
context = new Context();
// What indexing consumer(s) are configured in this DSpace?
// TODO: This really should use the ConfigurationService, BUT for
// some reason the DSpace Kernel is often not yet initialized at this point
String consumers = ConfigurationManager.getProperty("event.dispatcher.default.consumers");
if(consumers==null)
consumers = "";
List<String> consumerList = Arrays.asList(consumers.split("\\s*,\\s*"));
// If Discovery indexing is enabled
if (consumerList.contains("discovery"))
{
// Do nothing
// Because Solr is normally not running when the DatabaseManager initializes,
// Discovery autoindexing takes place in DatabaseUtils.checkReindexDiscovery(),
// which is automatically called when Discovery initializes.
}
// If Lucene indexing is enabled
if (consumerList.contains("search"))
{
log.info("Reindexing all content in Lucene search engine");
// Clean and update Lucene index
DSIndexer.cleanIndex(context);
DSIndexer.updateIndex(context, true);
log.info("Reindexing is complete");
}
// If traditional DBMS browse indexing is enabled
if (consumerList.contains("browse"))
{
log.info("Reindexing all content in DBMS Browse tables");
// Rebuild browse tables to perform a full index
// (recreating tables as needed)
IndexBrowse indexer = new IndexBrowse(context);
indexer.setRebuild(true);
indexer.setExecute(true);
indexer.initBrowse();
// Since the browse index is in the DB, we must commit & close context
context.complete();
log.info("Reindexing is complete");
}
}
catch(Exception e)
{
log.error("Error attempting to reindex all contents for search/browse. You may need to manually reindex Lucene or DBMS", e);
}
finally
{
// Clean up our context, if it still exists & it was never completed
if(context!=null && context.isValid())
context.abort();
}
}
@Override
public void beforeClean(Connection connection) {
}
@Override
public void afterClean(Connection connection) {
}
@Override
public void beforeMigrate(Connection connection) {
}
@Override
public void afterMigrate(Connection connection) {
reindex();
}
@Override
public void beforeEachMigrate(Connection connection, MigrationInfo migrationInfo) {
}
@Override
public void afterEachMigrate(Connection connection, MigrationInfo migrationInfo) {
}
@Override
public void beforeValidate(Connection connection) {
}
@Override
public void afterValidate(Connection connection) {
}
@Override
public void beforeInit(Connection connection) {
}
@Override
public void afterInit(Connection connection) {
}
@Override
public void beforeBaseline(Connection connection) {
}
@Override
public void afterBaseline(Connection connection) {
}
@Override
public void beforeRepair(Connection connection) {
}
@Override
public void afterRepair(Connection connection) {
}
@Override
public void beforeInfo(Connection connection) {
}
@Override
public void afterInfo(Connection connection) {
}
}